Skip to content
Snippets Groups Projects
main.cpp 9.03 KiB
Newer Older
  • Learn to ignore specific revisions
  • Kali Kaneko's avatar
    Kali Kaneko committed
    #include <csignal>
    
    #include <unistd.h>
    
    #include <QtGui/qfontdatabase.h>
    
    #include <QApplication>
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    #include <QTimer>
    
    #include <QTranslator>
    
    #include <QCommandLineParser>
    
    #include <QQuickWindow>
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    #include <QQuickStyle>
    
    #include <QSystemTrayIcon>
    #include <QtQml>
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    
    #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;
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    
    
    /* 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. */
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    
    void onStatusChanged() {
        char *ctx = RefreshContext();
    
        emit qw->jsonChanged(QString(ctx));
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        free(ctx);
    }
    
    std::string getEnv(std::string const& key)
    {
        char const* val = getenv(key.c_str());
        return val == NULL ? std::string() : std::string(val);
    }
    
    
    QString getAppName(QJsonValue info, QString provider) {
        for (auto p: info.toArray()) {
            QJsonObject item = p.toObject();
    
    Kali Kaneko's avatar
    Kali Kaneko committed
            if (item["name"].toString().toLower() == provider.toLower()) {
    
                return item["applicationName"].toString();
            }
        }
        return "BitmaskVPN";
    }
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    void catchUnixSignals(std::initializer_list<int> quitSignals) {
        auto handler = [](int sig) -> void {
            printf("\nCatched signal(%d): quitting\n", sig);
            Quit();
    
            QApplication::quit();
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        };
    
        sigset_t blocking_mask;
        sigemptyset(&blocking_mask);
        for (auto sig : quitSignals)
            sigaddset(&blocking_mask, sig);
    
        struct sigaction sa;
        sa.sa_handler = handler;
        sa.sa_mask    = blocking_mask;
        sa.sa_flags   = 0;
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        for (auto sig : quitSignals)
            sigaction(sig, &sa, nullptr);
    }
    
    int main(int argc, char **argv) {
    
        Backend backend;
    
        QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QApplication::setApplicationVersion(backend.getVersion());
    
        // There's a legend about brave coders than, from time to time, have the urge to change
        // the app object to a QGuiApplication. Resist the temptation, oh coder
        // from the future, or otherwise ye shall be punished for long hours wondering
        // why yer little systray resists to be displayed.
        QApplication app(argc, argv);
    
        app.setQuitOnLastWindowClosed(false);
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        app.setAttribute(Qt::AA_UseHighDpiPixmaps);
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        catchUnixSignals({SIGINT, SIGTERM});
    
    
        /* load providers json */
        QFile providerJson (":/providers.json");
        providerJson.open(QIODevice::ReadOnly | QIODevice::Text);
        QJsonModel *providers = new QJsonModel;
    
        QByteArray providerJsonBytes = providerJson.readAll();
        providers->loadJson(providerJsonBytes);
    
        QJsonValue defaultProvider = providers->json().object().value("default");
        QJsonValue providersInfo = providers->json().object().value("providers");
        QString appName = getAppName(providersInfo, defaultProvider.toString());
    
        QApplication::setApplicationName(appName);
    
    
        QCommandLineParser parser;
    
        parser.setApplicationDescription(
    
            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)."),
    
            {
                {"o", "obfs4"},
                QApplication::translate(
                    "main",
    
                    "Use obfs4 to obfuscate the traffic, if available in the provider."),
    
    Kali Kaneko's avatar
    Kali Kaneko committed
                {"a", "enable-autostart"},
    
                QApplication::translate(
                    "main",
    
    Kali Kaneko's avatar
    Kali Kaneko committed
                    "Enable autostart."),
    
        QCommandLineOption webPortOption("web-port", QApplication::translate("main", "Web API port (default: 8080)"), "port", "8080");
    
        parser.addOption(webPortOption);
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        // FIXME need to add note for the translation, on/off shouldn't be translated.
    
        QCommandLineOption startVPNOption("start-vpn", QApplication::translate("main", "Start the VPN, either 'on' or 'off'."), "status", "");
    
        parser.addOption(startVPNOption);
    
        parser.process(app);
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        bool hideSystray     = parser.isSet("no-systray");
    
        bool availableSystray = true;
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        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 enableAutostart = parser.isSet("enable-autostart");
    
        QString startVPN    = parser.value("start-vpn");
    
        if (version) {
            qDebug() << backend.getVersion();
            exit(0);
        }
    
        if (startVPN != "" && startVPN != "on" && startVPN != "off") {
            qDebug() << "Error: --start-vpn must be either 'on' or 'off'";
            exit(0);
        }
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        if (hideSystray)
    
            qDebug() << "Not showing systray icon because --no-systray option is set.";
    
        if (installHelpers) {
    
    Kali Kaneko's avatar
    Kali Kaneko committed
            qDebug() << "Will try to install helpers with sudo";
            InstallHelpers();
            exit(0);
        }
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    #ifdef Q_OS_UNIX
    
        if (getuid() == 0) {
            qDebug() << "Please don't run as root. Aborting.";
            exit(0);
        }
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    #endif
    
        if (!QSystemTrayIcon::isSystemTrayAvailable()) {
    
            qDebug() << "No systray icon available.";
            availableSystray = false;
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        /* set window icon */
    
        /* this one is set in the vendor.qrc resources, that needs to be passed to the project */
        /* there's something weird with icons being cached, need to investigate */
        if (appName == "CalyxVPN") {
            qDebug() << "setting calyx logo";
            app.setWindowIcon(QIcon(":/vendor/calyx.svg"));
        } else if (appName == "RiseupVPN") {
            app.setWindowIcon(QIcon(":/vendor/riseup.svg"));
        }
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    
        /* load translations */
    
        QTranslator translator;
        translator.load(QLocale(), QLatin1String("main"), QLatin1String("_"), QLatin1String(":/i18n"));
        app.installTranslator(&translator);
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        QQmlApplicationEngine engine;
        QQmlContext *ctx = engine.rootContext();
    
        QJsonModel *model = new QJsonModel;
    
    
        // FIXME use qgetenv
    
        QString desktop = QString::fromStdString(getEnv("XDG_CURRENT_DESKTOP"));
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        QString debug = QString::fromStdString(getEnv("DEBUG"));
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        /* 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 */
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        ctx->setContextProperty("jsonModel", model);
    
        ctx->setContextProperty("providers", providers);
    
        ctx->setContextProperty("desktop", desktop);
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        // we're relying on the binary name, for now, to switch themes
        ctx->setContextProperty("flavor", argv[0]);
    
    
        /* set some useful flags */
    
        ctx->setContextProperty("systrayVisible", !hideSystray);
    
        ctx->setContextProperty("systrayAvailable", availableSystray);
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        ctx->setContextProperty("qmlDebug", debug == "1");
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    
    
        //XXX we're doing configuration via config file, but this is a mechanism
        //to change to Dark Theme if desktop has it.
        //qputenv("QT_QUICK_CONTROLS_MATERIAL_VARIANT", "Dense");
        //QQuickStyle::setStyle("Material");
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    
        /* 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) {
    
    Kali Kaneko's avatar
    Kali Kaneko committed
            model->loadJson(js.toUtf8());
        });
    
        /* connect quitDone signal, exit app */
        QObject::connect(&backend, &Backend::quitDone, []() {
    
                QApplication::quit();
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        });
    
        /* register statusChanged callback with CGO */
        const char *stCh = "OnStatusChanged";
        GoString statusChangedEvt = {stCh, (long int)strlen(stCh)};
        SubscribeToEvent(statusChangedEvt, (void *)onStatusChanged);
    
    
        /* we send json as bytes because it breaks as a simple string */
        QString QProvidersJSON(providers->json().toJson(QJsonDocument::Compact));
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        /* let the Go side initialize its internal state */
    
        InitializeBitmaskContext(
                toGoStr(defaultProvider.toString()),
    
                (char*)providerJsonBytes.data(), providerJsonBytes.length(),
    
    Kali Kaneko's avatar
    Kali Kaneko committed
                obfs4, !enableAutostart, toGoStr(startVPN));
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    
    
        /* if requested, enable web api for controlling the VPN */
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        if (webAPI)
    
    Kali Kaneko's avatar
    Kali Kaneko committed
            EnableWebAPI(toGoStr(webPort));
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    
        if (engine.rootObjects().isEmpty())
            return -1;
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        /* kick off your shoes, put your feet up */
        return app.exec();
    }