Skip to content

Commit

Permalink
Reworked Switchboard protocol some more
Browse files Browse the repository at this point in the history
  • Loading branch information
p0t4t0sandwich committed Jun 9, 2024
1 parent 3de0806 commit 0cc8f62
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 185 deletions.
48 changes: 48 additions & 0 deletions modules/switchboard/encryption.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package switchboard

import (
"github.com/NeuralNexusDev/neuralnexus-api/modules/encryption"
"github.com/goccy/go-json"
)

// EncryptMessage encrypts a message
func EncryptMessage(message *Packet, key string) ([]byte, error) {
// Convert message to JSON
messageJSON, err := json.Marshal(message)
if err != nil {
return nil, err
}

// Encrypt message
encryptedMessage, err := encryption.EncryptAES(messageJSON, key)
if err != nil {
return nil, err
}

return encryptedMessage, nil
}

// DecryptMessage decrypts a message
func DecryptMessage(encryptedMessage []byte, key string) (*Packet, error) {
// Decrypt message
decryptedMessage, err := encryption.DecryptAES(encryptedMessage, key)

if err != nil {
return nil, err
}

// Take off the padding at the front of the byte array
// Thanks google common for having nice byte buffers that add padding,
// In which case I forgot about it and it took me 2 hours to figure out why the message was not decrypting
// TODO: Convert Java-side to just plain ol byte arrays with no fancy helper classes
decryptedMessage = decryptedMessage[2:]

// Convert message from JSON
var message Packet
err = json.Unmarshal(decryptedMessage, &message)
if err != nil {
return nil, err
}

return &message, nil
}
82 changes: 82 additions & 0 deletions modules/switchboard/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package switchboard

import (
"log"
"net/http"

"github.com/goccy/go-json"
"github.com/gorilla/websocket"
)

// -------------- Globals --------------
var (
upgrader = websocket.Upgrader{}
)

// -------------- Routes --------------
// ApplyRoutes - Apply the routes
func ApplyRoutes(mux *http.ServeMux) *http.ServeMux {
// mux.HandleFunc("GET /ws/v1/switchboard/relay", ebSocketRelayHandler)
mux.HandleFunc("GET /websocket/{id}", WebSocketRelayHandler)
return mux
}

// WebSocketRelayHandler relays switchboard messages
func WebSocketRelayHandler(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err.Error())
return
}
defer ws.Close()

for {
// Write
packet := &Packet{
Header: Header{
Version: 1,
Origin: "server",
Dest: "client",
},
Body: Body{
MessageID: "1",
MessageType: "message",
Encrypted: true,
EncScheme: "AES",
Content: "Hello, client!",
},
}
var packetBuffer []byte
if packet.Body.Encrypted {
packetBuffer, err = EncryptMessage(packet, "dgwjgsemfouvauxc")
if err != nil {
log.Println(err.Error())
}
} else {
packetBuffer, err = json.Marshal(packet)
if err != nil {
log.Println(err.Error())
}
}

err = ws.WriteMessage(websocket.BinaryMessage, packetBuffer)
if err != nil {
log.Println(err.Error())
}

// Read
msgType, msg, err := ws.ReadMessage()

if err != nil {
log.Println(err.Error())
} else if msgType != websocket.BinaryMessage {
log.Println("Message type is not binary")
}

packet, err = DecryptMessage(msg, "dgwjgsemfouvauxc")
if err != nil {
log.Println(err.Error())
}
log.Println(packet.Body.Content)
}
}
181 changes: 0 additions & 181 deletions modules/switchboard/switchboard.go

This file was deleted.

9 changes: 5 additions & 4 deletions modules/switchboard/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ type Header struct {
}

type Body struct {
MessageID string `json:"message_id"`
Encrypted bool `json:"encrypted,omitempty"`
EncScheme string `json:"enc_scheme,omitempty"`
Content string `json:"content"`
MessageID string `json:"message_id"`
MessageType string `json:"message_type,omitempty"`
Encrypted bool `json:"encrypted,omitempty"`
EncScheme string `json:"enc_scheme,omitempty"`
Content string `json:"content"`
}

type Packet struct {
Expand Down

0 comments on commit 0cc8f62

Please sign in to comment.