summaryrefslogtreecommitdiff
path: root/server.py
blob: 3f60c08eedd83506c7107fff027d6974a245985e (plain) (blame)
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
#!/usr/bin/python3
#################################################################################
#     This file is part of Mana Spheres.
#     Copyright (C) 2020  Jesusalva

#     This program is free software: you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.

#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.

#     You should have received a copy of the GNU General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.
#################################################################################

## Global Modules
import threading, time, json, syslog, base64
LOG_AUTH=(syslog.LOG_NOTICE | syslog.LOG_AUTH)
# Tweak as needed/desired

## Semi-local modules
from websock import WebSocketServer, WebSocket

## Local Modules
from utils import stdout, now, clients, debug
from consts import MAX_CLIENTS, PACKET_ACK
import protocol, security, player, utils, traceback

###############################################################
# Configuration
f=open("pass.json", "r")
p=json.load(f)
f.close()

PORT=p["PORT"]
SECURE=p["SSL"]

###############################################################
# Main Class
class WebSocketConn(WebSocket):
    def handle(self):
        global clients
        """
          Called when websocket frame is received.
          To access the frame data call self.data.
          i.e. The client sent us a message \o/

          If the frame is Text then self.data is a unicode object.
          If the frame is Binary then self.data is a bytearray object.
        """
        print("Message received from %s - %s" % (self.address[0], self.data))
        print("Cache Recv: %s" % self.cacher)
        print("Cache Send: %s" % self.caches)

        # Check cache first
        try:
            if (base64.b64encode(bytearray(self.data, 'utf-8')) == self.cacher):
                print("Replying from cache... (%s)" % self.caches)
                self.send_message(self.caches)
                if (self.caches == "NACK\n"):
                    security.score(self, 1)
                return
            else:
                print("Not cached, continue")
        except:
            traceback.print_exc()
            stdout("Cache lookup skipped (ERROR)")

        # Handle packet
        try:
            r=protocol.parse(self.data, self)
            stdout("Status: %s" % str(r[0]), 2)
            stdout("Reply: %s" % r[1], 2)
            if r[0] < PACKET_ACK:
                self.caches    = "NACK\n"
                stdout("%s - %s" % (self.address[0], r[1]))
                syslog.syslog(LOG_AUTH, "%s - %s" % (self.address[0], r[1]))
                self.send_message("NACK\n")
                security.score(self, security.get_score(r[0]))
            else:
                self.caches    = str(r[1])
                self.send_message(str(r[1]))
        except:
            traceback.print_exc()
            self.send_message("ERROR\n")
            self.caches = "ERROR\n"

        stdout("Message sent", 2)
        self.cacher = base64.b64encode(bytearray(self.data, 'utf-8'))

        #stdout(self.address[0] + u' - %s' % (self.data))
        #self.send_message('ACK')

    def connected(self):
        global clients, blacklist
        """
          Called when a websocket client connects to the server.
        """
        #print(self.address, 'connected')
        #for client in clients:
        #    print(repr(client))
        #    client.send_message(self.address[0] + u' - connected')
        stdout(self.address[0] + u' - connected')

        # Keep only a "sane" amount of clients connected to the server.
        # This program must not, under any circumstances, interfer
        # on the operation of Moubootaur Legends, which has total priority
        # over the VM resources
        if (len(clients) > MAX_CLIENTS):
            self.close(self, status=1000, reason='Server is full')
        else:
            clients.append(self)

        # Blacklisted
        if (security.is_banned(self.address[0])):
            stdout("Found in K-Line, connection was dropped.")
            self.close(self, status=1000, reason="K-Lined")
            return

        # TODO: Drop OLD connections when same IP tries to connect
        # TODO: Either auto-ban or limit to <= 3 connections from same IP at once
        # TODO: Also, inheir the previous connection MS score and MS Auth

        # Extend self class
        self.MS_score = 0
        self.MS_auth  = False
        self.token    = "0"
        self.userid   = 0
        self.caches    = "INIT"
        self.cacher    = "INIT"

    def handle_close(self):
        global clients
        """
          Called when a websocket server gets a Close frame from a client.
        """
        print(self.address, 'closed')
        stdout(self.address[0] + u' - disconnected')

        try:
            clients.remove(self)
        except ValueError:
            pass
        except:
            traceback.print_exc()

        if self.token != "0":
            try:
                player.clear(self.token)
            except:
                traceback.print_exc()
                stdout("Error at player.clear", 0)

    ##########################
    # Useful functions:
    # send_message()
    # send_fragment_start() / send_fragment() / send_fragment_end()
    #  
    ##########################


