tricrypt/cmd/server/main.go

115 lines
2.4 KiB
Go
Raw Normal View History

2024-02-23 14:28:59 +01:00
package main
import (
2024-02-25 02:36:00 +01:00
"crypto/rand"
2024-02-23 17:48:25 +01:00
"crypto/tls"
2024-06-07 08:48:13 +02:00
"encoding/binary"
2024-02-23 14:28:59 +01:00
"fmt"
2024-02-23 18:23:31 +01:00
"io"
2024-02-23 14:28:59 +01:00
"log"
2024-02-23 17:48:25 +01:00
"net"
2024-06-07 08:48:13 +02:00
com "git.qowevisa.me/Qowevisa/gotell/communication"
2024-02-23 17:48:25 +01:00
"git.qowevisa.me/Qowevisa/gotell/env"
2024-02-23 14:28:59 +01:00
)
2024-02-23 17:48:25 +01:00
func main() {
2024-06-07 08:48:13 +02:00
userCenter.Init()
2024-02-23 18:02:43 +01:00
host, err := env.GetHost()
if err != nil {
2024-02-23 18:23:31 +01:00
log.Fatal(err)
2024-02-23 18:02:43 +01:00
}
port, err := env.GetPort()
if err != nil {
2024-02-23 18:23:31 +01:00
log.Fatal(err)
2024-02-23 18:02:43 +01:00
}
2024-02-25 02:36:00 +01:00
cert, err := tls.LoadX509KeyPair(
env.ServerFullchainFileName,
env.ServerPrivKeyFileName,
)
2024-02-23 15:32:08 +01:00
if err != nil {
2024-02-25 02:36:00 +01:00
log.Fatalf("server: loadkeys: %s", err)
2024-02-23 14:28:59 +01:00
}
2024-02-25 02:36:00 +01:00
config := tls.Config{Certificates: []tls.Certificate{cert}, ClientAuth: tls.NoClientCert}
config.Rand = rand.Reader
service := fmt.Sprintf("%s:%d", host, port)
listener, err := tls.Listen("tcp", service, &config)
2024-02-23 18:23:31 +01:00
if err != nil {
2024-02-25 02:36:00 +01:00
log.Fatalf("server: listen: %s", err)
2024-02-23 18:23:31 +01:00
}
2024-02-25 02:36:00 +01:00
log.Printf("server: listening on %s", service)
2024-02-23 17:48:25 +01:00
2024-02-25 02:36:00 +01:00
for {
conn, err := listener.Accept()
if err != nil {
log.Printf("server: accept: %s", err)
break
}
log.Printf("server: accepted from %s", conn.RemoteAddr())
go handleClient(conn)
2024-02-23 18:23:31 +01:00
}
2024-02-25 02:36:00 +01:00
}
2024-02-23 17:48:25 +01:00
2024-02-25 02:36:00 +01:00
func handleClient(conn net.Conn) {
defer conn.Close()
buf := make([]byte, 512)
2024-06-07 08:48:13 +02:00
ask, err := com.ServerAskClientAboutNickname()
2024-05-01 16:20:53 +02:00
if err != nil {
log.Printf("ERROR: %#v\n", err)
} else {
2024-05-01 16:32:23 +02:00
log.Printf("Trying to send %#v\n", ask)
_, err = conn.Write(ask)
if err != nil {
log.Printf("ERROR: %#v\n", err)
}
2024-05-01 16:20:53 +02:00
}
2024-02-23 18:23:31 +01:00
for {
2024-02-25 02:36:00 +01:00
log.Print("server: conn: waiting")
n, err := conn.Read(buf)
if err != nil {
if err != io.EOF {
log.Printf("server: conn: read: %s", err)
}
break
}
2024-06-07 08:48:13 +02:00
msg, err := com.Decode(buf[:n])
2024-05-01 16:32:23 +02:00
if err != nil {
log.Printf("ERROR: %#v\n", err)
continue
}
2024-06-07 08:48:13 +02:00
if msg == nil {
log.Printf("ERROR MSG IS NIL\n")
continue
}
log.Printf("server: conn: receive %#v\n", *msg)
// Handle
switch msg.ID {
case com.ID_CLIENT_SEND_SERVER_NICKNAME:
id, err := userCenter.AddUser(string(msg.Data))
if err != nil {
answ, err := com.ServerSendClientDecline()
if err != nil {
log.Printf("ERROR: %v\n", err)
continue
}
conn.Write(answ)
} else {
idBytes := make([]byte, 4)
binary.BigEndian.PutUint32(idBytes, uint32(id))
answ, err := com.ServerSendClientHisID(idBytes)
if err != nil {
log.Printf("ERROR: %v\n", err)
continue
}
conn.Write(answ)
}
case com.ID_CLIENT_SEND_SERVER_LINK:
default:
2024-02-23 17:48:25 +01:00
}
2024-06-07 08:48:13 +02:00
// Handle
2024-02-23 17:48:25 +01:00
}
2024-02-25 02:36:00 +01:00
log.Println("server: conn: closed")
2024-02-23 17:48:25 +01:00
}