This commit is contained in:
ycc 2022-12-18 20:59:27 +01:00
parent 66927c4298
commit a2d4106d51
3 changed files with 65 additions and 72 deletions

View File

@ -1,29 +1,5 @@
package client 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) { func ProcessOutboundTextMessage(peer *Peer, text string, srv *InternalServer) ([]byte, error) {
// Creating User message // Creating User message
usermessage, err := peer.BuildSimpleUserMessage([]byte(text)) usermessage, err := peer.BuildSimpleUserMessage([]byte(text))
@ -31,57 +7,11 @@ func ProcessOutboundTextMessage(peer *Peer, text string, srv *InternalServer) ([
return nil, err return nil, err
} }
// Prepare cyphered + packed user message // Prepare cyphered + packed user message
packedMsg, err := ProcessOutboundUserMessage(peer, usermessage) packedMsg, err := peer.ProcessOutboundUserMessage(usermessage)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Creating Server message for transporting the user message // Creating Server message for transporting the user message
toServerMessage := srv.BuildToServerMessageFromUserMessage(packedMsg) toServerMessage := srv.BuildToServerMessageFromUserMessage(packedMsg)
return ProcessOutboundMessage(toServerMessage, srv) return srv.ProcessOutboundMessage(toServerMessage)
}
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
} }

View File

@ -197,6 +197,35 @@ func (p *Peer) GetConversationRequest() meowlib.ToServerMessage_ConversationRequ
return cr return cr
} }
func (p *Peer) ProcessOutboundUserMessage(usermessage *meowlib.UserMessage) (*meowlib.PackedUserMessage, error) {
serializedMessage, err := p.SerializeUserMessage(usermessage)
if err != nil {
return nil, err
}
// Encrypting it
EncMsg, EncMsgSignature, FriendServers, err := p.AsymEncryptMessage(serializedMessage)
if err != nil {
return nil, err
}
fmt.Println(len(FriendServers))
// Packing it
packedMsg := p.PackUserMessage(EncMsg, EncMsgSignature)
return packedMsg, nil
}
func (p *Peer) ProcessInboundUserMessage(message []byte, signature []byte) (*meowlib.UserMessage, error) {
dec, err := p.AsymDecryptMessage(message, signature)
if err != nil {
return nil, err
}
msg, err := p.DeserializeUserMessage(dec)
if err != nil {
return nil, err
}
return msg, nil
}
func (p *Peer) StoreMessage(msg []byte) { func (p *Peer) StoreMessage(msg []byte) {
} }

View File

@ -116,3 +116,37 @@ func (ints *InternalServer) UnPackServerMessage(protoPackedMessage []byte) (payl
} }
return msg.Payload, msg.Signature, nil return msg.Payload, msg.Signature, nil
} }
func (srv *InternalServer) ProcessOutboundMessage(toServerMessage *meowlib.ToServerMessage) ([]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 (srv *InternalServer) ProcessInboundServerResponse(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
}