-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworked Switchboard protocol some more
- Loading branch information
1 parent
3de0806
commit 0cc8f62
Showing
4 changed files
with
135 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters