forked from lqqyt2423/wechat_spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (51 loc) · 1.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
const AnyProxy = require('anyproxy');
const exec = require('child_process').exec;
const ip = require('ip');
const { log } = console;
const config = require('./config');
const redis = require('./utils/redis');
const {
redis: redisConfig,
anyproxy: anyproxyConfig,
serverPort,
} = config;
const { POST_LIST_KEY, PROFILE_LIST_KEY } = redisConfig;
// 引导安装HTTPS证书
if (!AnyProxy.utils.certMgr.ifRootCAFileExists()) {
AnyProxy.utils.certMgr.generateRootCA((error, keyPath) => {
if (!error) {
const certDir = require('path').dirname(keyPath);
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);
}
});
}
const proxyServer = new AnyProxy.ProxyServer({
...anyproxyConfig,
// 所有的抓取规则
rule: require('./rule'),
});
proxyServer.on('ready', () => {
const ipAddress = ip.address();
log(`请配置代理: ${ipAddress}:8101`);
});
proxyServer.on('error', (e) => {
throw e;
});
// 删除redis中对应缓存后再启动
redis('del', POST_LIST_KEY, PROFILE_LIST_KEY).then(() => {
proxyServer.start();
});
// when finished
// proxyServer.close();
require('./server').listen(serverPort, () => {
log('可视化界面: http://localhost:8104');
});