forked from freeciv/freeciv-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreeciv-proxy.py
executable file
·136 lines (104 loc) · 4.17 KB
/
freeciv-proxy.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Freeciv - Copyright (C) 2011-2014 - Andreas Røsdal [email protected]
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 2, 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.
'''
from os import path as op
import time
from tornado import web, websocket, ioloop, httpserver
from debugging import *
import logging
from civcom import *
import json
import uuid
import gc
PROXY_PORT = 8002
CONNECTION_LIMIT = 1000
civcoms = {}
class IndexHandler(web.RequestHandler):
"""Serves the Freeciv-proxy index page """
def get(self):
self.write("Freeciv-web websocket proxy, port: " + str(PROXY_PORT))
class StatusHandler(web.RequestHandler):
"""Serves the Freeciv-proxy status page, on the url: /status """
def get(self):
self.write(get_debug_info(civcoms))
class WSHandler(websocket.WebSocketHandler):
logger = logging.getLogger("freeciv-proxy")
def open(self):
self.id = str(uuid.uuid4())
self.is_ready = False
self.set_nodelay(True)
def on_message(self, message):
if (not self.is_ready and len(civcoms) <= CONNECTION_LIMIT):
# called the first time the user connects.
login_message = json.loads(message)
self.username = login_message['username']
self.civserverport = login_message['port']
self.ip = self.request.headers.get("X-Real-IP", "missing")
self.loginpacket = message
self.is_ready = True
self.civcom = self.get_civcom(
self.username,
self.civserverport,
self)
return
# get the civcom instance which corresponds to this user.
if (self.is_ready):
self.civcom = self.get_civcom(self.username, self.civserverport, self)
if (self.civcom is None):
self.write_message("Error: Could not authenticate user.")
return
# send JSON request to civserver.
self.civcom.queue_to_civserver(message)
def on_close(self):
if hasattr(self, 'civcom') and self.civcom is not None:
self.civcom.stopped = True
self.civcom.close_connection()
if self.civcom.key in list(civcoms.keys()):
del civcoms[self.civcom.key]
del(self.civcom)
gc.collect()
# enables support for allowing alternate origins. See check_origin in websocket.py
def check_origin(self, origin):
return True;
# get the civcom instance which corresponds to the requested user.
def get_civcom(self, username, civserverport, ws_connection):
key = username + str(civserverport) + ws_connection.id
if key not in list(civcoms.keys()):
if (int(civserverport) < 5000):
return None
civcom = CivCom(username, int(civserverport), key, self)
civcom.start()
civcoms[key] = civcom
return civcom
else:
return civcoms[key]
if __name__ == "__main__":
try:
print('Started Freeciv-proxy. Use Control-C to exit')
if len(sys.argv) == 2:
PROXY_PORT = int(sys.argv[1])
print(('port: ' + str(PROXY_PORT)))
LOG_FILENAME = '/tmp/logging' + str(PROXY_PORT) + '.out'
# logging.basicConfig(filename=LOG_FILENAME,level=logging.INFO)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("freeciv-proxy")
application = web.Application([
(r'/civsocket/' + str(PROXY_PORT), WSHandler),
(r"/", IndexHandler),
(r"/status", StatusHandler),
])
http_server = httpserver.HTTPServer(application)
http_server.listen(PROXY_PORT)
ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
print('Exiting...')