Skip to content

Updated HTTPParser as original was too old and did not work #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"bugs": {
"url": "https://github.com/sjitech/proxy-login-automator/issues"
},
"homepage": "https://github.com/sjitech/proxy-login-automator#readme"
"homepage": "https://github.com/sjitech/proxy-login-automator#readme",
"dependencies": {
"http-parser-js": "^0.5.3"
}
}
28 changes: 20 additions & 8 deletions proxy-login-automator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#!/usr/bin/env node
'use strict';
var net = require('net'), tls = require('tls');
var HTTPParser = process.binding('http_parser').HTTPParser;
var HTTPParser = require('http-parser-js').HTTPParser;
var http = require('http'), https = require('https');
var url = require('url');

const kOnHeaders = HTTPParser.kOnHeaders | 0;
const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
const kOnBody = HTTPParser.kOnBody | 0;
const kOnMessageComplete = HTTPParser.kOnMessageComplete | 0;
const kOnExecute = HTTPParser.kOnExecute | 0;

function main() {
//convert `-key value` to cfg[key]=value
var cfg = process.argv.slice(2/*skip ["node", "xxx.js"]*/).reduce(function (cfg, arg, i, argv) {
Expand Down Expand Up @@ -36,7 +42,7 @@ function main() {
return console.error('when use as a PAC server, the local_host parameter must be a definite address');
}
console.log('Using parameters: ' + JSON.stringify(cfg, null, ' '));
cfg.buf_proxy_basic_auth = new Buffer('Proxy-Authorization: Basic ' + new Buffer(cfg.usr + ':' + cfg.pwd).toString('base64'));
cfg.buf_proxy_basic_auth = Buffer.from('Proxy-Authorization: Basic ' + Buffer.from(cfg.usr + ':' + cfg.pwd).toString('base64'));

if (cfg.as_pac_server) {
createPacServer(cfg.local_host, cfg.local_port, cfg.remote_host, cfg.remote_port, cfg.buf_proxy_basic_auth, cfg.is_remote_https, cfg.ignore_https_cert, cfg.are_remotes_in_pac_https);
Expand All @@ -45,9 +51,9 @@ function main() {
}
}

var CR = 0xd, LF = 0xa, BUF_CR = new Buffer([0xd]), BUF_CR_LF_CR_LF = new Buffer([0xd, 0xa, 0xd, 0xa]),
BUF_LF_LF = new Buffer([0xa, 0xa]), BUF_PROXY_CONNECTION_CLOSE = new Buffer('Proxy-Connection: close');
var STATE_NONE = 0, STATE_FOUND_LF = 1, STATE_FOUND_LF_CR = 2;
var CR = 0xd, LF = 0xa, BUF_CR = Buffer.from([0xd]), BUF_CR_LF_CR_LF = Buffer.from([0xd, 0xa, 0xd, 0xa]),
BUF_LF_LF = Buffer.from([0xa, 0xa]), BUF_PROXY_CONNECTION_CLOSE = Buffer.from('Proxy-Connection: close');
var STATE_NONE = 0, STATE_FOUND_LF = 1, STATE_FOUND_LF_CR = 2, STATE_CONNECTED = 3;

function createPortForwarder(local_host, local_port, remote_host, remote_port, buf_proxy_basic_auth, is_remote_https, ignore_https_cert) {
net.createServer({allowHalfOpen: true}, function (socket) {
Expand Down Expand Up @@ -117,10 +123,16 @@ function createPortForwarder(local_host, local_port, remote_host, remote_port, b

//find second CR LF or LF
if (buf[i] === LF) {
parser.__is_headers_complete = false;
parser.execute(buf.slice(unsavedStart, i + 1));
var headersComplete = false;
parser[kOnHeadersComplete] =
function(versionMajor, versionMinor, headers, method,
url, statusCode, statusMessage, upgrade, shouldKeepAlive) {
headersComplete = true;
//console.log('kOnHeadersComplete');
}

if (parser.__is_headers_complete) {
parser.execute(buf.slice(unsavedStart, i + 1));
if (headersComplete) {
buf_ary.push(buf.slice(unsavedStart, buf[i - 1] === CR ? i - 1 : i));
//console.log('insert auth header');
buf_ary.push(buf_proxy_basic_auth);
Expand Down