@@ -43,7 +43,7 @@ function update_list(msg, file, mapf, collectorf) {
43
43
sys . log ( msg ) ;
44
44
fs . readFile ( file , function ( err , data ) {
45
45
collectorf ( data . toString ( ) . split ( "\n" )
46
- . filter ( function ( rx ) { return rx . length } )
46
+ . filter ( function ( rx ) { return rx . length ; } )
47
47
. map ( mapf ) ) ;
48
48
} ) ;
49
49
}
@@ -74,17 +74,17 @@ function update_blacklist() {
74
74
update_list (
75
75
"Updating host black list." ,
76
76
config . black_list ,
77
- function ( rx ) { return RegExp ( rx ) } ,
78
- function ( list ) { blacklist = list }
77
+ function ( rx ) { return RegExp ( rx ) ; } ,
78
+ function ( list ) { blacklist = list ; }
79
79
) ;
80
80
}
81
81
82
82
function update_iplist ( ) {
83
83
update_list (
84
84
"Updating allowed ip list." ,
85
85
config . allow_ip_list ,
86
- function ( ip ) { return ip } ,
87
- function ( list ) { iplist = list }
86
+ function ( ip ) { return ip ; } ,
87
+ function ( list ) { iplist = list ; }
88
88
) ;
89
89
}
90
90
@@ -98,11 +98,38 @@ function host_allowed(host) {
98
98
return ! blacklist . some ( function ( host_ ) { return host_ . test ( host ) ; } ) ;
99
99
}
100
100
101
+ //header decoding
102
+ function authenticate ( request ) {
103
+ token = {
104
+ "login" :"anonymous" ,
105
+ "pass" :""
106
+ } ;
107
+ if ( request . headers . authorization && request . headers . authorization . search ( 'Basic ' ) === 0 ) {
108
+ // fetch login and password
109
+ basic = ( new Buffer ( request . headers . authorization . split ( ' ' ) [ 1 ] , 'base64' ) . toString ( ) ) ;
110
+ sys . log ( "Authentication token received: " + basic ) ;
111
+ basic = basic . split ( ':' ) ;
112
+ token . login = basic [ 0 ] ;
113
+ token . pass = basic [ 1 ] ; //fixme: potential trouble if there is a ":" in the pass
114
+ }
115
+ return token ;
116
+ }
117
+
101
118
//proxying
102
119
//handle 2 rules:
103
120
// * redirect (301)
104
121
// * proxyto
105
- function handle_proxy_rule ( rule , target ) {
122
+ function handle_proxy_rule ( rule , target , token ) {
123
+ //handle authorization
124
+ if ( "validuser" in rule ) {
125
+ if ( ! ( token . login in rule . validuser ) || ( rule . validuser [ token . login ] != token . pass ) ) {
126
+ target . action = "authenticate" ;
127
+ target . msg = rule . description || "" ;
128
+ return target ;
129
+ }
130
+ }
131
+
132
+ //handle real acions
106
133
if ( "redirect" in rule ) {
107
134
target = hosthelper ( rule . redirect ) ;
108
135
target . action = "redirect" ;
@@ -113,24 +140,24 @@ function handle_proxy_rule(rule, target){
113
140
return target ;
114
141
}
115
142
116
- function host_filter ( host ) {
143
+ function host_filter ( host , token ) {
117
144
//extract target host and port
118
145
action = hosthelper ( host ) ;
119
146
action . action = "proxyto" ;
120
147
121
148
//try to find a matching rule
122
149
if ( action . host + ':' + action . port in hostfilters ) {
123
150
rule = hostfilters [ action . host + ':' + action . port ] ;
124
- action = handle_proxy_rule ( rule , action ) ;
151
+ action = handle_proxy_rule ( rule , action , token ) ;
125
152
} else if ( action . host in hostfilters ) {
126
153
rule = hostfilters [ action . host ] ;
127
- action = handle_proxy_rule ( rule , action ) ;
154
+ action = handle_proxy_rule ( rule , action , token ) ;
128
155
} else if ( "*:" + action . port in hostfilters ) {
129
156
rule = hostfilters [ '*:' + action . port ] ;
130
- action = handle_proxy_rule ( rule , action ) ;
157
+ action = handle_proxy_rule ( rule , action , token ) ;
131
158
} else if ( "*" in hostfilters ) {
132
159
rule = hostfilters [ '*' ] ;
133
- action = handle_proxy_rule ( rule , action ) ;
160
+ action = handle_proxy_rule ( rule , action , token ) ;
134
161
}
135
162
return action ;
136
163
}
@@ -148,6 +175,13 @@ function prevent_loop(request, response){
148
175
}
149
176
}
150
177
178
+ function action_authenticate ( response , msg ) {
179
+ response . writeHead ( 401 , {
180
+ 'WWW-Authenticate' : "Basic realm=\"" + msg + "\""
181
+ } ) ;
182
+ response . end ( ) ;
183
+ }
184
+
151
185
function action_deny ( response , msg ) {
152
186
response . writeHead ( 403 ) ;
153
187
response . write ( msg ) ;
@@ -225,19 +259,25 @@ function server_cb(request, response) {
225
259
226
260
sys . log ( ip + ": " + request . method + " " + request . url ) ;
227
261
262
+ //get authorization token
263
+ authorization = authenticate ( request ) ;
264
+
228
265
//calc new host info
229
- var action = host_filter ( request . headers . host ) ;
266
+ var action = host_filter ( request . headers . host , authorization ) ;
230
267
host = hostporthelper ( action ) ;
231
268
232
269
//handle action
233
270
if ( action . action == "redirect" ) {
234
271
action_redirect ( response , host ) ;
235
272
} else if ( action . action == "proxyto" ) {
236
273
action_proxy ( response , request , host ) ;
274
+ } else if ( action . action == "authenticate" ) {
275
+ action_authenticate ( response , action . msg ) ;
237
276
}
238
277
}
239
278
240
279
//last chance error handler
280
+ //de-comment it in a production env for more stability :)
241
281
/*process.on('uncaughtException', function (err) {
242
282
console.log('LAST ERROR: Caught exception: ' + err);
243
283
});*/
0 commit comments