<commit_before>global._mckay_statistics_opt_out = true; // Opt out of node-steam-user stats

const optionDefinitions = [
    { name: 'config', alias: 'c', type: String, defaultValue: './config.js' }, // Config file location
    { name: 'steam_data', alias: 's', type: String } // Steam data directory
];

const fs = require('fs'),
    winston = require('winston'),
    args = require('command-line-args')(optionDefinitions),
    utils = require('./lib/utils'),
    queue = new (require('./lib/queue'))(),
    InspectURL = require('./lib/inspect_url'),
    botController = new (require('./lib/bot_controller'))(),
    resHandler = require('./lib/res_handler'),
    CONFIG = require(args.config),
    DB = new (require('./lib/db'))(CONFIG.database_url),
    gameData = new (require('./lib/game_data'))(CONFIG.game_files_update_interval, CONFIG.enable_game_file_updates);

if (CONFIG.max_simultaneous_requests === undefined) {
    CONFIG.max_simultaneous_requests = 1;
}

winston.level = CONFIG.logLevel || 'debug';

const errorMsgs = {
    1: 'Improper Parameter Structure',
    2: 'Invalid Inspect Link Structure',
    3: `You may only have ${CONFIG.max_simultaneous_requests} pending request(s) at a time`,
    4: 'Valve\'s servers didn\'t reply in time',
    5: 'Valve\'s servers appear to be offline, please try again later',
    6: 'Something went wrong on our end, please try again',
    7: 'Invalid origin'
};

if (CONFIG.logins.length === 0) {
    console.log('There are no bot logins. Please add some in config.json');
    process.exit(1);
}

if (args.steam_data) {
    CONFIG.bot_settings.steam_user.dataDirectory = args.steam_data;
}

for (let loginData of CONFIG.logins) {
    botController.addBot(loginData, CONFIG.bot_settings);
}

DB.connect();

function filterMinimalKeys(doc) {
    return utils.filterKeys(
        ['defindex', 'paintindex', 'paintseed', 'origin', 'stickers', 'floatvalue', 'min', 'max', 'origin_name',
               'low_rank', 'high_rank', 'rarity', 'quality'], doc);
}

const lookupHandler = function (params) {
    // Check if the item is already in the DB
    DB.getItemData(params).then((doc) => {
        // If we got the result, just return it
        if (doc) {
            if (!doc.price && params.price) {
                DB.updateItemPrice(doc.a, params.price);
            }

            gameData.addAdditionalItemProperties(doc);

            if (params.minimal) {
                doc = filterMinimalKeys(doc);
            }

            doc = utils.removeNullValues(doc);

            resHandler.respondFloatToUser(params, {'iteminfo': doc});
            return;
        }

        // Check if there is a bot online to process this request
        if (!botController.hasBotOnline()) {
            resHandler.respondErrorToUser(params, {error: errorMsgs[5], code: 5}, 503);
            return;
        }

        if (CONFIG.max_simultaneous_requests > 0 &&
            queue.getUserQueuedAmt(params.ip) >= CONFIG.max_simultaneous_requests) {
            resHandler.respondErrorToUser(params, {error: errorMsgs[3], code: 3}, 400);
            return;
        }

        queue.addJob(params, CONFIG.bot_settings.max_attempts);

        if (params.type === 'ws') {
            resHandler.respondInfoToUser(params, {'msg': `Your request for ${params.a} is in the queue`});
        }
    }).catch((err) => {
        winston.error(`getItemData Promise rejected: ${err.message}`);
        resHandler.respondErrorToUser(params, {error: errorMsgs[6], code: 6}, 500);
    });
};

// Setup and configure express
const app = require('express')();

if (CONFIG.trust_proxy === true) {
    app.enable('trust proxy');
}

CONFIG.allowed_regex_origins = CONFIG.allowed_regex_origins || [];
CONFIG.allowed_origins = CONFIG.allowed_origins || [];
const allowedRegexOrigins = CONFIG.allowed_regex_origins.map((origin) => new RegExp(origin));

