all files / jWebDriver/lib/ hostsproxy.js

87.59% Statements 120/137
68% Branches 17/25
77.42% Functions 24/31
87.59% Lines 120/137
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229                                                              37× 37× 37× 37×     34×             39× 39× 39× 39×   39× 39×       39×         39× 39× 39× 39×                 30× 30×     30× 30×               30× 30× 30×   30×       30×                   39× 39× 39× 39×                                                 37× 74×   37× 37×              
'use strict';
const net = require('net');
const http = require('http');
const url = require('url');
const util = require('util');
const EventEmitter = require('events').EventEmitter;
const extend = require('xtend');
 
const defaultConfig = {
    hosts: ''
};
 
const HostsProxyServer = function(){
    let self = this;
    return self._init.apply(self, arguments);
};
util.inherits(HostsProxyServer, EventEmitter);
 
const HostsProxyServerPrototype = HostsProxyServer.prototype;
 
HostsProxyServerPrototype._init = function(config){
    let self = this;
    config = extend({}, defaultConfig, config);
 
    self.port = null;
    self.workPort = null;
    self.httpPort = null;
 
    self._portSever = null;
    self._httpServer = null;
 
    Iif(config.mode === 'forward'){
        self.setForward(config.forwardHost, config.forwardPort);
    }
    else{
        self.setHosts(config.hosts);
    }
};
 
// set hosts
HostsProxyServerPrototype.setHosts = function(strHosts){
    let self = this;
    self.forwardHost = null;
    self.forwardPort = null;
    let mapHosts = {};
    let arrLines = strHosts.split(/\r?\n/);
    arrLines.forEach(function(line){
        let match = line.match(/^\s*([\da-z\-\.]{3,})\s+([^#]+)/i);
        Eif(match){
            match[2].trim().split(/\s+/).forEach(function(domain){
                domain = domain.toLowerCase();
                Eif(mapHosts[domain] === undefined){
                    mapHosts[domain] = match[1];
                }
            });
        }
    });
    self._mapHosts = mapHosts;
};
 
// set forward proxy
HostsProxyServerPrototype.setForward = function(forwardHost, forwardPort){
    let self = this;
    self.forwardHost = forwardHost;
    self.forwardPort = forwardPort;
};
 
// get new hostname
HostsProxyServerPrototype._getNewHostname = function(hostname){
    let self = this;
    let mapHosts = self._mapHosts;
    for(let domain in mapHosts){
        if(shExpMatch(hostname, domain)){
            return mapHosts[domain];
        }
    }
    return hostname;
};
 
// start proxy server
HostsProxyServerPrototype.listen = function(port, callback){
    let self = this;
    self.port = port;
 
    Eif(callback !== undefined){
        self.on('ready', callback);
    }
    // create port proxy
    let portSever = self._portSever = net.createServer(function(client) {
        let forwardPort = self.forwardPort || self.httpPort;
        let forwardHost = self.forwardHost || '127.0.0.1';
        let forwardServer = net.connect(forwardPort, forwardHost, function() {
            forwardServer.pipe(client);
        });
        client.pipe(forwardServer);
        forwardServer.on('error', function(err){
            client.end();
            self.emit('error', err);
        });
        client.on('error', function(err){
            forwardServer.end();
            self.emit('error', err);
        });
    });
    let mapConnection1 = self._mapConnection1 = {};
    portSever.on('connection', function(conn){
        let key = conn.remoteAddress + ':' + conn.remotePort;
        mapConnection1[key] = conn;
        conn.on('close', function() {
            delete mapConnection1[key];
        });
    });
    portSever.listen(port, '0.0.0.0', function() {
        let workPort = self.workPort =portSever.address().port;
        self.emit('ready', {
            port: workPort
        });
    });
    portSever.on('error', function(err){
        self.emit('error', err);
    });
    // create http proxy
    let httpServer = self._httpServer = http.createServer(function (clientRequest, clientResponse) {
        let urlInfo = url.parse(clientRequest.url);
        let userIp = clientRequest.connection.remoteAddress ||
            clientRequest.socket.remoteAddress ||
            clientRequest.connection.socket.remoteAddress;
        clientRequest.headers['X-Forwarded-For'] = userIp;
        let reqOptions = {
            hostname: self._getNewHostname(urlInfo.hostname),
            port: urlInfo.port || 80,
            method: clientRequest.method,
            path: urlInfo.path,
            headers: clientRequest.headers,
            agent: false
        };
        let remoteServer = http.request(reqOptions, function (remoteResponse) {
            clientResponse.writeHead(remoteResponse.statusCode, remoteResponse.headers);
            remoteResponse.pipe(clientResponse);
        });
        remoteServer.on('error', function (err) {
            clientResponse.end();
            self.emit('error', err);
        });
        clientRequest.pipe(remoteServer);
    });
    // create https proxy
    httpServer.on('connect', function (httpRequest, reqSocket) {
        let urlInfo = url.parse('http://' + httpRequest.url);
        let remoteSocket = net.connect(urlInfo.port, self._getNewHostname(urlInfo.hostname), function () {
            reqSocket.write("HTTP/1.1 200 Connection established\r\n\r\n");
            remoteSocket.pipe(reqSocket).pipe(remoteSocket);
        });
        remoteSocket.on('error', function (err) {
            reqSocket.end();
            self.emit('error', err);
        });
    });
    httpServer.on('error', function(err){
        self.emit('error', err);
    });
    let mapConnection2 = self._mapConnection2 = {};
    httpServer.on('connection', function(conn){
        let key = conn.remoteAddress + ':' + conn.remotePort;
        mapConnection2[key] = conn;
        conn.on('close', function() {
            delete mapConnection2[key];
        });
    });
    httpServer.listen(0, '0.0.0.0', function() {
        let httpPort = self.httpPort = httpServer.address().port;
        self.emit('httpReady', {
            port: httpPort
        });
    });
};
 
// close proxy
HostsProxyServerPrototype.close = function(callback){
    let self = this;
    let httpServer = self._httpServer;
    let portSever = self._portSever;
    Eif(httpServer !== null){
        let closeCount = 0;
        httpServer.close(checkAllClose);
        portSever.close(checkAllClose);
        // destroy all connection
        let mapConnection1 = self._mapConnection1;
        for (let key in mapConnection1){
            mapConnection1[key].destroy();
        }
        let mapConnection2 = self._mapConnection2;
        for (let key in mapConnection2){
            mapConnection2[key].destroy();
        }
        function checkAllClose(){
            closeCount ++;
            if(closeCount == 2){
                callback && callback();
                self._httpServer = null;
                self._portSever = null;
                self.emit('close');
            }
        }
    }
};
 
const hostsproxy = {
    Server: HostsProxyServer,
    createServer: function(config){
        return new HostsProxyServer(config);
    }
};
 
// check shExp match
function shExpMatch(text, exp){
    exp = exp.replace(/\.|\*|\?/g, function(c){
        return { '.': '\\.', '*': '.*?', '?': '.' }[c];
    });
    try{
        return new RegExp('^'+exp+'$').test(text);
    }
    catch(e){
        return false;
    }
}
 
module.exports = hostsproxy;