cron backup
This commit is contained in:
+135
-6
@@ -63,7 +63,7 @@ class Bot
|
||||
$c['admin'] = [$c['admin']];
|
||||
file_put_contents($file, "<?php\n\n\$c = " . var_export($c, true) . ";\n");
|
||||
} elseif (!in_array($this->input['from'], $c['admin'])) {
|
||||
$this->send($this->input['chat'], 'you are not authorized', $this->input['message_id']);
|
||||
// $this->send($this->input['chat'], 'you are not authorized', $this->input['message_id']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
@@ -127,6 +127,9 @@ class Bot
|
||||
case preg_match('~^/debug$~', $this->input['callback'], $m):
|
||||
$this->debug();
|
||||
break;
|
||||
case preg_match('~^/backup$~', $this->input['callback'], $m):
|
||||
$this->backup();
|
||||
break;
|
||||
case preg_match('~^/generateSecret$~', $this->input['callback'], $m):
|
||||
$this->generateSecret();
|
||||
break;
|
||||
@@ -276,7 +279,7 @@ class Bot
|
||||
$this->showpac();
|
||||
break;
|
||||
case preg_match('~^/export$~', $this->input['callback'], $m):
|
||||
$this->export();
|
||||
$this->exportManual();
|
||||
break;
|
||||
case preg_match('~^/import$~', $this->input['callback'], $m):
|
||||
$this->import();
|
||||
@@ -554,10 +557,54 @@ class Bot
|
||||
while (true) {
|
||||
$this->shutdownClient();
|
||||
$this->checkVersion();
|
||||
$this->checkBackup();
|
||||
sleep(10);
|
||||
}
|
||||
}
|
||||
|
||||
public function checkBackup()
|
||||
{
|
||||
$c = time();
|
||||
$conf = $this->getPacConf();
|
||||
$time = $conf['backup'];
|
||||
if ($time) {
|
||||
preg_match('~(\d+\s\w+)(?:\s+)?/(?:\s+)?(\d{2}:\d{2})~', $time, $m);
|
||||
$period = strtotime($m[1]) - $c;
|
||||
$start = strtotime($m[2]);
|
||||
$last = $conf['pinbackup'];
|
||||
if ($last) {
|
||||
[$pin, $time] = explode('/', $last);
|
||||
if ($c - $time >= $period) {
|
||||
$this->pinAdmin($pin, 1);
|
||||
$this->pinBackup();
|
||||
return;
|
||||
}
|
||||
} elseif ($c - $start > 0 && $c - $start < 30) {
|
||||
$this->pinBackup();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function pinAdmin($pin, $unpin = false)
|
||||
{
|
||||
require __DIR__ . '/config.php';
|
||||
if ($unpin) {
|
||||
return $this->unpin($c['admin'][0], $pin);
|
||||
} else {
|
||||
return $this->pin($c['admin'][0], $pin);
|
||||
}
|
||||
}
|
||||
|
||||
public function pinBackup()
|
||||
{
|
||||
require __DIR__ . '/config.php';
|
||||
$conf = $this->getPacConf();
|
||||
$pin = $this->upload('vpnbot_export_' . date('d_m_Y_H_i') . '.json', $this->export(), $c['admin'][0])['result']['message_id'];
|
||||
$conf['pinbackup'] = "$pin/" . time();
|
||||
$this->setPacConf($conf);
|
||||
$this->pinAdmin($pin);
|
||||
}
|
||||
|
||||
public function checkVersion()
|
||||
{
|
||||
try {
|
||||
@@ -673,7 +720,28 @@ class Bot
|
||||
'mtproto' => file_get_contents('/config/mtprotosecret'),
|
||||
|
||||
];
|
||||
$this->upload('vpnbot_export_' . date('d_m_Y_H_i') . '.json', json_encode($conf, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||||
return json_encode($conf, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
|
||||
public function exportManual()
|
||||
{
|
||||
$conf = [
|
||||
'wg' => [
|
||||
'server' => $this->readConfig(),
|
||||
'clients' => json_decode(file_get_contents($this->clients), true) ?: [],
|
||||
],
|
||||
'ss' => $this->getSSConfig(),
|
||||
'sl' => $this->getSSLocalConfig(),
|
||||
'ad' => yaml_parse_file('/config/adguard/AdGuardHome.yaml'),
|
||||
'pac' => $this->getPacConf(),
|
||||
'ssl' => [
|
||||
'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,
|
||||
],
|
||||
'mtproto' => file_get_contents('/config/mtprotosecret'),
|
||||
|
||||
];
|
||||
return $this->upload('vpnbot_export_' . date('d_m_Y_H_i') . '.json', $this->export());
|
||||
}
|
||||
|
||||
public function import()
|
||||
@@ -888,15 +956,16 @@ class Bot
|
||||
}
|
||||
}
|
||||
|
||||
public function upload($name, $code)
|
||||
public function upload($name, $code, $chat = false)
|
||||
{
|
||||
$path = "/logs/$name";
|
||||
file_put_contents($path, $code);
|
||||
$this->sendFile(
|
||||
$this->input['chat'],
|
||||
$r = $this->sendFile(
|
||||
$chat ?: $this->input['chat'],
|
||||
curl_file_create($path),
|
||||
);
|
||||
unlink($path);
|
||||
return $r;
|
||||
}
|
||||
|
||||
public function proxy()
|
||||
@@ -2662,6 +2731,12 @@ DNS-over-HTTPS with IP:
|
||||
'callback_data' => "/export",
|
||||
],
|
||||
];
|
||||
$data[] = [
|
||||
[
|
||||
'text' => $this->i18n('backup') . ': ' . (implode(' / ', explode('/', $conf['backup'])) ?: 'off'),
|
||||
'callback_data' => "/backup",
|
||||
],
|
||||
];
|
||||
$data[] = [
|
||||
[
|
||||
'text' => $this->i18n($conf['blinkmenu'] ? 'blinkmenuon' : 'blinkmenuoff'),
|
||||
@@ -2686,6 +2761,41 @@ DNS-over-HTTPS with IP:
|
||||
];
|
||||
}
|
||||
|
||||
public function backup()
|
||||
{
|
||||
$r = $this->send(
|
||||
$this->input['chat'],
|
||||
"@{$this->input['username']} enter like 1 day/00:00",
|
||||
$this->input['message_id'],
|
||||
reply: 'enter like 1 day/00:00',
|
||||
);
|
||||
$_SESSION['reply'][$r['result']['message_id']] = [
|
||||
'start_message' => $this->input['message_id'],
|
||||
'start_callback' => $this->input['callback_id'],
|
||||
'callback' => 'setBackup',
|
||||
'args' => [],
|
||||
];
|
||||
}
|
||||
|
||||
public function setBackup($text)
|
||||
{
|
||||
$text = trim($text);
|
||||
$c = $this->getPacConf();
|
||||
if (empty($text)) {
|
||||
$c['backup'] = '';
|
||||
} elseif (preg_match('~(\d+\s\w+)(?:\s+)?/(?:\s+)?(\d{2}:\d{2})~', $text, $m)) {
|
||||
$period = $m[1];
|
||||
$start = $m[2];
|
||||
$c['backup'] = $text;
|
||||
}
|
||||
if ($pin = explode('/', $c['pinbackup'])[0]) {
|
||||
$this->pinAdmin($pin, 1);
|
||||
$c['pinbackup'] = '';
|
||||
}
|
||||
$this->setPacConf($c);
|
||||
$this->menu('config');
|
||||
}
|
||||
|
||||
public function debug()
|
||||
{
|
||||
$file = __DIR__ . '/config.php';
|
||||
@@ -3127,4 +3237,23 @@ DNS-over-HTTPS with IP:
|
||||
];
|
||||
return $this->request('deleteMessage', $data);
|
||||
}
|
||||
|
||||
public function pin($chat, $message_id, $notnotify = true)
|
||||
{
|
||||
$data = [
|
||||
'chat_id' => $chat,
|
||||
'message_id' => $message_id,
|
||||
'disable_notification' => $notnotify,
|
||||
];
|
||||
return $this->request('pinChatMessage', $data);
|
||||
}
|
||||
|
||||
public function unpin($chat, $message_id)
|
||||
{
|
||||
$data = [
|
||||
'chat_id' => $chat,
|
||||
'message_id' => $message_id,
|
||||
];
|
||||
return $this->request('unpinChatMessage', $data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,4 +241,8 @@ $i = [
|
||||
'en' => 'debug mode',
|
||||
'ru' => 'режим отладки',
|
||||
],
|
||||
'backup' => [
|
||||
'en' => 'backup',
|
||||
'ru' => 'бэкап',
|
||||
],
|
||||
];
|
||||
|
||||
@@ -6,11 +6,11 @@ run apt update && \
|
||||
apt install -y git net-tools lsof ssh wget && \
|
||||
apt clean autoclean && \
|
||||
apt autoremove -y && \
|
||||
wget https://github.com/AdguardTeam/AdGuardHome/releases/download/v0.107.31/AdGuardHome_linux_amd64.tar.gz && \
|
||||
wget https://github.com/ameshkov/dnslookup/releases/download/v1.8.1/dnslookup-linux-amd64-v1.8.1.tar.gz && \
|
||||
tar -xf dnslookup-linux-amd64-v1.8.1.tar.gz
|
||||
run wget https://github.com/AdguardTeam/AdGuardHome/releases/download/v0.107.31/AdGuardHome_linux_amd64.tar.gz && \
|
||||
tar -xf AdGuardHome_linux_amd64.tar.gz && \
|
||||
mkdir -p /opt/adguardhome && \
|
||||
mkdir /root/.ssh && \
|
||||
wget https://github.com/ameshkov/dnslookup/releases/download/v1.8.1/dnslookup-linux-amd64-v1.8.1.tar.gz && \
|
||||
tar -xf dnslookup-linux-amd64-v1.8.1.tar.gz
|
||||
copy config/AdGuardHome.yaml /opt/adguardhome/AdGuardHome.yaml
|
||||
env PATH="$PATH:/linux-amd64"
|
||||
|
||||
Reference in New Issue
Block a user