summaryrefslogtreecommitdiff
path: root/gui/main.cpp
blob: 42123ae3fb05416fb4e833ba5bded2c180e7189a (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
191
192
193
194
195
196
197
198
199
200
#include <csignal>
#include <QApplication>
#include <QTimer>
#include <QTranslator>
#include <QCommandLineParser>
#include <QQuickWindow>
#include <QSystemTrayIcon>
#include <QtQml>

#include "handlers.h"
#include "qjsonmodel.h"
#include "lib/libgoshim.h"

/* Hi! I'm Troy McClure and I'll be your guide today. You probably remember me
   from blockbusters like "here be dragons" and "darling, I wrote a little
   contraption". */

QJsonWatch *qw = new QJsonWatch;

/* onStatusChanged is the C function that we register as a callback with CGO.
   It pulls a string serialization of the context object, than we then pass
   along to Qml via signals. */

void onStatusChanged() {
    char *ctx = RefreshContext();
    emit qw->jsonChanged(QString(ctx));
    free(ctx);
}

std::string getEnv(std::string const& key)
{
    char const* val = getenv(key.c_str());
    return val == NULL ? std::string() : std::string(val);
}

void signalHandler(int) {
    Quit();
    exit(0);
}

int main(int argc, char **argv) {
    signal(SIGINT, signalHandler);

    Backend backend;

    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication::setApplicationName(backend.getAppName());
    QApplication::setApplicationVersion(backend.getVersion());
    QApplication app(argc, argv);
    app.setQuitOnLastWindowClosed(false);

    QCommandLineParser parser;
    parser.setApplicationDescription(
        backend.getAppName() +
        QApplication::translate(
            "main", ": a fast and secure VPN. Powered by Bitmask."));
    parser.addHelpOption();
    parser.addVersionOption();
    parser.addOptions({
        {
            {"n", "no-systray"},
            QApplication::translate("main",
                                    "Do not show the systray icon (useful "
                                    "together with gnome shell "
                                    "extension, or to control VPN by other means)."),
        },
        {
            {"w", "web-api"},
            QApplication::translate(
                "main",
                "Enable web api."),
        },
        {
            {"i", "install-helpers"},
            QApplication::translate(
                "main",
                "Install helpers (linux only, requires sudo)."),
        },
        {
            {"v", "version"},
            QApplication::translate(
                "main",
		"Version of the bitmask-vpn."),
        },
        {
            {"o", "obfs4"},
            QApplication::translate(
                "main",
		"Use obfs4 to obfuscate the traffic is available in the provider."),
        },
        {
            {"a", "disable-autostart"},
            QApplication::translate(
                "main",
		"Disable the autostart for the next run."),
        },
        {
            {"s", "start-vpn"},
            QApplication::translate(
                "main",
		"Start the vpn in turned 'on' or 'off'."),
        },
    });
    QCommandLineOption webPortOption("web-port", QApplication::translate("main", "Web api port (default: 8080)"), "port", "8080");
    parser.addOption(webPortOption);
    QCommandLineOption startVPNOption("start-vpn", QApplication::translate("main", "Start the vpn in turned 'on' or 'off'."), "status", "");
    parser.addOption(startVPNOption);
    parser.process(app);

    bool hideSystray    = parser.isSet("no-systray");
    bool installHelpers = parser.isSet("install-helpers");
    bool webAPI         = parser.isSet("web-api");
    QString webPort     = parser.value("web-port");
    bool version        = parser.isSet("version");
    bool obfs4          = parser.isSet("obfs4");
    bool disAutostart   = parser.isSet("disable-autostart");
    QString startVPN    = parser.value("start-vpn");

    if (version) {
        qDebug() << backend.getVersion();
        exit(0);
    }

    if (hideSystray) {
        qDebug() << "Not showing systray icon because --no-systray option is set.";
    }

    if (installHelpers) {
        qDebug() << "Will try to install helpers with sudo";
        InstallHelpers();
        exit(0);
    }

    if (!QSystemTrayIcon::isSystemTrayAvailable()) {
        qDebug() << "No systray icon available. Things might not work for now, sorry...";
    }

    QTranslator translator;
    translator.load(QLocale(), QLatin1String("main"), QLatin1String("_"), QLatin1String(":/i18n"));
    app.installTranslator(&translator);

    QQmlApplicationEngine engine;
    QQmlContext *ctx = engine.rootContext();

    QJsonModel *model = new QJsonModel;

    /* load providers json */
    QFile providerJson (":/providers.json");
    providerJson.open(QIODevice::ReadOnly | QIODevice::Text);
    QJsonModel *providers = new QJsonModel;
    providers->loadJson(providerJson.readAll());

    /* the backend handler has slots for calling back to Go when triggered by
       signals in Qml. */
    ctx->setContextProperty("backend", &backend);

    /* set the json model, load providers.json */
    ctx->setContextProperty("jsonModel", model);
    ctx->setContextProperty("providers", providers);

    /* set some useful flags */
    ctx->setContextProperty("systrayVisible", !hideSystray);

    engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));

    /* connect the jsonChanged signal explicitely.
        In the lambda, we reload the json in the model every time we receive an
        update from Go */
    QObject::connect(qw, &QJsonWatch::jsonChanged, [model](QString js) {
        model->loadJson(js.toUtf8());
    });

    /* connect quitDone signal, exit app */
    QObject::connect(&backend, &Backend::quitDone, []() {
            QGuiApplication::quit();
    });

    /* register statusChanged callback with CGO */
    const char *stCh = "OnStatusChanged";
    GoString statusChangedEvt = {stCh, (long int)strlen(stCh)};
    SubscribeToEvent(statusChangedEvt, (void *)onStatusChanged);

    QJsonValue defaultProvider = providers->json().object().value("default");
    /* we send json as bytes because it breaks as a simple string */
    QString QProvidersJSON(providers->json().toJson(QJsonDocument::Compact));

    /* let the Go side initialize its internal state */
    InitializeBitmaskContext(
            toGoStr(defaultProvider.toString()),
            (char*)QProvidersJSON.toUtf8().data(), strlen(QProvidersJSON.toUtf8().data()),
	    obfs4, disAutostart, toGoStr(startVPN));

    /* if requested, enable web api for controlling the VPN */
    if (webAPI) {
        EnableWebAPI(toGoStr(webPort));
    };

    /* kick off your shoes, put your feet up */
    return app.exec();
}