2024-04-16 12:23:00 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-04-17 14:09:42 +01:00
|
|
|
"PD1/internal/protocol"
|
2024-04-18 13:06:16 +01:00
|
|
|
"PD1/internal/utils/cryptoUtils"
|
2024-04-17 14:09:42 +01:00
|
|
|
"PD1/internal/utils/networking"
|
2024-04-16 12:23:00 +01:00
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2024-04-18 01:18:51 +01:00
|
|
|
func clientHandler(connection networking.Connection[protocol.Packet], dataStore DataStore) {
|
|
|
|
defer connection.Conn.Close()
|
|
|
|
|
2024-04-18 17:15:47 +01:00
|
|
|
clientCert := connection.GetPeerCertificate()
|
|
|
|
oidValueMap := cryptoUtils.ExtractAllOIDValues(clientCert)
|
|
|
|
fmt.Println(oidValueMap)
|
|
|
|
|
2024-04-17 14:09:42 +01:00
|
|
|
|
|
|
|
for {
|
2024-04-18 01:18:51 +01:00
|
|
|
pac := connection.Receive()
|
2024-04-17 14:09:42 +01:00
|
|
|
switch pac.Flag {
|
2024-04-18 01:18:51 +01:00
|
|
|
case protocol.ReqUserCertPkt:
|
2024-04-18 17:15:47 +01:00
|
|
|
//userCertPacket := dataStore.GetUserCertificate(uid)
|
|
|
|
//connection.Send(userCertPacket)
|
2024-04-18 01:18:51 +01:00
|
|
|
case protocol.ReqAllMsgPkt:
|
2024-04-17 14:09:42 +01:00
|
|
|
fmt.Println("ReqAllMsg")
|
2024-04-18 01:18:51 +01:00
|
|
|
case protocol.ReqMsgPkt:
|
2024-04-17 14:09:42 +01:00
|
|
|
fmt.Println("ReqMsg")
|
2024-04-18 01:18:51 +01:00
|
|
|
case protocol.SubmitMsgPkt:
|
2024-04-17 15:44:38 +01:00
|
|
|
fmt.Println("SubmitMsg")
|
2024-04-17 14:09:42 +01:00
|
|
|
}
|
|
|
|
}
|
2024-04-16 12:23:00 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func Run(port int) {
|
2024-04-18 01:18:51 +01:00
|
|
|
//Open connection to DB
|
|
|
|
dataStore := OpenDB()
|
|
|
|
defer dataStore.db.Close()
|
|
|
|
|
2024-04-18 17:15:47 +01:00
|
|
|
//FIX: Get the server's keystore path instead of hardcoding it
|
2024-04-18 13:06:16 +01:00
|
|
|
|
|
|
|
//Read server keystore
|
2024-04-18 17:15:47 +01:00
|
|
|
password := AskServerPassword()
|
2024-04-18 20:15:29 +01:00
|
|
|
serverKeyStore := cryptoUtils.LoadKeyStore("certs/server/server.p12",password)
|
2024-04-18 13:06:16 +01:00
|
|
|
|
2024-04-18 01:18:51 +01:00
|
|
|
//Create server listener
|
2024-04-18 17:15:47 +01:00
|
|
|
server := networking.NewServer[protocol.Packet](&serverKeyStore,port)
|
2024-04-18 01:18:51 +01:00
|
|
|
go server.ListenLoop()
|
2024-04-17 14:09:42 +01:00
|
|
|
|
2024-04-16 12:23:00 +01:00
|
|
|
for {
|
2024-04-17 15:44:38 +01:00
|
|
|
//Receive Connection via channel
|
2024-04-17 14:09:42 +01:00
|
|
|
conn := <-server.C
|
|
|
|
//Launch client handler via clientHandler
|
2024-04-18 01:18:51 +01:00
|
|
|
go clientHandler(conn, dataStore)
|
2024-04-16 12:23:00 +01:00
|
|
|
}
|
|
|
|
}
|