11package main
22
33import (
4- "crypto"
5- "crypto/rand"
64 "crypto/rsa"
7- "crypto/sha256"
8- "crypto/x509"
9- "encoding/pem"
105 "flag"
6+ "github.com/RoboCup-SSL/ssl-game-controller/internal/app/client"
117 "github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
128 "github.com/RoboCup-SSL/ssl-go-tools/pkg/sslconn"
13- "github.com/golang/protobuf/proto"
14- "io/ioutil"
159 "log"
1610 "net"
1711)
1812
13+ var udpAddress = flag .String ("udpAddress" , "224.5.23.1:10003" , "The multicast address of ssl-game-controller" )
14+ var autoDetectAddress = flag .Bool ("autoDetectHost" , true , "Automatically detect the game-controller host and replace it with the host given in address" )
1915var refBoxAddr = flag .String ("address" , "localhost:10007" , "Address to connect to" )
2016var privateKeyLocation = flag .String ("privateKey" , "" , "A private key to be used to sign messages" )
2117var clientIdentifier = flag .String ("identifier" , "test" , "The identifier of the client" )
@@ -30,47 +26,28 @@ type Client struct {
3026func main () {
3127 flag .Parse ()
3228
33- loadPrivateKey ()
29+ client .LoadPrivateKey (* privateKeyLocation )
30+
31+ if * autoDetectAddress {
32+ host := client .DetectHost (* udpAddress )
33+ if host != "" {
34+ log .Print ("Detected game-controller host: " , host )
35+ * refBoxAddr = client .SetHost (* refBoxAddr , host )
36+ }
37+ }
3438
3539 conn , err := net .Dial ("tcp" , * refBoxAddr )
3640 if err != nil {
3741 log .Fatal ("could not connect to game-controller at " , * refBoxAddr )
3842 }
3943 defer conn .Close ()
4044 log .Printf ("Connected to game-controller at %v" , * refBoxAddr )
41- client := Client {}
42- client .conn = conn
43-
44- client .register ()
45- client .sendGameEvent ()
46- client .sendAutoRefMessage ("Hello World" )
47- }
45+ c := Client {}
46+ c .conn = conn
4847
49- func loadPrivateKey () {
50- if * privateKeyLocation != "" {
51- privateKey = readPrivateKey ()
52- if privateKey != nil {
53- log .Print ("Found private key" )
54- } else {
55- log .Print ("No private key available" )
56- }
57- }
58- }
59-
60- func readPrivateKey () * rsa.PrivateKey {
61- b , err := ioutil .ReadFile (* privateKeyLocation )
62- if err != nil {
63- log .Fatal ("Could not find private key at " , * privateKeyLocation )
64- }
65- p , _ := pem .Decode (b )
66- if p .Type != "RSA PRIVATE KEY" {
67- log .Fatal ("Private key type is wrong: " , p .Type )
68- }
69- privateKey , err := x509 .ParsePKCS1PrivateKey (p .Bytes )
70- if err != nil {
71- log .Fatal (err )
72- }
73- return privateKey
48+ c .register ()
49+ c .sendGameEvent ()
50+ c .sendAutoRefMessage ("Hello World" )
7451}
7552
7653func (c * Client ) register () {
@@ -86,7 +63,7 @@ func (c *Client) register() {
8663 registration .Identifier = clientIdentifier
8764 if privateKey != nil {
8865 registration .Signature = & refproto.Signature {Token : controllerReply .NextToken , Pkcs1V15 : []byte {}}
89- registration .Signature .Pkcs1V15 = sign (privateKey , & registration )
66+ registration .Signature .Pkcs1V15 = client . Sign (privateKey , & registration )
9067 }
9168 log .Print ("Sending registration" )
9269 if err := sslconn .SendMessage (c .conn , & registration ); err != nil {
@@ -136,7 +113,7 @@ func (c *Client) sendAutoRefMessage(msg string) {
136113func (c * Client ) sendRequest (request * refproto.AutoRefToControllerRequest ) {
137114 if privateKey != nil {
138115 request .Signature = & refproto.Signature {Token : & c .token , Pkcs1V15 : []byte {}}
139- request .Signature .Pkcs1V15 = sign (privateKey , request )
116+ request .Signature .Pkcs1V15 = client . Sign (privateKey , request )
140117 }
141118
142119 log .Print ("Sending " , request )
@@ -160,18 +137,3 @@ func (c *Client) sendRequest(request *refproto.AutoRefToControllerRequest) {
160137 c .token = ""
161138 }
162139}
163-
164- func sign (privateKey * rsa.PrivateKey , message proto.Message ) []byte {
165- messageBytes , err := proto .Marshal (message )
166- if err != nil {
167- log .Fatal (err )
168- }
169- hash := sha256 .New ()
170- hash .Write (messageBytes )
171- d := hash .Sum (nil )
172- signature , err := rsa .SignPKCS1v15 (rand .Reader , privateKey , crypto .SHA256 , d )
173- if err != nil {
174- log .Fatal (err )
175- }
176- return signature
177- }
0 commit comments