tricrypt/cmd/cgui/main.go

156 lines
3.3 KiB
Go
Raw Normal View History

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() {
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-07 20:57:28 +02:00
2024-06-07 08:48:16 +02:00
func readFromServer(conn net.Conn, ws *websocket.Conn) {
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
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-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-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
case com.ID_CLIENT_ASK_CLIENT_HANDSHAKE:
if !r.IsRegistered {
continue
}
msg.FromID = r.ID
2024-06-07 20:57:28 +02:00
}
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)
}
}