luminati-proxy-stripped
Version:
A configurable local proxy for luminati.io
81 lines (74 loc) • 2.01 kB
JavaScript
// LICENSE_CODE ZON ISC
; /*jslint node:true, esnext:true*/
const _ = require('lodash');
const string = require('../util/string.js');
const diacritic = require('diacritic');
const qw = string.qw;
const boolean_part = {
raw: true,
direct: true,
};
const abbr = {
cu: 'customer',
z: 'zone',
k: 'key',
d: 'direct',
s: 'session',
to: 'timeout',
dbg: 'debug',
cy: 'country',
st: 'state',
ct: 'city',
};
const fields = qw`customer zone country state city session asn dns
cid ip raw direct debug mobile vip carrier timeout`;
// XXX krzysztof: copied from /protocol. try to import and reuse
const escape_geo = val=>
diacritic.clean(val).toLowerCase().replace(/[^a-z0-9]/g, '');
// XXX colin/ovidiu: remove this function and us from util if possible
let short = {};
for (let key in abbr)
short[abbr[key]] = key;
const parse = header=>{
if (!header)
return;
let m = header.match(/^Basic (.*)/);
if (!m)
return;
header = new Buffer(m[1], 'base64').toString('ascii');
let cred = header.split(':');
let auth = {};
let parts = cred[0].split('-');
while (parts.length)
{
let key = parts.shift();
if (key=='lum')
continue;
if (abbr[key])
key = abbr[key];
auth[key] = boolean_part[key] || parts.shift();
}
auth.password = cred[1];
return auth;
};
const calc = (opt, make_short)=>{
const parts = ['lum'];
for (let key in _.pick(opt, fields))
{
let val = opt[key], bool = boolean_part[key];
if (!val || val=='*')
continue;
if (key=='city')
val = escape_geo(val);
if (make_short && short[key])
key = short[key];
parts.push(key);
if (!bool)
{
parts.push((''+val).toLowerCase()
.replace(/ /g, make_short ? '' : '_'));
}
}
return parts.join('-');
};
module.exports = {parse, calc, fields};