Skip to content

Commit

Permalink
Update to latest.
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-rieke committed Dec 20, 2023
2 parents 73c2000 + 1183cd5 commit 26dd2d2
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 34 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
The Wanderer

Welcome to the wanderer. The 3 (or 4) commands (tiara, brimfeather, tipcap, and tipcaptwo) below illustrate
the activities of the wanderer.

To compile, run:

make tiara

make brimfeather

make captip

make captiptwo

To run:

./bin/tiara

./bin/brimfeather

./bin/tipcap

./bin/tipcaptwo # Optional
4 changes: 2 additions & 2 deletions brimfeather/brimfeather.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ func main() {
sessionIdentifier := "FeatherSessionOne"
env := "SomeEnv"

featherCtx := captiplib.FeatherCtlInit(interruptChan, &localHostAddr, &encryptPass, &encryptSalt, &hostAddr, &handshakeCode, &sessionIdentifier, &env, captiplib.AcceptRemote, interrupted)
featherCtx := captiplib.FeatherCtlInit(interruptChan, &localHostAddr, &encryptPass, &encryptSalt, &hostAddr, &handshakeCode, &sessionIdentifier, &env, captiplib.AcceptRemoteNoTimeout, interrupted)

go brimFeatherer(featherCtx)

sessionIdentifierTwo := "FeatherSessionTwo"

featherCtxTwo := captiplib.FeatherCtlInit(interruptChan, &localHostAddr, &encryptPass, &encryptSalt, &hostAddr, &handshakeCode, &sessionIdentifierTwo, &env, captiplib.AcceptRemote, interrupted)
featherCtxTwo := captiplib.FeatherCtlInit(interruptChan, &localHostAddr, &encryptPass, &encryptSalt, &hostAddr, &handshakeCode, &sessionIdentifierTwo, &env, captiplib.AcceptRemoteNoTimeout, interrupted)

go brimFeatherer(featherCtxTwo)

Expand Down
116 changes: 84 additions & 32 deletions cap/cap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"log"
"net"
"os"
Expand Down Expand Up @@ -115,32 +117,43 @@ func hasMode(msg []byte, mode byte) bool {
}

func handlePluck(conn *kcp.UDPSession, acceptRemote func(int, string) bool) {

buf := make([]byte, 50)
for {
conn.SetDeadline(time.Now().Add(500 * time.Millisecond))
n, err := conn.Read(buf)
if err != nil {
conn.Close()
return
}
message := buf[:n]

if hasMode(message, MODE_PLUCK) {
message = bytes.TrimLeft(message, "\x00")
if len(message) > 2 {
if _, ok := penseFeatherPluckMap.Pop(string(message[2:])); ok {
conn.Write([]byte{MODE_PLUCK})
if acceptRemote(FEATHER_COMMON, conn.RemoteAddr().String()) {
lastReadN := 0
for {
time.Sleep(time.Second * 3)
conn.SetDeadline(time.Now().Add(15 * time.Second))
n, err := conn.Read(buf)
if lastReadN != n {
lastReadN = n
conn.SetReadBuffer(lastReadN)
}
if err != nil {
conn.Close()
return
}
message := buf[:n]

if hasMode(message, MODE_PLUCK) {
message = bytes.TrimLeft(message, "\x00")
if len(message) > 2 {
if _, ok := penseFeatherPluckMap.Pop(string(message[2:])); ok {
conn.Write([]byte{MODE_PLUCK})
continue
} else {
conn.Write([]byte{MOID_VOID})
continue
}
}
} else {
conn.Write([]byte{MOID_VOID})
conn.Close()
return
continue
}
}
} else {
conn.Close()
return
break
}
}
}
Expand Down Expand Up @@ -251,6 +264,7 @@ func handleMessage(handshakeCode string, conn *kcp.UDPSession, acceptRemote func
if ccmap, ok := clientCodeMap.Get(conn.RemoteAddr().String()); ok {
clientCodeMap.Set(conn.RemoteAddr().String(), append(ccmap, append([]byte{}, buf[:n]...)))
}
defer conn.Close()
}
}
}
Expand All @@ -262,13 +276,14 @@ func Feather(encryptPass string, encryptSalt string, hostAddr string, handshakeC
for {
pluckS, err := pluckListener.AcceptKCP()
if err != nil {
if errors.Is(err, os.ErrDeadlineExceeded) || err.Error() == "timeout" || err == io.EOF {
pluckS.Close()
}
time.Sleep(time.Second)
continue
}
if acceptRemote(FEATHER_COMMON, pluckS.RemoteAddr().String()) {
go handlePluck(pluckS, acceptRemote)
} else {
pluckS.Close()
}

go handlePluck(pluckS, acceptRemote)
}
}
}()
Expand Down Expand Up @@ -300,26 +315,63 @@ func PluckCtlEmit(featherCtx *FeatherContext, pense []byte) (bool, error) {
hostAddr := *featherCtx.HostAddr + "1"
responseBuf := make([]byte, 100)

for {
penseConn, penseErr := kcp.Dial(hostAddr)
if penseErr != nil {
time.Sleep(time.Second)
continue
var penseConn net.Conn
var penseErr error
retries := 0

retryEstablish:
penseConn, penseErr = kcp.Dial(hostAddr)
if penseErr != nil {
time.Sleep(time.Second)
if retries < 10 && penseErr != io.EOF {
retries = retries + 1
penseConn.Close()
goto retryEstablish
} else {
// break immediately
return true, penseErr
}
}

defer penseConn.Close()
defer penseConn.Close()

for {
time.Sleep(3 * time.Second)
penseConn.SetDeadline(time.Time{})
_, penseWriteErr := penseConn.Write(pluckPacket)
if penseWriteErr != nil {
time.Sleep(time.Second)
if errors.Is(penseWriteErr, os.ErrDeadlineExceeded) || penseWriteErr.Error() == "timeout" || penseWriteErr == io.EOF || strings.Contains(penseWriteErr.Error(), "timeout") {
if retries < 10 {
time.Sleep(time.Second)
retries = retries + 1
penseConn.Close()
goto retryEstablish
} else {
// break immediately
return true, penseWriteErr
}
}
continue
}

penseConn.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
penseConn.SetDeadline(time.Time{})
n, penseResponseErr := penseConn.Read(responseBuf)
if penseResponseErr != nil {
time.Sleep(time.Second)
if errors.Is(penseResponseErr, os.ErrDeadlineExceeded) || penseResponseErr.Error() == "timeout" || penseResponseErr == io.EOF {
if retries < 10 {
time.Sleep(time.Second)
retries = retries + 1
penseConn.Close()
goto retryEstablish
} else {
// break immediately
penseConn.Close()
return true, penseResponseErr
}
}
continue
}
retries = 0

response := responseBuf[:n]
if hasMode(response, MODE_PLUCK) {
Expand All @@ -330,10 +382,10 @@ func PluckCtlEmit(featherCtx *FeatherContext, pense []byte) (bool, error) {
return false, nil
} else {
if breakImmediate, accErr := featherCtx.AcceptRemoteFunc(featherCtx, FEATHER_CTL, penseConn.RemoteAddr().String()); breakImmediate {
// Break, but don't exit encapsulating calling function.
if accErr != nil {
return true, accErr
} else {
// Break, but don't exit encapsulating calling function.
return false, accErr
}
} else {
Expand Down
16 changes: 16 additions & 0 deletions captip/captiplib/captiplib.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ func acceptInterruptFun(featherCtx *cap.FeatherContext, tickerContinue *time.Tic
return true, errors.New("not possible")
}

func acceptInterruptNoTimeoutFun(featherCtx *cap.FeatherContext, tickerContinue *time.Ticker) (bool, error) {
select {
case <-featherCtx.InterruptChan:
cap.FeatherCtlEmit(featherCtx, string(cap.MODE_PERCH), *featherCtx.SessionIdentifier, true)
return true, errors.New(YOU_SHALL_NOT_PASS)
case <-tickerContinue.C:
// don't break... continue...
return false, nil
}
return true, errors.New("not possible")
}

func AcceptRemoteNoTimeout(featherCtx *cap.FeatherContext, x int, y string) (bool, error) {
return acceptInterruptNoTimeoutFun(featherCtx, featherCtx.MultiSecondInterruptTicker)
}

func AcceptRemote(featherCtx *cap.FeatherContext, x int, y string) (bool, error) {
return acceptInterruptFun(featherCtx, featherCtx.MultiSecondInterruptTicker, featherCtx.FifteenSecondInterruptTicker, featherCtx.ThirtySecondInterruptTicker)
}
Expand Down

0 comments on commit 26dd2d2

Please sign in to comment.