add logging, write vscode config to run debugger

This commit is contained in:
mykola2312 2025-01-14 03:01:20 +02:00
parent a28a84eafb
commit 4a2f315b7c
6 changed files with 38 additions and 3 deletions

19
.vscode/launch.json vendored Normal file
View file

@ -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"
}
]
}

1
go.mod
View file

@ -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
)

2
go.sum
View file

@ -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=

5
net/lux_log.go Normal file
View file

@ -0,0 +1,5 @@
package net
import "github.com/op/go-logging"
var log = logging.MustGetLogger("net")

View file

@ -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
}

View file

@ -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")
}