update to 4.0

This commit is contained in:
Otto Mao
2017-12-01 21:30:49 +08:00
parent e392fefc64
commit 4be5aa8954
267 changed files with 27008 additions and 84482 deletions

114
bin/anyproxy Executable file
View File

@@ -0,0 +1,114 @@
#!/usr/bin/env node
'use strict'
const program = require('commander'),
color = require('colorful'),
packageInfo = require('../package.json'),
ruleLoader = require('../lib/ruleLoader'),
util = require('../lib/util'),
logUtil = require('../lib/log');
program
.version(packageInfo.version)
.option('-p, --port [value]', 'proxy port, 8001 for default')
.option('-w, --web [value]', 'web GUI port, 8002 for default')
.option('-r, --rule [value]', 'path for rule file,')
.option('-l, --throttle [value]', 'throttle speed in kb/s (kbyte / sec)')
.option('-i, --intercept', 'intercept(decrypt) https requests when root CA exists')
.option('-s, --silent', 'do not print anything into terminal')
.option('-c, --clear', 'clear all the certificates and temp files')
.option('--ignore-unauthorized-ssl', 'ignore all ssl error')
.parse(process.argv);
if (program.clear) {
require('../lib/certMgr').clearCerts(() => {
util.deleteFolderContentsRecursive(util.getAnyProxyPath('cache'));
console.log(color.green('done !'));
process.exit(0);
});
} else if (program.root) {
require('../lib/certMgr').generateRootCA(() => {
process.exit(0);
});
} else {
const AnyProxy = require('../proxy.js');
let proxyServer;
if (program.silent) {
logUtil.setPrintStatus(false);
}
// load rule module
new Promise((resolve, reject) => {
if (program.rule) {
resolve(ruleLoader.requireModule(program.rule));
} else {
resolve(null);
}
})
.catch(e => {
logUtil.printLog('Failed to load rule file', logUtil.T_ERR);
logUtil.printLog(e, logUtil.T_ERR);
process.exit();
})
//start proxy
.then(ruleModule => {
proxyServer = new AnyProxy.ProxyServer({
type: 'http',
port: program.port || 8001,
throttle: program.throttle,
rule: ruleModule,
webInterface: {
enable: true,
webPort: program.web,
},
forceProxyHttps: program.intercept,
dangerouslyIgnoreUnauthorized: !!program.ignoreUnauthorizedSsl,
silent: program.silent
});
// proxyServer.on('ready', () => {});
proxyServer.start();
})
.catch(e => {
logUtil.printLog(e, logUtil.T_ERR);
if (e && e.code) {
logUtil.printLog('code ' + e.code, logUtil.T_ERR);
}
logUtil.printLog(e.stack, logUtil.T_ERR);
});
process.on('exit', (code) => {
if (code > 0) {
logUtil.printLog('AnyProxy is about to exit with code: ' + code, logUtil.T_ERR);
}
process.exit();
});
//exit cause ctrl+c
process.on('SIGINT', () => {
try {
proxyServer && proxyServer.close();
} catch (e) {
console.error(e);
}
process.exit();
});
process.on('uncaughtException', (err) => {
let errorTipText = 'got an uncaught exception, is there anything goes wrong in your rule file ?\n';
try {
if (err && err.stack) {
errorTipText += err.stack;
} else {
errorTipText += err;
}
} catch (e) {}
logUtil.printLog(errorTipText, logUtil.T_ERR);
try {
proxyServer && proxyServer.close();
} catch (e) {}
process.exit();
});
}

102
bin/anyproxy-ca Executable file
View File

@@ -0,0 +1,102 @@
#!/usr/bin/env node
'use strict'
// exist-false, trusted-false : create CA
// exist-true, trusted-false : trust CA
// exist-true, trusted-true : all things done
const program = require('commander');
const color = require('colorful');
const certMgr = require('../lib/certMgr');
const AnyProxy = require('../proxy');
const exec = require('child_process').exec;
const co = require('co');
const path = require('path');
const inquirer = require('inquirer');
program
.option('-c, --clear', 'clear all the tmp certificates and root CA')
.option('-g, --generate', 'generate a new rootCA')
.parse(process.argv);
function openFolderOfFile(filePath) {
const isWin = /^win/.test(process.platform);
if (isWin) {
exec('start .', { cwd: path.dirname(filePath) });
} else {
exec(`open -R ${filePath}`);
}
}
function guideToGenrateCA() {
AnyProxy.utils.certMgr.generateRootCA((error, keyPath, crtPath) => {
if (!error) {
const certDir = path.dirname(keyPath);
console.log(`The cert is generated at ${certDir}. Please trust the ${color.bold('rootCA.crt')}.`);
// TODO: console.log('guide to install');
openFolderOfFile(crtPath);
} else {
console.error('failed to generate rootCA', error);
}
});
}
function guideToTrustCA() {
const certPath = AnyProxy.utils.certMgr.getRootCAFilePath();
if (certPath) {
// TODO: console.log('guide to install');
openFolderOfFile(certPath);
} else {
console.error('failed to get cert path');
}
}
if (program.clear) {
AnyProxy.utils.certMgr.clearCerts(() => {
console.log(color.green('done !'));
});
} else if (program.generate) {
guideToGenrateCA();
} else {
console.log('detecting CA status...');
co(certMgr.getCAStatus)
.then(status => {
if (!status.exist) {
console.log('AnyProxy CA does not exist.');
const questions = [{
type: 'confirm',
name: 'ifCreate',
message: 'Would you like to generate one ?',
default: true
}];
inquirer.prompt(questions).then(answers => {
if (answers.ifCreate) {
guideToGenrateCA();
}
});
} else if (!status.trusted) {
if (/^win/.test(process.platform)) {
console.log('AnyProxy CA exists, make sure it has been trusted');
} else {
console.log('AnyProxy CA exists, but not be trusted');
const questions = [{
type: 'confirm',
name: 'ifGotoTrust',
message: 'Would you like to open the folder and trust it ?',
default: true
}];
inquirer.prompt(questions).then(answers => {
if (answers.ifGotoTrust) {
guideToTrustCA();
}
});
}
// AnyProxy.utils.certMgr.clearCerts()
} else {
console.log(color.green('AnyProxy CA has already been trusted'));
}
})
.catch(e => {
console.log(e);
})
}