diff --git a/source/manager.go b/source/manager.go index be826a1..9049c80 100755 --- a/source/manager.go +++ b/source/manager.go @@ -6,6 +6,7 @@ import ( "net/http" "strconv" "time" + "strings" "github.com/gorilla/websocket" ) @@ -75,70 +76,94 @@ func RefereeBoxHandler() { func ClientHandler() { - addr := net.UDPAddr{ - Port: 8124, - IP: net.ParseIP(GetIP()), - } - ser, err := net.ListenUDP("udp", &addr) - if err != nil { - fmt.Printf("Some error %v\n", err) - return - } - - for { - bytes := make([]byte, 100) - _, remoteaddr, err := ser.ReadFromUDP(bytes) - - if err != nil { - fmt.Printf("Some error %v", err) - continue - } - - received := CleanString(string(bytes)) - - dataAfterParseLoc := ParseLoc(received) - - s := Split(received) - swap := Swap(s[10]) - - var container string - for i := 0; i < 9; i++ { - container = container + s[i] - } - container = CleanString(container) - swap = CleanString(swap) - - if container == swap { - id := GetID(dataAfterParseLoc) - - t := time.Now() - // insert to global for send to ws - if id[0] == '1' { - times.timeR1 = t.Unix() - staging.R1 = dataAfterParseLoc - - } else if id[0] == '2' { - times.timeR2 = t.Unix() - staging.R2 = dataAfterParseLoc - - } else if id[0] == '3' { - times.timeR3 = t.Unix() - staging.R3 = dataAfterParseLoc - - } else if id[0] == '4' { - times.timeR4 = t.Unix() - staging.R4 = dataAfterParseLoc - - } else if id[0] == '5' { - times.timeR5 = t.Unix() - staging.R5 = dataAfterParseLoc - } - - rvRobot := WhoIsExecute(id) - go ClientResponse(ser, remoteaddr, rvRobot) - } - - } + addr := net.UDPAddr{ + Port: 8124, + IP: net.ParseIP(GetIP()), + } + + ser, err := net.ListenUDP("udp", &addr) + if err != nil { + fmt.Printf("Some error %v\n", err) + return + } + + for { + + bytes := make([]byte, 512) + + n, remoteaddr, err := ser.ReadFromUDP(bytes) + if err != nil { + fmt.Printf("[ERROR] ReadFromUDP: %v\n", err) + continue + } + + raw := strings.TrimSpace(string(bytes[:n])) + if raw == "" { + fmt.Println("[WARN] Received empty data") + continue + } + + parts := strings.Split(raw, "|") + if len(parts) < 3 { + fmt.Printf("[ERROR] Incomplete encrypted data: %s\n", raw) + continue + } + + cipher := parts[0] + tag := parts[1] + iv := parts[2] + plaintext, err := DecryptAESGCM(iv, tag, cipher) + + if err != nil { + fmt.Printf("[ERROR] Decryption failed: %v\n", err) + continue + } + + fmt.Printf("[OK] Decrypted data: %s\n", plaintext) + + received := CleanString(plaintext) + dataAfterParseLoc := ParseLoc(received) + + s := Split(received) + swap := Swap(s[10]) + + var container string + for i := 0; i < 9; i++ { + container = container + s[i] + } + + container = CleanString(container) + swap = CleanString(swap) + + if container == swap { + + id := GetID(dataAfterParseLoc) + + t := time.Now() + + switch id[0] { + case '1': + times.timeR1 = t.Unix() + staging.R1 = dataAfterParseLoc + case '2': + times.timeR2 = t.Unix() + staging.R2 = dataAfterParseLoc + case '3': + times.timeR3 = t.Unix() + staging.R3 = dataAfterParseLoc + case '4': + times.timeR4 = t.Unix() + staging.R4 = dataAfterParseLoc + case '5': + times.timeR5 = t.Unix() + staging.R5 = dataAfterParseLoc + } + + rvRobot := WhoIsExecute(id) + go ClientResponse(ser, remoteaddr, rvRobot) + } + + } } func ClientResponse(conn *net.UDPConn, addr *net.UDPAddr, rvRobot string) { diff --git a/source/utilities.go b/source/utilities.go index 9889e38..d828cb4 100755 --- a/source/utilities.go +++ b/source/utilities.go @@ -1,10 +1,14 @@ package manager import ( + "crypto/aes" + "crypto/cipher" + "encoding/hex" + "strconv" + "strings" + "unicode" "net" - "strconv" - "strings" - "unicode" + "fmt" ) func CleanString(data string) string { @@ -79,3 +83,50 @@ func GetIP() string { } return string(ip) } +func DecryptAESGCM(ivHex, tagHex, cipherHex string) (string, error) { + + key := []byte("R5C9u@D!A7xP#LQ2mZ8F$wKJH4S1ErT0") + + + // Convert HEX → BYTES + iv, err := hex.DecodeString(ivHex) + if err != nil { + return "", fmt.Errorf("[ERR] hex iv decode: %v", err) + } + tag, err := hex.DecodeString(tagHex) + if err != nil { + return "", fmt.Errorf("[ERR] hex tag decode: %v", err) + } + ciphertext, err := hex.DecodeString(cipherHex) + if err != nil { + return "", fmt.Errorf("[ERR] hex cipher decode: %v", err) + } + + // Validate decoded sizes (bytes) + if len(iv) != 12 { + return "", fmt.Errorf("[ERR] IV must be 12 bytes, got: %s", strconv.Itoa(len(iv))) + } + if len(tag) != 16 { + return "", fmt.Errorf("[ERR] Tag must be 16 bytes, got: %s", strconv.Itoa(len(tag))) + } + + // Append tag to ciphertext (GCM expects ciphertext|tag passed to Open) + ciphertext = append(ciphertext, tag...) + + block, err := aes.NewCipher(key) + if err != nil { + return "", fmt.Errorf("[ERR] new cipher: %v", err) + } + + gcm, err := cipher.NewGCM(block) + if err != nil { + return "", fmt.Errorf("[ERR] new GCM: %v", err) + } + + plain, err := gcm.Open(nil, iv, ciphertext, nil) + if err != nil { + return "", fmt.Errorf("[ERR] gcm open: %v", err) + } + + return string(plain), nil +}