<commit_before>/**
 * MozIoT Gateway Tunnelservice.
 *
 * Manages the tunnel service.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

const fs = require('fs');
const config = require('config');
const Settings = require('./models/settings');
var spawnSync = require('child_process').spawn;

var TunnelService = {

    pagekiteProcess: null,
    tunneltoken: null,
    switchToHttps: null,

    // method that starts the client if the box has a registered tunnel
    start: function(response, urlredirect) {
        Settings.get('tunneltoken').then(function(result) {
            if (typeof result === 'object') {
                let endpoint = result.name + '.' +
                    config.get('ssltunnel.domain');
                this.pagekiteProcess  =
                    spawnSync(config.get('ssltunnel.pagekite_cmd'),
                              ['--clean', '--frontend=' + endpoint + ':' +
                              config.get('ssltunnel.port'),
                              '--service_on=https:' + endpoint +
                              ':localhost:' +
                              config.get('ports.https')+':moziot']);

                // TODO: we should replace the hardcoded secret by the token
                // after change the server
                this.pagekiteProcess.stdout.on('data', (data) => {
                    console.log(`[pagekite] stdout: ${data}`);
                    if (response) {
                        if (data.indexOf('err=Error in connect') > -1) {
                            response.status(400).end();
                        } else if (data.indexOf('connect=') > -1) {
                            response.send(urlredirect);
                            response.status(200).end();
                        }
                    }
                });
                this.pagekiteProcess.on('data', (data) => {
                    console.log(`[pagekite] stderr: ${data}`);
                });
                this.pagekiteProcess.on('close', (code) => {
                    console.log(`[pagekite] process exited with code ${code}`);
                });
            } else {
                console.error('tunneltoken not set');
                if (response) {
                    response.status(400).end();
                }
            }
        }).catch(function(e) {
            console.error('Failed to get tunneltoken setting');
            console.error(e);

            if (response) {
                response.status(400).send(e);
            }
        });
    },

    // method to stop pagekite process
    stop: function() {
        if (this.pagekiteProcess) {
            this.pagekiteProcess.kill('SIGHUP');
        }
    },

    // method to check if the box has certificates
    hasCertificates: function() {
        return fs.existsSync('./certificate.pem') &&
            fs.existsSync('./privatekey.pem');
    },

    // method to check if the box has a registered tunnel
    hasTunnelToken: async function() {
        let tunneltoken = await Settings.get('tunneltoken');
        return typeof tunneltoken === 'object';
    },

    // method to check if user skipped the sl tunnel setup
    userSkipped: async function() {
        let notunnel = await Settings.get('notunnel');
        if (typeof notunnel === 'boolean' && notunnel) {
            return true;
        }

        return false;
    }
}

module.exports = TunnelService;<commit_msg>Call /ping periodically to track domain activity.<commit_after>/**
 * MozIoT Gateway Tunnelservice.
 *
 * Manages the tunnel service.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

const fs = require('fs');
const config = require('config');
const Settings = require('./models/settings');
const fetch = require('node-fetch');
var spawnSync = require('child_process').spawn;

var TunnelService = {

    pagekiteProcess: null,
    tunneltoken: null,
    switchToHttps: null,

    // method that starts the client if the box has a registered tunnel
    start: function(response, urlredirect) {
        Settings.get('tunneltoken').then((result) => {
            if (typeof result === 'object') {
                let endpoint = result.name + '.' +
                    config.get('ssltunnel.domain');
                this.pagekiteProcess  =
                    spawnSync(config.get('ssltunnel.pagekite_cmd'),
                              ['--clean', '--frontend=' + endpoint + ':' +
                              config.get('ssltunnel.port'),
                              '--service_on=https:' + endpoint +
                              ':localhost:' +
                              config.get('ports.https')+':moziot']);

                // TODO: we should replace the hardcoded secret by the token
                // after change the server
                this.pagekiteProcess.stdout.on('data', (data) => {
                    console.log(`[pagekite] stdout: ${data}`);
                    if (response) {
                        if (data.indexOf('err=Error in connect') > -1) {
                            response.status(400).end();
                        } else if (data.indexOf('connect=') > -1) {
                            response.send(urlredirect);
                            response.status(200).end();
                        }
                    }
                });
                this.pagekiteProcess.on('data', (data) => {
                    console.log(`[pagekite] stderr: ${data}`);
                });
                this.pagekiteProcess.on('close', (code) => {
                    console.log(`[pagekite] process exited with code ${code}`);
                });

                // Ping the registration server every hour.
                const delay = 60 * 60 * 1000;
                setInterval(() => this.pingRegistrationServer(), delay);
            } else {
                console.error('tunneltoken not set');
                if (response) {
                    response.status(400).end();
                }
            }
        }).catch(function(e) {
            console.error('Failed to get tunneltoken setting');
            console.error(e);

            if (response) {
                response.status(400).send(e);
            }
        });
    },

    // method to stop pagekite process
    stop: function() {
        if (this.pagekiteProcess) {
            this.pagekiteProcess.kill('SIGHUP');
        }
    },

    // method to check if the box has certificates
    hasCertificates: function() {
        return fs.existsSync('./certificate.pem') &&
            fs.existsSync('./privatekey.pem');
    },

    // method to check if the box has a registered tunnel
    hasTunnelToken: async function() {
        let tunneltoken = await Settings.get('tunneltoken');
        return typeof tunneltoken === 'object';
    },

    // method to check if user skipped the ssl tunnel setup
    userSkipped: async function() {
        let notunnel = await Settings.get('notunnel');
        if (typeof notunnel === 'boolean' && notunnel) {
            return true;
        }

        return false;
    },

    // method to ping the registration server to track active domains
    pingRegistrationServer: function() {
        Settings.get('tunneltoken').then((result) => {
            if (typeof result === 'object') {
                fetch(config.get('ssltunnel.registration_endpoint') +
                      '/ping?token=' + result.token)
                    .catch((e) => {
                        console.debug('Failed to ping registration server:', e);
                    });
            } else {
                console.error('tunneltoken not set');
            }
        }).catch(function(e) {
            console.error('Failed to get tunneltoken setting:', e);
        });
    },
}

module.exports = TunnelService;
{'fraction_non_alphanumeric': 0.12521631644004944, 'fraction_numerical': 0.005438813349814586, 'mean_word_length': 1.5523659305993691, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2175811', 'n_tokens_mistral': 2154, 'n_tokens_neox': 2038, 'n_words': 522}