app.get('/', function(req, res) {
    // Allow some origins
    if (CONFIG.allowed_origins.length > 0 && req.get('origin') != undefined) {
        // check to see if its a valid domain
        const allowed = CONFIG.allowed_origins.indexOf(req.get('origin')) > -1 ||
            allowedRegexOrigins.findIndex((reg) => reg.test(req.get('origin'))) > -1;

        if (allowed) {
            res.header('Access-Control-Allow-Origin', req.get('origin'));
            res.header('Access-Control-Allow-Methods', 'GET');
        } else {
            res.status(400).json({error: errorMsgs[7], code: 7});
            return;
        }
    } else if (CONFIG.allowed_origins.length > 0 && (req.get('User-Agent') || '').indexOf('Chrome') > -1) {
        // If we have explicit allowed origins but the client omits the origin header and is a browser, block
        res.status(400).json({error: errorMsgs[7], code: 7});
        return;
    }

    // Get and parse parameters
    let thisLink;<commit_msg>Revert "Adds Measures Against Browser Tools Omitting Headers"<commit_after>global._mckay_statistics_opt_out = true; // Opt out of node-steam-user stats

const optionDefinitions = [
    { name: 'config', alias: 'c', type: String, defaultValue: './config.js' }, // Config file location
    { name: 'steam_data', alias: 's', type: String } // Steam data directory
];

const fs = require('fs'),
    winston = require('winston'),
    args = require('command-line-args')(optionDefinitions),
    utils = require('./lib/utils'),
    queue = new (require('./lib/queue'))(),
    InspectURL = require('./lib/inspect_url'),
    botController = new (require('./lib/bot_controller'))(),
    resHandler = require('./lib/res_handler'),
    CONFIG = require(args.config),
    DB = new (require('./lib/db'))(CONFIG.database_url),
    gameData = new (require('./lib/game_data'))(CONFIG.game_files_update_interval, CONFIG.enable_game_file_updates);

if (CONFIG.max_simultaneous_requests === undefined) {
    CONFIG.max_simultaneous_requests = 1;
}

winston.level = CONFIG.logLevel || 'debug';

const errorMsgs = {
    1: 'Improper Parameter Structure',
    2: 'Invalid Inspect Link Structure',
    3: `You may only have ${CONFIG.max_simultaneous_requests} pending request(s) at a time`,
    4: 'Valve\'s servers didn\'t reply in time',
    5: 'Valve\'s servers appear to be offline, please try again later',
    6: 'Something went wrong on our end, please try again',
    7: 'Invalid origin'
};

if (CONFIG.logins.length === 0) {
    console.log('There are no bot logins. Please add some in config.json');
    process.exit(1);
}

if (args.steam_data) {
    CONFIG.bot_settings.steam_user.dataDirectory = args.steam_data;
}

for (let loginData of CONFIG.logins) {
    botController.addBot(loginData, CONFIG.bot_settings);
}

DB.connect();

function filterMinimalKeys(doc) {
    return utils.filterKeys(
        ['defindex', 'paintindex', 'paintseed', 'origin', 'stickers', 'floatvalue', 'min', 'max', 'origin_name',
               'low_rank', 'high_rank', 'rarity', 'quality'], doc);
}

const lookupHandler = function (params) {
    // Check if the item is already in the DB
    DB.getItemData(params).then((doc) => {
        // If we got the result, just return it
        if (doc) {
            if (!doc.price && params.price) {
                DB.updateItemPrice(doc.a, params.price);
            }

            gameData.addAdditionalItemProperties(doc);

            if (params.minimal) {
                doc = filterMinimalKeys(doc);
            }

            doc = utils.removeNullValues(doc);

            resHandler.respondFloatToUser(params, {'iteminfo': doc});
            return;
        }

        // Check if there is a bot online to process this request
        if (!botController.hasBotOnline()) {
            resHandler.respondErrorToUser(params, {error: errorMsgs[5], code: 5}, 503);
            return;
        }

        if (CONFIG.max_simultaneous_requests > 0 &&
            queue.getUserQueuedAmt(params.ip) >= CONFIG.max_simultaneous_requests) {
            resHandler.respondErrorToUser(params, {error: errorMsgs[3], code: 3}, 400);
            return;
        }

        queue.addJob(params, CONFIG.bot_settings.max_attempts);

        if (params.type === 'ws') {
            resHandler.respondInfoToUser(params, {'msg': `Your request for ${params.a} is in the queue`});
        }
    }).catch((err) => {
        winston.error(`getItemData Promise rejected: ${err.message}`);
        resHandler.respondErrorToUser(params, {error: errorMsgs[6], code: 6}, 500);
    });
};

// Setup and configure express
const app = require('express')();

if (CONFIG.trust_proxy === true) {
    app.enable('trust proxy');
}

CONFIG.allowed_regex_origins = CONFIG.allowed_regex_origins || [];
CONFIG.allowed_origins = CONFIG.allowed_origins || [];
const allowedRegexOrigins = CONFIG.allowed_regex_origins.map((origin) => new RegExp(origin));

app.get('/', function(req, res) {
    // Allow some origins
    if (CONFIG.allowed_origins.length > 0 && req.get('origin') != undefined) {
        // check to see if its a valid domain
        const allowed = CONFIG.allowed_origins.indexOf(req.get('origin')) > -1 ||
            allowedRegexOrigins.findIndex((reg) => reg.test(req.get('origin'))) > -1;

        if (allowed) {
            res.header('Access-Control-Allow-Origin', req.get('origin'));
            res.header('Access-Control-Allow-Methods', 'GET');
        } else {
            res.status(400).json({error: errorMsgs[7], code: 7});
            return;
        }
    }

    // Get and parse parameters
    let thisLink;
{'fraction_non_alphanumeric': 0.1475929054054054, 'fraction_numerical': 0.007918074324324325, 'mean_word_length': 2.958629335562056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '1383003', 'n_tokens_mistral': 2929, 'n_tokens_neox': 2842, 'n_words': 686}