浏览代码

送信支持显示服务器信息

luolongfei 3 年之前
父节点
当前提交
be97c3167b

+ 4 - 1
.env.example

@@ -12,7 +12,7 @@
 #####################################################################
 
 # .env 文件版本
-ENV_FILE_VERSION='v2.3'
+ENV_FILE_VERSION='v2.4'
 
 ######################  账户配置 Account config  #########################
 # Freenom 账户 Freenom Account
@@ -135,3 +135,6 @@ NEW_VERSION_DETECTION=1
 
 # 应用语言配置 Application language configuration, the supported values are zh or en, zh means Chinese, en means English
 LANGUAGE=zh
+
+# 送信时是否显示服务器信息 1:显示 0:不显示 Whether to display server information when sending messages 1: Display 0: Do not display
+SHOW_SERVER_INFO=0

+ 1 - 3
app/Console/Upgrade.php

@@ -58,8 +58,6 @@ class Upgrade extends Base
     {
         $this->pushedVerFile = DATA_PATH . DS . 'pushed_version.txt';
 
-        $this->language = config('language', 'zh');
-
         $this->client = new Client([
             'base_uri' => 'https://api.github.com',
             'headers' => [
@@ -149,7 +147,7 @@ class Upgrade extends Base
      */
     public function genMsgContent()
     {
-        if ($this->language === 'zh') {
+        if (is_chinese()) {
             $content = sprintf(
                 lang('100025'),
                 $this->friendlyDateFormat($this->releaseInfo['published_at'], 'UTC'),

+ 25 - 1
app/helpers.php

@@ -299,4 +299,28 @@ if (!function_exists('get_local_num')) {
                 return $num . 'th';
         }
     }
-}
+}
+
+if (!function_exists('is_chinese')) {
+    /**
+     * 判断当前语言环境
+     *
+     * @return bool
+     */
+    function is_chinese()
+    {
+        return config('language', 'zh') === 'zh';
+    }
+}
+
+if (!function_exists('get_ip_info')) {
+    /**
+     * 获取 ip 信息
+     *
+     * @return string
+     */
+    function get_ip_info()
+    {
+        return \Luolongfei\Libs\IP::getInstance()->get();
+    }
+}

+ 21 - 0
libs/Connector/MessageGateway.php

@@ -50,4 +50,25 @@ abstract class MessageGateway implements MessageServiceInterface
     {
         return preg_replace("/\n/u", '<br>', $content);
     }
+
+    /**
+     * 设置公共页脚
+     *
+     * @param $footer
+     * @param $newline
+     * @param $enable
+     *
+     * @return void
+     */
+    public function setCommonFooter(&$footer, $newline = "\n", $enable = true)
+    {
+        if (env('SHOW_SERVER_INFO')) {
+            $footer .= $newline . $newline . lang('100134');
+            $footer .= $newline . get_ip_info();
+        }
+
+        if ($enable) {
+            $footer .= $newline . $newline . lang('100133');
+        }
+    }
 }

+ 108 - 0
libs/IP.php

@@ -0,0 +1,108 @@
+<?php
+/**
+ * IP 信息
+ *
+ * @author luolongf <[email protected]>
+ * @date 2022-05-14
+ * @time 8:28
+ */
+
+namespace Luolongfei\Libs;
+
+use GuzzleHttp\Client;
+
+class IP extends Base
+{
+    const TIMEOUT = 2.14;
+
+    /**
+     * 匹配 ip 的正则
+     */
+    const REGEX_IP = '/(?:\d{1,3}\.){3}\d{1,3}/u';
+
+    const REGEX_LOC = '/^.*:(?P<country>[^\s]+?)\s+?(?P<region>[^\s]+?)\s+?(?P<city>[^\s]+?)\s/iu';
+
+    /**
+     * @var string ip 地址
+     */
+    public static $ip = '';
+
+    /**
+     * @var string 位置信息
+     */
+    public static $loc = '';
+
+    /**
+     * @var Client
+     */
+    protected $client;
+
+    /**
+     * @var string 用于查询 ip 的地址
+     */
+    protected $url;
+
+    public function init()
+    {
+        $this->client = new Client([
+            'headers' => [
+                'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36',
+            ],
+            'cookies' => false,
+            'timeout' => self::TIMEOUT,
+            'verify' => config('verify_ssl'),
+            'debug' => config('debug'),
+        ]);
+
+        $this->url = is_chinese() ? 'https://myip.ipip.net' : 'https://ipinfo.io/json';
+    }
+
+    /**
+     * @return string
+     * @throws \GuzzleHttp\Exception\GuzzleException
+     */
+    public function get()
+    {
+        try {
+            if (!self::$ip && !self::$loc) {
+                $res = $this->client->get($this->url);
+                $body = $res->getBody()->getContents();
+
+                $this->matchIpInfo($body);
+            }
+
+            return sprintf(lang('100130'), self::$ip, self::$loc);
+        } catch (\Exception $e) {
+            Log::error(lang('100132') . $e->getMessage());
+
+            return lang('100131');
+        }
+    }
+
+    /**
+     * 匹配 ip 信息
+     *
+     * @param $body
+     *
+     * @return bool
+     */
+    protected function matchIpInfo($body)
+    {
+        if (is_chinese()) {
+            if (preg_match(self::REGEX_IP, $body, $m)) {
+                self::$ip = $m[0];
+            }
+            if (preg_match(self::REGEX_LOC, $body, $m)) {
+                self::$loc = sprintf('%s %s %s', $m['country'], $m['region'], $m['city']);
+            }
+
+            return true;
+        }
+
+        $body = (array)json_decode($body, true);
+        self::$ip = $body['ip'] ?? '';
+        self::$loc = sprintf('%s %s %s', $body['country'] ?? '', $body['region'] ?? '', $body['city'] ?? '');
+
+        return true;
+    }
+}

+ 1 - 3
libs/MessageServices/Bark.php

@@ -150,9 +150,7 @@ class Bark extends MessageGateway
 
         $footer .= lang('100078');
 
-        if (!$isRenewalResult) {
-            $footer .= lang('100079');
-        }
+        $this->setCommonFooter($footer, "\n", !$isRenewalResult);
 
         return $footer;
     }

+ 6 - 1
libs/MessageServices/Mail.php

@@ -213,9 +213,14 @@ class Mail extends MessageGateway
             $message = $this->genMessageContent($realData, $template);
         } else if ($type === 3) {
             $template = file_get_contents($this->noRenewalRequiredTemplatePath);
+
+            $footer = '';
+            $this->setCommonFooter($footer, '<br>');
+
             $realData = [
                 $data['username'],
-                $this->genDomainStatusHtml($data['domainStatusArr'])
+                $this->genDomainStatusHtml($data['domainStatusArr']),
+                $footer
             ];
             $message = $this->genMessageContent($realData, $template);
         } else if ($type === 4) {

+ 1 - 3
libs/MessageServices/ServerChan.php

@@ -70,9 +70,7 @@ class ServerChan extends MessageGateway
 
         $footer .= lang('100091');
 
-        if (!$isRenewalResult) {
-            $footer .= lang('100092');
-        }
+        $this->setCommonFooter($footer, "\n", !$isRenewalResult);
 
         return $footer;
     }

+ 1 - 3
libs/MessageServices/TelegramBot.php

@@ -103,9 +103,7 @@ class TelegramBot extends MessageGateway
 
         $footer .= lang('100103');
 
-        if (!$isRenewalResult) {
-            $footer .= lang('100104');
-        }
+        $this->setCommonFooter($footer, "\n", !$isRenewalResult);
 
         return $footer;
     }

+ 1 - 3
libs/MessageServices/WeChat.php

@@ -162,9 +162,7 @@ class WeChat extends MessageGateway
 
         $footer .= lang('100116');
 
-        if (!$isRenewalResult) {
-            $footer .= lang('100117');
-        }
+        $this->setCommonFooter($footer, "\n", !$isRenewalResult);
 
         return $footer;
     }

