From 74e7f7d079bac3c65e5834bccdfa69e0963a0060 Mon Sep 17 00:00:00 2001 From: N Date: Wed, 7 Sep 2022 16:29:17 +0200 Subject: [PATCH] refactor --- contactcard.go | 8 +++---- endtoend_test.go | 2 +- server/server.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 server/server.go diff --git a/contactcard.go b/contactcard.go index 65b3f5c..4a89db3 100644 --- a/contactcard.go +++ b/contactcard.go @@ -65,7 +65,7 @@ func (Contact *ContactCard) WritePng(filename string) { } -func (Contact *ContactCard) WriteQr(FileName string) { +func (Contact *ContactCard) WriteQr(filename string) { jsonContact, _ := json.Marshal(Contact) qwriter := qrcode.NewQRCodeWriter() code, err := qwriter.Encode(string(jsonContact), gozxing.BarcodeFormat_QR_CODE, 512, 512, nil) @@ -81,8 +81,8 @@ func (Contact *ContactCard) WriteQr(FileName string) { } -func ReadQr(FileName string) ContactCard { - var Contact ContactCard +func ReadQr(Filename string) ContactCard { + var contact ContactCard // open and decode image file file, _ := os.Open("qrcode.jpg") img, _, _ := image.Decode(file) @@ -95,5 +95,5 @@ func ReadQr(FileName string) ContactCard { result, _ := qrReader.Decode(bmp, nil) fmt.Println(result) - return Contact + return contact } diff --git a/endtoend_test.go b/endtoend_test.go index dbf66a1..82bae4a 100644 --- a/endtoend_test.go +++ b/endtoend_test.go @@ -85,7 +85,7 @@ func TestEndToEnd(t *testing.T) { if err2 != nil { fmt.Println(err2.Error()) } - fmt.Println("--- myFriend.decryptedMess = ", string(decMess)) + fmt.Println("--- NBA myFriend.decryptedMess = ", string(decMess)) // simulates a new server to send a message to var intS1 client.InternalServer diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..da6290b --- /dev/null +++ b/server/server.go @@ -0,0 +1,55 @@ +package server + +import ( + "fmt" + "time" + + "forge.redroom.link/yves/meowlib" +) + +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)) + } +} + +// AsymEncryptMessage prepares a message to send to a specific internal server +func (ints *InternalServer) AsymEncryptMessage(Message []byte) (EncryptedMsg []byte, Signature []byte, err error) { + EncryptedMsg, Signature, err = meowlib.EncryptAndSign(ints.ServerData.PublicKey, ints.Me.Private, Message) + if err != nil { + fmt.Println(err.Error()) + return nil, nil, err + } + return EncryptedMsg, Signature, err +} + +// AsymDecryptMessage reads a message from a specific internal server +func (ints *InternalServer) AsymDecryptMessage(Message []byte, Signature []byte) (DecryptedMsg []byte, err error) { + DecryptedMsg, err = meowlib.DecryptAndCheck(ints.Me.Private, ints.ServerData.PublicKey, Message, Signature) + if err != nil { + fmt.Println(err.Error()) + return nil, err + } + return DecryptedMsg, err +}