-
Notifications
You must be signed in to change notification settings - Fork 16
/
controller.py
207 lines (179 loc) · 6.34 KB
/
controller.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
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
#!/usr/bin/env python
__author__ = 'katharine'
import gevent.monkey
gevent.monkey.patch_all(subprocess=True)
import gevent
import gevent.pool
from flask import Flask, request, jsonify, abort
from flask.ext.cors import CORS
from time import time as now
import ssl
import os
import pwd
import grp
import sys
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
import geventwebsocket
import websocket
import settings
from emulator import Emulator
from uuid import uuid4, UUID
import logging
import atexit
app = Flask(__name__)
cors = CORS(app, headers=["X-Requested-With", "X-CSRFToken", "Content-Type"], resources="/qemu/*")
logging.basicConfig(level=logging.DEBUG, format="%(levelname)s %(asctime)s: %(message)s")
emulators = {}
@app.route('/qemu/launch', methods=['POST'])
def launch():
if request.headers.get('authorization', None) != settings.LAUNCH_AUTH_HEADER:
abort(403)
if len(emulators) >= settings.EMULATOR_LIMIT:
abort(503)
uuid = uuid4()
if '/' in request.form['platform'] or '/' in request.form['version']:
abort(400)
emu = Emulator(
request.form['token'],
request.form['platform'],
request.form['version'],
tz_offset=(int(request.form['tz_offset']) if 'tz_offset' in request.form else None),
oauth=request.form.get('oauth', None)
)
emulators[uuid] = emu
emu.last_ping = now()
emu.run()
return jsonify(uuid=uuid, ws_port=emu.ws_port, vnc_display=emu.vnc_display, vnc_ws_port=emu.vnc_ws_port)
@app.route('/qemu/<emu>/ping', methods=['POST'])
def ping(emu):
try:
emu = UUID(emu)
except ValueError:
abort(404)
if emu not in emulators:
return jsonify(alive=False)
if emulators[emu].is_alive():
emulators[emu].last_ping = now()
return jsonify(alive=True)
else:
try:
emulators[emu].kill()
except:
logging.exception("failed to kill emulator")
pass
else:
del emulators[emu]
return jsonify(alive=False)
@app.route('/qemu/<emu>/kill', methods=['POST'])
def kill(emu):
try:
emu = UUID(emu)
except ValueError:
abort(404)
if emu in emulators:
try:
emulators[emu].kill()
except Exception:
logging.exception("failed to kill")
else:
del emulators[emu]
return jsonify(status='ok')
def proxy_ws(emu, attr, subprotocols=[]):
server_ws = request.environ.get('wsgi.websocket', None)
if server_ws is None:
return "websocket endpoint", 400
try:
emulator = emulators[UUID(emu)]
except ValueError:
abort(404)
return # unreachable but makes IDE happy.
target_url = "ws://localhost:%d/" % getattr(emulator, attr)
try:
client_ws = websocket.create_connection(target_url, subprotocols=subprotocols, sslopt={
'ssl_version': ssl.PROTOCOL_TLSv1,
'ca_certs': '%s/ca-cert.pem' % settings.SSL_ROOT,
'cert_reqs': ssl.CERT_NONE,
})
except:
logging.exception("connection to %s failed.", target_url)
return 'failed', 500
alive = [True]
def do_recv(receive, send):
try:
while alive[0]:
send(receive())
except (websocket.WebSocketException, geventwebsocket.WebSocketError, TypeError):
alive[0] = False
except:
alive[0] = False
raise
group = gevent.pool.Group()
group.spawn(do_recv, lambda: server_ws.receive(), lambda x: client_ws.send_binary(x))
group.spawn(do_recv, lambda: bytearray(client_ws.recv()), lambda x: server_ws.send(x))
group.join()
return ''
app.app_protocol = lambda x: 'binary' if 'vnc' in x else None
@app.route('/qemu/<emu>/ws/phone')
def ws_phone(emu):
return proxy_ws(emu, 'ws_port')
@app.route('/qemu/<emu>/ws/vnc')
def ws_vnc(emu):
return proxy_ws(emu, 'vnc_ws_port', subprotocols=['binary'])
def _kill_idle_emulators():
try:
while True:
try:
logging.info("running idle killer for %d emulators", len(emulators))
for key, emulator in emulators.items():
logging.debug("checking %s", key)
if now() - emulator.last_ping > 300:
logging.info("killing idle emulator %s", key)
emulator.kill()
del emulators[key]
else:
logging.debug("okay; last ping: %s", emulator.last_ping)
except Exception:
logging.exception('Failed to kill idle emulator')
sys.stdout.flush()
gevent.sleep(60)
except Exception:
logging.exception("IDLE EMULATOR WATCHDOG DIED. %d emulators were running.", len(emulators))
raise
idle_killer = gevent.spawn(_kill_idle_emulators)
@atexit.register
def kill_emulators():
for emulator in emulators.itervalues():
emulator.kill()
emulators.clear()
def drop_privileges(uid_name='nobody', gid_name='nogroup'):
if os.getuid() != 0:
# We're not root so, like, whatever dude
return
# Get the uid/gid from the name
running_uid = pwd.getpwnam(uid_name).pw_uid
running_gid = grp.getgrnam(gid_name).gr_gid
# Remove group privileges
os.setgroups([])
# Try setting the new uid/gid
os.setgid(running_gid)
os.setuid(running_uid)
# Ensure a very conservative umask
os.umask(077)
logging.info("Emulator limit: %d", settings.EMULATOR_LIMIT)
if __name__ == '__main__':
app.debug = settings.DEBUG
ssl_args = {}
if settings.SSL_ROOT is not None:
ssl_args = {
'keyfile': '%s/server-key.pem' % settings.SSL_ROOT,
'certfile': '%s/server-cert.pem' % settings.SSL_ROOT,
'ca_certs': '%s/ca-cert.pem' % settings.SSL_ROOT,
'ssl_version': ssl.PROTOCOL_TLSv1_2,
'ciphers': 'EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4',
}
server = pywsgi.WSGIServer(('', settings.PORT), app, handler_class=WebSocketHandler, **ssl_args)
server.start()
if settings.RUN_AS_USER is not None:
drop_privileges(settings.RUN_AS_USER, settings.RUN_AS_USER)
server.serve_forever()