-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
301 lines (216 loc) · 6.26 KB
/
main.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
package main
import (
"fmt"
"net"
"os"
"io"
"time"
"github.com/rdv-dev/pyrosaurus-server/ContestServer"
"github.com/rdv-dev/pyrosaurus-server/ContestServer/util"
"github.com/rdv-dev/pyrosaurus-server/ModemServer"
// "github.com/rdv-dev/pyrosaurus-server/Database"
// "strconv"
)
const (
SERVER_HOST="127.0.0.1"
SERVER_PORT="8888"
SERVER_TYPE="tcp"
)
func main() {
//config := &net.ListenConfig(Control: reusePort)
fmt.Println("Setting up server...")
pyroJobs := make(chan *ModemServer.PyroUser)
//server, err := config.Listen(context.Background(), SERVER_TYPE, SERVER_HOST+":"+SERVER_PORT)
server, err := net.Listen(SERVER_TYPE, SERVER_HOST+":"+SERVER_PORT)
if err != nil {
fmt.Println("Error setting up socket", err.Error())
os.Exit(1)
}
defer server.Close()
fmt.Println("Listening on " + SERVER_HOST+":"+SERVER_PORT)
// KeyArray = LoadValidationKey()
// ModemServer.LoadValidationKey()
for {
conn, err := server.Accept()
if err != nil {
fmt.Println("Error accepting modem", err.Error())
os.Exit(1)
}
// defer server.Close()
go func() {
fmt.Println("Got connection")
user := &ModemServer.PyroUser {
PyroUserId: 0,
PyroCheckId: 0,
PyroVersion: 0,
// active: 1,
Conn: conn,
Mode: 0,
Submode: 0,
Data: make([]byte,1024)}
validated, err := ModemServer.DoChallenge(user)
if err != nil {
fmt.Println("Error during Challenge", err.Error())
os.Exit(1)
}
// defer server.Close()
if validated == 1 {
go handleModemJobs(pyroJobs)
idTotal := 0
mmode := make([]byte, 0)
for idTotal < 2 {
nread, err := user.Conn.Read(user.Data)
idTotal += nread
if err != nil {
fmt.Println("Error reading from socket: Mode", err.Error())
break;
}
mmode = append(mmode, user.Data[:nread]...)
}
user.Mode = int(mmode[0])
for {
pyroJobs <- user
if user.Mode == 0 {
fmt.Println("Closing connection to client")
break;
}
}
}
}()
}
fmt.Println("Closing...")
// fmt.Println("Writing fundata to file...")
// f, err := os.Create("T.TMP.1")
// if err != nil {
// fmt.Println("Error writing file")
// os.Exit(1)
// }
// defer server.Close()
// f.Write(fundata)
server.Close()
}
func handleModemJobs(pyroJobs chan *ModemServer.PyroUser) {
for {
job := <- pyroJobs
// midCheckIndex := 4
// lastCheckIndex := 4
// fileTotal := 0
// fundata := make([]byte,0)
// doModeLoop := 1
// for doModeLoop == 1 {
// if job.Mode == 0 {
// break;
// }
fmt.Printf("Select mode: %d\n", job.Mode)
switch job.Mode {
case 0:
// stop looping
break;
case 1:
// Backup data, Get Messages
success, err := ModemServer.DoSpecialModes(job)
if err != nil {
fmt.Println("Error during special mode", err.Error())
}
time.Sleep(1*time.Second)
if success == 1 {
job.Mode = 0
}
// doModeLoop = 0
case 2, 4:
// TODO: MAJOR ISSUE - Contest/Message files can be handled separately now
// as it is coded here now, sending a message will cause an issue.
// Server gets file
contestEntry, err := ModemServer.GetFile(job)
if err != nil {
fmt.Println("Error getting file", err.Error())
}
// fmt.Sprintf("%s", fileData[0])
var levelData []byte
// levelFile, err := os.Open("/home/rob/pyro-c/db/levels/LEVEL.000")
levelFile, err := os.Open("Assets/Levels/LEVEL.000")
if err != nil {
fmt.Println(err)
os.Exit(1)
} else {
levelData, err = io.ReadAll(levelFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// if 2, process entry, run contest
team1, err := util.NewContestEntry(contestEntry.TeamData)
if err != nil {
fmt.Println("Error processing player team entry", err.Error())
}
team2EntryId, team2 := ContestServer.FindOpponent(job.InternalPlayerId)
fmt.Printf("Found opponent: %d\n", team2EntryId)
if team2 != nil {
result, err := ContestServer.RunContest(team1, team2, levelData)
fmt.Printf("Contest length: %d\n", len(result.Actions))
if err != nil {
fmt.Println("Failed to run contest!", err.Error())
job.Mode = 0
}
if len(result.Actions) < 0 {
fmt.Println("No result produced")
job.Mode = 0
} else {
if job.PyroVersion > 2 {
// Modded MODEM versions
outdata, err := ContestServer.ExportContest(team1, team2, levelData, result)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
job.Contest = outdata
} else {
// Original MODEM version
ContestServer.SaveContest(team1, team2, levelData, result, job.InternalPlayerId, team2EntryId)
}
}
} else {
}
// if 4, process message
// }
case 3:
// Server sends data
// export contest, send file
job.Conn.Write([]byte{0x03})
err := ModemServer.CheckForContest(job)
if err != nil {
fmt.Println("Failed to check for contest", err)
// Contest not available
job.Conn.Write([]byte{0x21})
} else {
sentFile, err := ModemServer.SendFile(job, job.Contest)
if err != nil {
fmt.Println("Error sending file", err.Error())
}
if sentFile == 1 {
fmt.Println("Sent Contest file!")
}
}
job.Mode = 0
case 7:
success, err := ModemServer.DoTestConnection(job)
if err != nil {
fmt.Println("Error during test: ", err.Error())
os.Exit(1)
}
if success == 1 {
job.Mode = 0
fmt.Println("Writing updated phone number")
sum := 0x30 + 0x30 + 0x30 + 0x30 + 0x30 + 0x31 + 0x38 + 0x38 + 0x38 + 0x38 + 0x00 + 0x00
sum = sum & 0xFF
job.Conn.Write([]byte{0x2,0x02,0x30,0x30,0x30,0x30,0x30,0x31,0x38,0x38,0x38,0x38,0x00,0x00, byte(sum)})
}
// doModeLoop = 0
default:
fmt.Println("Invalid mode, exiting")
break;
}
// }
}
}