From 2f9a8f7b8fdb4787bdd950ec22d5258d5888208d Mon Sep 17 00:00:00 2001 From: mercury Date: Sun, 12 Feb 2023 02:52:57 +0400 Subject: [PATCH] fix export/import config bot --- app/bot.php | 30 +++++++++++++++++++++++------- config/nginx_default.conf | 16 ++++++++-------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/app/bot.php b/app/bot.php index 5060071..143ae6c 100644 --- a/app/bot.php +++ b/app/bot.php @@ -363,7 +363,6 @@ class Bot 'private' => file_exists('/certs/cert_private') ? file_get_contents('/certs/cert_private') : false, 'public' => file_exists('/certs/cert_public') ? file_get_contents('/certs/cert_public') : false, ], - 'nginx' => file_get_contents('/config/nginx.conf') ]; $this->upload(date('d_m_Y_H_i') . '.json', json_encode($conf, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); @@ -437,13 +436,30 @@ class Bot file_put_contents('/certs/cert_public', $json['ssl']['public']); } // nginx - if (!empty($json['nginx'])) { - $out[] = 'reload nginx'; - $this->update($this->input['chat'], $this->input['message_id'], implode("\n", $out)); - file_put_contents('/config/nginx.conf', $json['nginx']); - $this->ssh("nginx -s reload 2>&1", 'ng'); + $out[] = 'reset nginx'; + $this->update($this->input['chat'], $this->input['message_id'], implode("\n", $out)); + + $t = file_get_contents('/config/nginx_default.conf'); + if (!empty($json['pac']['domain'])) { + $t = preg_replace('/server_name([^\n]+)?/', "server_name {$json['pac']['domain']};", $t); + preg_match_all('~#-domain.+?#-domain~s', $t, $m); + foreach ($m[0] as $k => $v) { + $t = preg_replace('~#-domain.+?#-domain~s', $this->uncomment($v, 'domain'), $t, 1); + } } - $out[] = 'end import'; + if (!empty($json['ssl'])) { + $name = $json['pac']['letsencrypt'] ? 'letsencrypt' : 'self'; + $t = preg_replace('/#~([^\n]+)?/', "#~$name", $t); + preg_match_all('~#-ssl.+?#-ssl~s', $t, $m); + foreach ($m[0] as $k => $v) { + $t = preg_replace('~#-ssl.+?#-ssl~s', $this->uncomment($v, 'ssl'), $t, 1); + } + } + file_put_contents('/config/nginx.conf', $t); + $out[] = $this->ssh("nginx -s reload 2>&1", 'ng'); + $this->update($this->input['chat'], $this->input['message_id'], implode("\n", $out)); + + $out[] = "end import"; $this->update($this->input['chat'], $this->input['message_id'], implode("\n", $out)); sleep(3); $this->menu(); diff --git a/config/nginx_default.conf b/config/nginx_default.conf index 48de78a..40ce8f0 100644 --- a/config/nginx_default.conf +++ b/config/nginx_default.conf @@ -61,18 +61,18 @@ http { #~ -#~domain +#-domain # server { # listen 80; # server_name ; -#~domain -#~ssl +#-domain +#-ssl # listen 443 ssl http2; # ssl_certificate /certs/cert_public; # ssl_certificate_key /certs/cert_private; -#~ssl +#-ssl -#~domain +#-domain # access_log /logs/nginx_domain_access; # location / { @@ -119,7 +119,7 @@ http { # proxy_pass http://dohloop; # } # } -#~domain +#-domain # This upstream connects to a local Stream service which converts HTTP -> DNS upstream dohloop { @@ -154,11 +154,11 @@ stream { # Terminate DoT and DNS TCP, and proxy onto standard DNS server { listen 53; - #~ssl + #-ssl # listen 853 ssl; # ssl_certificate /certs/cert_public; # ssl_certificate_key /certs/cert_private; - #~ssl + #-ssl js_preread dns.preread_dns_request; proxy_pass dns; }