diff --git a/go.mod b/go.mod index ac05433..13a8141 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.22.3 require ( github.com/teeworlds-go/huffman v1.0.0 // indirect - github.com/teeworlds-go/teeworlds v0.0.0-20240621051401-94b6c7787cee // indirect + github.com/teeworlds-go/teeworlds v0.0.0-20240622051525-1cf0184d605b // indirect ) diff --git a/go.sum b/go.sum index 91ca55b..ff5bc12 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,4 @@ github.com/teeworlds-go/huffman v1.0.0 h1:XSNMNAJZb+njNrPACsxcDrrLDXTGjZZt35FqLAuHi4I= github.com/teeworlds-go/huffman v1.0.0/go.mod h1:kjaXpL6C6xL7CM+tWPNYjdEgVZB2GumKhx7rCDdXArU= -github.com/teeworlds-go/teeworlds v0.0.0-20240621051401-94b6c7787cee h1:kHlXfFQ22RcvVPltdS0JaP60P3symbvobL0tQWD3vvA= -github.com/teeworlds-go/teeworlds v0.0.0-20240621051401-94b6c7787cee/go.mod h1:aKhyLQAZ0cXv3e6ivNMiolKLbycQlDTIq9Yldn7klWM= +github.com/teeworlds-go/teeworlds v0.0.0-20240622051525-1cf0184d605b h1:L2O3FCBy3ShdNMIzGV0IEVJ6bvMGuzWoR7XLwhSWFTY= +github.com/teeworlds-go/teeworlds v0.0.0-20240622051525-1cf0184d605b/go.mod h1:aKhyLQAZ0cXv3e6ivNMiolKLbycQlDTIq9Yldn7klWM= diff --git a/proxy.go b/proxy.go index 2f566a9..059a5df 100644 --- a/proxy.go +++ b/proxy.go @@ -92,52 +92,52 @@ func RunConnection(conn *Connection, twconn *protocol7.Connection) { } srvMsg := buffer[0:n] - result, err := twconn.OnPacket(srvMsg) + + packet := &protocol7.Packet{} + err = packet.Unpack(srvMsg) if err != nil { panic(err) } - // inspecting incoming trafic - if result != nil && result.Packet != nil { - for i, msg := range result.Packet.Messages { - if msg.MsgId() == network7.MsgGameSvChat { - var chat *messages7.SvChat - var ok bool - if chat, ok = result.Packet.Messages[i].(*messages7.SvChat); ok { - fmt.Printf("got chat msg: %s\n", chat.Message) - chat.Message = "capitalism." - - // modify chat if this was a proxy - result.Packet.Messages[i] = chat - - repack := result.Packet.Pack(twconn) - srvMsg = repack - fmt.Printf("repack %x\n", repack) - } - } else if msg.MsgId() == network7.MsgCtrlToken { - fmt.Printf("got token=%x registering sigint handler ...\n", twconn.ServerToken) - - c := make(chan os.Signal) - signal.Notify(c, os.Interrupt, syscall.SIGINT) - go func() { - <-c - Vlogf(0, "Got ctrl+c sending disconnect token=%x ack=%d ...\n", twconn.ServerToken, twconn.Ack) - - packet := protocol7.Packet{} - packet.Messages = append(packet.Messages, &messages7.CtrlClose{}) - packet.Header.Token = twconn.ServerToken - packet.Header.Ack = twconn.Ack - disconnectPacked := packet.Pack(twconn) - _, err = conn.ServerConn.Write(disconnectPacked) - - time.Sleep(10_000_000) - - Vlogf(0, "disconnected. token=%x ack=%d\n", twconn.ServerToken, twconn.Ack) - Vlogf(0, "disconnect: %x\n", disconnectPacked) - - os.Exit(0) - }() - - } + + // count sequence numbers so we can spoof the client + // and send a disconnect message on behalf of the client + twconn.OnPacket(packet) + + for i, msg := range packet.Messages { + switch msg := msg.(type) { + case *messages7.SvChat: + // inspect outgoing traffic + fmt.Printf("%s -> capitalism.\n", msg.Message) + + // change outgoing traffic + msg.Message = "capitalism." + packet.Messages[i] = msg + srvMsg = packet.Pack(twconn) + case *messages7.CtrlToken: + fmt.Printf("got token=%x registering sigint handler ...\n", msg.Token) + + c := make(chan os.Signal) + signal.Notify(c, os.Interrupt, syscall.SIGINT) + go func() { + <-c + Vlogf(0, "Got ctrl+c sending disconnect token=%x ack=%d ...\n", msg.Token, twconn.Ack) + + packet := protocol7.Packet{} + packet.Messages = append(packet.Messages, &messages7.CtrlClose{}) + packet.Header.Token = msg.Token + packet.Header.Ack = twconn.Ack + disconnectPacked := packet.Pack(twconn) + _, err = conn.ServerConn.Write(disconnectPacked) + + time.Sleep(10_000_000) + + Vlogf(0, "disconnected. token=%x ack=%d\n", msg.Token, twconn.Ack) + Vlogf(0, "disconnect: %x\n", disconnectPacked) + + os.Exit(0) + }() + + default: } }