2022-09-06 09:30:45 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-27 21:08:34 +01:00
|
|
|
"io"
|
|
|
|
"os"
|
2022-09-06 09:30:45 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
2022-10-29 20:07:35 +02:00
|
|
|
"github.com/google/uuid"
|
2022-09-19 14:09:32 +02:00
|
|
|
"google.golang.org/protobuf/proto"
|
2022-09-06 09:30:45 +02:00
|
|
|
)
|
|
|
|
|
2023-11-08 21:52:09 +01:00
|
|
|
// Peer manages the peer messaging functions
|
|
|
|
// - Building simple user messages
|
|
|
|
// - Utility functions for packing/unpacking, encrypting/decrypting messages for peer communication
|
2022-09-06 09:30:45 +02:00
|
|
|
type Peer struct {
|
2023-07-27 15:48:06 +02:00
|
|
|
Name string `json:"name,omitempty"`
|
2023-12-26 13:54:20 +01:00
|
|
|
Avatar string `json:"avatar,omitempty"`
|
2023-07-27 15:48:06 +02:00
|
|
|
MyName string `json:"my_name,omitempty"`
|
|
|
|
MyAvatar string `json:"my_avatar,omitempty"`
|
2022-09-06 09:30:45 +02:00
|
|
|
// Conversation []InternalMessage `json:"conversation,omitempty"`
|
|
|
|
// My own keys for that peer
|
2023-11-08 21:52:09 +01:00
|
|
|
MyIdentity meowlib.KeyPair `json:"my_identity,omitempty"`
|
|
|
|
MyEncryptionKp meowlib.KeyPair `json:"my_encryption_kp,omitempty"`
|
|
|
|
MyLookupKp meowlib.KeyPair `json:"my_lookup_kp,omitempty"`
|
|
|
|
MyPullServers []meowlib.ServerCard `json:"my_pull_servers,omitempty"`
|
2023-12-31 10:24:15 +01:00
|
|
|
MyContact meowlib.ContactCard `json:"my_contact,omitempty"`
|
2022-09-06 09:30:45 +02:00
|
|
|
// Peer keys and infos
|
2023-12-31 10:24:15 +01:00
|
|
|
Contact meowlib.ContactCard `json:"contact,omitempty"`
|
|
|
|
InvitationId string `json:"invitation_id,omitempty"`
|
|
|
|
InvitationUrl string `json:"invitation_url,omitempty"`
|
|
|
|
InvitationExpiry time.Time `json:"invitation_expiry,omitempty"`
|
2022-09-06 09:30:45 +02:00
|
|
|
// Internal management attributes
|
|
|
|
Visible bool `json:"visible,omitempty"`
|
|
|
|
VisiblePassword string `json:"visible_password,omitempty"`
|
|
|
|
PasswordType string `json:"password_type,omitempty"`
|
|
|
|
Blocked bool `json:"blocked,omitempty"`
|
|
|
|
MessageNotification string `json:"message_notification,omitempty"`
|
2023-12-26 13:54:20 +01:00
|
|
|
MatriochkaMode bool `json:"matriochka_mode,omitempty"`
|
|
|
|
DirectMode bool `json:"direct_mode,omitempty"`
|
2022-09-06 09:30:45 +02:00
|
|
|
LastMessage time.Time `json:"last_message,omitempty"`
|
2022-11-27 21:08:34 +01:00
|
|
|
DbIds []string `json:"db_ids,omitempty"`
|
2023-01-07 00:39:05 +01:00
|
|
|
dbPassword string
|
2022-09-06 09:30:45 +02:00
|
|
|
}
|
|
|
|
|
2023-11-08 21:52:09 +01:00
|
|
|
//
|
|
|
|
// Messages building
|
|
|
|
//
|
2022-12-16 22:10:55 +01:00
|
|
|
|
2022-11-30 21:31:01 +01:00
|
|
|
func (p *Peer) BuildSimpleUserMessage(message []byte) (*meowlib.UserMessage, error) {
|
2022-09-19 14:09:32 +02:00
|
|
|
var msg meowlib.UserMessage
|
2022-12-17 20:17:05 +01:00
|
|
|
msg.Destination = p.Contact.LookupPublicKey
|
2022-11-27 21:08:34 +01:00
|
|
|
msg.From = p.MyIdentity.Public
|
2022-09-19 14:09:32 +02:00
|
|
|
msg.Data = message
|
|
|
|
msg.Type = "1"
|
2023-11-06 22:44:21 +01:00
|
|
|
msg.Status = &meowlib.ConversationStatus{}
|
2022-10-29 20:07:35 +02:00
|
|
|
msg.Status.LocalUuid = uuid.New().String()
|
2022-10-22 14:41:48 +02:00
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2022-12-18 19:47:44 +01:00
|
|
|
func (p *Peer) BuildSingleFileMessage(filename string, message []byte) ([]meowlib.UserMessage, error) {
|
2022-11-27 21:08:34 +01:00
|
|
|
var msgs []meowlib.UserMessage
|
|
|
|
fi, err := os.Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
// declare chunk size
|
|
|
|
maxSz := GetConfig().Chunksize
|
|
|
|
// create buffer
|
|
|
|
b := make([]byte, maxSz)
|
|
|
|
chunk := 0
|
|
|
|
for {
|
|
|
|
// read content to buffer
|
|
|
|
readTotal, err := file.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
var msg meowlib.UserMessage
|
2022-12-18 19:47:44 +01:00
|
|
|
var file meowlib.File
|
2022-12-17 20:17:05 +01:00
|
|
|
msg.Destination = p.Contact.LookupPublicKey
|
2022-11-27 21:08:34 +01:00
|
|
|
msg.From = p.MyIdentity.Public
|
2022-12-18 19:47:44 +01:00
|
|
|
file.Filename = fi.Name()
|
|
|
|
file.Chunk = uint32(chunk)
|
|
|
|
file.Data = b[:readTotal]
|
|
|
|
file.Size = uint64(fi.Size())
|
|
|
|
msg.Files = append(msg.Files, &file)
|
2022-11-27 21:08:34 +01:00
|
|
|
msg.Type = "2"
|
|
|
|
if chunk == 0 {
|
2023-11-06 22:44:21 +01:00
|
|
|
msg.Status = &meowlib.ConversationStatus{}
|
2022-11-27 21:08:34 +01:00
|
|
|
msg.Status.LocalUuid = uuid.New().String()
|
|
|
|
}
|
|
|
|
msgs = append(msgs, msg)
|
|
|
|
chunk++
|
|
|
|
}
|
|
|
|
return msgs, nil
|
|
|
|
}
|
|
|
|
|
2023-11-14 16:32:50 +01:00
|
|
|
// Builds an invitation answer user message.
|
|
|
|
// it takes as input a contactcard generated by Identity.AnswerInvitation
|
|
|
|
func (p *Peer) BuildInvitationAnswserMessage(myContactCard *meowlib.ContactCard) (*meowlib.UserMessage, error) {
|
|
|
|
var msg meowlib.UserMessage
|
|
|
|
var invitation meowlib.Invitation
|
|
|
|
invitation.Step = 3
|
|
|
|
out, err := proto.Marshal(myContactCard)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
invitation.Payload = out
|
|
|
|
msg.Destination = p.Contact.LookupPublicKey
|
|
|
|
msg.From = p.MyIdentity.Public
|
|
|
|
msg.Type = "1"
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2023-11-08 21:52:09 +01:00
|
|
|
//
|
|
|
|
// Messages encryption and packaging
|
|
|
|
//
|
|
|
|
|
2022-10-22 14:41:48 +02:00
|
|
|
func (p *Peer) SerializeUserMessage(msg *meowlib.UserMessage) ([]byte, error) {
|
|
|
|
out, err := proto.Marshal(msg)
|
2022-09-19 14:09:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2022-12-17 19:57:02 +01:00
|
|
|
func (p *Peer) DeserializeUserMessage(data []byte) (*meowlib.UserMessage, error) {
|
|
|
|
var msg meowlib.UserMessage
|
|
|
|
err := proto.Unmarshal(data, &msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2022-09-06 17:07:35 +02:00
|
|
|
// AsymEncryptMessage prepares a message to send to a specific peer contact
|
2022-12-19 20:15:53 +01:00
|
|
|
func (p *Peer) AsymEncryptMessage(Message []byte) (*meowlib.EncryptedMessage, error) {
|
|
|
|
var enc *meowlib.EncryptedMessage
|
|
|
|
enc, err := meowlib.AsymEncryptAndSign(p.Contact.EncryptionPublicKey, p.MyIdentity.Private, Message)
|
2022-09-06 09:30:45 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
2022-12-19 20:15:53 +01:00
|
|
|
return enc, err
|
2022-09-06 09:30:45 +02:00
|
|
|
}
|
2022-12-19 20:15:53 +01:00
|
|
|
return enc, err
|
2022-09-06 09:30:45 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 17:07:35 +02:00
|
|
|
// AsymDecryptMessage reads a message from a specific peer contact
|
|
|
|
func (p *Peer) AsymDecryptMessage(Message []byte, Signature []byte) (DecryptedMessage []byte, err error) {
|
2022-12-17 21:50:22 +01:00
|
|
|
DecryptedMessage, err = meowlib.AsymDecryptAndCheck(p.MyEncryptionKp.Private, p.Contact.ContactPublicKey, Message, Signature)
|
2022-09-06 09:30:45 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-06 17:07:35 +02:00
|
|
|
return DecryptedMessage, err
|
2022-09-06 09:30:45 +02:00
|
|
|
}
|
2022-09-18 18:09:27 +02:00
|
|
|
|
2023-11-08 21:52:09 +01:00
|
|
|
// PackUserMessage will package the previously encrypted message
|
2022-12-03 00:05:28 +01:00
|
|
|
func (p *Peer) PackUserMessage(message []byte, signature []byte) *meowlib.PackedUserMessage {
|
2022-09-18 18:09:27 +02:00
|
|
|
var msg meowlib.PackedUserMessage
|
|
|
|
msg.Destination = p.Contact.LookupPublicKey
|
|
|
|
msg.Payload = message
|
|
|
|
msg.Signature = signature
|
2022-12-03 00:05:28 +01:00
|
|
|
return &msg
|
2022-09-18 18:09:27 +02:00
|
|
|
}
|
|
|
|
|
2023-11-08 21:52:09 +01:00
|
|
|
// UnPackUserMessage unpacks a user message
|
2022-12-09 22:00:31 +01:00
|
|
|
func (p *Peer) UnPackUserMessage(protoPackedMessage []byte) (payload []byte, signature []byte, err error) {
|
2022-10-22 14:41:48 +02:00
|
|
|
msg := &meowlib.PackedServerMessage{}
|
|
|
|
if err := proto.Unmarshal(protoPackedMessage, msg); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return msg.Payload, msg.Signature, nil
|
|
|
|
}
|
|
|
|
|
2023-11-06 22:44:21 +01:00
|
|
|
func (p *Peer) GetConversationRequest() *meowlib.ConversationRequest {
|
|
|
|
var cr meowlib.ConversationRequest
|
2023-01-07 00:39:05 +01:00
|
|
|
return &cr
|
2022-09-18 18:09:27 +02:00
|
|
|
}
|
|
|
|
|
2023-11-08 21:52:09 +01:00
|
|
|
// ProcessOutboundUserMessage is a helper function that serializes, encrypts and packs a user message
|
2022-12-18 20:59:27 +01:00
|
|
|
func (p *Peer) ProcessOutboundUserMessage(usermessage *meowlib.UserMessage) (*meowlib.PackedUserMessage, error) {
|
|
|
|
serializedMessage, err := p.SerializeUserMessage(usermessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Encrypting it
|
2022-12-19 20:15:53 +01:00
|
|
|
enc, err := p.AsymEncryptMessage(serializedMessage)
|
2022-12-18 20:59:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Packing it
|
2022-12-19 20:15:53 +01:00
|
|
|
packedMsg := p.PackUserMessage(enc.Data, enc.Signature)
|
2022-12-18 20:59:27 +01:00
|
|
|
return packedMsg, nil
|
|
|
|
}
|
|
|
|
|
2023-11-08 21:52:09 +01:00
|
|
|
// ProcessInboundUserMessage is a helper function that decrypts and deserializes a user message
|
2022-12-18 20:59:27 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-08 21:52:09 +01:00
|
|
|
//
|
|
|
|
// Messages database
|
|
|
|
//
|
|
|
|
|
|
|
|
// SetDbPassword sets a specific password for a hidden user, it won't be saved, but kept in memory only for the next operations
|
2023-01-07 00:39:05 +01:00
|
|
|
func (p *Peer) SetDbPassword(password string) {
|
|
|
|
p.dbPassword = password
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) GetDbPassword() string {
|
|
|
|
return p.dbPassword
|
|
|
|
}
|
|
|
|
|
2022-09-18 18:09:27 +02:00
|
|
|
func (p *Peer) StoreMessage(msg []byte) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) LoadMessage(uid string) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) LoadLastMessages(qty int) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-18 21:17:28 +02:00
|
|
|
func (p *Peer) GetLastMessageUuid(msg []byte) {
|
2022-09-18 18:09:27 +02:00
|
|
|
|
|
|
|
}
|