+ 5 - 4
resources/lang/en.php

@@ -102,7 +102,6 @@ return [
         '100076' => 'Renewal Error: %s<br>',
         '100077' => 'Mail sending failure: ',
         '100078' => "\nFor further information, please visit https://my.freenom.com/domains.php?a=renewals (click \"copy content\" to copy this URL)",
-        '100079' => "\n\n(If you don't want to get a push every time you execute, adjust the value of NOTICE_FREQ in the .env file to 0 so that the application only pushes when there is a renewal operation.)",
         '100080' => "No data.\n",
         '100081' => '%s expires in %d days, ',
         '100082' => ".\n",
@@ -115,7 +114,6 @@ return [
         '100089' => 'Bark Failed to send message: <red>%s</red>',
         '100090' => "I've just checked and found that the Freenom account [%s](#) does not have any domains that need to be renewed today. The following is the current state of all domains:\n\n",
         '100091' => "\nFor more information, please visit [Freenom’s official website](https://my.freenom.com/domains.php?a=renewals)",
-        '100092' => "\n\n(If you don't want to get a push every time you execute, adjust the value of NOTICE_FREQ in the .env file to 0 so that the application only pushes when there is a renewal operation.)",
         '100093' => "No data.\n",
         '100094' => '[%s](http://%s) expires in *%d* days, ',
         '100095' => ".\n",
@@ -127,7 +125,6 @@ return [
         '100101' => 'ServerChan Failed to send message: <red>%s</red>',
         '100102' => "I've just checked and found that the Freenom account %s does not have any domains that need to be renewed today. The following is the current state of all domains:\n\n",
         '100103' => "\nFor further information, please visit [Freenom’s official website](https://my.freenom.com/domains.php?a=renewals)",
-        '100104' => "\n\n(If you don't want to get a push every time you execute, adjust the value of NOTICE_FREQ in the .env file to 0 so that the application only pushes when there is a renewal operation.)",
         '100105' => "No data.\n",
         '100106' => '[%s](http://%s) expires in *%d* days, ',
         '100107' => ".\n",
@@ -140,7 +137,6 @@ return [
         '100114' => 'Get Corporate WeChat access_token Failed: ',
         '100115' => 'No known reason',
         '100116' => "\nFor further information, please visit <a href=\"https://my.freenom.com/domains.php?a=renewals\">Freenom’s official website</a>",
-        '100117' => "\n\n(If you don't want to get a push every time you execute, adjust the value of NOTICE_FREQ in the .env file to 0 so that the application only pushes when there is a renewal operation.)",
         '100118' => "No data.\n",
         '100119' => '<a href="http://%s">%s</a> expires in <a href="http://%s">%d</a> days, ',
         '100120' => ".\n",
@@ -153,5 +149,10 @@ return [
         '100127' => 'Corporate WeChat interface did not return the expected data response, this response data is: ',
         '100128' => 'Multiple invalid access_token prompts have been detected, possibly because the access_token was not properly obtained, please intervene to investigate: ',
         '100129' => 'Warning: <light_yellow>%s</light_yellow>',
+        '100130' => 'IP: %s  Loc: %s',
+        '100131' => 'Unknown',
+        '100132' => 'get ip info error:',
+        '100133' => "(If you don't want to get a push every time you execute, adjust the value of NOTICE_FREQ in the .env file to 0 so that the application only pushes when there is a renewal operation.)",
+        '100134' => '[Server Info]',
     ],
 ];

+ 5 - 4
resources/lang/zh.php

@@ -102,7 +102,6 @@ return [
         '100076' => '续期出错:%s<br>',
         '100077' => '邮件发送失败:',
         '100078' => "\n更多信息可以参考:https://my.freenom.com/domains.php?a=renewals(点击“复制内容”即可复制此网址)",
-        '100079' => "\n\n(如果你不想每次执行都收到推送,请将 .env 中 NOTICE_FREQ 的值设为 0,使程序只在有续期操作时才推送)",
         '100080' => "无数据。\n",
         '100081' => '%s 还有 %d 天到期,',
         '100082' => "。\n",
@@ -115,7 +114,6 @@ return [
         '100089' => 'Bark 送信失败:<red>%s</red>',
         '100090' => "我刚刚帮小主看了一下,账户 [%s](#) 今天并没有需要续期的域名。所有域名情况如下:\n\n",
         '100091' => "\n更多信息可以参考 [Freenom官网](https://my.freenom.com/domains.php?a=renewals) 哦~",
-        '100092' => "\n\n(如果你不想每次执行都收到推送,请将 .env 中 NOTICE_FREQ 的值设为 0,使程序只在有续期操作时才推送)",
         '100093' => "无数据。\n",
         '100094' => '[%s](http://%s) 还有 *%d* 天到期,',
         '100095' => "。\n",
@@ -127,7 +125,6 @@ return [
         '100101' => 'Server酱 消息发送失败:<red>%s</red>',
         '100102' => "我刚刚帮小主看了一下,账户 %s 今天并没有需要续期的域名。所有域名情况如下:\n\n",
         '100103' => "\n更多信息可以参考 [Freenom官网](https://my.freenom.com/domains.php?a=renewals) 哦~",
-        '100104' => "\n\n(如果你不想每次执行都收到推送,请将 .env 中 NOTICE_FREQ 的值设为 0,使程序只在有续期操作时才推送)",
         '100105' => "无数据。\n",
         '100106' => '[%s](http://%s) 还有 *%d* 天到期,',
         '100107' => "。\n",
@@ -140,7 +137,6 @@ return [
         '100114' => '获取企业微信 access_token 失败:',
         '100115' => '未知原因',
         '100116' => "\n更多信息可以参考 <a href=\"https://my.freenom.com/domains.php?a=renewals\">Freenom官网</a> 哦~",
-        '100117' => "\n\n(如果你不想每次执行都收到推送,请将 .env 中 NOTICE_FREQ 的值设为 0,使程序只在有续期操作时才推送)",
         '100118' => "无数据。\n",
         '100119' => '<a href="http://%s">%s</a> 还有 <a href="http://%s">%d</a> 天到期,',
         '100120' => "。\n",
@@ -153,5 +149,10 @@ return [
         '100127' => '企业微信接口未返回预期的数据响应,本次响应数据为:',
         '100128' => '检测到多次提示 access_token 失效,可能是未能正确获取 access_token,请介入调查:',
         '100129' => '警告:<light_yellow>%s</light_yellow>',
+        '100130' => 'IP:%s  位于:%s',
+        '100131' => '未知',
+        '100132' => '获取 ip 信息出错:',
+        '100133' => '(如果你不想每次执行都收到推送,请将 .env 中 NOTICE_FREQ 的值设为 0,使程序只在有续期操作时才推送)',
+        '100134' => '【服务器信息】',
     ],
 ];

+ 1 - 3
resources/mail/en/no_renewal_required.html

@@ -355,9 +355,7 @@
                         For more information, please visit <a href="https://my.freenom.com/domains.php?a=renewals"
                                                               target="_blank" rel="noopener">Freenom’s official
                         website</a>
-                        <br>
-                        <br>(If you don't want to get a push every time you execute, adjust the value of NOTICE_FREQ in
-                        the .env file to 0 so that the application only pushes when there is a renewal operation.)
+                        %s
                     </p>
                 </div>
                 <div class="mmsgLetterInscribe" style="padding:40px 0 0;">

+ 1 - 2
resources/mail/zh/no_renewal_required.html

@@ -344,8 +344,7 @@
                         <br>
                         <br><span class="domainDays">%s</span>
                         <br><br>更多信息可以参考 <a href="https://my.freenom.com/domains.php?a=renewals" target="_blank" rel="noopener">Freenom官网</a> 哦~
-                        <br>
-                        <br>(如果你不想每次执行都收到推送,请将 .env 中 NOTICE_FREQ 的值设为 0,使程序只在有续期操作时才推送)
+                        %s
                     </p>
                 </div>
                 <div class="mmsgLetterInscribe" style="padding:40px 0 0;">