meowlib/client/message.go
2022-12-18 20:37:26 +01:00

88 lines
2.4 KiB
Go

package client
import (
"fmt"
"forge.redroom.link/yves/meowlib"
"google.golang.org/protobuf/proto"
)
func ProcessOutboundUserMessage(peer *Peer, usermessage *meowlib.UserMessage) (*meowlib.PackedUserMessage, error) {
serializedMessage, err := peer.SerializeUserMessage(usermessage)
if err != nil {
return nil, err
}
// Encrypting it
EncMsg, EncMsgSignature, FriendServers, err := peer.AsymEncryptMessage(serializedMessage)
if err != nil {
return nil, err
}
fmt.Println(len(FriendServers))
// Packing it
packedMsg := peer.PackUserMessage(EncMsg, EncMsgSignature)
return packedMsg, nil
}
func ProcessOutboundTextMessage(peer *Peer, text string, srv *InternalServer) ([]byte, error) {
// Creating User message
usermessage, err := peer.BuildSimpleUserMessage([]byte(text))
if err != nil {
return nil, err
}
// Prepare cyphered + packed user message
packedMsg, err := ProcessOutboundUserMessage(peer, usermessage)
if err != nil {
return nil, err
}
// Creating Server message for transporting the user message
toServerMessage := srv.BuildToServerMessageFromUserMessage(packedMsg)
return ProcessOutboundMessage(toServerMessage, srv)
}
func ProcessOutboundMessage(toServerMessage *meowlib.ToServerMessage, srv *InternalServer) ([]byte, error) {
byteToServerMessage, err := proto.Marshal(toServerMessage)
if err != nil {
return nil, err
}
// Encrypting it
encToServerMessage, encToServerMessageSignature, err := srv.AsymEncryptMessage(byteToServerMessage)
if err != nil {
return nil, err
}
// Packing it
protoPackedServerMsg, err := srv.PackServerMessage(encToServerMessage, encToServerMessageSignature)
if err != nil {
return nil, err
}
return protoPackedServerMsg, nil
}
func ProcessInboundServerResponse(srv *InternalServer, msg []byte) (*meowlib.FromServerMessage, error) {
fsmsg := &meowlib.FromServerMessage{}
payload, signature, err := srv.UnPackServerMessage(msg)
if err != nil {
return nil, err
}
decrypted, err := srv.AsymDecryptMessage(payload, signature)
if err != nil {
return nil, err
}
if err := proto.Unmarshal(decrypted, fsmsg); err != nil {
return nil, err
}
return fsmsg, nil
}
func ProcessInboundUserMessage(peer *Peer, message []byte, signature []byte) (*meowlib.UserMessage, error) {
dec, err := peer.AsymDecryptMessage(message, signature)
if err != nil {
return nil, err
}
msg, err := peer.DeserializeUserMessage(dec)
if err != nil {
return nil, err
}
return msg, nil
}