2024-06-07 08:48:16 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2024-06-07 11:19:43 +02:00
|
|
|
"encoding/binary"
|
2024-06-07 08:48:16 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2024-06-07 11:28:01 +02:00
|
|
|
"time"
|
2024-06-07 08:48:16 +02:00
|
|
|
|
2024-06-07 11:19:43 +02:00
|
|
|
com "git.qowevisa.me/Qowevisa/gotell/communication"
|
2024-06-07 08:48:16 +02:00
|
|
|
"git.qowevisa.me/Qowevisa/gotell/env"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-06-12 21:33:33 +02:00
|
|
|
tlepCenter.Init()
|
2024-06-07 08:48:16 +02:00
|
|
|
loadingFileName := env.ServerFullchainFileName
|
|
|
|
cert, err := os.ReadFile(loadingFileName)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("client: load root cert: %s", err)
|
|
|
|
}
|
|
|
|
log.Printf("Certificate %s loaded successfully!\n", loadingFileName)
|
|
|
|
//
|
|
|
|
roots := x509.NewCertPool()
|
|
|
|
if ok := roots.AppendCertsFromPEM(cert); !ok {
|
|
|
|
log.Fatalf("client: failed to parse root certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &tls.Config{
|
|
|
|
RootCAs: roots,
|
|
|
|
}
|
|
|
|
|
|
|
|
host, err := env.GetHost()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
port, err := env.GetPort()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
service := fmt.Sprintf("%s:%d", host, port)
|
|
|
|
conn, err := tls.Dial("tcp", service, config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("client: dial: %s", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
log.Printf("client: connected to %s", service)
|
|
|
|
|
|
|
|
// Connect to the Electron.js application via WebSocket
|
|
|
|
u := url.URL{Scheme: "ws", Host: "localhost:8081", Path: "/ws"}
|
2024-06-07 11:28:01 +02:00
|
|
|
var ws *websocket.Conn
|
|
|
|
for {
|
|
|
|
ws, _, err = websocket.DefaultDialer.Dial(u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error: dial: %v\n", err)
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
2024-06-07 08:48:16 +02:00
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
go readFromServer(conn, ws)
|
|
|
|
go readFromWebSocket(conn, ws)
|
|
|
|
select {}
|
|
|
|
}
|
|
|
|
|
2024-06-07 20:57:28 +02:00
|
|
|
var r com.RegisteredUser
|
2024-06-08 20:39:35 +02:00
|
|
|
var tmpLink *com.Link
|
2024-06-12 21:30:19 +02:00
|
|
|
var tmpNick string
|
2024-06-07 20:57:28 +02:00
|
|
|
|
2024-06-07 08:48:16 +02:00
|
|
|
func readFromServer(conn net.Conn, ws *websocket.Conn) {
|
2024-06-07 11:29:30 +02:00
|
|
|
buf := make([]byte, 70000)
|
2024-06-07 08:48:16 +02:00
|
|
|
for {
|
|
|
|
n, err := conn.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("client: read: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2024-06-07 11:19:43 +02:00
|
|
|
msg, err := com.Decode(buf[:n])
|
2024-06-07 08:48:16 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: %#v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if msg == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-06-07 11:28:09 +02:00
|
|
|
log.Printf("client: readServer: received message from server: %v", *msg)
|
2024-06-07 11:19:43 +02:00
|
|
|
switch msg.ID {
|
|
|
|
case com.ID_SERVER_APPROVE_CLIENT_NICKNAME:
|
|
|
|
newID := binary.BigEndian.Uint16(msg.Data)
|
|
|
|
msg.FromID = newID
|
|
|
|
msg.Data = []byte{}
|
2024-06-07 20:57:28 +02:00
|
|
|
r.ID = newID
|
2024-06-12 21:30:19 +02:00
|
|
|
if tmpNick != "" {
|
|
|
|
r.Name = tmpNick
|
|
|
|
}
|
2024-06-07 20:57:28 +02:00
|
|
|
r.IsRegistered = true
|
|
|
|
break
|
2024-06-08 20:39:35 +02:00
|
|
|
case com.ID_SERVER_APPROVE_CLIENT_LINK:
|
|
|
|
if tmpLink == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
msg.ToID = tmpLink.UseCount
|
|
|
|
msg.Data = tmpLink.Data
|
2024-06-12 21:30:19 +02:00
|
|
|
// Crypto stuff
|
|
|
|
case com.ID_CLIENT_SEND_CLIENT_ECDH_PUBKEY:
|
|
|
|
t, err := tlepCenter.GetTLEP(msg.ToID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: GetTLEP: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = t.ECDHApplyOtherKeyBytes(msg.Data)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: ECDHApplyOtherKeyBytes: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
msg.Data = []byte{}
|
|
|
|
case com.ID_CLIENT_SEND_CLIENT_CBES_SPECS:
|
|
|
|
t, err := tlepCenter.GetTLEP(msg.ToID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: GetTLEP: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cbes, err := t.DecryptMessageEA(msg.Data)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: DecryptMessageEA: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = t.CBESSetFromBytes(cbes)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: CBESSetFromBytes: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// message
|
|
|
|
case com.ID_CLIENT_SEND_CLIENT_MESSAGE:
|
|
|
|
t, err := tlepCenter.GetTLEP(msg.ToID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: GetTLEP: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
decrypedMsg, err := t.DecryptMessageAtMax(msg.Data)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: DecryptMessageAtMax: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
msg.Data = decrypedMsg
|
|
|
|
// switch
|
2024-06-07 11:19:43 +02:00
|
|
|
}
|
2024-06-07 11:28:09 +02:00
|
|
|
log.Printf("client: readServer: sending message to websocket: %v", *msg)
|
2024-06-07 08:48:16 +02:00
|
|
|
ws.WriteJSON(*msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readFromWebSocket(conn net.Conn, ws *websocket.Conn) {
|
|
|
|
for {
|
2024-06-07 11:19:43 +02:00
|
|
|
var msg com.Message
|
2024-06-07 08:48:16 +02:00
|
|
|
err := ws.ReadJSON(&msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("WebSocket read error: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2024-06-07 11:28:09 +02:00
|
|
|
log.Printf("client: readWS: received message from Electron: %v", msg)
|
2024-06-07 20:57:28 +02:00
|
|
|
msg.Version = com.V1
|
|
|
|
switch msg.ID {
|
2024-06-12 21:30:19 +02:00
|
|
|
case com.ID_CLIENT_SEND_SERVER_NICKNAME:
|
|
|
|
tmpNick = string(msg.Data)
|
2024-06-08 20:39:35 +02:00
|
|
|
case com.ID_CLIENT_SEND_SERVER_LINK:
|
|
|
|
if !r.IsRegistered {
|
|
|
|
continue
|
|
|
|
}
|
2024-06-07 20:57:28 +02:00
|
|
|
l, err := r.GenerateLink(msg.ToID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error: link: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2024-06-08 20:39:35 +02:00
|
|
|
tmpLink = &l
|
2024-06-08 21:14:38 +02:00
|
|
|
answ, err := com.ClientSendServerLink(r.ID, l)
|
2024-06-07 20:57:28 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error: com: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Printf("client: readWS: sending data to server: %v", answ)
|
|
|
|
conn.Write(answ)
|
|
|
|
continue
|
2024-06-10 06:54:41 +02:00
|
|
|
case com.ID_CLIENT_ASK_CLIENT_HANDSHAKE,
|
|
|
|
com.ID_CLIENT_APPROVE_CLIENT_HANDSHAKE,
|
|
|
|
com.ID_CLIENT_DECLINE_CLIENT_HANDSHAKE,
|
|
|
|
com.ID_CLIENT_SEND_CLIENT_ECDH_PUBKEY,
|
|
|
|
com.ID_CLIENT_SEND_CLIENT_CBES_SPECS,
|
|
|
|
com.ID_CLIENT_SEND_CLIENT_MKLG_FINGERPRINT,
|
|
|
|
com.ID_CLIENT_DECLINE_CLIENT_MKLG_FINGERPRINT,
|
|
|
|
com.ID_CLIENT_SEND_CLIENT_MESSAGE:
|
2024-06-09 06:39:09 +02:00
|
|
|
if !r.IsRegistered {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
msg.FromID = r.ID
|
2024-06-10 06:54:41 +02:00
|
|
|
// switch
|
2024-06-07 20:57:28 +02:00
|
|
|
}
|
2024-06-12 21:30:19 +02:00
|
|
|
// Crypto stuff
|
|
|
|
switch msg.ID {
|
|
|
|
case com.ID_CLIENT_ASK_CLIENT_HANDSHAKE,
|
|
|
|
com.ID_CLIENT_APPROVE_CLIENT_HANDSHAKE:
|
2024-06-12 21:38:26 +02:00
|
|
|
err := tlepCenter.AddTLEP(msg.ToID, fmt.Sprintf("%s-%d", r.Name, msg.ToID))
|
2024-06-12 21:30:19 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlepCenter.AddUser: %v\n", err)
|
|
|
|
}
|
|
|
|
case com.ID_CLIENT_SEND_CLIENT_ECDH_PUBKEY:
|
|
|
|
t, err := tlepCenter.GetTLEP(msg.ToID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: GetTLEP: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
key, err := t.ECDHGetPublicKey()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: ECDHGetPublicKey: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
msg.Data = key
|
|
|
|
case com.ID_CLIENT_SEND_CLIENT_CBES_SPECS:
|
|
|
|
t, err := tlepCenter.GetTLEP(msg.ToID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: GetTLEP: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = t.CBESInitRandom()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: CBESInitRandom: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cbes, err := t.CBESGetBytes()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: ECDHGetPublicKey: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cbesEAEncr, err := t.EncryptMessageEA(cbes)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: EncryptMessageEA: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
msg.Data = cbesEAEncr
|
|
|
|
// message
|
|
|
|
case com.ID_CLIENT_SEND_CLIENT_MESSAGE:
|
|
|
|
t, err := tlepCenter.GetTLEP(msg.ToID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: GetTLEP: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
encrypedMsg, err := t.EncryptMessageAtMax(msg.Data)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: tlep: EncryptMessageAtMax: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
msg.Data = encrypedMsg
|
|
|
|
// switch
|
|
|
|
}
|
2024-06-07 08:48:16 +02:00
|
|
|
encodedMsg, err := msg.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Encoding error: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
2024-06-07 11:28:09 +02:00
|
|
|
log.Printf("client: readWS: sending data to server: %v", encodedMsg)
|
2024-06-07 08:48:16 +02:00
|
|
|
conn.Write(encodedMsg)
|
|
|
|
}
|
|
|
|
}
|