-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
360 lines (293 loc) · 6.64 KB
/
server.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package plasma
import (
"bufio"
"crypto/aes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/gofrs/uuid"
"github.com/specspace/plasma/protocol"
"github.com/specspace/plasma/protocol/cfb8"
"net"
"os"
"sync"
)
type HandlerFunc func(w ResponseWriter, r *Request)
func (f HandlerFunc) ServeProtocol(w ResponseWriter, r *Request) {
f(w, r)
}
type Handler interface {
ServeProtocol(w ResponseWriter, r *Request)
}
type Version struct {
// Name of the version eg. "1.16.5"
Name string `json:"name"`
// Protocol version number eg. 754
ProtocolNumber int `json:"protocol"`
}
type PlayerInfo struct {
Name string `json:"name"`
UUID string `json:"id"`
}
type PlayersInfo struct {
MaxPlayers int `json:"max"`
PlayersOnline int `json:"online"`
Players []PlayerInfo `json:"sample"`
}
type StatusResponse struct {
Version Version
PlayersInfo PlayersInfo
IconPath string
MOTD string
}
func (sr StatusResponse) JSON() ([]byte, error) {
response := struct {
Version Version `json:"version"`
Players PlayersInfo `json:"players"`
Description struct {
Text string `json:"text"`
} `json:"description"`
Favicon string `json:"favicon"`
}{}
response.Version = sr.Version
response.Players = sr.PlayersInfo
response.Description.Text = sr.MOTD
if sr.IconPath != "" {
img64, err := loadImageAndEncodeToBase64String(sr.IconPath)
if err != nil {
return nil, err
}
response.Favicon = fmt.Sprintf("data:image/png;base64,%s", img64)
}
return json.Marshal(response)
}
func loadImageAndEncodeToBase64String(path string) (string, error) {
if path == "" {
return "", nil
}
imgFile, err := os.Open(path)
if err != nil {
return "", err
}
defer imgFile.Close()
fileInfo, err := imgFile.Stat()
if err != nil {
return "", err
}
buffer := make([]byte, fileInfo.Size())
fileReader := bufio.NewReader(imgFile)
_, err = fileReader.Read(buffer)
if err != nil {
return "", nil
}
return base64.StdEncoding.EncodeToString(buffer), nil
}
const DefaultAddr string = ":25565"
// Server defines the struct of a running Minecraft server
type Server struct {
ID string
Addr string
Encryption bool
MaxPlayers int
SessionEncrypter SessionEncrypter
SessionAuthenticator SessionAuthenticator
Handler Handler
listener net.Listener
players map[*conn]player
mu sync.RWMutex
}
func (srv *Server) getPlayer(conn *conn) player {
srv.mu.RLock()
defer srv.mu.RUnlock()
return srv.players[conn]
}
func (srv *Server) putPlayer(c *conn, p player) {
srv.mu.Lock()
defer srv.mu.Unlock()
srv.players[c] = p
}
func (srv *Server) removePlayer(c *conn) {
srv.mu.Lock()
defer srv.mu.Unlock()
delete(srv.players, c)
}
func (srv *Server) Player(r *Request) Player {
return srv.getPlayer(r.conn)
}
func (srv *Server) Players() []Player {
srv.mu.RLock()
defer srv.mu.RUnlock()
var players []Player
for _, player := range srv.players {
players = append(players, player)
}
return players
}
func (srv *Server) AddPlayer(r *Request, username string) {
srv.putPlayer(r.conn, player{
conn: r.conn,
uuid: uuid.NewV3(uuid.NamespaceOID, "OfflinePlayer:"+username),
username: username,
})
}
func (srv *Server) PlayersInfo() PlayersInfo {
pp := srv.Players()
var players []PlayerInfo
for _, p := range pp {
players = append(players, PlayerInfo{
Name: p.Username(),
UUID: p.UUID().String(),
})
}
srv.mu.RLock()
defer srv.mu.RUnlock()
return PlayersInfo{
MaxPlayers: srv.MaxPlayers,
PlayersOnline: len(pp),
Players: players,
}
}
func (srv *Server) IsRunning() bool {
return srv.listener != nil
}
func (srv *Server) Close() error {
if srv.listener != nil {
return srv.listener.Close()
}
return nil
}
func (srv *Server) ListenAndServe() error {
if srv.listener != nil {
return errors.New("server is already running")
}
addr := srv.Addr
if addr == "" {
addr = DefaultAddr
}
srv.players = map[*conn]player{}
srv.mu = sync.RWMutex{}
l, err := net.Listen("tcp", addr)
if err != nil {
return err
}
defer l.Close()
srv.listener = l
for {
c, err := l.Accept()
if err != nil {
return err
}
go srv.serve(c)
}
}
func (srv *Server) serve(c net.Conn) {
conn := wrapConn(c)
defer func() {
srv.removePlayer(conn)
conn.Close()
}()
r := Request{
server: srv,
conn: conn,
}
w := responseWriter{
conn: conn,
}
for {
pk, err := conn.ReadPacket()
if err != nil {
return
}
r.Packet = pk
srv.Handler.ServeProtocol(&w, &r)
}
}
func ListenAndServe(addr string, handler Handler) error {
encrypter, err := NewDefaultSessionEncrypter()
if err != nil {
return err
}
if handler == nil {
handler = DefaultServeMux
}
srv := &Server{
Addr: addr,
Encryption: true,
SessionEncrypter: encrypter,
SessionAuthenticator: &MojangSessionAuthenticator{},
Handler: handler,
}
return srv.ListenAndServe()
}
type ResponseWriter interface {
PacketWriter
SetState(state protocol.State)
SetEncryption(sharedSecret []byte) error
SetCompression(threshold int)
}
type responseWriter struct {
nextState bool
conn *conn
}
func (w *responseWriter) SetState(state protocol.State) {
w.conn.state = state
}
func (w *responseWriter) WritePacket(p protocol.Packet) error {
return w.conn.WritePacket(p)
}
func (w *responseWriter) SetEncryption(sharedSecret []byte) error {
block, err := aes.NewCipher(sharedSecret)
if err != nil {
return err
}
w.conn.SetCipher(
cfb8.NewEncrypter(block, sharedSecret),
cfb8.NewDecrypter(block, sharedSecret),
)
return nil
}
func (w *responseWriter) SetCompression(threshold int) {
w.conn.threshold = threshold
}
type Request struct {
Packet protocol.Packet
server *Server
conn *conn
}
func (r Request) Server() *Server {
return r.server
}
// ClonePacket makes a deep copy of the packet and returns it.
func (r Request) ClonePacket() protocol.Packet {
data := make([]byte, len(r.Packet.Data))
copy(data, r.Packet.Data)
return protocol.Packet{
ID: r.Packet.ID,
Data: data,
}
}
func (r Request) ProtocolState() protocol.State {
return r.conn.state
}
func (r Request) Conn() Conn {
return r.conn
}
func (r *Request) Player() Player {
return r.server.Player(r)
}
func (r *Request) UpdatePlayerUsername(username string) {
player := r.server.getPlayer(r.conn)
player.username = username
r.server.putPlayer(r.conn, player)
}
func (r *Request) UpdatePlayerUUID(uuid uuid.UUID) {
player := r.server.getPlayer(r.conn)
player.uuid = uuid
r.server.putPlayer(r.conn, player)
}
func (r *Request) UpdatePlayerSkin(skin Skin) {
player := r.server.getPlayer(r.conn)
player.skin = skin
r.server.putPlayer(r.conn, player)
}