2022-09-06 09:30:45 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
2022-09-19 20:16:57 +02:00
|
|
|
"github.com/google/uuid"
|
2022-09-18 18:09:27 +02:00
|
|
|
"google.golang.org/protobuf/proto"
|
2022-09-06 09:30:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type InternalServer struct {
|
2022-12-27 16:59:52 +01:00
|
|
|
ServerData meowlib.Server `json:"server_data,omitempty"`
|
|
|
|
Presence bool `json:"presence,omitempty"`
|
|
|
|
LastCheck time.Time `json:"last_check,omitempty"`
|
|
|
|
Uptime time.Duration `json:"uptime,omitempty"`
|
|
|
|
Login string `json:"login,omitempty"`
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
Me meowlib.KeyPair `json:"me,omitempty"`
|
|
|
|
Country string `json:"country,omitempty"`
|
|
|
|
AllowedDelay int `json:"allowed_delay,omitempty"`
|
2022-09-06 09:30:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type InternalServerList struct {
|
|
|
|
Name string
|
|
|
|
Servers []InternalServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func InternalServerFromUrl(url string) *InternalServer {
|
|
|
|
var is InternalServer
|
|
|
|
is.ServerData.Url = url
|
|
|
|
return &is
|
|
|
|
}
|
|
|
|
|
2022-09-19 13:26:27 +02:00
|
|
|
func InternalServerFromServer(server *meowlib.Server) *InternalServer {
|
|
|
|
var is InternalServer
|
|
|
|
is.ServerData = *server
|
|
|
|
is.Me = meowlib.NewKeyPair()
|
|
|
|
return &is
|
|
|
|
}
|
|
|
|
|
2022-09-06 09:30:45 +02:00
|
|
|
func (sl *InternalServerList) AddUrls(urls []string) {
|
|
|
|
for _, url := range urls {
|
|
|
|
sl.Servers = append(sl.Servers, *InternalServerFromUrl(url))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 17:07:35 +02:00
|
|
|
// AsymEncryptMessage prepares a message to send to a specific internal server
|
2022-12-19 20:15:53 +01:00
|
|
|
func (ints *InternalServer) AsymEncryptMessage(Message []byte) (*meowlib.EncryptedMessage, error) {
|
|
|
|
var enc *meowlib.EncryptedMessage
|
|
|
|
enc, err := meowlib.AsymEncryptAndSign(ints.ServerData.PublicKey, ints.Me.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 nil, 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 internal server
|
|
|
|
func (ints *InternalServer) AsymDecryptMessage(Message []byte, Signature []byte) (DecryptedMessage []byte, err error) {
|
2022-09-18 21:17:28 +02:00
|
|
|
DecryptedMessage, err = meowlib.AsymDecryptAndCheck(ints.Me.Private, ints.ServerData.PublicKey, 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
|
|
|
|
|
|
|
// Creates a basic message to server from a single packed user message and returns it as protobuf serialized byte array
|
2022-12-18 20:37:26 +01:00
|
|
|
func (ints *InternalServer) BuildToServerMessageFromUserMessage(usermsg *meowlib.PackedUserMessage) *meowlib.ToServerMessage {
|
2022-09-18 18:09:27 +02:00
|
|
|
var msg meowlib.ToServerMessage
|
2022-09-19 20:16:57 +02:00
|
|
|
msg.Uuid = uuid.New().String()
|
2022-09-18 18:09:27 +02:00
|
|
|
msg.Type = "1"
|
|
|
|
msg.From = ints.Me.Public
|
|
|
|
msg.Messages = append(msg.Messages, usermsg)
|
2022-12-18 20:37:26 +01:00
|
|
|
return &msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a basic message to server from a single packed user message and returns it as protobuf serialized byte array
|
|
|
|
func (ints *InternalServer) BuildMessageSendingMessage(usermsg *meowlib.PackedUserMessage) ([]byte, error) {
|
|
|
|
msg := ints.BuildToServerMessageFromUserMessage(usermsg)
|
|
|
|
out, err := proto.Marshal(msg)
|
2022-09-18 18:09:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a basic message to server from a single packed user message and returns it as protobuf serialized byte array
|
2022-09-19 13:26:27 +02:00
|
|
|
func (ints *InternalServer) BuildMessageRequestMessage(lookupKeys []string) ([]byte, error) {
|
2022-09-18 18:09:27 +02:00
|
|
|
var msg meowlib.ToServerMessage
|
2022-09-19 20:16:57 +02:00
|
|
|
msg.Uuid = uuid.New().String()
|
2022-09-18 18:09:27 +02:00
|
|
|
msg.Type = "1"
|
|
|
|
msg.From = ints.Me.Public
|
|
|
|
|
|
|
|
out, err := proto.Marshal(&msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
2022-09-19 13:26:27 +02:00
|
|
|
|
2023-08-29 23:40:30 +02:00
|
|
|
// Creates a basic message to server from a single packed user message and returns it as protobuf serialized byte array
|
|
|
|
func (ints *InternalServer) BuildToServerMessageInvitation(invitation *meowlib.ContactCard, password string, timeout int, urllen int) (*meowlib.ToServerMessage, error) {
|
|
|
|
var msg meowlib.ToServerMessage
|
|
|
|
var inv meowlib.Invitation
|
|
|
|
payload, err := invitation.Compress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
msg.Type = "1"
|
|
|
|
msg.From = ints.Me.Public
|
|
|
|
inv.Password = password
|
|
|
|
inv.Timeout = int32(timeout)
|
|
|
|
inv.Urllen = int32(urllen)
|
|
|
|
inv.Payload = payload
|
|
|
|
msg.Invitation = &inv
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2022-09-19 13:26:27 +02:00
|
|
|
func (ints *InternalServer) PackServerMessage(payload []byte, signature []byte) (protoPackedMessage []byte, err error) {
|
|
|
|
var msg meowlib.PackedServerMessage
|
|
|
|
msg.From = ints.Me.Public
|
|
|
|
msg.Payload = payload
|
|
|
|
msg.Signature = signature
|
|
|
|
out, err := proto.Marshal(&msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
2022-10-22 14:41:48 +02:00
|
|
|
|
|
|
|
func (ints *InternalServer) UnPackServerMessage(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
|
|
|
|
}
|
2022-12-18 20:59:27 +01:00
|
|
|
|
|
|
|
func (srv *InternalServer) ProcessOutboundMessage(toServerMessage *meowlib.ToServerMessage) ([]byte, error) {
|
|
|
|
byteToServerMessage, err := proto.Marshal(toServerMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Encrypting it
|
2022-12-19 20:15:53 +01:00
|
|
|
encToServer, err := srv.AsymEncryptMessage(byteToServerMessage)
|
2022-12-18 20:59:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Packing it
|
2022-12-19 20:15:53 +01:00
|
|
|
protoPackedServerMsg, err := srv.PackServerMessage(encToServer.Data, encToServer.Signature)
|
2022-12-18 20:59:27 +01:00
|
|
|
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
|
|
|
|
}
|