meowlib/client/peer.go

193 lines
5.3 KiB
Go
Raw Normal View History

2022-09-06 09:30:45 +02:00
package client
import (
"fmt"
"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"
"google.golang.org/protobuf/proto"
2022-09-06 09:30:45 +02:00
)
type Peer struct {
Name string `json:"name,omitempty"`
// Conversation []InternalMessage `json:"conversation,omitempty"`
// My own keys for that peer
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.Server `json:"my_pull_servers,omitempty"`
2022-09-06 09:30:45 +02:00
// Peer keys and infos
Contact meowlib.ContactCard `json:"contact,omitempty"`
InvitationId string `json:"invitation_id,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"`
OnionMode bool `json:"onion_mode,omitempty"`
LastMessage time.Time `json:"last_message,omitempty"`
DbIds []string `json:"db_ids,omitempty"`
DbPassword string `json:"db_password,omitempty"`
2022-09-06 09:30:45 +02:00
}
type PeerList []Peer
type Group struct {
Name string `json:"name,omitempty"`
Members []Peer `json:"members,omitempty"`
}
func (pl *PeerList) GetFromPublicKey(publickey string) *Peer {
for _, peer := range *pl {
if peer.Contact.ContactPublicKey == publickey {
return &peer
}
}
return nil
}
func (pl *PeerList) GetFromName(name string) *Peer {
for _, peer := range *pl {
if peer.Contact.Name == name {
return &peer
}
}
return nil
}
2022-12-16 22:12:23 +01:00
func (pl *PeerList) GetConversationRequests(name string) []meowlib.ToServerMessage_ConversationRequest {
var list []meowlib.ToServerMessage_ConversationRequest
for _, peer := range *pl {
var cr meowlib.ToServerMessage_ConversationRequest
cr.LookupKey = peer.MyLookupKp.Public
// TODO Add key signature
list = append(list, cr)
}
return list
}
func (p *Peer) BuildSimpleUserMessage(message []byte) (*meowlib.UserMessage, error) {
var msg meowlib.UserMessage
msg.From = p.MyIdentity.Public
msg.Data = message
msg.Type = "1"
msg.Status = &meowlib.UserMessage_ConversationStatus{}
2022-10-29 20:07:35 +02:00
msg.Status.LocalUuid = uuid.New().String()
return &msg, nil
}
func (p *Peer) BuildFileMessage(filename string, message []byte) ([]meowlib.UserMessage, error) {
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
msg.From = p.MyIdentity.Public
msg.File.Filename = fi.Name()
msg.File.Chunk = uint32(chunk)
msg.File.Data = b[:readTotal]
msg.File.Size = uint64(fi.Size())
msg.Type = "2"
if chunk == 0 {
msg.Status = &meowlib.UserMessage_ConversationStatus{}
msg.Status.LocalUuid = uuid.New().String()
}
msgs = append(msgs, msg)
chunk++
}
return msgs, nil
}
func (p *Peer) SerializeUserMessage(msg *meowlib.UserMessage) ([]byte, error) {
out, err := proto.Marshal(msg)
if err != nil {
return nil, err
}
return out, nil
}
2022-09-06 17:07:35 +02:00
// AsymEncryptMessage prepares a message to send to a specific peer contact
func (p *Peer) AsymEncryptMessage(Message []byte) (EncryptedMessage []byte, Signature []byte, Servers []*meowlib.Server, err error) {
EncryptedMessage, Signature, 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())
return nil, nil, nil, err
2022-09-06 09:30:45 +02:00
}
return EncryptedMessage, Signature, p.Contact.PullServers, 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) {
DecryptedMessage, err = meowlib.AsymDecryptAndCheck(p.MyIdentity.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
}
// PackUserMessage will package the previously encrypted message for sending it to the peer in protobuff format
2022-12-03 00:05:28 +01:00
func (p *Peer) PackUserMessage(message []byte, signature []byte) *meowlib.PackedUserMessage {
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-12-09 22:00:31 +01:00
func (p *Peer) UnPackUserMessage(protoPackedMessage []byte) (payload []byte, signature []byte, err error) {
msg := &meowlib.PackedServerMessage{}
if err := proto.Unmarshal(protoPackedMessage, msg); err != nil {
return nil, nil, err
}
return msg.Payload, msg.Signature, nil
}
func (p *Peer) GetConversationRequest() meowlib.ToServerMessage_ConversationRequest {
var cr meowlib.ToServerMessage_ConversationRequest
return cr
}
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) {
}