<commit_before>/*
 * Runs an ident server to auth a list of usernames.
 *
 * This purposefully has no dependencies on any other library and is kept as
 * generic as possible. It consists of three functions:
 *
 * configure(opts) : opts => { port: {Number}, hashUsernames: {Boolean} }
 *      Configure the ident server.
 *
 * run()
 *      Start listening on the configured port for incoming requests.
 *
 * setMapping(username, port) : username => {String}, port => {Number}
 *      Assign a username/port mapping. Setting a port of 0 removes the mapping.
 */
"use strict";
var net = require('net');

var log = require("../logging").get("irc-ident");
// TODO:
// - username hashing
// - removing mappings
var config = {
    port: 113,
    hashUsernames: false
};
var portMappings = {
    // port: username
};

var respond = function(sock, localPort, remotePort, username) {
    if (username) {
        sock.end(localPort+", "+remotePort+" : USERID : UNIX : "+username);
    }
    else {
        sock.end(localPort+", "+remotePort+" : ERROR : NO-USER");
    }
};

module.exports = {
    configure: function(opts) {
        log.info("Configuring ident server => %s", JSON.stringify(opts));
        config = opts;
    },
    run: function() {
        net.createServer(function(sock) {
            log.debug("CONNECT %s %s", sock.remoteAddress, sock.remotePort);
            sock.on("data", function(data) {
                log.debug("DATA "+data);
                var ports = data.toString().split(",");
                var remoteConnectPort = Number(ports[1]);
                var localOutgoingPort = Number(ports[0]);
                if (!remoteConnectPort || !localOutgoingPort) {
                    log.debug("BAD DATA");
                    return;
                }
                var username = portMappings[String(localOutgoingPort)];
                if (!username) {
                    log.debug("No user on port %s", localOutgoingPort);
                    respond(sock, localOutgoingPort, remoteConnectPort, null);
                    return;
                }
                log.debug("Port %s is %s", localOutgoingPort, username);
                respond(sock, localOutgoingPort, remoteConnectPort, username);
            });
            sock.on("close", function() {
                log.debug("CLOSE");
            })
        }).listen(config.port, "0.0.0.0");
    },
    setMapping: function(username, port) {
        if (port) {
            portMappings[port] = username;
        }
    }
};<commit_msg>jshint<commit_after>/*
 * Runs an ident server to auth a list of usernames.
 *
 * This purposefully has no dependencies on any other library and is kept as
 * generic as possible. It consists of three functions:
 *
 * configure(opts) : opts => { port: {Number}, hashUsernames: {Boolean} }
 *      Configure the ident server.
 *
 * run()
 *      Start listening on the configured port for incoming requests.
 *
 * setMapping(username, port) : username => {String}, port => {Number}
 *      Assign a username/port mapping. Setting a port of 0 removes the mapping.
 */
"use strict";
var net = require('net');

var log = require("../logging").get("irc-ident");
// TODO:
// - username hashing
// - removing mappings
var config = {
    port: 113,
    hashUsernames: false
};
var portMappings = {
    // port: username
};

var respond = function(sock, localPort, remotePort, username) {
    if (username) {
        sock.end(localPort+", "+remotePort+" : USERID : UNIX : "+username);
    }
    else {
        sock.end(localPort+", "+remotePort+" : ERROR : NO-USER");
    }
};

module.exports = {
    configure: function(opts) {
        log.info("Configuring ident server => %s", JSON.stringify(opts));
        config = opts;
    },
    run: function() {
        net.createServer(function(sock) {
            log.debug("CONNECT %s %s", sock.remoteAddress, sock.remotePort);
            sock.on("data", function(data) {
                log.debug("DATA "+data);
                var ports = data.toString().split(",");
                var remoteConnectPort = Number(ports[1]);
                var localOutgoingPort = Number(ports[0]);
                if (!remoteConnectPort || !localOutgoingPort) {
                    log.debug("BAD DATA");
                    return;
                }
                var username = portMappings[String(localOutgoingPort)];
                if (!username) {
                    log.debug("No user on port %s", localOutgoingPort);
                    respond(sock, localOutgoingPort, remoteConnectPort, null);
                    return;
                }
                log.debug("Port %s is %s", localOutgoingPort, username);
                respond(sock, localOutgoingPort, remoteConnectPort, username);
            });
            sock.on("close", function() {
                log.debug("CLOSE");
            });
        }).listen(config.port, "0.0.0.0");
    },
    setMapping: function(username, port) {
        if (port) {
            portMappings[port] = username;
        }
    }
};
{'fraction_non_alphanumeric': 0.13528009535160906, 'fraction_numerical': 0.003972983710766786, 'mean_word_length': 2.068251066422913, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5244226', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1368, 'n_words': 372}