@luminati-io/luminati-proxy
Version:
A configurable local proxy for luminati.io
76 lines (73 loc) • 2.46 kB
JavaScript
// LICENSE_CODE ZON ISC
; /*jslint node:true, esnext:true*/
const path = require('path');
const fs = require('fs');
const tls = require('tls');
const forge = require('node-forge');
const pki = forge.pki;
const lpm_file = require('../util/lpm_file.js');
const logger = require('./logger.js');
const {spawn} = require('child_process');
const E = module.exports = ssl;
let keys;
let key_path;
let crt_path;
const cust_key_path = lpm_file.get_file_path('lpm.key');
const cust_crt_path = lpm_file.get_file_path('lpm.crt');
if (fs.existsSync(cust_key_path) && fs.existsSync(cust_crt_path))
{
key_path = cust_key_path;
crt_path = cust_crt_path;
}
else
{
key_path = path.join(__dirname, '../bin/ca.key');
crt_path = path.join(__dirname, '../bin/ca.crt');
}
E.ca = {cert: fs.readFileSync(crt_path), key: fs.readFileSync(key_path)};
function ssl(){
if (!keys)
{
keys = pki.rsa.generateKeyPair(2048);
keys.privateKeyPem = pki.privateKeyToPem(keys.privateKey);
keys.publicKeyPem = pki.publicKeyToPem(keys.publicKey);
}
const hosts = {};
return {SNICallback: (name, cb)=>{
if (hosts[name])
return cb(null, hosts[name]);
const cert = pki.createCertificate();
cert.publicKey = pki.publicKeyFromPem(keys.publicKeyPem);
cert.serialNumber = ''+Date.now();
cert.validity.notBefore = new Date();
cert.validity.notAfter = new Date(Date.now()+1*365*86400000);
cert.setSubject([{name: 'commonName', value: name}]);
cert.setIssuer(pki.certificateFromPem(E.ca.cert).issuer.attributes);
cert.setExtensions([{
name: 'subjectAltName',
altNames: [{
type: 2,
value: name,
}],
}]);
cert.sign(pki.privateKeyFromPem(E.ca.key), forge.md.sha256.create());
hosts[name] = tls.createSecureContext({
key: keys.privateKeyPem,
cert: pki.certificateToPem(cert),
ca: E.ca.cert,
});
cb(null, hosts[name]);
}};
}
E.gen_cert = function(){
const child = spawn('bash', [path.join(__dirname, '../bin/cert_gen.sh'),
cust_key_path, cust_crt_path], {
stdio: ['inherit', 'inherit', 'inherit'],
});
child.on('exit', (code, signal)=>{
if (code==0)
logger.notice('CA generated successfully');
else
logger.warn('Could not generate CA');
});
};