meowlib/client/server.go

83 lines
2.5 KiB
Go
Raw Normal View History

2022-09-06 09:30:45 +02:00
package client
import (
"fmt"
"time"
"forge.redroom.link/yves/meowlib"
"google.golang.org/protobuf/proto"
2022-09-06 09:30:45 +02:00
)
type InternalServer struct {
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"`
}
type InternalServerList struct {
Name string
Servers []InternalServer
}
func InternalServerFromUrl(url string) *InternalServer {
var is InternalServer
is.ServerData.Url = url
return &is
}
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
func (ints *InternalServer) AsymEncryptMessage(Message []byte) (EncryptedMessage []byte, Signature []byte, err error) {
2022-09-18 21:17:28 +02:00
EncryptedMessage, Signature, 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())
return nil, nil, err
}
2022-09-06 17:07:35 +02:00
return EncryptedMessage, Signature, 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
}
// Creates a basic message to server from a single packed user message and returns it as protobuf serialized byte array
func (ints *InternalServer) CreateMessageSendingMessage(usermsg *meowlib.PackedUserMessage) ([]byte, error) {
var msg meowlib.ToServerMessage
msg.Type = "1"
msg.From = ints.Me.Public
msg.Messages = append(msg.Messages, usermsg)
out, err := proto.Marshal(&msg)
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
func (ints *InternalServer) CreateMessageRequestMessage(lookupKeys []string) ([]byte, error) {
var msg meowlib.ToServerMessage
msg.Type = "1"
msg.From = ints.Me.Public
out, err := proto.Marshal(&msg)
if err != nil {
return nil, err
}
return out, nil
}