-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
199 lines (186 loc) · 4.6 KB
/
main.ts
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const config = JSON.parse(Deno.readTextFileSync("./config.json"));
//设置请求头
const commonHeader = {
Host: config.url,
Origin: "http://" + config.url,
Pragma: "no-cache",
Referer: "http://" + config.url + "/",
"Accept-Encoding": "gzip, deflate, br",
};
//读取正则
const configMatch = new RegExp(config.match);
//初始化ookie
let cookie: string;
//设置屏蔽规则
const banClient = (client: string): boolean => {
if (client.match(configMatch)) return true;
return false;
};
//自定义错误类
class fetchErr extends Error {
constructor(status: number) {
super(status.toString());
this.name = "fetchErr";
if (status === 403) {
this.message = "正在尝试重新登录";
getCookie();
}
}
}
//登录获取cookie,并清除旧ip
const getCookie = async () => {
cookie = (await login())?.headers.get("set-cookie") ?? "";
console.log("登录成功");
await setPreferences();
console.log(`清除旧ip成功`);
};
//登录接口
const login = async () => {
try {
const response = await fetch(
"http://" + config.url + "/api/v2/auth/login",
{
method: "POST",
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
Accept: "*/*",
...commonHeader,
},
body: `username=${config.username}&password=${config.password}`,
}
);
if (!response.ok) throw new Error("登录出错");
return response;
} catch (err) {
console.log(err);
}
};
//设置接口
const setPreferences = async (): Promise<void> => {
try {
const response = await fetch(
"http://" + config.url + "/api/v2/app/setPreferences",
{
method: "POST",
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
Accept: "application/json",
...commonHeader,
cookie,
},
body: 'json={"banned_IPs":""}',
}
);
if (!response.ok) throw response.status;
} catch (err) {
throw new fetchErr(err);
}
};
//获取任务列表接口
interface torrents {
torrents: {
[prop: string]: string[];
};
}
const maindata = async (): Promise<{ torrents: torrents }> => {
try {
const response = await fetch(
"http://" + config.url + "/api/v2/sync/maindata",
{
method: "GET",
headers: {
Accept: "application/json",
...commonHeader,
cookie,
},
}
);
if (!response.ok) throw response.status;
return response.json() as Promise<{ torrents: torrents }>;
} catch (err) {
throw new fetchErr(err);
}
};
//获取任务信息接口
interface peers {
[index: number]: {
client: string;
ip: string;
port: string;
};
}
const torrentPeers = async (hash: string): Promise<{ peers: peers }> => {
try {
const response = await fetch(
"http://" + config.url + `/api/v2/sync/torrentPeers?hash=${hash}`,
{
method: "GET",
headers: {
Accept: "application/json",
...commonHeader,
cookie,
},
}
);
if (!response.ok) throw response.status;
return response.json() as Promise<{ peers: peers }>;
} catch (err) {
throw new fetchErr(err);
}
};
//屏蔽接口
const banPeers = async (hash: string, ip: string): Promise<void> => {
try {
const response = await fetch(
"http://" + config.url + "/api/v2/transfer/banPeers",
{
method: "POST",
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
Accept: "application/json",
...commonHeader,
cookie,
},
body: `hash=${hash}&peers=${ip}`,
}
);
if (!response.ok) throw response.status;
} catch (err) {
throw new fetchErr(err);
}
};
interface peersValues {
client: string;
ip: string;
port: string;
}
getCookie();
//初始化函数
const main = async () => {
try {
const missions = (await maindata()).torrents;
const hashList: string[] = Object.keys(missions);
hashList.map(async (hash:string) => {
const peers: peersValues[] = Object.values(
(await torrentPeers(hash)).peers
);
if (!peers) return;
const banIps: string[] = peers
.filter((peer) => banClient(peer.client))
.map((peer) => `${peer.ip}:${peer.port}`);
const ip:string = banIps.join("|");
await banPeers(hash, ip);
if (ip) console.log(`屏蔽ip: ${ip}`);
});
} catch (err) {
console.log(err);
}
};
//定时调用
setInterval(() => {
try {
main();
} catch (err) {
console.log(err);
}
}, 1000 * config.date);