[PD1] small changes

This commit is contained in:
Afonso Franco 2024-04-23 11:12:18 +01:00
parent 568b6e6739
commit 2cafc3163c
Signed by: afonso
SSH key fingerprint: SHA256:aiLbdlPwXKJS5wMnghdtod0SPy8imZjlVvCyUX9DJNk
10 changed files with 160 additions and 71 deletions

View file

@ -4,12 +4,10 @@ import (
"PD1/internal/protocol"
"PD1/internal/utils/cryptoUtils"
"PD1/internal/utils/networking"
"log"
)
//TODO: CREATE SERVER SIDE CHECKS FOR EVERYTHING
//TODO: LOGGING SYSTEM
//TODO: TELL THE USER THAT THE MESSAGE HAS BEEN RECEIVED BY THE SERVER
//TODO: ERROR PACKET TO SEND BACK TO USER
func clientHandler(connection networking.Connection[protocol.Packet], dataStore DataStore) {
defer connection.Conn.Close()
@ -18,10 +16,16 @@ func clientHandler(connection networking.Connection[protocol.Packet], dataStore
clientCert := connection.GetPeerCertificate()
//Get the OID values
oidMap := cryptoUtils.ExtractAllOIDValues(clientCert)
//Check if certificate usage is MSG SERVICE
usage := oidMap["2.5.4.11"]
if usage == "" {
log.Println("User certificate does not have the correct usage")
return
}
//Get the UID of this user
UID := oidMap["2.5.4.65"]
if UID == "" {
panic("User certificate does not specify it's PSEUDONYM")
log.Println("User certificate does not specify it's PSEUDONYM")
}
dataStore.storeUserCertIfNotExists(UID, *clientCert)
F:
@ -34,31 +38,47 @@ F:
case protocol.FlagGetUserCert:
reqUserCert := protocol.UnmarshalGetUserCert(pac.Body)
userCertPacket := dataStore.GetUserCertificate(reqUserCert.UID)
if active := connection.Send(userCertPacket); !active {
if !connection.Send(userCertPacket) {
break F
}
case protocol.FlagGetUnreadMsgsInfo:
getUnreadMsgsInfo := protocol.UnmarshalGetUnreadMsgsInfo(pac.Body)
messages := dataStore.GetUnreadMsgsInfo(UID,getUnreadMsgsInfo.Page,getUnreadMsgsInfo.PageSize)
var messages protocol.Packet
if getUnreadMsgsInfo.Page <= 0 || getUnreadMsgsInfo.PageSize <= 0 {
messages = protocol.NewReportErrorPacket("Page and PageSize need to be >= 1")
} else {
messages = dataStore.GetUnreadMsgsInfo(UID, getUnreadMsgsInfo.Page, getUnreadMsgsInfo.PageSize)
}
if !connection.Send(messages) {
break F
}
case protocol.FlagGetMsg:
reqMsg := protocol.UnmarshalGetMsg(pac.Body)
message := dataStore.GetMessage(UID, reqMsg.Num)
if active := connection.Send(message); !active {
var message protocol.Packet
if reqMsg.Num <= 0 {
message = protocol.NewReportErrorPacket("Message NUM needs to be >= 1")
} else {
message = dataStore.GetMessage(UID, reqMsg.Num)
}
if !connection.Send(message) {
break F
}
dataStore.MarkMessageInQueueAsRead(UID, reqMsg.Num)
case protocol.FlagSendMsg:
submitMsg := protocol.UnmarshalSendMsg(pac.Body)
if submitMsg.ToUID != UID && dataStore.userExists(submitMsg.ToUID) {
dataStore.AddMessageToQueue(UID, submitMsg)
var answerSendMsgPacket protocol.Packet
if submitMsg.ToUID == UID {
answerSendMsgPacket = protocol.NewReportErrorPacket("Cannot message yourself")
} else if !dataStore.userExists(submitMsg.ToUID) {
answerSendMsgPacket = protocol.NewReportErrorPacket("Message receiver does not exist in database")
} else {
answerSendMsgPacket = dataStore.AddMessageToQueue(UID, submitMsg)
}
if !connection.Send(answerSendMsgPacket) {
break F
}
}
}
}
func Run(port int) {
@ -69,7 +89,7 @@ func Run(port int) {
//FIX: Get the server's keystore path instead of hardcoding it
//Read server keystore
password := AskServerPassword()
password := readStdin("Insert keystore passphrase")
serverKeyStore := cryptoUtils.LoadKeyStore("certs/server/server.p12", password)
//Create server listener