mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-08-04 21:39:04 +00:00
add ca helper when run anyproxy -i
check and help to generate the root CA when run in https interception mode, and install the CA after user's confirmation (Mac only for now)
This commit is contained in:
95
bin/anyproxy
95
bin/anyproxy
@@ -4,9 +4,11 @@
|
||||
|
||||
const program = require('commander'),
|
||||
color = require('colorful'),
|
||||
co = require('co'),
|
||||
packageInfo = require('../package.json'),
|
||||
ruleLoader = require('../lib/ruleLoader'),
|
||||
util = require('../lib/util'),
|
||||
rootCACheck = require('./rootCaCheck'),
|
||||
startServer = require('./startServer'),
|
||||
logUtil = require('../lib/log');
|
||||
|
||||
program
|
||||
@@ -33,85 +35,20 @@ if (program.clear) {
|
||||
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,
|
||||
},
|
||||
wsIntercept: program.wsIntercept,
|
||||
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);
|
||||
co(function *() {
|
||||
if (program.silent) {
|
||||
logUtil.setPrintStatus(false);
|
||||
}
|
||||
|
||||
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;
|
||||
if (program.intercept) {
|
||||
try {
|
||||
yield rootCACheck();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
} catch (e) {}
|
||||
logUtil.printLog(errorTipText, logUtil.T_ERR);
|
||||
try {
|
||||
proxyServer && proxyServer.close();
|
||||
} catch (e) {}
|
||||
process.exit();
|
||||
});
|
||||
}
|
||||
|
||||
return startServer(program);
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
33
bin/rootCACheck.js
Normal file
33
bin/rootCACheck.js
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* check if root CA exists and installed
|
||||
* will prompt to generate when needed
|
||||
*/
|
||||
|
||||
const thunkify = require('thunkify');
|
||||
const AnyProxy = require('../proxy');
|
||||
const logUtil = require('../lib/log');
|
||||
|
||||
const certMgr = AnyProxy.utils.certMgr;
|
||||
|
||||
function checkRootCAExists() {
|
||||
return certMgr.isRootCAFileExists();
|
||||
}
|
||||
|
||||
module.exports = function *() {
|
||||
try {
|
||||
if (!checkRootCAExists()) {
|
||||
logUtil.warn('Missing root CA, generating now');
|
||||
yield thunkify(certMgr.generateRootCA)();
|
||||
yield certMgr.trustRootCA();
|
||||
} else {
|
||||
const isCATrusted = yield thunkify(certMgr.ifRootCATrusted)();
|
||||
if (!isCATrusted) {
|
||||
logUtil.warn('ROOT CA NOT INSTALLED YET');
|
||||
yield certMgr.trustRootCA();
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
};
|
||||
|
||||
86
bin/startServer.js
Normal file
86
bin/startServer.js
Normal file
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* start the AnyProxy server
|
||||
*/
|
||||
|
||||
const ruleLoader = require('../lib/ruleLoader');
|
||||
const logUtil = require('../lib/log');
|
||||
const AnyProxy = require('../proxy');
|
||||
|
||||
module.exports = function startServer(program) {
|
||||
let proxyServer;
|
||||
// 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,
|
||||
},
|
||||
wsIntercept: program.wsIntercept,
|
||||
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();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user