UNPKG

@luminati-io/luminati-proxy

Version:

A configurable local proxy for luminati.io

94 lines (86 loc) 2.36 kB
// LICENSE_CODE ZON ISC 'use strict'; /*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, unblocker: true, }; const abbr = { cu: 'customer', z: 'zone', k: 'key', d: 'direct', s: 'session', cy: 'country', st: 'state', ct: 'city', ub: 'unblocker', unblock: 'unblocker', }; const fields = qw`customer zone country state city session asn dns cid ip raw direct mobile vip carrier route_err unblocker debug os`; // XXX krzysztof: copied from /protocol. try to import and reuse const escape_geo = val=> diacritic.clean(val).toLowerCase().replace(/[^a-z0-9]/g, ''); const parse = header=>{ if (!header) return; let m = header.match(/^Basic (.*)/); if (!m) return; header = Buffer.from(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=>{ const parts = ['lum']; for (let key in _.pick(opt, fields)) { let val = opt[key]; if (val==='' || val=='*' || val==undefined) continue; if (key=='city') val = escape_geo(val); parts.push(key); if (!boolean_part[key]) parts.push((''+val).toLowerCase().replace(/ /g, '_')); } return parts.join('-'); }; const calculate_username = function(opt){ if (opt.ext_proxy) { return Object.assign({password: opt.password}, _.pick(opt.ext_proxy, 'username', 'password')); } const opt_usr = Object.assign({}, opt); if (opt_usr.ip) delete opt_usr.session; if (!opt_usr.mobile) delete opt_usr.mobile; if (opt_usr.mobile && opt_usr.os) opt_usr.mobile = false; else if (opt_usr.os) delete opt_usr.mobile; return { username: _calc(opt_usr), password: opt.password, }; }; module.exports = {parse, calculate_username};