{"size": 1638, "ext": "js", "max_stars_repo_path": "src/app.js", "max_stars_repo_name": "CreepSore/Node-Source-Query", "max_stars_repo_stars_event_min_datetime": "2021-07-11T03:26:35.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-11T03:26:35.000Z", "max_issues_repo_path": "src/app.js", "max_issues_repo_name": "CreepSore/Node-Source-Query", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/app.js", "max_forks_repo_name": "CreepSore/Node-Source-Query", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.2413793103, "max_line_length": 85, "alphanum_fraction": 0.6013431013}
"use strict";
const PacketFactory = require("./PacketFactory.js");

const dgram = require("dgram");
const fs = require("fs");

const QUERY_HEADER = [0xFF, 0xFF, 0xFF, 0xFF];

const socket = dgram.createSocket("udp4");
let config;

const init = function() {
    let file = fs.readFileSync(__dirname + "/../config/config.json", "utf8");
    config = JSON.parse(file);

    socket.on("message", onPacket);
    socket.bind(config.connection.port, config.connection.mask);
};

const onPacket = function(msg, remote) {
    if(matchArrayPattern(msg, QUERY_HEADER)) {
        let pckt = String(msg).substring(4);
        console.log("[VALID]" + remote.address + ":" + remote.port + " -> " + pckt);
        handlePacket(pckt, remote);
    } else {
        console.log("[INVALID]" + remote.address + ":" + remote.port + " -> " + msg);
    }
};

const handlePacket = function(pckt, remote) {
    if(pckt.startsWith("TSource Engine Query")) {
        let toSend = PacketFactory.constructQueryPacket(config.serverdata);
        socket.send(toSend, remote.port, remote.address, (err) => {
            if(err) console.log(err);
        });
    } else if(pckt.startsWith("U")) {
        let toSend = PacketFactory.constructPlayerPacket(config.serverdata);
        socket.send(toSend, remote.port, remote.address, (err) => {
            if(err) console.log(err);
        })
    }
};

const matchArrayPattern = function(src, toMatch) {
    if(toMatch.length > src.length) {
        return false;
    }

    for(let i = 0; i < toMatch.length; i++) {
        if(src[i] !== toMatch[i]) {
            return false;
        }
    }

    return true;
};

init();