Skip to content

Instantly share code, notes, and snippets.

@kLiHz
Last active September 18, 2024 10:01
Show Gist options
  • Save kLiHz/5673b5c1d50794edb68c7d863dfc1242 to your computer and use it in GitHub Desktop.
Save kLiHz/5673b5c1d50794edb68c7d863dfc1242 to your computer and use it in GitHub Desktop.
Automatically ban (suspicious) qBittorrent peers (which comes from several CN IP CIDRs and takes hundreds of GBs traffic by downloading something again and agian), based on client name and source IP prefixes, with JavaScript (Deno)
const endpoint = 'http://127.0.0.1:9876' || 'http://localhost:8080';
const username = 'admin';
const password = 'adminadmin';
const mainDataUrl = new URL('/api/v2/sync/maindata', endpoint);
const torrentPeersUrl = new URL('/api/v2/sync/torrentPeers', endpoint);
const banPeers = new URL('/api/v2/transfer/banPeers', endpoint);
async function login(username: string, password: string) {
const res = await fetch(
new URL('/api/v2/auth/login', endpoint),
{
method: 'POST',
headers: {
'Referer': endpoint,
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `username=${username}&password=${password}`,
}
);
if (res.status == 200) {
const cookies = res.headers.getSetCookie();
for (const c of cookies) {
const parts = c.split(';');
if (parts[0] && parts[0].startsWith('SID=')) {
console.log(`Login: ${res.status} ${await res.text()} ${parts[0]}`);
return parts[0];
}
}
}
return null;
}
const cookie = await login(username, password) || '';
async function logout(cookie: string) {
const headers = new Headers();
headers.append('Referer', endpoint);
if (cookie) headers.append('Cookie', cookie);
const res = await fetch(new URL('/api/v2/auth/logout', endpoint), { method: 'POST', headers });
console.log(`Logout: ${res.status}`);
}
const f = async () => {
const r = await fetch(mainDataUrl, { headers: { 'Cookie': cookie } });
const d = await r.json() as { torrents: { [hash: string]: { upspeed: number } } };
for (const [hash, _o] of Object.entries(d.torrents).filter(([_hash, { upspeed }]) => upspeed != 0)) {
torrentPeersUrl.search = `?hash=${hash}`;
const r = await fetch(torrentPeersUrl, { headers: { 'Cookie': cookie } });
const d = await r.json() as { peers: { [socket: string]: { up_speed: number, client: string } } };
const { peers } = d;
for (const [socket, _o] of Object.entries(peers).filter(
([socket, { up_speed, client }]) => up_speed != 0 && (
[
'hp/torrent',
'dt/torrent',
// 'aria',
'Gopeed',
].some(s => client.startsWith(s))
||
[
'42.248.192',
'180.97.209',
'218.92.139',
'182.32.205',
'58.216.63',
'183.131.169',
'61.147.218',
'125.77.27',
'125.77.176',
'222.185.229',
'222.185.228',
'61.160.196',
'61.160.241',
'115.231.45',
].some(i => socket.startsWith(i))
)
)) {
const q = await fetch(banPeers, {
body: new URLSearchParams({ hash, peers: socket }).toString(),
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': cookie,
},
});
console.log(`Banning ${socket} (${_o.client}). Up speed was ${_o.up_speed}. Status: ${q.status} ${await q.text()}`);
}
}
};
let running = true;
Deno.addSignalListener("SIGINT", () => { running = false; });
const wait = (ms: number) => new Promise(
(resolve, _reject) => {
setTimeout(() => { resolve(ms); }, ms);
}
);
while (running) {
const interval = await wait(1000);
try {
await f();
} catch (e) {
if (e instanceof Error) {
console.log(e.message);
}
}
await interval;
}
await logout(cookie);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment