From ae2e9c020540b6568642799f29c810d592d95673 Mon Sep 17 00:00:00 2001 From: mercury Date: Wed, 28 Feb 2024 14:53:11 +0400 Subject: [PATCH] mirror --- .env | 4 +- app/bot.php | 78 ++++++++++++++++++++++++++++++++++++++- app/i18n.php | 8 ++++ app/index.php | 29 +++++++++------ docker-compose.yml | 13 +++++++ dockerfile/php.dockerfile | 1 + makefile | 2 +- mirror/docker-compose.yml | 18 +++++++++ mirror/dockerfile | 2 + mirror/makefile | 11 ++++++ mirror/start_socat.sh | 8 ++++ 11 files changed, 159 insertions(+), 15 deletions(-) create mode 100644 mirror/docker-compose.yml create mode 100644 mirror/dockerfile create mode 100644 mirror/makefile create mode 100644 mirror/start_socat.sh diff --git a/.env b/.env index 3d66ee7..950084c 100644 --- a/.env +++ b/.env @@ -3,6 +3,6 @@ WGADDRESS=10.0.1.1/24 WGPORT=51820 SSPORT=8388 TGPORT=4443 -SYSTEM=ubuntu -RELEASE=18.04 IMAGE=alpine:3.18.2 +IP= +VER= diff --git a/app/bot.php b/app/bot.php index 9357047..a5318f1 100644 --- a/app/bot.php +++ b/app/bot.php @@ -117,7 +117,7 @@ class Bot case preg_match('~^/menu (?Paddpeer) (?P(?:-)?\d+)$~', $this->input['callback'], $m): case preg_match('~^/menu (?Pwg) (?P(?:-)?\d+)$~', $this->input['callback'], $m): case preg_match('~^/menu (?Pclient) (?P\d+(?:_(?:-)?\d+)?)$~', $this->input['callback'], $m): - case preg_match('~^/menu (?Ppac|adguard|config|ss|lang|oc|naive)$~', $this->input['callback'], $m): + case preg_match('~^/menu (?Ppac|adguard|config|ss|lang|oc|naive|mirror)$~', $this->input['callback'], $m): case preg_match('~^/menu (?Psubzoneslist|reverselist|includelist|excludelist) (?P(?:-)?\d+)$~', $this->input['callback'], $m): $this->menu(type: $m['type'] ?? false, arg: $m['arg'] ?? false); break; @@ -127,6 +127,9 @@ class Bot case preg_match('~^/mtproto$~', $this->input['callback'], $m): $this->mtproto(); break; + case preg_match('~^/getMirror$~', $this->input['callback'], $m): + $this->getMirror(); + break; case preg_match('~^/logs$~', $this->input['callback'], $m): $this->logs(); break; @@ -2936,6 +2939,12 @@ DNS-over-HTTPS with IP: 'callback_data' => "/menu pac", ], ], + [ + [ + 'text' => $this->i18n('mirror'), + 'callback_data' => "/menu mirror", + ], + ], [ [ 'text' => $this->i18n('config'), @@ -2968,6 +2977,7 @@ DNS-over-HTTPS with IP: 'lang' => $type == 'lang' ? $this->menuLang() : false, 'oc' => $type == 'oc' ? $this->ocMenu() : false, 'naive' => $type == 'naive' ? $this->naiveMenu() : false, + 'mirror' => $type == 'mirror' ? $this->mirrorMenu() : false, ]; $text = $menu[$type ?: 'main' ]['text']; @@ -3034,6 +3044,72 @@ DNS-over-HTTPS with IP: ]; } + public function mirrorMenu() + { + $ip = $this->getPacConf()['domain'] ?: $this->ip; + $hash = substr(md5($this->key), 0, 8); + $scheme = empty($this->nginxGetTypeCert()) ? 'http' : 'https'; + $text[] = "Menu -> Mirror"; + $text[] = <<client -> intermediate VPS -> vpnbot + ^ | + | install | + | mirror | + ----------- + + PNG; + $text[] = "$scheme://$ip/pac?h=$hash&t=mirror"; + $data[] = [ + [ + 'text' => $this->i18n('download'), + 'callback_data' => "/getMirror", + ], + ]; + $data[] = [ + [ + 'text' => $this->i18n('back'), + 'callback_data' => "/menu", + ], + ]; + return [ + 'text' => implode("\n", $text), + 'data' => $data, + ]; + } + + public function getMirror() + { + unlink('/config/mirror.tar.gz'); + unlink('/config/mirror.tar'); + $s = file_get_contents('/mirror/start_socat.sh'); + $t = preg_replace([ + '~{ip}~', + '~{tg}~', + '~{ss}~', + '~{wg}~', + ], [ + getenv('IP'), + getenv('TGPORT'), + getenv('SSPORT'), + getenv('WGPORT'), + ], $s); + file_put_contents('/mirror/start_socat.sh', $t); + $a = new PharData('/config/mirror.tar'); + $a->buildFromDirectory('/mirror'); + $a->compress(Phar::GZ); + if (!empty($this->input)) { + $this->sendFile($this->input['from'], curl_file_create('/config/mirror.tar.gz')); + } else { + header('Content-Disposition: attachment; filename=mirror.tar.gz'); + header('Content-Type: application/tar+gzip'); + echo file_get_contents('/config/mirror.tar.gz'); + exit; + } + unlink('/config/mirror.tar.gz'); + unlink('/config/mirror.tar'); + file_put_contents('/mirror/start_socat.sh', $s); + } + public function ocMenu() { $pac = $this->getPacConf(); diff --git a/app/i18n.php b/app/i18n.php index 0caa44b..7318b27 100644 --- a/app/i18n.php +++ b/app/i18n.php @@ -293,4 +293,12 @@ $i = [ 'en' => 'dns', 'ru' => 'днс', ], + 'mirror' => [ + 'en' => 'mirror', + 'ru' => 'зеркало', + ], + 'download' => [ + 'en' => 'download', + 'ru' => 'скачать', + ], ]; diff --git a/app/index.php b/app/index.php index 6f2c1a8..de39972 100644 --- a/app/index.php +++ b/app/index.php @@ -22,17 +22,24 @@ $address = $_GET['a'] ?: '127.0.0.1'; $port = $_GET['p'] ?: '1080'; $hash = $_GET['h']; if ($hash == substr(md5($c['key']), 0, 8)) { - if (file_exists($file = __DIR__ . "/zapretlists/$type")) { - $pac = file_get_contents($file); - header('Content-Type: text/plain'); - echo str_replace([ - '~address~', - '~port~', - ], [ - $address, - $port, - ], $pac); - exit; + if ($type == 'mirror') { + require __DIR__ . '/bot.php'; + require __DIR__ . '/i18n.php'; + $bot = new Bot($c['key'], $i); + $bot->getMirror(); + } else { + if (file_exists($file = __DIR__ . "/zapretlists/$type")) { + $pac = file_get_contents($file); + header('Content-Type: text/plain'); + echo str_replace([ + '~address~', + '~port~', + ], [ + $address, + $port, + ], $pac); + exit; + } } } diff --git a/docker-compose.yml b/docker-compose.yml index b8580ca..8485046 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -32,6 +32,7 @@ services: ports: - 443:443 hostname: upstream + container_name: upstream-${VER} depends_on: php: condition: service_healthy @@ -66,6 +67,7 @@ services: ports: - 80:80 hostname: nginx + container_name: nginx-${VER} depends_on: up: condition: service_started @@ -94,6 +96,8 @@ services: - ./scripts/start_php.sh:/start_php.sh - ./scripts/check_file.sh:/check_file.sh - ./version:/version + - ./mirror:/mirror + - ./.env:/mirror/.env environment: TZ: ${TZ} IP: ${IP} @@ -102,6 +106,7 @@ services: SSPORT: ${SSPORT} TGPORT: ${TGPORT} hostname: php + container_name: php-${VER} restart: unless-stopped stop_grace_period: 1s command: ["/bin/sh", "/start_php.sh"] @@ -129,6 +134,7 @@ services: - ./config/sshd_config:/etc/ssh/sshd_config - ./scripts/start_proxy.sh:/start_proxy.sh hostname: proxy + container_name: proxy-${VER} depends_on: php: condition: service_healthy @@ -159,6 +165,7 @@ services: - ./ssh:/ssh - ./config/sshd_config:/etc/ssh/sshd_config hostname: wireguard + container_name: wireguard-${VER} depends_on: php: condition: service_healthy @@ -197,6 +204,7 @@ services: - ./logs/:/logs/ - ./scripts/start_ad.sh:/start_ad.sh hostname: adguard + container_name: adguard-${VER} depends_on: php: condition: service_healthy @@ -222,6 +230,7 @@ services: - ./config/sshd_config:/etc/ssh/sshd_config - ./scripts/start_ss.sh:/start_ss.sh hostname: shadowsocks + container_name: shadowsocks-${VER} depends_on: php: condition: service_healthy @@ -247,6 +256,7 @@ services: - ./scripts/start_tg.sh:/start_tg.sh - ./config/mtprotosecret:/mtprotosecret hostname: telegram + container_name: mtproto-${VER} depends_on: php: condition: service_healthy @@ -274,6 +284,7 @@ services: - ./config/xray.json:/xray.json - ./scripts/start_xray.sh:/start_xray.sh hostname: xray + container_name: xray-${VER} depends_on: php: condition: service_healthy @@ -298,6 +309,7 @@ services: - ./certs:/certs - ./scripts/start_oc.sh:/start_oc.sh hostname: ocserv + container_name: openconnect-${VER} depends_on: php: condition: service_healthy @@ -327,6 +339,7 @@ services: - ./certs:/certs - ./scripts/start_np.sh:/start_np.sh hostname: naive + container_name: naive-${VER} depends_on: php: condition: service_healthy diff --git a/dockerfile/php.dockerfile b/dockerfile/php.dockerfile index 885fdf7..0cf79eb 100644 --- a/dockerfile/php.dockerfile +++ b/dockerfile/php.dockerfile @@ -3,6 +3,7 @@ FROM $image RUN apk add --no-cache --update php81 \ php81-mbstring \ php81-session \ + php81-phar \ php81-curl \ php81-opcache \ php81-openssl \ diff --git a/makefile b/makefile index a46a87e..1addbbd 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,7 @@ b: docker compose build u: # запуск контейнеров - IP=$(shell ip -4 addr | sed -ne 's|^.* inet \([^/]*\)/.* scope global.*$$|\1|p' | awk '{print $1}' | head -1) docker compose up -d --build --force-recreate + IP=$(shell ip -4 addr | sed -ne 's|^.* inet \([^/]*\)/.* scope global.*$$|\1|p' | awk '{print $1}' | head -1) VER=$(shell git describe --tags) docker compose up -d --build --force-recreate d: # остановка контейнеров docker compose down dv: # остановка контейнеров diff --git a/mirror/docker-compose.yml b/mirror/docker-compose.yml new file mode 100644 index 0000000..280c8bc --- /dev/null +++ b/mirror/docker-compose.yml @@ -0,0 +1,18 @@ +version: "3" + +services: + socat: + build: + dockerfile: ./dockerfile + ports: + - 80:80 + - 443:443 + - 853:853 + - ${TGPORT}:${TGPORT} + - ${SSPORT}:${SSPORT} + - ${SSPORT}:${SSPORT}/udp + - ${WGPORT}:${WGPORT}/udp + volumes: + - ./start_socat.sh:/start_socat.sh + command: /bin/sh /start_socat.sh + stop_grace_period: 1s diff --git a/mirror/dockerfile b/mirror/dockerfile new file mode 100644 index 0000000..7265553 --- /dev/null +++ b/mirror/dockerfile @@ -0,0 +1,2 @@ +FROM alpine:3.18 +RUN apk add socat htop net-tools \ No newline at end of file diff --git a/mirror/makefile b/mirror/makefile new file mode 100644 index 0000000..9893c3d --- /dev/null +++ b/mirror/makefile @@ -0,0 +1,11 @@ +b: + docker compose build --no-cache +u: + docker compose up -d --build +d: + docker compose down +ps: + docker compose ps +e: + docker compose exec socat sh +r: d u \ No newline at end of file diff --git a/mirror/start_socat.sh b/mirror/start_socat.sh new file mode 100644 index 0000000..a731760 --- /dev/null +++ b/mirror/start_socat.sh @@ -0,0 +1,8 @@ +socat TCP-LISTEN:80,fork TCP:{ip}:80 & +socat TCP-LISTEN:443,fork TCP:{ip}:443 & +socat TCP-LISTEN:853,fork TCP:{ip}:853 & +socat TCP-LISTEN:{tg},fork TCP:{ip}:{tg} & +socat TCP-LISTEN:{ss},fork TCP:{ip}:{ss} & +socat UDP-LISTEN:{ss},fork UDP:{ip}:{ss} & +socat UDP-LISTEN:{wg},fork UDP:{ip}:{wg} & +tail -f /dev/null \ No newline at end of file