[PD2] Server done?
Co-authored-by: tsousa111 <tiagao2001@hotmail.com>
This commit is contained in:
parent
08a73a4f76
commit
49a29e43a7
66 changed files with 2777 additions and 5 deletions
23
Projs/PD2/internal/utils/networking/client.go
Normal file
23
Projs/PD2/internal/utils/networking/client.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package networking
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
)
|
||||
|
||||
|
||||
type ClientTLSConfigProvider interface {
|
||||
GetClientTLSConfig() *tls.Config
|
||||
}
|
||||
|
||||
type Client[T any] struct {
|
||||
Connection Connection[T]
|
||||
}
|
||||
|
||||
func NewClient[T any](clientTLSConfigProvider ClientTLSConfigProvider) (Client[T],error) {
|
||||
dialConn, err := tls.Dial("tcp", "localhost:8080", clientTLSConfigProvider.GetClientTLSConfig())
|
||||
if err != nil {
|
||||
return Client[T]{},err
|
||||
}
|
||||
conn := NewConnection[T](dialConn)
|
||||
return Client[T]{Connection: conn},nil
|
||||
}
|
51
Projs/PD2/internal/utils/networking/connection.go
Normal file
51
Projs/PD2/internal/utils/networking/connection.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package networking
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Connection[T any] struct {
|
||||
Conn *tls.Conn
|
||||
encoder *json.Encoder
|
||||
decoder *json.Decoder
|
||||
}
|
||||
|
||||
func NewConnection[T any](netConn *tls.Conn) Connection[T] {
|
||||
return Connection[T]{
|
||||
Conn: netConn,
|
||||
encoder: json.NewEncoder(netConn),
|
||||
decoder: json.NewDecoder(netConn),
|
||||
}
|
||||
}
|
||||
|
||||
func (c Connection[T]) Send(obj T) error {
|
||||
if err := c.encoder.Encode(&obj); err!=nil {
|
||||
if err == io.EOF {
|
||||
log.Println("Connection closed by peer")
|
||||
}
|
||||
return err
|
||||
}
|
||||
//Return true as connection active
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Connection[T]) Receive() (*T, error) {
|
||||
var obj T
|
||||
if err := c.decoder.Decode(&obj); err != nil {
|
||||
if err == io.EOF {
|
||||
log.Println("Connection closed by peer")
|
||||
}
|
||||
return nil,err
|
||||
}
|
||||
//Return true as connection active
|
||||
return &obj, nil
|
||||
}
|
||||
|
||||
func (c Connection[T]) GetPeerCertificate() *x509.Certificate {
|
||||
state := c.Conn.ConnectionState()
|
||||
return state.PeerCertificates[0]
|
||||
}
|
56
Projs/PD2/internal/utils/networking/server.go
Normal file
56
Projs/PD2/internal/utils/networking/server.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package networking
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
type ServerTLSConfigProvider interface {
|
||||
GetServerTLSConfig() *tls.Config
|
||||
}
|
||||
|
||||
type Server[T any] struct {
|
||||
listener net.Listener
|
||||
C chan Connection[T]
|
||||
}
|
||||
|
||||
func NewServer[T any](serverTLSConfigProvider ServerTLSConfigProvider) (Server[T], error) {
|
||||
|
||||
listener, err := tls.Listen("tcp", "127.0.0.1:8080", serverTLSConfigProvider.GetServerTLSConfig())
|
||||
if err != nil {
|
||||
return Server[T]{}, err
|
||||
}
|
||||
return Server[T]{
|
||||
listener: listener,
|
||||
C: make(chan Connection[T]),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server[T]) ListenLoop() {
|
||||
|
||||
for {
|
||||
listenerConn, err := s.listener.Accept()
|
||||
if err != nil {
|
||||
log.Println("Server could not accept connection")
|
||||
continue
|
||||
}
|
||||
tlsConn, ok := listenerConn.(*tls.Conn)
|
||||
if !ok {
|
||||
log.Println("Connection is not a TLS connection")
|
||||
continue
|
||||
}
|
||||
if err := tlsConn.Handshake(); err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
state := tlsConn.ConnectionState()
|
||||
if len(state.PeerCertificates) == 0 {
|
||||
log.Println("Client did not provide a certificate")
|
||||
continue
|
||||
}
|
||||
conn := NewConnection[T](tlsConn)
|
||||
s.C <- conn
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue