Skip to content

Commit 11998ec

Browse files
author
tom zhou
committed
added http basic-auth
1 parent 6d72ef1 commit 11998ec

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

bin/peer-proxy

+22-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var help = [
1919
" -a --authmode srv or both SSL auth mode: srv or both",
2020
" -k, --key User key Key for the registered user",
2121
" -l, --level srv or both proxy level: srv or both server and web site",
22+
" -b --basicauth user:pass Http basic-auth as username:password",
2223
" -u, --user USER User to drop privileges once server socket is bound",
2324
" -h, --help You're staring at it"
2425
].join('\n');
@@ -27,12 +28,13 @@ if (argv.h || argv.help || Object.keys(argv).length === 2) {
2728
return util.puts(help);
2829
}
2930

30-
var target = argv.t || argv.target,
31-
secmode = argv.s || argv.secmode,
32-
authmode = argv.a || argv.authmode,
33-
user = argv.u || argv.user,
34-
usrkey = argv.k || argv.key,
35-
level = argv.l || argv.level;
31+
var target = argv.t || argv.target,
32+
secmode = argv.s || argv.secmode,
33+
authmode = argv.a || argv.authmode,
34+
user = argv.u || argv.user,
35+
basicauth = argv.b || argv.basicauth,
36+
level = argv.l || argv.level,
37+
usrkey = argv.k || argv.key;
3638

3739

3840
//
@@ -58,16 +60,25 @@ if (target) {
5860
return util.puts(help);
5961
}
6062

63+
if (!basicauth) {
64+
console.log('Warning: Please set http auth with -b username:password');
65+
}
66+
6167
//
6268
// Create the server with the specified options
6369
//
6470

6571
var server = new Proxy(websites, function(err, proxyURL){
66-
console.log('Website Proxy URL(please open it on browser)');
67-
for (var k in proxyURL) {
68-
console.log(k+' '+proxyURL[k]);
69-
}
70-
}, {secmode: secmode, usrkey: usrkey, sslmode: authmode, level: level});
72+
console.log('Website Proxy URL(please open it on browser)');
73+
for (var k in proxyURL) {
74+
console.log(k+' '+proxyURL[k]);
75+
}
76+
},
77+
{
78+
secmode: secmode, usrkey: usrkey,
79+
sslmode: authmode, level: level,
80+
auth: basicauth
81+
});
7182

7283
//
7384
// Drop privileges if requested

lib/proxy.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ var WEBPP = require('iwebpp.io'),
1313
trumpet = require('trumpet'),
1414
NET = require('net'),
1515
UDT = require('udt');
16-
16+
17+
// authentication module
18+
var httpauth = require('http-auth');
19+
1720
// security hash
1821
// sipkey can be any user defined 4 integers
1922
var siphash = require('siphash'),
@@ -31,7 +34,9 @@ var Debug = 0;
3134
// - options: user custom parameters, like {secmode: ..., usrkey: ..., mode: ..., domain: ..., endpoints: ..., turn: ...}
3235
// - options.secmode: ssl, enable ssl/https; acl, enable ssl/https,host-based ACL
3336
// - options.sslmode: srv, only verify server side cert; both, verify both server and client side cert
37+
///
3438
// - options.level: srv, only proxy url/link on server side; both, proxy url/link on both server and web site
39+
// - options.auth: http basic-auth as username:password
3540
var Proxy = module.exports = function(websites, fn, options){
3641
var self = this;
3742

@@ -43,6 +48,16 @@ var Proxy = module.exports = function(websites, fn, options){
4348
options = options || {};
4449
options.level = options.level || 'srv';
4550

51+
// check basic auth
52+
var basicauth = false;
53+
if (options && options.auth) {
54+
var astr = options.auth.split(':');
55+
basicauth = {username: astr && astr[0], password: astr && astr[1]};
56+
}
57+
58+
// check upload
59+
var fileupload = false;
60+
4661
// 0.
4762
// proxy cache and URLs
4863
self.proxyCache = {}; // http proxy for website
@@ -147,6 +162,19 @@ var Proxy = module.exports = function(websites, fn, options){
147162
// 4.1
148163
// add third-party connect middle-ware
149164

165+
// 4.1.1
166+
// add http basic-auth module
167+
if (basicauth) {
168+
var basic = httpauth.basic({
169+
realm: "iwebpp.com"
170+
}, function (username, password, callback) {
171+
callback(username === basicauth.username &&
172+
password === basicauth.password);
173+
});
174+
175+
appHttp.use(httpauth.connect(basic));
176+
}
177+
150178
// 4.1.2
151179
// compress
152180
// TBD...

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Tom Zhou <[email protected]>",
33
"name": "peer-proxy",
44
"description": "Proxy web service from peer",
5-
"version": "1.0.3",
5+
"version": "1.1.0",
66
"repository": {
77
"type": "git",
88
"url": "git://github.com/InstantWebP2P/peer-proxy.git"
@@ -31,6 +31,7 @@
3131
"connect": "2.8.x",
3232
"jschardet": "1.0.x",
3333
"iconv-lite": "0.2.x",
34-
"trumpet": "0.3.x"
34+
"trumpet": "0.3.x",
35+
"http-auth": "2.2.5"
3536
}
3637
}

0 commit comments

Comments
 (0)