{"size": 2028, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "BattleDash/DungeonsRPC", "max_stars_repo_stars_event_min_datetime": "2020-05-30T03:51:22.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-11T02:19:07.000Z", "max_issues_repo_path": "index.js", "max_issues_repo_name": "BattleDash/DungeonsRPC", "max_issues_repo_issues_event_min_datetime": "2021-06-27T19:30:14.000Z", "max_issues_repo_issues_event_max_datetime": "2021-12-05T23:12:13.000Z", "max_forks_repo_path": "index.js", "max_forks_repo_name": "BattleDash/DungeonsRPC", "max_forks_repo_forks_event_min_datetime": "2021-05-08T13:42:59.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-15T15:59:59.000Z"}
{"max_stars_count": 9, "max_issues_count": 3, "max_forks_count": 2, "avg_line_length": 33.2459016393, "max_line_length": 191, "alphanum_fraction": 0.5350098619}
const sudo = require('sudo-prompt');
const fs = require('fs');
const log4js = require("log4js");
const readline = require('readline');

var logger = log4js.getLogger("Hosts");
logger.level = "all";

var hosts = fs.readFileSync('C:\\Windows\\System32\\drivers\\etc\\hosts', 'utf8');

var lines = hosts.split('\r\n');
if (lines.includes('127.0.0.1       C839E.playfabapi.com')) {
    require('./web');
} else {
    lines.push('127.0.0.1       C839E.playfabapi.com');
    try {
        fs.writeFileSync('C:\\Windows\\System32\\drivers\\etc\\hosts', lines.join('\r\n'));
    } catch (e) {
        (async () => {
            var ans = await askHosts();
            if (!ans) {
                logger.error('Since hosts are incorrect, server cannot proceed.');
                return process.exit(0);
            }
            sudo.exec('node index',
              err => {
                if (err) {
                    logger.error('We need Windows Authorization to edit a system file like Hosts, since you chose no, we can\'t proceed.');
                    return process.exit(0);
                }
                logger.info('Updated Hosts file to redirect requests to this server, proceeding...');
                require('./web');
              }
            );
        })();
    }
}

function askHosts() {
    return new Promise(async resolve => {
        logger.warn('We couldn\'t find the required settings in your system hosts file to redirect Minecraft Dungeons\' requests to this server. Should we automatically modify Hosts? [y/n]');
        var ans = await askQuestion();
        if (ans == 'y' || ans == 'n' || ans == 'yes' || ans == 'no') {
            resolve(ans == 'y' || ans == 'yes');
        } else {
            resolve(await askHosts());
        }
    });
}

function askQuestion() {
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
    });

    return new Promise(resolve => rl.question("", ans => {
        rl.close();
        resolve(ans);
    }));
}