mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-08-04 21:39:04 +00:00
update to 4.0
This commit is contained in:
47
module_sample/core_reload.js
Normal file
47
module_sample/core_reload.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const AnyProxy = require('../proxy');
|
||||
const exec = require('child_process').exec;
|
||||
|
||||
const AnyProxyRecorder = require('../lib/recorder');
|
||||
const WebInterfaceLite = require('../lib/webInterface');
|
||||
|
||||
/*
|
||||
-------------------------------
|
||||
| ProxyServerA | ProxyServerB |
|
||||
------------------------------- ----------------------------
|
||||
| Common Recorder | -------(by events)------| WebInterfaceLite |
|
||||
------------------------------- ----------------------------
|
||||
*/
|
||||
|
||||
|
||||
const commonRecorder = new AnyProxyRecorder();
|
||||
|
||||
// web interface依赖recorder
|
||||
new WebInterfaceLite({ // common web interface
|
||||
webPort: 8002
|
||||
}, commonRecorder);
|
||||
|
||||
// proxy core只依赖recorder,与webServer无关
|
||||
const optionsA = {
|
||||
port: 8001,
|
||||
recorder: commonRecorder, // use common recorder
|
||||
};
|
||||
|
||||
const optionsB = {
|
||||
port: 8005,
|
||||
recorder: commonRecorder, // use common recorder
|
||||
};
|
||||
|
||||
const proxyServerA = new AnyProxy.ProxyCore(optionsA);
|
||||
const proxyServerB = new AnyProxy.ProxyCore(optionsB);
|
||||
|
||||
proxyServerA.start();
|
||||
proxyServerB.start();
|
||||
|
||||
// after both ready
|
||||
setTimeout(() => {
|
||||
exec('curl http://www.qq.com --proxy http://127.0.0.1:8001');
|
||||
exec('curl http://www.sina.com.cn --proxy http://127.0.0.1:8005');
|
||||
}, 1000);
|
||||
|
||||
// visit http://127.0.0.1 , there should be two records
|
||||
|
||||
23
module_sample/https_config.js
Normal file
23
module_sample/https_config.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const AnyProxy = require('../proxy');
|
||||
const exec = require('child_process').exec;
|
||||
|
||||
if (!AnyProxy.utils.certMgr.ifRootCAFileExists()) {
|
||||
AnyProxy.utils.certMgr.generateRootCA((error, keyPath) => {
|
||||
// let users to trust this CA before using proxy
|
||||
if (!error) {
|
||||
const certDir = require('path').dirname(keyPath);
|
||||
console.log('The cert is generated at', certDir);
|
||||
const isWin = /^win/.test(process.platform);
|
||||
if (isWin) {
|
||||
exec('start .', { cwd: certDir });
|
||||
} else {
|
||||
exec('open .', { cwd: certDir });
|
||||
}
|
||||
} else {
|
||||
console.error('error when generating rootCA', error);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// clear all the certificates
|
||||
// AnyProxy.utils.certMgr.clearCerts()
|
||||
}
|
||||
70
module_sample/normal_use.js
Normal file
70
module_sample/normal_use.js
Normal file
@@ -0,0 +1,70 @@
|
||||
const AnyProxy = require('../proxy');
|
||||
|
||||
const options = {
|
||||
type: 'http',
|
||||
port: 8001,
|
||||
rule: null,
|
||||
webInterface: {
|
||||
enable: true,
|
||||
webPort: 8002,
|
||||
wsPort: 8003,
|
||||
},
|
||||
throttle: 10000,
|
||||
forceProxyHttps: true,
|
||||
silent: false
|
||||
};
|
||||
const proxyServer = new AnyProxy.ProxyServer(options);
|
||||
|
||||
proxyServer.on('ready', () => {
|
||||
console.log('ready');
|
||||
// set as system proxy
|
||||
proxyServer.close().then(() => {
|
||||
const proxyServerB = new AnyProxy.ProxyServer(options);
|
||||
proxyServerB.start();
|
||||
});
|
||||
|
||||
console.log('closed');
|
||||
// setTimeout(() => {
|
||||
|
||||
// }, 2000);
|
||||
|
||||
|
||||
// AnyProxy.utils.systemProxyMgr.enableGlobalProxy('127.0.0.1', '8001');
|
||||
});
|
||||
|
||||
proxyServer.on('error', (e) => {
|
||||
console.log('proxy error');
|
||||
console.log(e);
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
// AnyProxy.utils.systemProxyMgr.disableGlobalProxy();
|
||||
proxyServer.close();
|
||||
process.exit();
|
||||
});
|
||||
|
||||
|
||||
proxyServer.start();
|
||||
|
||||
|
||||
// const WebSocketServer = require('ws').Server;
|
||||
// const wsServer = new WebSocketServer({ port: 8003 },function(){
|
||||
// console.log('ready');
|
||||
|
||||
// try {
|
||||
// const serverB = new WebSocketServer({ port: 8003 }, function (e, result) {
|
||||
// console.log('---in B---');
|
||||
// console.log(e);
|
||||
// console.log(result);
|
||||
// });
|
||||
// } catch(e) {
|
||||
// console.log(e);
|
||||
// console.log('e');
|
||||
// }
|
||||
|
||||
// // wsServer.close(function (e, result) {
|
||||
// // console.log('in close');
|
||||
// // console.log(e);
|
||||
// // console.log(result);
|
||||
// // });
|
||||
// });
|
||||
10
module_sample/simple_use.js
Normal file
10
module_sample/simple_use.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const AnyProxy = require('../proxy');
|
||||
|
||||
const options = {
|
||||
port: 8001,
|
||||
webInterface: {
|
||||
enable: true
|
||||
}
|
||||
};
|
||||
const proxyServer = new AnyProxy.ProxyServer(options);
|
||||
proxyServer.start();
|
||||
Reference in New Issue
Block a user