def MainWebsocket():
    if SECURE:
        server = WebSocketServer('', PORT, WebSocketConn,
                                 certfile="certificate.pem", keyfile="key.pem")
        print("Begin Secure Websocket at %d" % PORT)
    else:
        server = WebSocketServer('', PORT, WebSocketConn)
        print("Begin UNENCRYPTED Websocket at %d" % PORT)

    t = threading.Thread(target=server.serve_forever)
    t.daemon = True # Main server should not be a daemon?
    t.start()
    return
    #while True:
    #    time.sleep(86400)

# Broadcast function
def sendmsg(m, t="raw"):
    global clients
    # Format message according to type
    if t == "ping":
        msg="pong"
    else:
        msg=u"%s" % m

    # Send message
    for c in clients:
        c.send_message(msg)
    return

# Disconnect function
def disconnect(ip):
    global clients
    totaldc=0
    for c in clients:
        if c.address[0] == ip:
            c.close(status=1000, reason="Remote host closed connection.")
            totaldc+=1
    return totaldc

###############################################################
# Begin stuff
stdout("Starting at: T-%d" % (now()), 0)
syslog.openlog()
MainWebsocket()
try:
    print("Serving at port %d" % PORT)
    while True:
        command=str(input("> "))
        # No command inserted? Do nothing
        if command == "":
            continue
        # We have a command, prepare it for processing
        stdout("CONSOLE: %s" % command)
        cmd=command.split(' ')[0].lower()
        com=" ".join(command.split(' ')[1:])
        # Parse the command
        # TODO: grant gems to an user
        if cmd in ["broadcast", "global", "msg", "say", "kami"]:
            sendmsg("NOTICE:" + com)
        elif cmd in ["ban", "nuke", "kb"]:
            security.ban_ip(com)
            totaldc=disconnect(com)
            stdout("BAN: Disconnected %d clients." % totaldc)
            del totaldc
        elif cmd in ["unban"]:
            security.unban_ip(com)
        elif cmd in ["permaban", "block", "kline"]:
            security.ban_ip(com)
            f=open("K-Line.txt", "a")
            f.write(com+"\n")
            f.close()
            stdout("%s has been K-Lined." % com)
            totaldc=disconnect(com)
            stdout("Disconnected %d clients." % totaldc)
            del totaldc
        elif cmd in ["exit", "quit", "close", "term", "end"]:
            stdout("Preparing to close server...")
            sendmsg("NOTICE:Server is going down for scheduled maintenance. Please close, wait five minutes, and then re-open the app.")
            break
        elif cmd in ["dbg", "debug"]:
            if debug == 2:
                debug = 0
                utils.debug = 0
            else:
                debug += 1
                utils.debug += 1
            stdout("Changed debug mode to %d" % debug, 0)
        elif cmd in ["raw", "eval"] and debug:
            try:
                print(eval(com))
            except:
                traceback.print_exc()
                print("[RAW] Error.")
        elif cmd in ["run", "exec"] and debug:
            try:
                exec(com)
            except:
                traceback.print_exc()
                print("[RUN] Error.")
        elif cmd in ["status", "st"]:
            stdout("Total clients connected: %d" % len(clients))
            stdout("Total blacklist size: %d" % len(security.blacklist))
        elif cmd in ["list", "all"]:
            stdout("Total clients connected: %d" % len(clients))
            for cli in clients:
                print("[%d] %s - %s" % (cli.userid, cli.token, cli.address[0]))
        elif cmd in ["kick", "dc"]:
            totaldc=disconnect(com)
            stdout("Disconnected %d clients." % totaldc)
            del totaldc
        elif cmd in ["ddos", "dcall"]:
            totaldc=0
            for c in clients:
                if c.token == "0" or c.userid < 1:
                    c.close(status=1000, reason="Remote host closed connection.")
                    totaldc+=1
            stdout("Disconnected %d clients." % totaldc)
            del totaldc
        elif cmd in ["ddosban", "dcbanall"]:
            totaldc=0
            for c in clients:
                if c.token == "0" or c.userid < 1:
                    security.ban_ip(c.address[0], now()+1800)
                    c.close(status=1000, reason="Remote host closed connection.")
                    totaldc+=1
            stdout("Disconnected and banned %d clients." % totaldc)
            del totaldc
        else:
            stdout("ERROR: Unknown command.")
except:
    stdout("Abrupt error: Terminating!", 0)
    traceback.print_exc()

# Wait a bit before disconnecting all clients
# To make sure any pending sendmsg() will arrive
time.sleep(0.20)
for c in clients:
    c.close(reason="Server is shutting down")

# Make cleanup, just in case c.close() missed
time.sleep(1.0)
player.sql_routine()

print("Server finished.")