forked from RoboCup-SSL/ssl-game-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoRefServer.go
146 lines (130 loc) · 3.82 KB
/
autoRefServer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package rcon
import (
"github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
"github.com/RoboCup-SSL/ssl-go-tools/pkg/sslconn"
"github.com/odeke-em/go-uuid"
"github.com/pkg/errors"
"io"
"log"
"net"
)
type AutoRefServer struct {
ProcessRequest func(string, refproto.AutoRefToController) error
*Server
}
type AutoRefClient struct {
*Client
}
func NewAutoRefServer() (s *AutoRefServer) {
s = new(AutoRefServer)
s.ProcessRequest = func(string, refproto.AutoRefToController) error { return nil }
s.Server = NewServer()
s.ConnectionHandler = s.handleClientConnection
return
}
func (c *AutoRefClient) receiveRegistration(server *AutoRefServer) error {
registration := refproto.AutoRefRegistration{}
if err := sslconn.ReceiveMessage(c.Conn, ®istration); err != nil {
return err
}
if registration.Identifier == nil || len(*registration.Identifier) < 1 {
return errors.New("No identifier specified")
}
c.Id = *registration.Identifier
if _, exists := server.Clients[c.Id]; exists {
return errors.New("Client with given identifier already registered: " + c.Id)
}
c.PubKey = server.TrustedKeys[c.Id]
if c.PubKey != nil {
if err := c.verifyRegistration(registration); err != nil {
return err
}
} else {
c.Token = ""
}
c.reply(c.Ok())
return nil
}
func (c *AutoRefClient) verifyRegistration(registration refproto.AutoRefRegistration) error {
if registration.Signature == nil {
return errors.New("Missing signature")
}
if registration.Signature.Token == nil || *registration.Signature.Token != c.Token {
sendToken := ""
if registration.Signature.Token != nil {
sendToken = *registration.Signature.Token
}
return errors.Errorf("Client %v sent an invalid token: %v != %v", c.Id, sendToken, c.Token)
}
signature := registration.Signature.Pkcs1V15
registration.Signature.Pkcs1V15 = []byte{}
err := VerifySignature(c.PubKey, ®istration, signature)
registration.Signature.Pkcs1V15 = signature
if err != nil {
return errors.New("Invalid signature")
}
c.Token = uuid.New()
return nil
}
func (c *AutoRefClient) verifyRequest(req refproto.AutoRefToController) error {
if req.Signature == nil {
return errors.New("Missing signature")
}
if req.Signature.Token == nil || *req.Signature.Token != c.Token {
sendToken := ""
if req.Signature.Token != nil {
sendToken = *req.Signature.Token
}
return errors.Errorf("Invalid token: %v != %v", sendToken, c.Token)
}
signature := req.Signature.Pkcs1V15
req.Signature.Pkcs1V15 = []byte{}
err := VerifySignature(c.PubKey, &req, signature)
req.Signature.Pkcs1V15 = signature
if err != nil {
return errors.Wrap(err, "Verification failed.")
}
c.Token = uuid.New()
return nil
}
func (s *AutoRefServer) handleClientConnection(conn net.Conn) {
defer conn.Close()
client := AutoRefClient{Client: &Client{Conn: conn, Token: uuid.New()}}
client.reply(client.Ok())
err := client.receiveRegistration(s)
if err != nil {
client.reply(client.Reject(err.Error()))
return
}
s.Clients[client.Id] = client.Client
defer s.CloseConnection(conn, client.Id)
log.Printf("Client %v connected", client.Id)
for {
req := refproto.AutoRefToController{}
if err := sslconn.ReceiveMessage(conn, &req); err != nil {
if err == io.EOF {
return
}
log.Print(err)
continue
}
if client.PubKey != nil {
if err := client.verifyRequest(req); err != nil {
client.reply(client.Reject(err.Error()))
continue
}
}
if err := s.ProcessRequest(client.Id, req); err != nil {
client.reply(client.Reject(err.Error()))
} else {
client.reply(client.Ok())
}
}
}
func (c *AutoRefClient) reply(reply refproto.ControllerReply) {
msg := refproto.ControllerToAutoRef_ControllerReply{ControllerReply: &reply}
response := refproto.ControllerToAutoRef{Msg: &msg}
if err := sslconn.SendMessage(c.Conn, &response); err != nil {
log.Print("Failed to send reply: ", err)
}
}