/
player.go
310 lines (260 loc) · 7.42 KB
/
player.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
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"crypto/md5"
"crypto/subtle"
"fmt"
"net"
"sync"
"time"
"github.com/dchest/uniuri"
"github.com/kurafuto/kyubu/modern/minimal"
"github.com/kurafuto/kyubu/packets"
)
type PlayerState int
const (
Connecting PlayerState = iota
Identification
Idle // this means we're just proxying packets for this user now.
Disconnected
)
// compareHash compares a player's given "MpPass" against the computed hash
// using the server's salt and the player's username. It uses crypto/subtle
// to avoid any super-easy timing attacks.
func compareHash(salt, name, mpPass string) bool {
h := md5.New()
h.Write([]byte(salt))
h.Write([]byte(name))
sum := fmt.Sprintf("%x", h.Sum(nil))
return subtle.ConstantTimeCompare([]byte(sum), []byte(mpPass)) == 1
}
type BoundInfo struct {
Conn net.Conn
Parser *Parser
C chan packets.Packet
}
// Empty attempts to eat up all the packets in the packet channel.
func (b *BoundInfo) Empty() {
for {
_, ok := <-b.C
if !ok {
break
}
}
}
type Player struct {
Id string
Name string // From 0x00 Identification packet
CPE bool // Does this player claim CPE support?
Client BoundInfo // Client <-> Balancer
Server BoundInfo // Balancer <-> Server
State PlayerState
quit, quitting bool
hub string
ku *Kurafuto
qMutex sync.Mutex
}
// Remote returns a player's remote address (connecting IP) as a string.
func (p *Player) Remote() string {
return p.Client.Conn.RemoteAddr().String()
}
func (p *Player) Quit() {
p.qMutex.Lock()
if p.quit || p.quitting {
p.qMutex.Unlock()
return
}
p.quitting = true
p.qMutex.Unlock()
p.State = Disconnected
rem := p.ku.Remove(p) // Ensure we're removed from the server's player list
Debugf("(%s) Remove(p) == %v", p.Id, rem)
go func() {
// Wait a bit to write any packets still in the queue.
time.Sleep(300 * time.Millisecond)
p.qMutex.Lock()
p.quit = true
p.qMutex.Unlock()
p.Client.Parser.Finish()
p.Server.Parser.Finish()
// p.{Client,Server}.Parser might be nil if we hit an error whilst
// dialing the remote.
if p.Client.Parser != nil {
p.Client.Parser.Disable = true
p.Client.Parser.UnregisterAll()
}
if p.Server.Parser != nil {
p.Server.Parser.Disable = true
p.Server.Parser.UnregisterAll()
}
if p.Client.Conn != nil {
p.Client.Conn.Close()
}
if p.Server.Conn != nil {
p.Server.Conn.Close()
}
close(p.Client.C)
close(p.Server.C)
}()
}
// Kick sends attempts to send a player a DisconnectPlayer packet, then quits
// their connection. If packets.NewDisconnectPlayer returns an error, p.Quit is
// called, and the error is returned.
func (p *Player) Kick(msg string) error {
disc, err := classic.NewDisconnectPlayer(msg)
if err != nil {
p.Quit()
return err
}
p.Client.C <- disc
p.Quit()
return nil
}
// Dial (attempts to) make an outbound connection to the stored hub address.
func (p *Player) Dial() bool {
server, err := net.Dial("tcp", p.hub)
if err != nil {
Infof("(%s) Unable to dial hub: %s", p.Remote(), p.hub)
Debugf("(%s) Unable to dial remote server: %s (%s)", p.Id, p.hub, err.Error())
p.Quit()
return false
}
p.Server.Conn = server
p.State = Identification
return true
}
func (p *Player) Redirect(address string, port int) error {
// TODO: Close p.Server, reopen, reboot write() loop.
// This will mean we'll have to buffer packets to retransmit, won't we?
return nil
}
func (p *Player) readParse(parser packets.Parser, to chan packets.Packet) {
defer func() {
err := recover()
if err == nil {
return
}
p.qMutex.Lock()
defer p.qMutex.Unlock()
if p.quit || p.quitting {
return
}
panic(err) // Like the mutex matters now..
}()
for {
packet, err := parser.Next()
if err == ErrParserFinished {
break
}
if err == ErrPacketSkipped {
continue
}
if packet == nil || err != nil {
Debugf("(%s) readParse(): packet:%+v, err:%#v", p.Id, packet, err)
p.Quit()
return
}
to <- packet
}
}
func (p *Player) writeParse(pack <-chan packets.Packet, conn net.Conn) {
defer func() {
if err := recover(); !p.quitting && err != nil {
panic(err)
}
}()
for {
packet, ok := <-pack
if packet == nil || !ok {
Debugf("(%s) writeParse(): nil packet? ok:%v", p.Id, ok)
p.Quit()
return
}
n, err := conn.Write(packet.Bytes())
if err != nil {
Debugf("(%s) writeParse(): conn.Write err: %#v", p.Id, err)
p.Quit()
return
}
if n != packet.Size() {
Debugf("(%s) Packet %#.2x is %d bytes, but %d was written", p.Id, packet.Id(), packet.Size(), n)
}
}
}
func (p *Player) Parse() {
// TODO: Dial in a goroutine so that we can parse the client's Identification
// in the mean time.
if !p.Dial() {
return
}
Debugf("(%s) Dialed %s!", p.Id, p.Server.Conn.RemoteAddr().String())
t := 2 * time.Second // TODO: Higher, lower? Notchian does 2-3s.
p.Client.Parser = NewParser(p, p.Client.Conn, packets.ServerBound, t).(*Parser)
p.Server.Parser = NewParser(p, p.Server.Conn, packets.ClientBound, t).(*Parser)
// TODO: Config option to log messages?
//p.client.Register(packets.Message{}, LogMessage)
//p.server.Register(packets.Message{}, LogMessage)
// General hooks to drop/debug log packets first.
//p.client.Register(AllPackets{}, DebugPacket) // TODO
//p.server.Register(AllPackets{}, DebugPacket) // TODO
p.Client.Parser.Register(AllPackets{}, DropPacket)
p.Server.Parser.Register(AllPackets{}, DropPacket)
if p.ku.Config.EdgeCommands {
p.Client.Parser.Register(classic.Message{}, EdgeCommand)
}
// So we can shove packets down the pipe about identification.
go p.readParse(p.Client.Parser, p.Server.C) // C -> B
go p.writeParse(p.Client.C, p.Client.Conn) // C <- B
packet, err := p.Client.Parser.Next()
// This might indicate a read timeout, so just in case we shove down a
// DisconnectPlayer packet and kill their connections.
if err == ErrParserFinished {
Infof("(%s) Connected, but didn't send anything in time.", p.Remote())
p.Kick("You need to log in!")
return
}
if packet == nil || err != nil || packet.Id() != 0x00 {
// 0x00 = Identification
Infof("%s didn't identify correctly.", p.Remote())
Debugf("(%s) !ident: packet:%#v err:%#v", p.Id, packet, err)
p.Quit()
return
}
// We'll pass it on eventually.
p.Server.C <- packet
// Store their username!
var ident *classic.Identification
ident = packet.(*classic.Identification)
p.Name = ident.Name
p.CPE = ident.UserType == 0x42 // Magic value for CPE
// We handle authentication here if it's enabled, otherwise we just
// pull out the username from the initial Identification packet.
// NOTE: This only supports ClassiCube.
// TODO: Support Notchian authentication.
// TODO: Tidy this trash up.
if p.ku.Config.Authenticate && !compareHash(p.ku.salt, p.Name, ident.KeyMotd) {
Infof("(%s) Connected, but didn't verify for %s", p.Remote(), p.Name)
disc, err := classic.NewDisconnectPlayer("Name wasn't verified!")
if err != nil {
p.Quit()
return
}
p.Client.C <- disc
p.Quit()
return
}
// Now we can start to pass things along to the server.
go p.readParse(p.Server.Parser, p.Client.C) // B <- S
go p.writeParse(p.Server.C, p.Server.Conn) // B -> S
}
func NewPlayer(c net.Conn, ku *Kurafuto) (p *Player, err error) {
p = &Player{
Id: uniuri.NewLen(8),
ku: ku,
hub: fmt.Sprintf("%s:%d", ku.Hub.Address, ku.Hub.Port),
Client: BoundInfo{C: make(chan packets.Packet, 64)},
Server: BoundInfo{C: make(chan packets.Packet, 64)},
State: Connecting,
qMutex: sync.Mutex{},
}
return
}