meowlib/client/message.go

88 lines
2.4 KiB
Go
Raw Normal View History

2022-12-02 23:18:13 +01:00
package client
2022-12-09 22:00:31 +01:00
import (
"fmt"
"forge.redroom.link/yves/meowlib"
"google.golang.org/protobuf/proto"
)
2022-12-02 23:18:13 +01:00
2022-12-18 20:36:44 +01:00
func ProcessOutboundUserMessage(peer *Peer, usermessage *meowlib.UserMessage) (*meowlib.PackedUserMessage, error) {
2022-12-02 23:18:13 +01:00
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)
2022-12-18 19:47:44 +01:00
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
2022-12-18 20:36:44 +01:00
packedMsg, err := ProcessOutboundUserMessage(peer, usermessage)
2022-12-18 19:47:44 +01:00
if err != nil {
return nil, err
}
2022-12-02 23:18:13 +01:00
// Creating Server message for transporting the user message
2022-12-18 20:36:44 +01:00
toServerMessage := srv.BuildToServerMessageFromUserMessage(packedMsg)
return ProcessOutboundMessage(toServerMessage, srv)
2022-12-02 23:18:13 +01:00
}
2022-12-09 22:00:31 +01:00
2022-12-16 22:29:12 +01:00
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
}
2022-12-18 20:36:44 +01:00
func ProcessInboundServerResponse(srv *InternalServer, msg []byte) (*meowlib.FromServerMessage, error) {
2022-12-15 22:34:59 +01:00
fsmsg := &meowlib.FromServerMessage{}
2022-12-09 22:00:31 +01:00
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
}
2022-12-17 19:57:02 +01:00
2022-12-18 20:36:44 +01:00
func ProcessInboundUserMessage(peer *Peer, message []byte, signature []byte) (*meowlib.UserMessage, error) {
2022-12-17 19:57:02 +01:00
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
}