-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51daili_white_list.js
172 lines (160 loc) · 4.59 KB
/
51daili_white_list.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
new Env('51代理IP白名单');
55 23 * * *
51daili_white_list.js
by:Shuai
51代理白名单自动替换
*/
// 定义appkey(从51代理平台获取)
const appkey = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.';
if (!appkey) {
console.log('❗ 请先定义appkey');
process.exit(0);
}
// 引入模块
const fs = require('fs');
const request = require('request');
const ipFileName = '51dailiIp.txt';
// 读取保存的IP
function readSavedIp() {
try {
const data = fs.readFileSync(ipFileName, 'utf8');
return data.trim();
} catch (error) {
return null;
}
}
// 保存IP
function saveIp(ip) {
fs.writeFileSync(ipFileName, ip);
}
// 获取当前IP
async function getCurrentIp(url) {
try {
let currentIP = await new Promise((resolve, reject) => {
request.get(url, (error, response, body) => {
if (error) {
reject(error);
} else {
resolve(body.trim());
}
});
});
// 使用表情符号
const emojis = ['😊', '😎', '🚀', '🎉', '👍'];
const randomEmoji = emojis[Math.floor(Math.random() * emojis.length)];
const ipMatch = currentIP.match(/\d{1,3}(\.\d{1,3}){3}/);
if (ipMatch) {
currentIP = ipMatch[0];
console.log(`${randomEmoji} 当前IP: ${currentIP}`);
await delay(2000);
return currentIP;
} else {
console.log('💡 未获取到有效的IP地址');
return null;
}
} catch (error) {
console.error('💡 获取当前IP发生错误:', error);
return null;
}
}
// 添加IP到白名单
async function addIpToWhiteList(ip) {
const url = `http://aapi.51daili.com/whiteIP?op=add&appkey=${appkey}&whiteip=${ip}`;
try {
const response = await new Promise((resolve, reject) => {
request.get(url, (error, response, body) => {
if (error) {
reject(error);
} else {
resolve(body);
}
});
});
const emojis = ['😊', '🎉'];
const randomEmoji = emojis[Math.floor(Math.random() * emojis.length)];
const success = response.includes('success');
const message = success ? `🎉 IP地址已更新:${ip}` : `💡 IP地址添加失败: ${response}`;
console.log(`${randomEmoji} 添加IP到白名单的响应:`, response);
await delay(2000);
return { success, message };
} catch (error) {
console.error('💡 添加IP到白名单发生错误:', error);
return { success: false, message: `💡 IP地址添加失败: ${error.message}` };
}
}
// 删除白名单IP
async function delWhiteIp(ip) {
const url = `http://aapi.51daili.com/whiteIP?op=del&appkey=${appkey}&whiteip=${ip}`;
try {
const response = await new Promise((resolve, reject) => {
request.get(url, (error, response, body) => {
if (error) {
reject(error);
} else {
resolve(body);
}
});
});
console.log('💡 白名单中删除IP:', ip, ',', response);
await delay(2000);
return response;
} catch (error) {
console.error('💡 删除IP发生错误:', error);
return null;
}
}
// 获取白名单IP
async function getWhiteIp() {
const url = `http://aapi.51daili.com/whiteIP?op=list&appkey=${appkey}`;
try {
const response = await new Promise((resolve, reject) => {
request.get(url, (error, response, body) => {
if (error) {
reject(error);
} else {
resolve(body);
}
});
});
console.log('💡 获取当前白名单的响应:', response);
await delay(2000);
return response;
} catch (error) {
console.error('💡 获取白名单发生错误:', error);
return null;
}
}
// 延迟函数
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// 主函数
async function main() {
console.log('');
let currentIP = await getCurrentIp('http://checkip.amazonaws.com/');
if (!currentIP) {
console.log('💡 使用备用IP获取方式……');
currentIP = await getCurrentIp('http://api.ipify.org');
}
const oldIp = readSavedIp();
if (currentIP) {
const whiteIpList = await getWhiteIp();
if (oldIp && oldIp !== currentIP) {
if (whiteIpList && whiteIpList.includes(oldIp)) {
await delWhiteIp(oldIp);
}
}
if (whiteIpList && whiteIpList.includes(currentIP)) {
console.log('😎 当前IP在白名单中,无需添加');
} else {
console.log('💡 当前IP不在白名单中,尝试添加');
const result = await addIpToWhiteList(currentIP);
console.log(result.message);
}
saveIp(currentIP);
} else {
console.log('💡 获取公网IP失败,终止执行!');
}
}
main();