From 111e5ffb8dea8095d1ecfcd5454e0841ea5534b4 Mon Sep 17 00:00:00 2001 From: mercury Date: Tue, 14 Feb 2023 03:00:08 +0400 Subject: [PATCH] the ability to set the port for wireguard and shadowsocks at startup --- .env | 3 ++- app/bot.php | 25 +++++++++++++++++++------ app/init.php | 1 + config/AdGuardHome.yaml | 2 +- docker-compose.yml | 15 +++++++++------ docker-compose53.yml | 15 +++++++++------ dockerfile/php.dockerfile | 1 - makefile | 2 +- scripts/start_proxy.sh | 7 +++++++ scripts/start_ss.sh | 7 +++++++ scripts/start_wg.sh | 4 +++- 11 files changed, 59 insertions(+), 23 deletions(-) diff --git a/.env b/.env index 4e2f45d..8c224d1 100644 --- a/.env +++ b/.env @@ -1,3 +1,4 @@ TZ=Europe/Samara -WGPORT=51820 WGADDRESS=10.0.1.1/24 +WGPORT=51820 +SSPORT=8388 diff --git a/app/bot.php b/app/bot.php index 7f972f2..caa0d7c 100644 --- a/app/bot.php +++ b/app/bot.php @@ -311,12 +311,14 @@ class Bot unset($l['plugin']); unset($l['plugin_opts']); $l['server'] = 'ss'; - $l['server_port'] = 8388; + $l['server_port'] = (int) getenv('SSPORT'); + $c['server_port'] = (int) getenv('SSPORT'); } else { $c['plugin'] = 'v2ray-plugin'; $c['plugin_opts'] = 'server;loglevel=none'; $l['server'] = 'ng'; $l['server_port'] = 443; + $c['server_port'] = 443; $l['plugin'] = 'v2ray-plugin'; $l['plugin_opts'] = "tls;fast-open;path=/v2ray;host=$domain"; } @@ -512,7 +514,7 @@ class Bot $domain = $conf['domain'] ?: $ip; $scheme = empty($ssl = $this->nginxGetTypeCert()) ? 'http' : 'https'; $ss = $this->getSSConfig(); - $port = !empty($ssl) && !empty($ss['plugin']) ? 443 : 8388; + $port = !empty($ssl) && !empty($ss['plugin']) ? 443 : getenv('SSPORT'); $ss_link = 'ss://' . base64_encode("{$ss['method']}:{$ss['password']}@$domain:$port"); $qr_file = __DIR__ . "/qr/shadowsocks.png"; exec("qrencode -t png -o $qr_file '$ss_link'"); @@ -884,7 +886,6 @@ class Bot $out[] = $this->ssh("/AdGuardHome/AdGuardHome -s stop 2>&1", 'ad'); $this->update($this->input['chat'], $this->input['message_id'], implode("\n", $out)); $c = yaml_parse_file('/config/AdGuardHome.yaml'); - $this->sd($c); yaml_emit_file('/config/adguard/AdGuardHome.yaml', $c); $out[] = $this->ssh("/AdGuardHome/AdGuardHome -s start 2>&1", 'ad'); $this->update($this->input['chat'], $this->input['message_id'], implode("\n", $out)); @@ -1132,7 +1133,7 @@ DNS-over-HTTPS with IP: { $conf = $this->readConfig(); $address = getenv('ADDRESS'); - $port = getenv('PORT_WG'); + $port = getenv('WGPORT'); $r = $this->ssh("/bin/sh /reset_wg.sh $address $port"); file_put_contents($this->clients, ''); $this->menu(); @@ -1568,7 +1569,7 @@ DNS-over-HTTPS with IP: $scheme = empty($ssl = $this->nginxGetTypeCert()) ? 'http' : 'https'; $ss = $this->getSSConfig(); $v2ray = !empty($ss['plugin']) ? 'ON' : 'OFF'; - $port = !empty($ssl) && !empty($ss['plugin']) ? 443 : 8388; + $port = !empty($ssl) && !empty($ss['plugin']) ? 443 : getenv('SSPORT'); $options = !empty($ssl) && !empty($ss['plugin']) ? "tls;fast-open;path=/v2ray;host=$domain" : "path=/v2ray;host=$domain"; $text = "Menu -> ShadowSocks"; @@ -2016,7 +2017,7 @@ DNS-over-HTTPS with IP: 'peers' => [ [ 'PublicKey' => $public_server_key, - 'Endpoint' => file_get_contents('https://ipinfo.io/ip') . ":" . getenv('PORT_WG'), + 'Endpoint' => file_get_contents('https://ipinfo.io/ip') . ":" . getenv('WGPORT'), 'AllowedIPs' => $ips_user ?: "0.0.0.0/0", 'PersistentKeepalive' => 20, ] @@ -2041,6 +2042,18 @@ DNS-over-HTTPS with IP: return count($r) - 1; } + public function syncPortClients() + { + $endpoint = file_get_contents('https://ipinfo.io/ip') . ':' . getenv('WGPORT'); + $clients = $this->readClients(); + foreach ($clients as $k => $v) { + foreach ($v['peers'] as $i => $j) { + $clients[$k]['peers'][$i]['Endpoint'] = $endpoint; + } + } + $this->saveClients($clients); + } + public function saveClients(array $clients) { file_put_contents($this->clients, json_encode($clients, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); diff --git a/app/init.php b/app/init.php index 0f96ee9..0814a65 100644 --- a/app/init.php +++ b/app/init.php @@ -7,3 +7,4 @@ require __DIR__ . '/config.php'; $bot = new Bot($c['key']); $bot->setwebhook($c['key']); $bot->setcommands(); +$bot->syncPortClients(); diff --git a/config/AdGuardHome.yaml b/config/AdGuardHome.yaml index db1fd12..c6272d5 100644 --- a/config/AdGuardHome.yaml +++ b/config/AdGuardHome.yaml @@ -27,7 +27,7 @@ dns: blocked_response_ttl: 10 parental_block_host: family-block.dns.adguard.com safebrowsing_block_host: standard-block.dns.adguard.com - ratelimit: 20 + ratelimit: 200 ratelimit_whitelist: [] refuse_any: true upstream_dns: diff --git a/docker-compose.yml b/docker-compose.yml index e9d246a..3d8b549 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -38,8 +38,6 @@ services: unit: build: dockerfile: dockerfile/php.dockerfile - args: - IP: ${IP} volumes: - ./config/.profile:/root/.bashrc:ro - ./config/php.ini:/usr/local/etc/php/php.ini @@ -62,7 +60,8 @@ services: environment: TZ: ${TZ} ADDRESS: ${WGADDRESS} - PORT_WG: ${WGPORT} + WGPORT: ${WGPORT} + SSPORT: ${SSPORT} hostname: unit restart: unless-stopped stop_grace_period: 1s @@ -71,6 +70,8 @@ services: networks: default: ipv4_address: 10.10.0.7 + extra_hosts: + - "host.docker.internal:host-gateway" proxy: build: dockerfile: dockerfile/shadowsocks.dockerfile @@ -87,6 +88,7 @@ services: ipv4_address: 10.10.0.3 environment: TZ: ${TZ} + SSPORT: ${SSPORT} stop_grace_period: 1s command: ["/bin/sh", "/start_proxy.sh"] wg: @@ -105,7 +107,7 @@ services: - ${WGPORT}:${WGPORT}/udp environment: TZ: ${TZ} - PORT_WG: ${WGPORT} + WGPORT: ${WGPORT} ADDRESS: ${WGADDRESS} cap_add: - NET_ADMIN @@ -147,10 +149,11 @@ services: depends_on: - unit ports: - - 8388:8388/udp - - 8388:8388/tcp + - ${SSPORT}:${SSPORT}/tcp + - ${SSPORT}:${SSPORT}/udp environment: TZ: ${TZ} + SSPORT: ${SSPORT} stop_grace_period: 1s command: ["/bin/sh", "/start_ss.sh"] networks: diff --git a/docker-compose53.yml b/docker-compose53.yml index 5e284ce..19bdbd4 100644 --- a/docker-compose53.yml +++ b/docker-compose53.yml @@ -40,8 +40,6 @@ services: unit: build: dockerfile: dockerfile/php.dockerfile - args: - IP: ${IP} volumes: - ./config/.profile:/root/.bashrc:ro - ./config/php.ini:/usr/local/etc/php/php.ini @@ -64,7 +62,8 @@ services: environment: TZ: ${TZ} ADDRESS: ${WGADDRESS} - PORT_WG: ${WGPORT} + WGPORT: ${WGPORT} + SSPORT: ${SSPORT} hostname: unit restart: unless-stopped stop_grace_period: 1s @@ -73,6 +72,8 @@ services: networks: default: ipv4_address: 10.10.0.7 + extra_hosts: + - "host.docker.internal:host-gateway" proxy: build: dockerfile: dockerfile/shadowsocks.dockerfile @@ -89,6 +90,7 @@ services: ipv4_address: 10.10.0.3 environment: TZ: ${TZ} + SSPORT: ${SSPORT} stop_grace_period: 1s command: ["/bin/sh", "/start_proxy.sh"] wg: @@ -107,7 +109,7 @@ services: - ${WGPORT}:${WGPORT}/udp environment: TZ: ${TZ} - PORT_WG: ${WGPORT} + WGPORT: ${WGPORT} ADDRESS: ${WGADDRESS} cap_add: - NET_ADMIN @@ -149,10 +151,11 @@ services: depends_on: - unit ports: - - 8388:8388/udp - - 8388:8388/tcp + - ${SSPORT}:${SSPORT}/tcp + - ${SSPORT}:${SSPORT}/udp environment: TZ: ${TZ} + SSPORT: ${SSPORT} stop_grace_period: 1s command: ["/bin/sh", "/start_ss.sh"] networks: diff --git a/dockerfile/php.dockerfile b/dockerfile/php.dockerfile index 94deefe..98662c0 100644 --- a/dockerfile/php.dockerfile +++ b/dockerfile/php.dockerfile @@ -1,5 +1,4 @@ from nginx/unit:1.29.0-php8.1 -arg IP run apt update && apt install -y qrencode wget libssh2-1-dev ssh libicu-dev libyaml-dev certbot && \ pecl install https://pecl.php.net/get/ssh2-1.3.1.tgz && \ pecl install https://pecl.php.net/get/yaml-2.2.2.tgz && \ diff --git a/makefile b/makefile index c788d6a..e8db655 100644 --- a/makefile +++ b/makefile @@ -5,7 +5,7 @@ hosts: unhosts # маппинг доменов на локалку unhosts: sed -i '/test.ru/d' /mnt/c/Windows/System32/drivers/etc/hosts u: # запуск контейнеров - IP=$(shell curl https://ipinfo.io/ip) docker compose up -d --build --force-recreate + docker compose up -d --build --force-recreate # sleep 1 # docker compose logs unit wg ss proxy d: # остановка контейнеров diff --git a/scripts/start_proxy.sh b/scripts/start_proxy.sh index 86591b4..36bea11 100644 --- a/scripts/start_proxy.sh +++ b/scripts/start_proxy.sh @@ -1,4 +1,11 @@ cat /ssh/key.pub > /root/.ssh/authorized_keys service ssh start +sed -n "s/\"server_port\": \([0-9]\+\),/\1/p" /config.json > current_port +CURRENT_PORT=$(cat current_port | tr -d " ") +if [ "$CURRENT_PORT" -ne "443" ] +then + sed "s/\"server_port\": [0-9]\+/\"server_port\": $SSPORT/" /config.json > change_port + cat change_port > /config.json +fi /sslocal -v -d -c /config.json tail -f /dev/null diff --git a/scripts/start_ss.sh b/scripts/start_ss.sh index 5b8df30..0f2ce60 100644 --- a/scripts/start_ss.sh +++ b/scripts/start_ss.sh @@ -1,4 +1,11 @@ cat /ssh/key.pub > /root/.ssh/authorized_keys service ssh start +sed -n "s/\"server_port\": \([0-9]\+\),/\1/p" /config.json > current_port +CURRENT_PORT=$(cat current_port | tr -d " ") +if [ "$CURRENT_PORT" -ne "443" ] +then + sed "s/\"server_port\": [0-9]\+/\"server_port\": $SSPORT/" /config.json > change_port + cat change_port > /config.json +fi /ssserver -v -d -c /config.json tail -f /dev/null diff --git a/scripts/start_wg.sh b/scripts/start_wg.sh index e0263c3..fe40cdf 100644 --- a/scripts/start_wg.sh +++ b/scripts/start_wg.sh @@ -5,10 +5,12 @@ then echo "[Interface]" > /etc/wireguard/wg0.conf echo "PrivateKey = $PRIVATEKEY" >> /etc/wireguard/wg0.conf echo "Address = $ADDRESS" >> /etc/wireguard/wg0.conf - echo "ListenPort = $PORT_WG" >> /etc/wireguard/wg0.conf + echo "ListenPort = $WGPORT" >> /etc/wireguard/wg0.conf echo "PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o $INTERFACE -j MASQUERADE" >> /etc/wireguard/wg0.conf echo "PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o $INTERFACE -j MASQUERADE" >> /etc/wireguard/wg0.conf fi +sed "s/ListenPort = [0-9]\+/ListenPort = $WGPORT/" /etc/wireguard/wg0.conf > change_port +cat change_port > /etc/wireguard/wg0.conf wg-quick up wg0 cat /ssh/key.pub > /root/.ssh/authorized_keys service ssh start