From 4a2f315b7c26471c13ba1435b1b93c4bbe0f4dd9 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Tue, 14 Jan 2025 03:01:20 +0200 Subject: [PATCH] add logging, write vscode config to run debugger --- .vscode/launch.json | 19 +++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ net/lux_log.go | 5 +++++ net/lux_packet.go | 4 +--- net/lux_router.go | 10 ++++++++++ 6 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 net/lux_log.go diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5b3d54d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Debug", + "type": "go", + "request": "launch", + "mode": "debug", + "program": "${workspaceFolder}", + "cwd": "${workspaceFolder}" + }, + { + "name": "Debug this test", + "type": "go", + "request": "launch", + "mode": "test" + } + ] +} \ No newline at end of file diff --git a/go.mod b/go.mod index b2558c6..ebd3420 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,6 @@ go 1.23.4 require ( github.com/google/uuid v1.6.0 // indirect + github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect gopkg.in/ini.v1 v1.67.0 // indirect ) diff --git a/go.sum b/go.sum index c563d2f..3a5a0f9 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88= +github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= diff --git a/net/lux_log.go b/net/lux_log.go new file mode 100644 index 0000000..5b27234 --- /dev/null +++ b/net/lux_log.go @@ -0,0 +1,5 @@ +package net + +import "github.com/op/go-logging" + +var log = logging.MustGetLogger("net") diff --git a/net/lux_packet.go b/net/lux_packet.go index b10a1a6..ac9641b 100644 --- a/net/lux_packet.go +++ b/net/lux_packet.go @@ -5,7 +5,6 @@ import ( "crypto/cipher" "crypto/rand" "errors" - "log" "lux/crypto" "lux/proto" "net" @@ -93,9 +92,8 @@ func EncryptLuxPacket(packet LuxPacket, key crypto.LuxKey, target *net.UDPAddr) var paddingLen int packetLen := LUX_PROTO_PACKET_HDRLEN + packet.Buffer.Length() if packetLen%encrypter.BlockSize() != 0 { - //paddingLen = ((packetLen / encrypter.BlockSize()) + 1) * encrypter.BlockSize() paddingLen = encrypter.BlockSize() - (packetLen % encrypter.BlockSize()) - log.Default().Print(packetLen, paddingLen) + log.Debugf("packetLen %d paddingLen %d\n", packetLen, paddingLen) } else { paddingLen = 0 } diff --git a/net/lux_router.go b/net/lux_router.go index adc7811..5719f7e 100644 --- a/net/lux_router.go +++ b/net/lux_router.go @@ -214,12 +214,16 @@ func (r *LuxRouter) Recv() (LuxPacket, error) { packet, err = DecryptLuxPacket(dgram, route.Key) if err != nil { // do we really fail here? + log.Debugf("DecryptLuxPacket err %v for route %v", err, route) return packet, err } // check if LuxID matches if !bytes.Equal(packet.Target.UUID[:], route.Key.Id.UUID[:]) { // not matches.. we discard route and throw away packet + log.Infof("packet from %s received at route %v mismatches associated target UUID %s", + dgram.Target.String(), route, route.Key.Id.String()) + r.DeleteRoute(route) return packet, errors.New("bogus packet from established route") /* NOTE: @@ -246,6 +250,9 @@ func (r *LuxRouter) Recv() (LuxPacket, error) { var ok bool var route *LuxRoute if route, ok = r.routes[packet.Target]; ok { + log.Debugf("updating route %s: %s -> %s", route.Key.Id.String, + route.Destination.String(), dgram.Target.String()) + route.Destination = dgram.Target route.Associated = dgram.Channel // since packet arrived from different transport, we flush nonces @@ -258,11 +265,14 @@ func (r *LuxRouter) Recv() (LuxPacket, error) { Nonces: NewLuxNonceList(), } route = r.routes[key.Id] + + log.Debugf("established route %s <-> %s", route.Key.Id.String(), route.Destination.String()) } // rotate nonce if !route.Nonces.RotateOrFail(packet.Nonce) { // failed nonce, discard packet + log.Debug("failed nonce") return packet, fmt.Errorf("packet failed nonce check") }