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-19 11:55:16 +01:00
|
|
|
|
|
|
|
//Get certificate sent by user
|
2024-04-19 02:19:22 +01:00
|
|
|
clientCert := connection.GetPeerCertificate()
|
2024-04-19 11:55:16 +01:00
|
|
|
//Get the OID values
|
|
|
|
oidMap := cryptoUtils.ExtractAllOIDValues(clientCert)
|
|
|
|
//Get the UID of this user
|
|
|
|
UID := oidMap["2.5.4.65"]
|
2024-04-19 23:59:26 +01:00
|
|
|
if UID == "" {
|
2024-04-19 11:55:16 +01:00
|
|
|
panic("User certificate does not specify it's PSEUDONYM")
|
|
|
|
}
|
2024-04-19 23:59:26 +01:00
|
|
|
dataStore.storeUserCertIfNotExists(UID, *clientCert)
|
|
|
|
F:
|
2024-04-17 14:09:42 +01:00
|
|
|
for {
|
2024-04-19 23:59:26 +01:00
|
|
|
pac, active := connection.Receive()
|
|
|
|
if !active {
|
|
|
|
break F
|
|
|
|
}
|
2024-04-17 14:09:42 +01:00
|
|
|
switch pac.Flag {
|
2024-04-18 01:18:51 +01:00
|
|
|
case protocol.ReqUserCertPkt:
|
2024-04-19 23:59:26 +01:00
|
|
|
reqUserCert := protocol.UnmarshalRequestUserCertPacket(pac.Body)
|
|
|
|
userCertPacket := dataStore.GetUserCertificate(reqUserCert.UID)
|
|
|
|
if active := connection.Send(userCertPacket); !active {
|
|
|
|
break F
|
|
|
|
}
|
|
|
|
case protocol.ReqMsgsQueue:
|
|
|
|
_ = protocol.UnmarshalRequestMsgsQueuePacket(pac.Body)
|
|
|
|
messages := dataStore.GetUnreadMessagesInfoQueue(UID)
|
|
|
|
fmt.Printf("Number of unread messages by user %v is %v\n",UID,len(messages))
|
|
|
|
for _, message := range messages {
|
|
|
|
if !connection.Send(message) {
|
|
|
|
break
|
|
|
|
}
|
2024-04-19 11:55:16 +01:00
|
|
|
}
|
2024-04-18 01:18:51 +01:00
|
|
|
case protocol.ReqMsgPkt:
|
2024-04-19 23:59:26 +01:00
|
|
|
reqMsg := protocol.UnmarshalRequestMsgPacket(pac.Body)
|
|
|
|
message := dataStore.GetMessage(UID, reqMsg.Num)
|
|
|
|
if active := connection.Send(message); !active {
|
|
|
|
break F
|
|
|
|
}
|
2024-04-20 00:55:16 +01:00
|
|
|
dataStore.MarkMessageInQueueAsRead(UID, reqMsg.Num)
|
2024-04-18 01:18:51 +01:00
|
|
|
case protocol.SubmitMsgPkt:
|
2024-04-19 23:59:26 +01:00
|
|
|
submitMsg := protocol.UnmarshalSubmitMessagePacket(pac.Body)
|
|
|
|
if submitMsg.ToUID != UID && dataStore.userExists(submitMsg.ToUID) {
|
|
|
|
dataStore.AddMessageToQueue(UID, 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-19 02:19:22 +01:00
|
|
|
//FIX: Get the server's keystore path instead of hardcoding it
|
2024-04-18 13:06:16 +01:00
|
|
|
|
2024-04-19 02:19:22 +01:00
|
|
|
//Read server keystore
|
|
|
|
password := AskServerPassword()
|
|
|
|
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-19 02:19:22 +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
|
|
|
}
|
|
|
|
}
|