summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/core/websocket.py
blob: 5569c6c7b9f4baa9c569d0754c4fbe6c999a87ae (plain)
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
# -*- coding: utf-8 -*-
# websocket.py
# Copyright (C) 2015, 2016 LEAP
#
# 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/>.

"""
WebSockets Dispatcher Service.
"""

import os
import pkg_resources

from twisted.internet import reactor
from twisted.application import service

from twisted.web.server import Site
from twisted.web.static import File

from autobahn.twisted.resource import WebSocketResource
from autobahn.twisted.websocket import WebSocketServerFactory
from autobahn.twisted.websocket import WebSocketServerProtocol

from leap.bitmask.core.dispatcher import CommandDispatcher


class WebSocketsDispatcherService(service.Service):

    """
    A Dispatcher for BitmaskCore exposing a WebSockets Endpoint.
    """

    def __init__(self, core, port=8080, debug=False):
        self._core = core
        self.port = port
        self.debug = debug

    def startService(self):

        factory = WebSocketServerFactory(u"ws://127.0.0.1:%d" % self.port,
                                         debug=self.debug)
        factory.protocol = DispatcherProtocol
        factory.protocol.dispatcher = CommandDispatcher(self._core)

        # FIXME: Site.start/stopFactory should start/stop factories wrapped as
        # Resources
        factory.startFactory()

        resource = WebSocketResource(factory)

        # we server static files under "/" ..
        webdir = os.path.abspath(
            pkg_resources.resource_filename("leap.bitmask.core", "web"))
        root = File(webdir)

        # and our WebSocket server under "/ws"
        root.putChild(u"bitmask", resource)

        # both under one Twisted Web Site
        site = Site(root)

        self.site = site
        self.factory = factory

        self.listener = reactor.listenTCP(self.port, site)

    def stopService(self):
        self.factory.stopFactory()
        self.site.stopFactory()
        self.listener.stopListening()


class DispatcherProtocol(WebSocketServerProtocol):

    def onMessage(self, msg, binary):
        parts = msg.split()
        r = self.dispatcher.dispatch(parts)
        r.addCallback(self.defer_reply, binary)

    def reply(self, response, binary):
        self.sendMessage(response, binary)

    def defer_reply(self, response, binary):
        reactor.callLater(0, self.reply, response, binary)

    def _get_service(self, name):
        return self.core.getServiceNamed(name)