summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/gui/app2.py
blob: 151dead5efcaa4198b81457ae1152123fcffbdf0 (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
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
# -*- coding: utf-8 -*-
# app2.py
# Copyright (C) 2016-2017 LEAP Encryption Acess Project
#
# 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/>.

"""
This is an alternative entrypoint for Bitmask, based on pywebview, to be used
in osx and windows.

For the moment, it requires also qt5 for the systray, but we should move to a
native solution on each platform.
"""

import getpass
import os
import platform
import signal
import sys
import time
import webbrowser

from functools import partial
from multiprocessing import Process

import webview
import psutil

from PyQt5.QtWidgets import QApplication

from leap.bitmask.core.launcher import run_bitmaskd, pid
from leap.common.config import get_path_prefix

from leap.bitmask.gui.systray import WithTrayIcon
from leap.bitmask.gui.housekeeping import cleanup, terminate, reset_authtoken
from leap.bitmask.gui.housekeeping import get_authenticated_url
from leap.bitmask.gui.housekeeping import NoAuthTokenError


DEBUG = os.environ.get("DEBUG", False)

BITMASK_URI = 'http://localhost:7070/'
PIXELATED_URI = 'http://localhost:9090/'

PROCNAME = 'bitmask-app'

qApp = None
bitmaskd = None


class Systray(WithTrayIcon):

    browser = None

    def closeFromSystray(self):
        self.user_closed = True
        if self.browser:
            self.browser.shutdown()
            self.close()
        self.close()

    def closeEvent(self, event):
        if self.user_closed:
            print "bye!"
            sys.exit()
        else:
            event.ignore()


def launch_systray():
    global qApp
    qApp = QApplication([])
    qApp.setQuitOnLastWindowClosed(True)
    systray = Systray()
    systray.setupSysTray()
    return systray


class BrowserWindow(object):
    """
    A browser window using pywebview.

    This BrowserWindow assumes that the backend is already running, since it is
    going to look for the authtoken in the configuration folder.
    """

    def __init__(self, *args, **kw):
        url = kw.pop('url', None)
        if not url:
            url = get_authenticated_url()
        self.url = url
        self.closing = False

    def create_window(self):
        # blocks. So this should be the last action you do during launch.
        webview.create_window('Bitmask', self.url)

    def loadPage(self, web_page):
        self.load(url)

    def shutdown(self, *args):
        global bitmaskd
        if self.closing:
            return
        self.closing = True
        bitmaskd.join()
        terminate(pid)
        cleanup()
        print('[bitmask] shutting down gui')


def launch_backend():
    global bitmaskd

    check_stale_pidfile()
    bitmaskd = Process(target=run_bitmaskd)
    bitmaskd.start()


def launch_gui():

    launch_backend()

    # there are some tricky movements here to synchronize
    # the different closing events:

    # 1. bitmask off button: proper way, does shutdown via js.
    # 2. systray quit: calls browser.shutdown() via reference.
    # 3. browser window close: has to call browser.shutdown() explicitely.

    try:
        systray = launch_systray()
        browser = BrowserWindow(None)
        systray.browser = browser
        browser.create_window()

        # here control is passed
        # to the pywebview event loop...

        # case 2.
        if not browser.closing:
            browser.shutdown()
        systray.browser = None

        # close systray if closed from cases 1. or 2.
        systray.closeFromSystray()
        sys.exit(qApp.exec_())

    except NoAuthTokenError as e:
        print('ERROR: ' + e.message)
        sys.exit(1)


def start_app():
    from leap.bitmask.util import STANDALONE
    mypid = os.getpid()

    # Kill a previously-running process
    for proc in psutil.process_iter():
        if proc.name() == PROCNAME and proc.pid != mypid:
            proc.kill()

    # Allow the frozen binary in the bundle double as the cli entrypoint
    # Why have only a user interface when you can have two?

    if STANDALONE and len(sys.argv) > 1:
        if sys.argv[1] == 'bitmask_helpers':
            from leap.bitmask.vpn.helpers import main
            return main()

        from leap.bitmask.cli import bitmask_cli
        return bitmask_cli.main()

    reset_authtoken()
    launch_gui()


if __name__ == "__main__":
    start_app()