/
match.go
297 lines (259 loc) · 6.07 KB
/
match.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package match
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"sync"
"sync/atomic"
"time"
"unicode/utf8"
"github.com/gorilla/websocket"
)
type Matcher struct {
log bool
currentUserID int64
poolLock sync.Mutex
userPool map[*user]struct{}
turnSecret []byte
}
func NewMatcher(log bool, turnSecret string) *Matcher {
matcher := &Matcher{}
matcher.log = log
matcher.userPool = make(map[*user]struct{})
matcher.turnSecret = []byte(turnSecret)
return matcher
}
var upgrader = websocket.Upgrader{}
func (matcher *Matcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
userID := atomic.AddInt64(&matcher.currentUserID, 1)
logOutput := ioutil.Discard
if matcher.log {
logOutput = os.Stderr
}
logger := log.New(logOutput, "userID: "+strconv.FormatInt(userID, 10)+" ", log.Lshortfile)
logger.Printf("new user connected")
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
logger.Printf("%v %v", "websocket upgrade error:", err)
return
}
defer conn.Close()
userInfos := struct {
Name string `json:"name"`
}{}
err = conn.ReadJSON(&userInfos)
if err != nil {
logger.Printf("%v %v", "user infos retrieval error:", err)
return
}
if userInfos.Name == "" {
logger.Printf("%v", "empty user name")
return
}
if utf8.RuneCountInString(userInfos.Name) > 15 {
logger.Printf("%v", "user name too long")
return
}
logger.Printf("user infos retrieved, name: %v", userInfos.Name)
self := &user{
name: userInfos.Name,
conn: conn,
userID: userID,
logger: logger,
}
matcher.put(self)
for {
m := message{}
err := conn.ReadJSON(&m)
if err != nil {
logger.Printf("receive error, disconnecting: %v", err)
self.handleDisconnection(matcher)
return
}
switch m.Type {
case "start":
fallthrough
case "end":
logger.Printf("%v", "start or end sent by client")
return
case "next":
logger.Printf("next received, matchID: %v", m.MatchID)
self.handleNext(matcher, m.MatchID)
default:
self.stateLock.Lock()
if self.remotePeer != nil && m.MatchID == self.match.matchID {
_ = self.remotePeer.send(&m)
}
self.stateLock.Unlock()
}
}
}
func (matcher *Matcher) put(u *user) {
SeekPeer:
u.logger.Printf("seeking a peer")
var peer *user
matcher.poolLock.Lock()
for peer = range matcher.userPool {
delete(matcher.userPool, peer)
break
}
matcher.poolLock.Unlock()
if peer != nil {
u.logger.Printf("peer found, matching with %v", peer.userID)
var matchID uint32
err := binary.Read(rand.Reader, binary.BigEndian, &matchID)
if err != nil {
log.Fatal(err)
}
match := match{matchID: matchID}
match.Lock()
peer.stateLock.Lock()
if peer.disconnected {
u.logger.Printf("peer %v was disconnected", peer.userID)
peer.stateLock.Unlock()
goto SeekPeer
}
peer.match = &match
peer.remotePeer = u
peer.stateLock.Unlock()
u.stateLock.Lock()
u.match = &match
u.remotePeer = peer
u.stateLock.Unlock()
turnUsername, turnPassword := matcher.newTurnCreds()
_ = u.sendStart(peer.name, matchID, true, turnUsername, turnPassword)
turnUsername, turnPassword = matcher.newTurnCreds()
_ = peer.sendStart(u.name, matchID, false, turnUsername, turnPassword)
match.Unlock()
} else {
u.logger.Printf("no peer found, put in wait queue")
matcher.poolLock.Lock()
matcher.userPool[u] = struct{}{}
matcher.poolLock.Unlock()
}
}
func (matcher *Matcher) delete(u *user) {
matcher.poolLock.Lock()
delete(matcher.userPool, u)
matcher.poolLock.Unlock()
u.logger.Printf("deleted from pool")
}
type user struct {
name string
connLock sync.Mutex
conn *websocket.Conn
stateLock sync.Mutex
match *match
remotePeer *user
disconnected bool
userID int64
logger *log.Logger
}
func (u *user) send(m interface{}) error {
defer u.connLock.Unlock()
u.connLock.Lock()
return u.conn.WriteJSON(m)
}
func (u *user) sendStart(peerName string, matchID uint32, offer bool, turnUsername, turnPassword string) error {
return u.send(struct {
messageHeader
PeerName string `json:"peerName"`
Offer bool `json:"offer"`
TurnUsername string `json:"turnUsername,omitempty"`
TurnPassword string `json:"turnPassword,omitempty"`
}{
messageHeader{
"start",
matchID,
},
peerName,
offer,
turnUsername,
turnPassword,
})
}
func (u *user) sendEnd(matchID uint32) error {
return u.send(messageHeader{
"end",
matchID,
})
}
func (u *user) handleDisconnection(matcher *Matcher) {
u.stateLock.Lock()
u.disconnected = true
u.stateLock.Unlock()
match := u.getMatch()
if match != nil {
match.Lock()
u.unMatch(match.matchID)
match.Unlock()
} else {
matcher.delete(u)
}
}
func (u *user) handleNext(matcher *Matcher, matchID uint32) {
match := u.getMatch()
if match != nil && matchID == match.matchID {
match.Lock()
u.unMatch(match.matchID)
match.Unlock()
matcher.put(u)
}
}
func (u *user) getMatch() *match {
defer u.stateLock.Unlock()
u.stateLock.Lock()
return u.match
}
func (u *user) unMatch(matchID uint32) {
u.stateLock.Lock()
remotePeer := u.remotePeer
u.match = nil
u.remotePeer = nil
u.stateLock.Unlock()
if remotePeer != nil {
remotePeer.stateLock.Lock()
remotePeer.remotePeer = nil
_ = remotePeer.sendEnd(matchID)
remotePeer.stateLock.Unlock()
}
}
type match struct {
sync.Mutex
matchID uint32
}
type messageHeader struct {
Type string `json:"type"`
MatchID uint32 `json:"matchID"`
}
type message struct {
messageHeader
Data json.RawMessage `json:"data"`
}
func (matcher *Matcher) newTurnCreds() (string, string) {
username := ""
password := ""
if len(matcher.turnSecret) > 0 {
b := make([]byte, 6)
c, err := rand.Read(b)
if err != nil || c != 6 {
log.Fatal(err)
}
name := base64.StdEncoding.EncodeToString(b)
timestamp := time.Now().Unix() + 10
username = fmt.Sprintf("%d:%s", timestamp, name)
mac := hmac.New(sha1.New, matcher.turnSecret)
_, _ = mac.Write([]byte(username))
password = base64.StdEncoding.EncodeToString(mac.Sum(nil))
}
return username, password
}