<commit_before>"use strict";
// a single connection
var path        = require('path');
var config      = require('./config');
var logger      = require('./logger');
var trans       = require('./transaction');
var dns         = require('dns');
var plugins     = require('./plugins');
var constants   = require('./constants');
var rfc1869     = require('./rfc1869');
var fs          = require('fs');
var Address     = require('./address').Address;
var uuid        = require('./utils').uuid;
var outbound    = require('./outbound');
var date_to_str = require('./utils').date_to_str;
var indexOfLF   = require('./utils').indexOfLF;
var ipaddr      = require('ipaddr.js');
var ResultStore = require('./result_store');

var version  = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'))).version;

var line_regexp = /^([^\n]*\n)/;

var connection = exports;

var states = exports.states = {
    STATE_CMD:             1,
    STATE_LOOP:            2,
    STATE_DATA:            3,
    STATE_PAUSE:           4,
    STATE_PAUSE_SMTP:      5,
    STATE_PAUSE_DATA:      6,
    STATE_DISCONNECTING:   99,
    STATE_DISCONNECTED:    100,
};

// copy logger methods into Connection:
for (var key in logger) {
    if (key.match(/^log\w/)) {
        Connection.prototype[key] = (function (key) {
            return function () {
                // pass the connection instance to logger
                var args = [ this ];
                for (var i=0, l=arguments.length; i<l; i++) {
                    args.push(arguments[i]);
                }
                logger[key].apply(logger, args);
            }
        })(key);
    }
}

