{"size": 2576, "ext": "js", "max_stars_repo_path": "src/connectionUtil/connectionUtil.js", "max_stars_repo_name": "OfryL/connectionControllerBot", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/connectionUtil/connectionUtil.js", "max_issues_repo_name": "OfryL/connectionControllerBot", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/connectionUtil/connectionUtil.js", "max_forks_repo_name": "OfryL/connectionControllerBot", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.2857142857, "max_line_length": 80, "alphanum_fraction": 0.5446428571}
const logger = require('../logger')('app.connectionUtil');
const request = require('request');
const ifconfig = require('ifconfig');
const exec = require('child_process').exec;

//todo move to config
const ethName = 'en0';
const remoteUrl = 'https://www.google.com/';
const isWindows = true;

const getInterfaces = function () {
    return new Promise(function (resolve, reject) {
        ifconfig(function(err, configs) {
            if(err){
                reject(err);
            } else {
                resolve(configs);
            }
        });
    });
};

const execCmd = function (term) {
    return new Promise(function (resolve, reject) {
        exec(term, function(err, stdout, stderr) {
            if (err) {
                logger.error('error in exec: %s\n%s',err,stderr);
                reject(err);
            } else {
                resolve(stdout);
            }
        });
    });
};

const setInterface = async function (state) {
    if(isWindows) {
        if (state == 'down') {
            await execCmd(`netsh interface set interface "${ethName}" disable`);
        } else {
            await execCmd(`netsh interface set interface "${ethName}" enable`);
        }
    } else {
        await execCmd(`ifconfig ${ethName} ${state}`);
    }
};

const resetSystem = async function () {
    logger.warn('execute system reboot');
    if(isWindows) {
        await execCmd('shutdown -r -t 0');
    } else {
        await execCmd('reboot');
    }
};

const sleep = async function(time) {
    if(isWindows) {
        await execCmd('ping 127.0.0.1 -n ' + time);
    } else {
        await execCmd('sleep ' + time);
    }
};

const resetInterface = async function () {
    logger.log('restarting interface name %s', ethName);
    await setInterface('down');
    await sleep(5);
    await setInterface('up');
    await sleep(5);
};

const checkConnectionViaHttpRequest = function () {
    return new Promise(function (resolve, reject) {
    logger.debug('check connection via http request started');
        request(remoteUrl, function (error, response, body) {
            if (error) {
                logger.warn(`error requesting ${remoteUrl} - ${error}`);
                resolve(0);
            } else {
                if (response.statusCode == 200) {
                    resolve(200);
                } else {
                    resolve(response.statusCode);
                }
            }
        });
    });
};


module.exports = {
    checkConnectionViaHttpRequest,
    setInterface,
    resetInterface,
    getInterfaces,
    resetSystem
};