summaryrefslogtreecommitdiff
path: root/service/pixelated/runserver.py
blob: 314a5d7192f5a94a4e8fa5d8661f790b2fee127c (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
#
# Copyright (c) 2014 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.

import os
import sys
import logging
from flask import Flask
from leap.common.events import server as events_server
from pixelated.config import app_factory
import pixelated.config.args as input_args
import pixelated.bitmask_libraries.register as leap_register
from pixelated.bitmask_libraries.leap_srp import LeapAuthException
import pixelated.config.credentials_prompt as credentials_prompt
import pixelated.config.reactor_manager as reactor_manager
import pixelated.support.ext_protobuf  # monkey patch for protobuf in OSX
import pixelated.support.ext_sqlcipher  # monkey patch for sqlcipher in debian


app = Flask(__name__, static_url_path='', static_folder=app_factory.get_static_folder())
credentials_pipe = os.path.join('/', 'data', 'credentials-fifo')


def setup():
    try:
        args = input_args.parse()
        app.config.update({'HOST': args.host, 'PORT': args.port})

        debugger = setup_debugger(args.debug)

        if args.register:
            register(*args.register[::-1])
        else:
            if args.dispatcher:
                provider, user, password = fetch_credentials_from_dispatcher()
                app.config['LEAP_SERVER_NAME'] = provider
                app.config['LEAP_USERNAME'] = user
                app.config['LEAP_PASSWORD'] = password
            else:
                configuration_setup(args.config)
            start_services(debugger)
    finally:
        reactor_manager.stop_reactor_on_exit()


def register(username, server_name):
    try:
        leap_register.register_new_user(username, server_name)
    except LeapAuthException:
        print('User already exists')


def fetch_credentials_from_dispatcher():
    if not os.path.exists(credentials_pipe):
        print('The credentials pipe doesn\'t exist')
        sys.exit(1)
    with open(credentials_pipe, 'r') as cred_file:
        provider = cred_file.read()
        username = cred_file.read()
        password = cred_file.read()
    return provider, username, password


def setup_debugger(enabled):
    debug_enabled = enabled or os.environ.get('DEBUG', False)
    if not debug_enabled:
        logging.basicConfig()
        logger = logging.getLogger('werkzeug')
        logger.setLevel(logging.INFO)
    return debug_enabled


def configuration_setup(config):
    if config is not None:
        config_file = os.path.abspath(os.path.expanduser(config))
        app.config.from_pyfile(config_file)
    else:
        provider, user, password = credentials_prompt.run()
        app.config['LEAP_SERVER_NAME'] = provider
        app.config['LEAP_USERNAME'] = user
        app.config['LEAP_PASSWORD'] = password


def start_services(debug):
    reactor_manager.start_reactor(logging=debug)
    events_server.ensure_server(port=8090)
    app_factory.create_app(app, debug)


if __name__ == '__main__':
    setup()