// Load HAProxy hosts into an object for fast lookups
// as this list is checked on every new connection.
var haproxy_hosts = {};
function loadHAProxyHosts() {
    var hosts = config.get('haproxy_hosts', 'list', function () {
        loadHAProxyHosts();
    });
    var new_host_list = [];
    for (var i=0; i<hosts.length; i++) {
        var host = hosts[i].split(/\//)
        new_host_list[i] = [ipaddr.IPv4.parse(host[0]), parseInt(host[1] || 32)];
    }
    haproxy_hosts = new_host_list;
}
loadHAProxyHosts();

function setupClient(self) {
    var ip = self.client.remoteAddress;
    if (!ip) {
        self.logdebug('setupClient got no IP address for this connection!');
        self.client.destroy();
        return;
    }

    var local_addr = self.server.address();
    if (local_addr && local_addr.address) {
        self.local_ip = ipaddr.process(local_addr.address).toString();
        self.local_port = local_addr.port;
    }
    self.remote_ip = ipaddr.process(ip).toString();
    self.remote_port = self.client.remotePort;
    self.lognotice('connect ip=' + self.remote_ip + ' port=' + self.remote_port +
                   ' local_ip=' + self.local_ip + ' local_port=' + self.local_port);

    self.client.on('end', function() {
        if (self.state >= states.STATE_DISCONNECTING) return;
        self.remote_close = true;
        self.fail('client ' + ((self.remote_host) ? self.remote_host + ' ' : '')
                            + '[' + self.remote_ip + '] half closed connection');
    });

    self.client.on('close', function(has_error) {
        if (self.state >= states.STATE_DISCONNECTING) return;
        self.remote_close = true;
        self.fail('client ' + ((self.remote_host) ? self.remote_host + ' ' : '')
                            + '[' + self.remote_ip + '] dropped connection');
    });

    self.client.on('error', function (err) {
        if (self.state >= states.STATE_DISCONNECTING) return;
        self.fail('client ' + ((self.remote_host) ? self.remote_host + ' ' : '')
                            + '[' + self.remote_ip + '] connection error: ' + err);
    });

    self.client.on('timeout', function () {
        if (self.state >= states.STATE_DISCONNECTING) return;
        self.respond(421, 'timeout', function () {
            self.fail('client ' + ((self.remote_host) ? self.remote_host + ' ' : '')
                                + '[' + self.remote_ip + '] connection timed out');
        });
    });

    self.client.on('data', function (data) {
        self.process_data(data);
    });

    if (haproxy_hosts.some(function (element, index, array) {
        return self.remote_ip.match(element[0], element[1]);
    })) {
        self.proxy = true;
        // Wait for PROXY command
        self.proxy_timer = setTimeout(function () {
            self.respond(421, 'PROXY timeout', function () {
                self.disconnect();
            });
        }, 30 * 1000);
    }
    else {
        plugins.run_hooks('lookup_rdns', self);
    }
}

function Connection(client, server) {
    this.client = client;
    this.server = server;
    this.local_ip = null;
    this.local_port = null;
    this.remote_ip = null;
    this.remote_host = null
    this.remote_port = null;
    this.remote_info = null;
    this.current_data = null;
    this.current_line = null;
    this.greeting = null;
    this.hello_host = null;
    this.using_tls = false;
    this.state = states.STATE_PAUSE;<commit_msg>Bug fix on haproxy ipAddress matching.<commit_after>"use strict";
// a single connection
var path        = require('path');
var config      = require('./config');
var logger      = require('./logger');
var trans       = require('./transaction');
var dns         = require('dns');
var plugins     = require('./plugins');
var constants   = require('./constants');
var rfc1869     = require('./rfc1869');
var fs          = require('fs');
var Address     = require('./address').Address;
var uuid        = require('./utils').uuid;
var outbound    = require('./outbound');
var date_to_str = require('./utils').date_to_str;
var indexOfLF   = require('./utils').indexOfLF;
var ipaddr      = require('ipaddr.js');
var ResultStore = require('./result_store');

var version  = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'))).version;

var line_regexp = /^([^\n]*\n)/;

var connection = exports;

var states = exports.states = {
    STATE_CMD:             1,
    STATE_LOOP:            2,
    STATE_DATA:            3,
    STATE_PAUSE:           4,
    STATE_PAUSE_SMTP:      5,
    STATE_PAUSE_DATA:      6,
    STATE_DISCONNECTING:   99,
    STATE_DISCONNECTED:    100,
};

// copy logger methods into Connection:
for (var key in logger) {
    if (key.match(/^log\w/)) {
        Connection.prototype[key] = (function (key) {
            return function () {
                // pass the connection instance to logger
                var args = [ this ];
                for (var i=0, l=arguments.length; i<l; i++) {
                    args.push(arguments[i]);
                }
                logger[key].apply(logger, args);
            }
        })(key);
    }
}

// Load HAProxy hosts into an object for fast lookups
// as this list is checked on every new connection.
var haproxy_hosts = {};
function loadHAProxyHosts() {
    var hosts = config.get('haproxy_hosts', 'list', function () {
        loadHAProxyHosts();
    });
    var new_host_list = [];
    for (var i=0; i<hosts.length; i++) {
        var host = hosts[i].split(/\//)
        new_host_list[i] = [ipaddr.IPv4.parse(host[0]), parseInt(host[1] || 32)];
    }
    haproxy_hosts = new_host_list;
}
loadHAProxyHosts();

function setupClient(self) {
    var ip = self.client.remoteAddress;
    if (!ip) {
        self.logdebug('setupClient got no IP address for this connection!');
        self.client.destroy();
        return;
    }

    var local_addr = self.server.address();
    if (local_addr && local_addr.address) {
        self.local_ip = ipaddr.process(local_addr.address).toString();
        self.local_port = local_addr.port;
    }
    self.remote_ip = ipaddr.process(ip).toString();
    self.remote_port = self.client.remotePort;
    self.lognotice('connect ip=' + self.remote_ip + ' port=' + self.remote_port +
                   ' local_ip=' + self.local_ip + ' local_port=' + self.local_port);

    self.client.on('end', function() {
        if (self.state >= states.STATE_DISCONNECTING) return;
        self.remote_close = true;
        self.fail('client ' + ((self.remote_host) ? self.remote_host + ' ' : '')
                            + '[' + self.remote_ip + '] half closed connection');
    });

    self.client.on('close', function(has_error) {
        if (self.state >= states.STATE_DISCONNECTING) return;
        self.remote_close = true;
        self.fail('client ' + ((self.remote_host) ? self.remote_host + ' ' : '')
                            + '[' + self.remote_ip + '] dropped connection');
    });

    self.client.on('error', function (err) {
        if (self.state >= states.STATE_DISCONNECTING) return;
        self.fail('client ' + ((self.remote_host) ? self.remote_host + ' ' : '')
                            + '[' + self.remote_ip + '] connection error: ' + err);
    });

    self.client.on('timeout', function () {
        if (self.state >= states.STATE_DISCONNECTING) return;
        self.respond(421, 'timeout', function () {
            self.fail('client ' + ((self.remote_host) ? self.remote_host + ' ' : '')
                                + '[' + self.remote_ip + '] connection timed out');
        });
    });

    self.client.on('data', function (data) {
        self.process_data(data);
    });

    if (haproxy_hosts.some(function (element, index, array) {
        return ipaddr.IPv4.parse(self.remote_ip).match(element[0], element[1]);
    })) {
        self.proxy = true;
        // Wait for PROXY command
        self.proxy_timer = setTimeout(function () {
            self.respond(421, 'PROXY timeout', function () {
                self.disconnect();
            });
        }, 30 * 1000);
    }
    else {
        plugins.run_hooks('lookup_rdns', self);
    }
}

function Connection(client, server) {
    this.client = client;
    this.server = server;
    this.local_ip = null;
    this.local_port = null;
    this.remote_ip = null;
    this.remote_host = null
    this.remote_port = null;
    this.remote_info = null;
    this.current_data = null;
    this.current_line = null;
    this.greeting = null;
    this.hello_host = null;
    this.using_tls = false;
    this.state = states.STATE_PAUSE;
{'fraction_non_alphanumeric': 0.15112634671890304, 'fraction_numerical': 0.007933398628795298, 'mean_word_length': 2.263342921061042, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4459745', 'n_tokens_mistral': 3216, 'n_tokens_neox': 3127, 'n_words': 650}