CSI-ES-2324/Projs/PD1/internal/protocol/protocol.go
afonso cdaae8fb7e
[PD1] Structured client.
Still need to create crypto object and use it to encrypt messages
Need to create TLS still
2024-04-17 15:44:38 +01:00

122 lines
2.2 KiB
Go

package protocol
import (
"time"
)
type PacketType int
const (
ReqPK PacketType = iota
ReqAllMsg
ReqMsg
SubmitMsg
SendPK
Msg
)
type PacketBody interface{}
type Packet struct {
Flag PacketType
Body PacketBody
}
// Client --> Server: Ask for a user's public key
type RequestPubKey struct {
FromUID string
KeyUID string
}
func NewRequestPubKey(fromUID, keyUID string) Packet {
return Packet{
Flag: ReqPK,
Body: RequestPubKey{
FromUID: fromUID,
KeyUID: keyUID,
},
}
}
// Client --> Server: Ask for all the client's messages in the queue
type RequestAllMsg struct {
FromUID string
}
func NewRequestAllMsg(fromUID string) Packet {
return Packet{
Flag: ReqAllMsg,
Body: RequestAllMsg{
FromUID: fromUID,
},
}
}
// Client --> Server: Ask for a specific message in the queue
type RequestMsg struct {
Num uint16
}
func NewRequestMsg(num uint16) Packet {
return Packet{
Flag: ReqMsg,
Body: RequestMsg{
Num: num,
},
}
}
// Client --> Server: Send message from client to server
type SubmitMessage struct {
ToUID string
Subject []byte
Body []byte
}
func NewSubmitMessage(toUID string, subject, body []byte) Packet {
return Packet{
Flag: SubmitMsg,
Body: SubmitMessage{
ToUID: toUID,
Subject: subject,
Body: body,
},
}
}
// Server --> Client: Send the client the requested public key
type SendPubKey struct {
Key []byte
}
func NewSendPubKey(key []byte) Packet {
return Packet{
Flag: SendPK,
Body: SendPubKey{
Key: key,
},
}
}
// Server --> Client: Send the client a message
type Message struct {
FromUID string
ToUID string
Subject []byte
Body []byte
Timestamp time.Time
}
func NewMessage(fromUID, toUID string, subject, body []byte, timestamp time.Time) Packet {
return Packet{
Flag: Msg,
Body: Message{
FromUID: fromUID,
ToUID: toUID,
Subject: subject,
Body: body,
Timestamp: timestamp,
},
}
}