2015-07-17 23:44:26 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
2015-07-18 03:49:24 +00:00
|
|
|
import json
|
2015-07-18 05:35:57 +00:00
|
|
|
import pprint
|
2015-07-18 03:17:27 +00:00
|
|
|
# Can't use trollius because then onConnect never gets called
|
2015-07-18 03:33:57 +00:00
|
|
|
# https://github.com/tavendo/AutobahnPython/issues/426
|
2015-07-17 23:44:26 +00:00
|
|
|
import asyncio
|
|
|
|
import aiohttp.web
|
|
|
|
from autobahn.asyncio.websocket import WebSocketServerFactory, WebSocketServerProtocol
|
|
|
|
|
2015-07-18 10:57:57 +00:00
|
|
|
class GrabberServerProtocol(WebSocketServerProtocol):
|
2015-07-18 02:11:18 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-07-18 13:29:19 +00:00
|
|
|
self.mode = None
|
2015-07-18 02:11:18 +00:00
|
|
|
|
2015-07-17 23:44:26 +00:00
|
|
|
def onConnect(self, request):
|
2015-07-18 02:11:18 +00:00
|
|
|
self.peer = request.peer
|
2015-07-18 08:26:58 +00:00
|
|
|
print("{} connected".format(self.peer))
|
2015-07-18 02:11:18 +00:00
|
|
|
self.factory.clients.add(self)
|
|
|
|
|
|
|
|
def onClose(self, wasClean, code, reason):
|
2015-07-18 08:26:58 +00:00
|
|
|
print("{} disconnected".format(self.peer))
|
2015-07-18 02:11:18 +00:00
|
|
|
self.factory.clients.remove(self)
|
|
|
|
|
2015-07-18 05:24:54 +00:00
|
|
|
def broadcastToDashboards(self, obj):
|
|
|
|
for client in self.factory.clients:
|
|
|
|
if client.mode == "dashboard":
|
2015-07-18 05:35:57 +00:00
|
|
|
##print("Broadcasting", pprint.pformat(obj))
|
2015-07-18 05:24:54 +00:00
|
|
|
client.sendMessage(json.dumps(obj).encode("utf-8"))
|
|
|
|
|
2015-07-18 02:11:18 +00:00
|
|
|
def onMessage(self, payload, isBinary):
|
2015-07-18 05:24:54 +00:00
|
|
|
##print(payload)
|
2015-07-18 02:11:18 +00:00
|
|
|
obj = json.loads(payload.decode('utf-8'))
|
|
|
|
type = obj["type"]
|
2015-07-18 13:32:26 +00:00
|
|
|
if self.mode is None and type == "hello" and obj.get("mode"):
|
2015-07-18 02:11:18 +00:00
|
|
|
mode = obj['mode']
|
|
|
|
if mode in ('dashboard', 'grabber'):
|
2015-07-18 13:29:19 +00:00
|
|
|
self.mode = mode
|
2015-07-18 08:26:58 +00:00
|
|
|
if mode == "grabber":
|
|
|
|
print("{} is grabbing {}".format(self.peer, obj['url']))
|
2015-07-18 13:29:19 +00:00
|
|
|
elif mode == "dashboard":
|
|
|
|
print("{} is dashboarding with {}".format(self.peer, obj['user_agent']))
|
2015-07-18 05:24:54 +00:00
|
|
|
elif type == "download":
|
|
|
|
self.broadcastToDashboards({
|
|
|
|
"type": type,
|
|
|
|
"job_data": obj["job_data"],
|
|
|
|
"url": obj["url"],
|
|
|
|
"response_code": obj["response_code"],
|
|
|
|
"wget_code": obj["response_message"]
|
|
|
|
})
|
|
|
|
elif type in ("stdout", "stderr"):
|
|
|
|
self.broadcastToDashboards({
|
|
|
|
"type": type,
|
|
|
|
"job_data": obj["job_data"],
|
|
|
|
"message": obj["message"]
|
|
|
|
})
|
2015-07-18 05:55:51 +00:00
|
|
|
elif type == "ignore":
|
|
|
|
self.broadcastToDashboards({
|
|
|
|
"type": type,
|
|
|
|
"job_data": obj["job_data"],
|
|
|
|
"url": obj["url"],
|
|
|
|
"pattern": obj["pattern"],
|
|
|
|
})
|
2015-07-17 23:44:26 +00:00
|
|
|
|
|
|
|
|
2015-07-18 10:57:57 +00:00
|
|
|
class GrabberServerFactory(WebSocketServerFactory):
|
|
|
|
protocol = GrabberServerProtocol
|
2015-07-18 02:11:18 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.clients = set()
|
|
|
|
|
|
|
|
|
2015-07-17 23:44:26 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def dashboard(request):
|
2015-07-18 04:10:15 +00:00
|
|
|
with open(os.path.join(os.path.dirname(__file__), "dashboard.html"), "rb") as f:
|
|
|
|
dashboardHtml = f.read()
|
|
|
|
return aiohttp.web.Response(body=dashboardHtml)
|
2015-07-17 23:44:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def httpServer(loop, interface, port):
|
|
|
|
app = aiohttp.web.Application(loop=loop)
|
|
|
|
app.router.add_route('GET', '/', dashboard)
|
|
|
|
|
|
|
|
srv = yield from loop.create_server(app.make_handler(), interface, port)
|
|
|
|
return srv
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
|
|
|
httpPort = int(os.environ.get('GRAB_SITE_HTTP_PORT', 29000))
|
|
|
|
httpInterface = os.environ.get('GRAB_SITE_HTTP_INTERFACE', '0.0.0.0')
|
|
|
|
wsPort = int(os.environ.get('GRAB_SITE_WS_PORT', 29001))
|
|
|
|
wsInterface = os.environ.get('GRAB_SITE_WS_INTERFACE', '0.0.0.0')
|
|
|
|
|
|
|
|
httpCoro = httpServer(loop, httpInterface, httpPort)
|
|
|
|
loop.run_until_complete(httpCoro)
|
|
|
|
|
2015-07-18 10:57:57 +00:00
|
|
|
wsFactory = GrabberServerFactory()
|
2015-07-17 23:44:26 +00:00
|
|
|
wsCoro = loop.create_server(wsFactory, wsInterface, wsPort)
|
|
|
|
loop.run_until_complete(wsCoro)
|
|
|
|
|
2015-07-18 02:11:18 +00:00
|
|
|
print(" HTTP server started on {}:{}".format(httpInterface, httpPort))
|
2015-07-17 23:44:26 +00:00
|
|
|
print("WebSocket server started on {}:{}".format(wsInterface, wsPort))
|
|
|
|
|
|
|
|
loop.run_forever()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|