From 67d47dfba9a44d206a559edd09664bf238d3a8b4 Mon Sep 17 00:00:00 2001 From: mercury Date: Thu, 11 May 2023 18:31:57 +0400 Subject: [PATCH] wg block/unblock torrent --- app/bot.php | 35 +++++++++++++++++++++++++++------ app/i18n.php | 10 +++++++--- docker-compose.yml | 2 ++ dockerfile/wireguard.dockerfile | 10 +--------- scripts/block_torrent.sh | 12 +++++++++++ scripts/init.sh | 5 ++++- scripts/unblock_torrent.sh | 12 +++++++++++ 7 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 scripts/block_torrent.sh create mode 100644 scripts/unblock_torrent.sh diff --git a/app/bot.php b/app/bot.php index 8f20b17..037f919 100644 --- a/app/bot.php +++ b/app/bot.php @@ -161,6 +161,9 @@ class Bot case preg_match('~^/download (\d+)$~', $this->input['callback'], $m): $this->downloadPeer($m[1]); break; + case preg_match('~^/switchTorrent (\d+)$~', $this->input['callback'], $m): + $this->switchTorrent($m[1]); + break; case preg_match('~^/switchClient (?P\d+(?:_(?:-)?\d+)?)$~', $this->input['callback'], $m): $this->switchClient(...explode('_', $m['arg'])); break; @@ -599,6 +602,19 @@ class Bot $this->menu('client', "{$client}_0"); } + public function switchTorrent($page) + { + $c = $this->getPacConf(); + $c['blocktorrent'] = $c['blocktorrent'] ? 0 : 1; + $this->setPacConf($c); + if ($c['blocktorrent']) { + $this->ssh('bash /block_torrent.sh'); + } else { + $this->ssh('bash /unblock_torrent.sh'); + } + $this->menu('wg', $page); + } + public function qrPeer($client) { $client = $this->readClients()[$client]; @@ -1353,15 +1369,22 @@ DNS-over-HTTPS with IP: } } $text = "Menu -> Wireguard\n\n" . implode(PHP_EOL, $text) . ''; + $bt = $this->getPacConf()['blocktorrent']; $data = [ - [[ - 'text' => $this->i18n('update status'), - 'callback_data' => "/menu wg 0", - ]], - [[ + [ + [ + 'text' => $this->i18n('update status'), + 'callback_data' => "/menu wg 0", + ], + [ 'text' => $this->i18n('add peer'), 'callback_data' => "/menu addpeer $page", - ]], + ], + [ + 'text' => $this->i18n(($bt ? 'block' : 'unblock') . 'torrent'), + 'callback_data' => "/switchTorrent $page", + ], + ], ]; if ($clients = $this->getClients($page)) { $data = array_merge($data, $clients); diff --git a/app/i18n.php b/app/i18n.php index bf75fe1..e9f7c92 100644 --- a/app/i18n.php +++ b/app/i18n.php @@ -189,8 +189,12 @@ $i = [ 'en' => 'on', 'ru' => 'вкл', ], - 'off' => [ - 'en' => 'off', - 'ru' => 'выкл', + 'blocktorrent' => [ + 'en' => 'torrent off', + 'ru' => 'торренты выкл', + ], + 'unblocktorrent' => [ + 'en' => 'torrent on', + 'ru' => 'торренты вкл', ], ]; diff --git a/docker-compose.yml b/docker-compose.yml index cec15da..925d38d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -127,6 +127,8 @@ services: - ./config/wg0.conf:/etc/wireguard/wg0.conf - ./scripts/start_wg.sh:/start_wg.sh - ./scripts/reset_wg.sh:/reset_wg.sh + - ./scripts/block_torrent.sh:/block_torrent.sh + - ./scripts/unblock_torrent.sh:/unblock_torrent.sh - ./ssh:/ssh hostname: wireguard depends_on: diff --git a/dockerfile/wireguard.dockerfile b/dockerfile/wireguard.dockerfile index be85e42..a977f04 100644 --- a/dockerfile/wireguard.dockerfile +++ b/dockerfile/wireguard.dockerfile @@ -2,12 +2,4 @@ arg SYSTEM arg RELEASE from ${SYSTEM}:${RELEASE} ENV DEBIAN_FRONTEND noninteractive -run apt update && \ -apt install -y wireguard \ -iproute2 \ -net-tools \ -lsof \ -iptables \ -linux-headers-$(uname -r) \ -ssh && \ -mkdir /root/.ssh +run apt update && apt install -y linux-headers-$(uname -r) iproute2 iptables xtables-addons-common xtables-addons-dkms wireguard ssh && mkdir /root/.ssh diff --git a/scripts/block_torrent.sh b/scripts/block_torrent.sh new file mode 100644 index 0000000..b05e9c2 --- /dev/null +++ b/scripts/block_torrent.sh @@ -0,0 +1,12 @@ +iptables -I FORWARD -p tcp -m ipp2p --bit -j DROP +iptables -I FORWARD -p udp -m ipp2p --bit -j DROP +iptables -I FORWARD -m string --algo bm --string "BitTorrent" -j DROP +iptables -I FORWARD -m string --algo bm --string "BitTorrent protocol" -j DROP +iptables -I FORWARD -m string --algo bm --string "peer_id=" -j DROP +iptables -I FORWARD -m string --algo bm --string ".torrent" -j DROP +iptables -I FORWARD -m string --algo bm --string "announce.php?passkey=" -j DROP +iptables -I FORWARD -m string --algo bm --string "torrent" -j DROP +iptables -I FORWARD -m string --algo bm --string "announce" -j DROP +iptables -I FORWARD -m string --algo bm --string "info_hash" -j DROP +iptables -I OUTPUT -p tcp -m ipp2p --bit -j DROP +iptables -I OUTPUT -p udp -m ipp2p --bit -j DROP diff --git a/scripts/init.sh b/scripts/init.sh index 13af3b5..10fec61 100644 --- a/scripts/init.sh +++ b/scripts/init.sh @@ -5,7 +5,10 @@ apt install -y \ gnupg \ lsb-release \ make \ - git + git \ + iptables \ + xtables-addons-common \ + xtables-addons-dkms mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/gpg | gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg echo \ diff --git a/scripts/unblock_torrent.sh b/scripts/unblock_torrent.sh new file mode 100644 index 0000000..f8c1430 --- /dev/null +++ b/scripts/unblock_torrent.sh @@ -0,0 +1,12 @@ +iptables -D FORWARD -p tcp -m ipp2p --bit -j DROP +iptables -D FORWARD -p udp -m ipp2p --bit -j DROP +iptables -D FORWARD -m string --algo bm --string "BitTorrent" -j DROP +iptables -D FORWARD -m string --algo bm --string "BitTorrent protocol" -j DROP +iptables -D FORWARD -m string --algo bm --string "peer_id=" -j DROP +iptables -D FORWARD -m string --algo bm --string ".torrent" -j DROP +iptables -D FORWARD -m string --algo bm --string "announce.php?passkey=" -j DROP +iptables -D FORWARD -m string --algo bm --string "torrent" -j DROP +iptables -D FORWARD -m string --algo bm --string "announce" -j DROP +iptables -D FORWARD -m string --algo bm --string "info_hash" -j DROP +iptables -D OUTPUT -p tcp -m ipp2p --bit -j DROP +iptables -D OUTPUT -p udp -m ipp2p --bit -j DROP