Programming/node.js2019. 3. 27. 17:38

stack의 것은.. 되긴 하는데 단순하게(?) https의 경우 소켓을 통해 바로 연결되는 구조로 되어있다.

http나 https 확실히 무언가 한번 걸린다는 느낌이 드는 딜레이는 어쩔수 없을려나?

 

아무튼 내가 원한건.. 얘가 아니라 직접 URL을 잡아낼수 있어야 하는 녀석... ㅠㅠ

 

// Install npm dependencies first
// npm init
// npm install --save url@0.10.3
// npm install --save http-proxy@1.11.1

var httpProxy = require("http-proxy");
var http = require("http");
var url = require("url");
var net = require('net');

var server = http.createServer(function (req, res) {
var urlObj = url.parse(req.url);
var target = urlObj.protocol + "//" + urlObj.host;

console.log("Proxy HTTP request for:", target);

var proxy = httpProxy.createProxyServer({});
proxy.on("error", function (err, req, res) {
console.log("proxy error", err);
res.end();
});

proxy.web(req, res, {target: target});
}).listen(8080); //this is the port your clients will connect to

var regex_hostport = /^([^:]+)(:([0-9]+))?$/;

var getHostPortFromString = function (hostString, defaultPort) {
var host = hostString;
var port = defaultPort;

var result = regex_hostport.exec(hostString);
if (result != null) {
host = result[1];
if (result[2] != null) {
port = result[3];
}
}

return ( [host, port] );
};

server.addListener('connect', function (req, socket, bodyhead) {
var hostPort = getHostPortFromString(req.url, 443);
var hostDomain = hostPort[0];
var port = parseInt(hostPort[1]);
console.log("Proxying HTTPS request for:", hostDomain, port);

var proxySocket = new net.Socket();
proxySocket.connect(port, hostDomain, function () {
proxySocket.write(bodyhead);
socket.write("HTTP/" + req.httpVersion + " 200 Connection established\r\n\r\n");
}
);

proxySocket.on('data', function (chunk) {
socket.write(chunk);
});

proxySocket.on('end', function () {
socket.end();
});

proxySocket.on('error', function () {
socket.write("HTTP/" + req.httpVersion + " 500 Connection error\r\n\r\n");
socket.end();
});

socket.on('data', function (chunk) {
proxySocket.write(chunk);
});

socket.on('end', function () {
proxySocket.end();
});

socket.on('error', function () {
proxySocket.end();
});

});


[링크 : 
https://stackoverflow.com/questions/8165570/https-proxy-server-in-node-js/32104777]

[링크 : https://www.npmjs.com/package/httpp-proxy]

 

이거랑 위에꺼랑 어떻게 믹스하지? ㅠㅠ

[링크 : http://blog.saltfactory.net/implements-nodejs-based-https-server/]

 

+

2019.04.02

취향대로 좀 정리한 버전

var http = require('http');
var httpProxy = require('http-proxy');
var url = require('url');
var net = require('net');

var server = http.createServer(onReq);
server.addListener('connect', onConn);
server.listen(8080);

function onReq(req, res)
{
        var urlObj = url.parse(req.url);
        var target = urlObj.protocol + "//" + urlObj.host;
        console.log(urlObj.href);

        var proxy = httpProxy.createProxyServer({});
        proxy.on("error", function (err, req, res) {
                console.log("proxy error", err);
                res.end();
        });

        proxy.web(req, res, {target: target});
}

function onConn(req, socket, bodyhead)
{
        var hostPort = getHostPortFromString(req.url, 443);
        var hostDomain = hostPort[0];
        var port = parseInt(hostPort[1]);
        console.log("Proxying HTTPS request for:", hostDomain, port);

        var proxySocket = new net.Socket();
        proxySocket.connect(port, hostDomain, function () {
                proxySocket.write(bodyhead);
                socket.write("HTTP/" + req.httpVersion + " 200 Connection established\r\n\r\n");
        });

        proxySocket.on('data', function (chunk) {
                //console.log(chunk.toString());
                socket.write(chunk);
        });

        proxySocket.on('end', function () {
                socket.end();
        });

        proxySocket.on('error', function () {
                socket.write("HTTP/" + req.httpVersion + " 500 Connection error\r\n\r\n");
                socket.end();
        });

        socket.on('data', function (chunk) {
                //console.log(chunk.toString());
                proxySocket.write(chunk);
        });

        socket.on('end', function () {
                proxySocket.end();
        });

        socket.on('error', function () {
                proxySocket.end();
        });
}

{
}

var regex_hostport = /^([^:]+)(:([0-9]+))?$/;

var getHostPortFromString = function (hostString, defaultPort)
{
        var host = hostString;
        var port = defaultPort;
        var result = regex_hostport.exec(hostString);
        if (result != null)
        {
                host = result[1];
                 if (result[2] != null) { port = result[3]; }
        }
        return ( [host, port] );
};

'Programming > node.js' 카테고리의 다른 글

proxy http node.js  (0) 2019.04.01
TypeError: Converting circular structure to JSON  (0) 2019.03.27
node.js unit test  (0) 2019.03.25
node.js http cache proxy  (0) 2019.03.20
node.js 사용자 모듈 만들기  (0) 2019.03.13
Posted by 구차니