Skip to content

Commit 84eabbb

Browse files
committed
[feature] Add a TLS protected optional endpoint for team/autoRef endpoint
1 parent c080a7b commit 84eabbb

File tree

6 files changed

+61
-0
lines changed

6 files changed

+61
-0
lines changed

config/ssl-game-controller.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ network:
55
server:
66
auto-ref:
77
address: :10007
8+
address-tls: :10107
89
trusted-keys-dir: config/trusted_keys/auto_ref
910
team:
1011
address: :10008
12+
address-tls: :10108
1113
trusted-keys-dir: config/trusted_keys/team
1214
game:
1315
yellow-card-duration: 2m

internal/app/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ type Server struct {
6161
// ServerAutoRef holds configs for the autoRef server
6262
type ServerAutoRef struct {
6363
Address string `yaml:"address"`
64+
AddressTls string `yaml:"address-tls"`
6465
TrustedKeysDir string `yaml:"trusted-keys-dir"`
6566
}
6667

6768
// ServerTeam holds configs for the team server
6869
type ServerTeam struct {
6970
Address string `yaml:"address"`
71+
AddressTls string `yaml:"address-tls"`
7072
TrustedKeysDir string `yaml:"trusted-keys-dir"`
7173
}
7274

@@ -130,8 +132,10 @@ func DefaultControllerConfig() (c Controller) {
130132
c.Game.DefaultDivision = DivA
131133

132134
c.Server.AutoRef.Address = ":10007"
135+
c.Server.AutoRef.AddressTls = ":10107"
133136
c.Server.AutoRef.TrustedKeysDir = "config/trusted_keys/auto_ref"
134137
c.Server.Team.Address = ":10008"
138+
c.Server.Team.AddressTls = ":10108"
135139
c.Server.Team.TrustedKeysDir = "config/trusted_keys/team"
136140

137141
c.Game.DefaultGeometry = map[Division]*Geometry{}

internal/app/config/testdata/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ network:
55
server:
66
auto-ref:
77
address: :10007
8+
address-tls: :10107
89
trusted-keys-dir: config/trusted_keys/auto_ref
910
team:
1011
address: :10008
12+
address-tls: :10108
1113
trusted-keys-dir: config/trusted_keys/team
1214
game:
1315
yellow-card-duration: 2m

internal/app/controller/controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ func (c *GameController) Run() {
8383
go c.mainLoop()
8484
go c.publishToNetwork()
8585
go c.AutoRefServer.Listen(c.Config.Server.AutoRef.Address)
86+
go c.AutoRefServer.ListenTls(c.Config.Server.AutoRef.AddressTls)
8687
go c.TeamServer.Listen(c.Config.Server.Team.Address)
88+
go c.TeamServer.ListenTls(c.Config.Server.Team.AddressTls)
8789
}
8890

8991
// setupTimeProvider changes the time provider to the vision receiver, if configured

internal/app/rcon/server.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"crypto"
55
"crypto/rsa"
66
"crypto/sha256"
7+
"crypto/tls"
78
"crypto/x509"
89
"encoding/pem"
910
"github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
1011
"github.com/golang/protobuf/proto"
1112
"io/ioutil"
1213
"log"
1314
"net"
15+
"os"
1416
"strings"
1517
)
1618

@@ -53,6 +55,41 @@ func (s *Server) Listen(address string) {
5355
}
5456
}
5557

58+
func (s *Server) ListenTls(address string) {
59+
60+
if _, err := os.Stat("server.crt"); os.IsNotExist(err) {
61+
log.Println("Missing certificate for TLS endpoint. Put a server.crt in the working dir.")
62+
return
63+
}
64+
if _, err := os.Stat("server.key"); os.IsNotExist(err) {
65+
log.Println("Missing certificate key for TLS endpoint. Put a server.key in the working dir.")
66+
return
67+
}
68+
69+
cer, err := tls.LoadX509KeyPair("server.crt", "server.key")
70+
if err != nil {
71+
log.Printf("Could not load X509 key pair: %v", err)
72+
return
73+
}
74+
75+
config := &tls.Config{Certificates: []tls.Certificate{cer}}
76+
listener, err := tls.Listen("tcp", address, config)
77+
if err != nil {
78+
log.Printf("Failed to listen on %v: %v", address, err)
79+
return
80+
}
81+
log.Print("Listening on ", address)
82+
83+
for {
84+
conn, err := listener.Accept()
85+
if err != nil {
86+
log.Print("Could not accept connection: ", err)
87+
} else {
88+
go s.ConnectionHandler(conn)
89+
}
90+
}
91+
}
92+
5693
func (s *Server) CloseConnection(conn net.Conn, id string) {
5794
delete(s.Clients, id)
5895
log.Printf("Connection to %v closed", id)

tools/newX509KeyPair.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
# Generate a new x509 key-pair.
4+
# Ref: https://github.com/denji/golang-tls
5+
6+
# Key considerations for algorithm "RSA" ≥ 2048-bit
7+
openssl genrsa -out server.key 2048
8+
9+
# Key considerations for algorithm "ECDSA" ≥ secp384r1
10+
# List ECDSA the supported curves (openssl ecparam -list_curves)
11+
openssl ecparam -genkey -name secp384r1 -out server.key
12+
13+
# Generation of self-signed(x509) public key (PEM-encodings .pem|.crt) based on the private (.key)
14+
openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650

0 commit comments

Comments
 (0)