the ability to set the port for wireguard and shadowsocks at startup

This commit is contained in:
mercury
2023-02-14 03:00:08 +04:00
parent e27678432c
commit 111e5ffb8d
11 changed files with 59 additions and 23 deletions
+2 -1
View File
@@ -1,3 +1,4 @@
TZ=Europe/Samara TZ=Europe/Samara
WGPORT=51820
WGADDRESS=10.0.1.1/24 WGADDRESS=10.0.1.1/24
WGPORT=51820
SSPORT=8388
+19 -6
View File
@@ -311,12 +311,14 @@ class Bot
unset($l['plugin']); unset($l['plugin']);
unset($l['plugin_opts']); unset($l['plugin_opts']);
$l['server'] = 'ss'; $l['server'] = 'ss';
$l['server_port'] = 8388; $l['server_port'] = (int) getenv('SSPORT');
$c['server_port'] = (int) getenv('SSPORT');
} else { } else {
$c['plugin'] = 'v2ray-plugin'; $c['plugin'] = 'v2ray-plugin';
$c['plugin_opts'] = 'server;loglevel=none'; $c['plugin_opts'] = 'server;loglevel=none';
$l['server'] = 'ng'; $l['server'] = 'ng';
$l['server_port'] = 443; $l['server_port'] = 443;
$c['server_port'] = 443;
$l['plugin'] = 'v2ray-plugin'; $l['plugin'] = 'v2ray-plugin';
$l['plugin_opts'] = "tls;fast-open;path=/v2ray;host=$domain"; $l['plugin_opts'] = "tls;fast-open;path=/v2ray;host=$domain";
} }
@@ -512,7 +514,7 @@ class Bot
$domain = $conf['domain'] ?: $ip; $domain = $conf['domain'] ?: $ip;
$scheme = empty($ssl = $this->nginxGetTypeCert()) ? 'http' : 'https'; $scheme = empty($ssl = $this->nginxGetTypeCert()) ? 'http' : 'https';
$ss = $this->getSSConfig(); $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"); $ss_link = 'ss://' . base64_encode("{$ss['method']}:{$ss['password']}@$domain:$port");
$qr_file = __DIR__ . "/qr/shadowsocks.png"; $qr_file = __DIR__ . "/qr/shadowsocks.png";
exec("qrencode -t png -o $qr_file '$ss_link'"); 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'); $out[] = $this->ssh("/AdGuardHome/AdGuardHome -s stop 2>&1", 'ad');
$this->update($this->input['chat'], $this->input['message_id'], implode("\n", $out)); $this->update($this->input['chat'], $this->input['message_id'], implode("\n", $out));
$c = yaml_parse_file('/config/AdGuardHome.yaml'); $c = yaml_parse_file('/config/AdGuardHome.yaml');
$this->sd($c);
yaml_emit_file('/config/adguard/AdGuardHome.yaml', $c); yaml_emit_file('/config/adguard/AdGuardHome.yaml', $c);
$out[] = $this->ssh("/AdGuardHome/AdGuardHome -s start 2>&1", 'ad'); $out[] = $this->ssh("/AdGuardHome/AdGuardHome -s start 2>&1", 'ad');
$this->update($this->input['chat'], $this->input['message_id'], implode("\n", $out)); $this->update($this->input['chat'], $this->input['message_id'], implode("\n", $out));
@@ -1132,7 +1133,7 @@ DNS-over-HTTPS with IP:
{ {
$conf = $this->readConfig(); $conf = $this->readConfig();
$address = getenv('ADDRESS'); $address = getenv('ADDRESS');
$port = getenv('PORT_WG'); $port = getenv('WGPORT');
$r = $this->ssh("/bin/sh /reset_wg.sh $address $port"); $r = $this->ssh("/bin/sh /reset_wg.sh $address $port");
file_put_contents($this->clients, ''); file_put_contents($this->clients, '');
$this->menu(); $this->menu();
@@ -1568,7 +1569,7 @@ DNS-over-HTTPS with IP:
$scheme = empty($ssl = $this->nginxGetTypeCert()) ? 'http' : 'https'; $scheme = empty($ssl = $this->nginxGetTypeCert()) ? 'http' : 'https';
$ss = $this->getSSConfig(); $ss = $this->getSSConfig();
$v2ray = !empty($ss['plugin']) ? 'ON' : 'OFF'; $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"; $options = !empty($ssl) && !empty($ss['plugin']) ? "tls;fast-open;path=/v2ray;host=$domain" : "path=/v2ray;host=$domain";
$text = "Menu -> ShadowSocks"; $text = "Menu -> ShadowSocks";
@@ -2016,7 +2017,7 @@ DNS-over-HTTPS with IP:
'peers' => [ 'peers' => [
[ [
'PublicKey' => $public_server_key, '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", 'AllowedIPs' => $ips_user ?: "0.0.0.0/0",
'PersistentKeepalive' => 20, 'PersistentKeepalive' => 20,
] ]
@@ -2041,6 +2042,18 @@ DNS-over-HTTPS with IP:
return count($r) - 1; 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) public function saveClients(array $clients)
{ {
file_put_contents($this->clients, json_encode($clients, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); file_put_contents($this->clients, json_encode($clients, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
+1
View File
@@ -7,3 +7,4 @@ require __DIR__ . '/config.php';
$bot = new Bot($c['key']); $bot = new Bot($c['key']);
$bot->setwebhook($c['key']); $bot->setwebhook($c['key']);
$bot->setcommands(); $bot->setcommands();
$bot->syncPortClients();
+1 -1
View File
@@ -27,7 +27,7 @@ dns:
blocked_response_ttl: 10 blocked_response_ttl: 10
parental_block_host: family-block.dns.adguard.com parental_block_host: family-block.dns.adguard.com
safebrowsing_block_host: standard-block.dns.adguard.com safebrowsing_block_host: standard-block.dns.adguard.com
ratelimit: 20 ratelimit: 200
ratelimit_whitelist: [] ratelimit_whitelist: []
refuse_any: true refuse_any: true
upstream_dns: upstream_dns:
+9 -6
View File
@@ -38,8 +38,6 @@ services:
unit: unit:
build: build:
dockerfile: dockerfile/php.dockerfile dockerfile: dockerfile/php.dockerfile
args:
IP: ${IP}
volumes: volumes:
- ./config/.profile:/root/.bashrc:ro - ./config/.profile:/root/.bashrc:ro
- ./config/php.ini:/usr/local/etc/php/php.ini - ./config/php.ini:/usr/local/etc/php/php.ini
@@ -62,7 +60,8 @@ services:
environment: environment:
TZ: ${TZ} TZ: ${TZ}
ADDRESS: ${WGADDRESS} ADDRESS: ${WGADDRESS}
PORT_WG: ${WGPORT} WGPORT: ${WGPORT}
SSPORT: ${SSPORT}
hostname: unit hostname: unit
restart: unless-stopped restart: unless-stopped
stop_grace_period: 1s stop_grace_period: 1s
@@ -71,6 +70,8 @@ services:
networks: networks:
default: default:
ipv4_address: 10.10.0.7 ipv4_address: 10.10.0.7
extra_hosts:
- "host.docker.internal:host-gateway"
proxy: proxy:
build: build:
dockerfile: dockerfile/shadowsocks.dockerfile dockerfile: dockerfile/shadowsocks.dockerfile
@@ -87,6 +88,7 @@ services:
ipv4_address: 10.10.0.3 ipv4_address: 10.10.0.3
environment: environment:
TZ: ${TZ} TZ: ${TZ}
SSPORT: ${SSPORT}
stop_grace_period: 1s stop_grace_period: 1s
command: ["/bin/sh", "/start_proxy.sh"] command: ["/bin/sh", "/start_proxy.sh"]
wg: wg:
@@ -105,7 +107,7 @@ services:
- ${WGPORT}:${WGPORT}/udp - ${WGPORT}:${WGPORT}/udp
environment: environment:
TZ: ${TZ} TZ: ${TZ}
PORT_WG: ${WGPORT} WGPORT: ${WGPORT}
ADDRESS: ${WGADDRESS} ADDRESS: ${WGADDRESS}
cap_add: cap_add:
- NET_ADMIN - NET_ADMIN
@@ -147,10 +149,11 @@ services:
depends_on: depends_on:
- unit - unit
ports: ports:
- 8388:8388/udp - ${SSPORT}:${SSPORT}/tcp
- 8388:8388/tcp - ${SSPORT}:${SSPORT}/udp
environment: environment:
TZ: ${TZ} TZ: ${TZ}
SSPORT: ${SSPORT}
stop_grace_period: 1s stop_grace_period: 1s
command: ["/bin/sh", "/start_ss.sh"] command: ["/bin/sh", "/start_ss.sh"]
networks: networks:
+9 -6
View File
@@ -40,8 +40,6 @@ services:
unit: unit:
build: build:
dockerfile: dockerfile/php.dockerfile dockerfile: dockerfile/php.dockerfile
args:
IP: ${IP}
volumes: volumes:
- ./config/.profile:/root/.bashrc:ro - ./config/.profile:/root/.bashrc:ro
- ./config/php.ini:/usr/local/etc/php/php.ini - ./config/php.ini:/usr/local/etc/php/php.ini
@@ -64,7 +62,8 @@ services:
environment: environment:
TZ: ${TZ} TZ: ${TZ}
ADDRESS: ${WGADDRESS} ADDRESS: ${WGADDRESS}
PORT_WG: ${WGPORT} WGPORT: ${WGPORT}
SSPORT: ${SSPORT}
hostname: unit hostname: unit
restart: unless-stopped restart: unless-stopped
stop_grace_period: 1s stop_grace_period: 1s
@@ -73,6 +72,8 @@ services:
networks: networks:
default: default:
ipv4_address: 10.10.0.7 ipv4_address: 10.10.0.7
extra_hosts:
- "host.docker.internal:host-gateway"
proxy: proxy:
build: build:
dockerfile: dockerfile/shadowsocks.dockerfile dockerfile: dockerfile/shadowsocks.dockerfile
@@ -89,6 +90,7 @@ services:
ipv4_address: 10.10.0.3 ipv4_address: 10.10.0.3
environment: environment:
TZ: ${TZ} TZ: ${TZ}
SSPORT: ${SSPORT}
stop_grace_period: 1s stop_grace_period: 1s
command: ["/bin/sh", "/start_proxy.sh"] command: ["/bin/sh", "/start_proxy.sh"]
wg: wg:
@@ -107,7 +109,7 @@ services:
- ${WGPORT}:${WGPORT}/udp - ${WGPORT}:${WGPORT}/udp
environment: environment:
TZ: ${TZ} TZ: ${TZ}
PORT_WG: ${WGPORT} WGPORT: ${WGPORT}
ADDRESS: ${WGADDRESS} ADDRESS: ${WGADDRESS}
cap_add: cap_add:
- NET_ADMIN - NET_ADMIN
@@ -149,10 +151,11 @@ services:
depends_on: depends_on:
- unit - unit
ports: ports:
- 8388:8388/udp - ${SSPORT}:${SSPORT}/tcp
- 8388:8388/tcp - ${SSPORT}:${SSPORT}/udp
environment: environment:
TZ: ${TZ} TZ: ${TZ}
SSPORT: ${SSPORT}
stop_grace_period: 1s stop_grace_period: 1s
command: ["/bin/sh", "/start_ss.sh"] command: ["/bin/sh", "/start_ss.sh"]
networks: networks:
-1
View File
@@ -1,5 +1,4 @@
from nginx/unit:1.29.0-php8.1 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 && \ 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/ssh2-1.3.1.tgz && \
pecl install https://pecl.php.net/get/yaml-2.2.2.tgz && \ pecl install https://pecl.php.net/get/yaml-2.2.2.tgz && \
+1 -1
View File
@@ -5,7 +5,7 @@ hosts: unhosts # маппинг доменов на локалку
unhosts: unhosts:
sed -i '/test.ru/d' /mnt/c/Windows/System32/drivers/etc/hosts sed -i '/test.ru/d' /mnt/c/Windows/System32/drivers/etc/hosts
u: # запуск контейнеров u: # запуск контейнеров
IP=$(shell curl https://ipinfo.io/ip) docker compose up -d --build --force-recreate docker compose up -d --build --force-recreate
# sleep 1 # sleep 1
# docker compose logs unit wg ss proxy # docker compose logs unit wg ss proxy
d: # остановка контейнеров d: # остановка контейнеров
+7
View File
@@ -1,4 +1,11 @@
cat /ssh/key.pub > /root/.ssh/authorized_keys cat /ssh/key.pub > /root/.ssh/authorized_keys
service ssh start 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 /sslocal -v -d -c /config.json
tail -f /dev/null tail -f /dev/null
+7
View File
@@ -1,4 +1,11 @@
cat /ssh/key.pub > /root/.ssh/authorized_keys cat /ssh/key.pub > /root/.ssh/authorized_keys
service ssh start 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 /ssserver -v -d -c /config.json
tail -f /dev/null tail -f /dev/null
+3 -1
View File
@@ -5,10 +5,12 @@ then
echo "[Interface]" > /etc/wireguard/wg0.conf echo "[Interface]" > /etc/wireguard/wg0.conf
echo "PrivateKey = $PRIVATEKEY" >> /etc/wireguard/wg0.conf echo "PrivateKey = $PRIVATEKEY" >> /etc/wireguard/wg0.conf
echo "Address = $ADDRESS" >> /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 "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 echo "PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o $INTERFACE -j MASQUERADE" >> /etc/wireguard/wg0.conf
fi 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 wg-quick up wg0
cat /ssh/key.pub > /root/.ssh/authorized_keys cat /ssh/key.pub > /root/.ssh/authorized_keys
service ssh start service ssh start