[PD1] merge

This commit is contained in:
Tiago Sousa 2024-04-22 19:30:50 +01:00
commit b8efcf19b7
Signed by: tiago
SSH key fingerprint: SHA256:odOD9vln9U7qNe1R8o3UCbE3jkQCkr5/q5mgd5hwua0
14 changed files with 729 additions and 192 deletions

View file

@ -94,9 +94,8 @@ func (k *KeyStore) GetServerTLSConfig() *tls.Config {
caCertPool.AddCert(caCert)
}
tlsConfig.ClientCAs = caCertPool
//Request one valid or invalid certificate
// FIX: SERVER ACCEPTS CONNECTIONS WITH UNMATCHING OR
// NO CERTIFICATE, NEEDS TO BE CHANGED SOMEHOW
//FIX: SERVER ACCEPTS CONNECTIONS WITH UNMATCHING OR
// NO CERTIFICATE, NEEDS TO BE CHANGED SOMEHOW
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
return tlsConfig
}

View file

@ -4,6 +4,8 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"io"
"log"
)
type Connection[T any] struct {
@ -20,18 +22,33 @@ func NewConnection[T any](netConn *tls.Conn) Connection[T] {
}
}
func (c Connection[T]) Send(obj T) {
if err := c.encoder.Encode(&obj); err != nil {
panic("Failed encoding data or sending it to connection")
}
func (c Connection[T]) Send(obj T) bool {
if err := c.encoder.Encode(&obj); err!=nil {
if err == io.EOF {
log.Println("Connection closed by peer")
//Return false as connection not active
return false
} else {
log.Panic(err)
}
}
//Return true as connection active
return true
}
func (c Connection[T]) Receive() T {
func (c Connection[T]) Receive() (*T, bool) {
var obj T
if err := c.decoder.Decode(&obj); err != nil {
panic("Failed decoding data or reading it from connection")
if err == io.EOF {
log.Println("Connection closed by peer")
//Return false as connection not active
return nil,false
} else {
log.Panic(err)
}
}
return obj
//Return true as connection active
return &obj, true
}
func (c Connection[T]) GetPeerCertificate() *x509.Certificate {

View file

@ -43,7 +43,6 @@ func (s *Server[T]) ListenLoop() {
state := tlsConn.ConnectionState()
if len(state.PeerCertificates) == 0 {
fmt.Println(state.PeerCertificates)
log.Panicln("Client did not provide a certificate")
}
conn := NewConnection[T](tlsConn)