meowlib/client/message.go
2022-12-09 22:00:31 +01:00

62 lines
1.6 KiB
Go

package client
import (
"fmt"
"forge.redroom.link/yves/meowlib"
"google.golang.org/protobuf/proto"
)
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
}
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)
// Creating Server message for transporting the user message
toServerMessage, err := srv.BuildMessageSendingMessage(packedMsg)
if err != nil {
return nil, err
}
// Encrypting it
encToServerMessage, encToServerMessageSignature, err := srv.AsymEncryptMessage(toServerMessage)
if err != nil {
return nil, err
}
// Packing it
protoPackedServerMsg, err := srv.PackServerMessage(encToServerMessage, encToServerMessageSignature)
if err != nil {
return nil, err
}
return protoPackedServerMsg, nil
}
func ProcessServerResponse(srv *InternalServer, msg []byte) (fsmsg *meowlib.FromServerMessage, err error) {
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
}