-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcodremaster.go
More file actions
249 lines (211 loc) · 5.35 KB
/
codremaster.go
File metadata and controls
249 lines (211 loc) · 5.35 KB
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
package main
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"math/rand"
"net"
"net/http"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
var db = struct {
sync.RWMutex
m map[string]int64
}{m: make(map[string]int64)}
var header string = "\xff\xff\xff\xff"
const (
codmaster = 20510
codauth = 20500
cod2master = 20710
cod2auth = 20700
cod4master = 20810
cod4auth = 20800
)
func main() {
go listenMaster(cod4master)
go listenAuth(cod4auth)
go purge(6)
http.HandleFunc("/getinfo", getinfoHandler)
http.ListenAndServe(":8080", nil)
}
func listenMaster(port int) {
udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprint(":", port))
if err != nil {
log.Fatal("Could not resolve port for master.")
}
conn, err := net.ListenUDP("udp", udpAddr)
if err != nil {
log.Fatal("Failed to open socket for master.")
}
defer conn.Close()
fmt.Printf("Master server is listening on port %d...\n", port)
for {
var buf [1024]byte
n, addr, err := conn.ReadFromUDP(buf[0:])
if err != nil {
return
}
msg := string(buf[0:n])
endpoint := string(addr.IP.String()) + ":" + fmt.Sprint(addr.Port)
switch {
case strings.HasPrefix(msg[4:], "statusResponse"):
db.Lock()
db.m[endpoint] = time.Now().Unix()
db.Unlock()
case strings.HasPrefix(msg[4:], "getservers "):
db.RLock()
if len(db.m) == 0 {
// db is empty
db.RUnlock()
continue
}
res := make([]byte, 0)
innerCount := 0
per := 20
for k, _ := range db.m {
current := make([]byte, 0)
if innerCount == 0 {
current = append(current, []byte(fmt.Sprint(header, "getserversResponse", "\n\x00\\"))...)
}
octets := strings.Split(k[:strings.Index(k, ":")], ".")
for i := 0; i < 4; i++ {
octet, err := strconv.Atoi(octets[i])
if err != nil {
continue
}
current = append(current, byte(octet))
}
addrport, err := strconv.Atoi(k[strings.Index(k, ":")+1:])
if err != nil {
continue
}
portbuf := &bytes.Buffer{}
binary.Write(portbuf, binary.BigEndian, uint16(addrport))
current = append(current, portbuf.Bytes()...)
current = append(current, []byte("\\")...)
res = append(res, current...)
innerCount++
if innerCount == per {
res = append(res, []byte("EOT")...)
conn.WriteToUDP(res, addr)
// reset
innerCount = 0
res = make([]byte, 0)
}
}
db.RUnlock()
res = append(res, []byte("EOF")...)
conn.WriteToUDP(res, addr)
case msg[4:] == "heartbeat flatline":
db.Lock()
delete(db.m, endpoint)
db.Unlock()
case strings.HasPrefix(msg[4:], "heartbeat"): //server checking in to MS
db.Lock()
if _, ok := db.m[endpoint]; ok {
// just checking in
db.m[endpoint] = time.Now().Unix()
} else {
// new server
nonce := generateNonce(9)
conn.WriteToUDP([]byte(fmt.Sprint(header, "getchallenge ", nonce, "\n")), addr)
conn.WriteToUDP([]byte(fmt.Sprint(header, "getstatus ", nonce, "\n")), addr)
}
db.Unlock()
}
}
}
// generateNonce creates a pseudorandom number.
// digits determines how long the number will be.
func generateNonce(digits int) string {
nonce := &bytes.Buffer{}
for i := 0; i < digits; i++ {
nonce.WriteString(strconv.Itoa(rand.Intn(10)))
}
return nonce.String()
}
// listenAuth mimics the authentication server
// Packets are received but not acted upon.
func listenAuth(port int) {
udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprint(":", port))
if err != nil {
log.Fatal("Could not resolve port for auth.")
}
conn, err := net.ListenUDP("udp", udpAddr)
if err != nil {
log.Fatal("Failed to open socket for auth.")
}
defer conn.Close()
fmt.Printf("Authentication server is listening on port %d...\n", port)
for {
var buf [1024]byte
n, _, err := conn.ReadFromUDP(buf[0:])
if err != nil {
return
}
msg := string(buf[0:n])
if strings.HasPrefix(msg[4:], "getIpAuthorize") {
// ignore
}
}
}
// purge removes inactive game servers from the database.
// A server is inactive if it has failed to send a hearbeat
// within the timeframe specified by interval in minutes.
func purge(interval int) {
for {
current := time.Now().Unix() - int64(interval*60)
db.Lock()
for k, v := range db.m {
if v < current {
delete(db.m, k)
}
}
db.Unlock()
time.Sleep(time.Duration(interval) * time.Minute)
}
}
// getInfo queries the specified server.
func getInfo(addr string) (map[string]string, error) {
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
conn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
return nil, err
}
defer conn.Close()
conn.Write([]byte("\xff\xff\xff\xffgetinfo xxx"))
var buf [512]byte
conn.SetReadDeadline(time.Now().Add(10000 * time.Millisecond))
n, err := conn.Read(buf[0:])
if err != nil {
return nil, err
}
response := string(buf[0:n])
fields := strings.Split(response[strings.Index(response, "\\")+1:], "\\")
ir := make(map[string]string)
for i := 0; i < len(fields)-1; i = i + 2 {
ir[fields[i]] = fields[i+1]
}
return ir, nil
}
// removeColorCodes returns a copy of string s without Quake color codes.
func removeColorCodes(s string) string {
re := regexp.MustCompile("\\^[0-7]")
return re.ReplaceAllString(s, "")
}
func getinfoHandler(w http.ResponseWriter, r *http.Request) {
response, err := getInfo(r.FormValue("addr"))
if err != nil {
fmt.Fprint(w, err)
return
}
fmt.Fprint(w, response)
}