Skip to content

Commit 3e76d37

Browse files
committed
支持新版检测;freenom 支持使用代理访问
1 parent 386633f commit 3e76d37

16 files changed

+309
-18
lines changed

.env.example

+10-4
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@
1212
#####################################################################
1313

1414
# .env 文件版本
15-
ENV_FILE_VERSION='v1'
15+
ENV_FILE_VERSION='v2'
1616

1717
###################### 账户配置 Account config #########################
18-
# Freenom账户 Freenom Account
18+
# Freenom 账户 Freenom Account
1919
FREENOM_USERNAME=[email protected]
2020

21-
# Freenom密码 Freenom password
21+
# Freenom 密码 Freenom password
2222
FREENOM_PASSWORD=''
2323

2424
# 多账户支持 Support for multiple accounts
2525
MULTIPLE_ACCOUNTS=''
26+
27+
# Freenom 代理 e.g. http://127.0.0.1:1081 or socks5://127.0.0.1:1080
28+
FREENOM_PROXY=''
2629
###################### end 账户配置 #########################
2730

2831
###################### 通知邮件配置 Email config #########################
@@ -64,7 +67,7 @@ TELEGRAM_CHAT_ID=''
6467
# 你的Telegram bot的token Token for your Telegram bot
6568
TELEGRAM_BOT_TOKEN=''
6669

67-
# Telegram 代理 e.g. http://127.0.0.1:1081
70+
# Telegram 代理 e.g. http://127.0.0.1:1081 or socks5://127.0.0.1:1080
6871
TELEGRAM_PROXY=''
6972

7073
# 是否启用 Telegram Bot 功能 1:启用 0:不启用 Whether to enable Telegram Bot features 1: enabled 0: not enabled
@@ -119,3 +122,6 @@ VERIFY_SSL=0
119122

120123
# 是否开启 Debug 模式 1:开启 0:关闭 Whether to turn on Debug mode 1: On 0: Off
121124
DEBUG=0
125+
126+
# 检测是否有新版本可用,发现新版本时推送消息通知 1:开启 0:关闭 Automatically detects if a new version is available and pushes a message notification when a new version is found 1: On 0: Off
127+
NEW_VERSION_DETECTION=1

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ app/tmp/
33
app/num_limit/
44
.idea/
55
app/Data/
6+
.env.old

app/Console/FreeNom.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
class FreeNom extends Base
2020
{
21-
const VERSION = 'v0.4.1';
21+
const VERSION = 'v0.4.2';
2222

2323
const TIMEOUT = 33;
2424

@@ -89,7 +89,8 @@ private function __construct()
8989
CURLOPT_FOLLOWLOCATION => true,
9090
CURLOPT_AUTOREFERER => true,
9191
'verify' => config('verify_ssl'),
92-
'debug' => config('debug')
92+
'debug' => config('debug'),
93+
'proxy' => config('freenom_proxy'),
9394
]);
9495

9596
system_log(sprintf('当前程序版本 %s', self::VERSION));
@@ -268,7 +269,7 @@ public function renewAllDomains(array $allDomains, string $token)
268269
system_log('当前通知频率为「仅当有续期操作时」,故本次不会推送通知');
269270
}
270271

271-
system_log(sprintf('%s:<green>执行成功,今次没有需要续期的域名</green>', $this->username));
272+
system_log(sprintf('%s:<green>执行成功,今次没有需要续期的域名</green>', $this->username));
272273

273274
return true;
274275
}
@@ -408,7 +409,7 @@ public function handle()
408409
{
409410
$accounts = $this->getAccounts();
410411

411-
system_log(sprintf('共发现 <green>%d</green> 个账户,正在处理...', count($accounts)));
412+
system_log(sprintf('共发现 <green>%d</green> 个账户,处理中', count($accounts)));
412413

413414
foreach ($accounts as $account) {
414415
try {

app/Console/Upgrade.php

+251
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
<?php
2+
/**
3+
* 升级
4+
*
5+
* @author mybsdc <[email protected]>
6+
* @date 2021/11/5
7+
* @time 10:52
8+
*/
9+
10+
namespace Luolongfei\App\Console;
11+
12+
use GuzzleHttp\Client;
13+
use Luolongfei\Libs\Log;
14+
use Luolongfei\Libs\Message;
15+
16+
class Upgrade extends Base
17+
{
18+
const TIMEOUT = 33;
19+
20+
/**
21+
* @var Upgrade
22+
*/
23+
private static $instance;
24+
25+
/**
26+
* @var array 与发布相关的信息
27+
*/
28+
public $releaseInfo = [];
29+
30+
/**
31+
* @var string 最新版本号
32+
*/
33+
public $latestVer;
34+
35+
/**
36+
* @var string 当前版本号
37+
*/
38+
public $currVer;
39+
40+
/**
41+
* @var string 记录已推送版本的文件
42+
*/
43+
public $pushedVerFile;
44+
45+
/**
46+
* @return Upgrade
47+
*/
48+
public static function getInstance()
49+
{
50+
if (!self::$instance instanceof self) {
51+
self::$instance = new self();
52+
}
53+
54+
return self::$instance;
55+
}
56+
57+
private function __construct()
58+
{
59+
$this->pushedVerFile = DATA_PATH . DS . 'pushed_version.txt';
60+
61+
$this->client = new Client([
62+
'base_uri' => 'https://api.github.com',
63+
'headers' => [
64+
'Accept' => 'application/vnd.github.v3+json'
65+
],
66+
'cookies' => false,
67+
'timeout' => self::TIMEOUT,
68+
'verify' => config('verify_ssl'),
69+
'debug' => config('debug'),
70+
]);
71+
}
72+
73+
private function __clone()
74+
{
75+
}
76+
77+
/**
78+
* 是否需要升级
79+
*
80+
* @return bool
81+
*/
82+
public function needToUpgrade()
83+
{
84+
try {
85+
$resp = $this->client->get('/repos/luolongfei/freenom/releases/latest', [
86+
'timeout' => 5,
87+
]);
88+
89+
$resp = $resp->getBody()->getContents();
90+
$resp = (array)json_decode($resp, true);
91+
92+
if (!isset($resp['tag_name'])
93+
|| !isset($resp['body'])
94+
|| !isset($resp['name'])
95+
|| !isset($resp['published_at'])
96+
|| !isset($resp['html_url'])) {
97+
throw new \Exception('Github 返回的数据与预期不一致:' . json_encode($resp, JSON_UNESCAPED_UNICODE));
98+
}
99+
100+
$this->releaseInfo = $resp;
101+
102+
$this->latestVer = $this->getVerNum($resp['tag_name']);
103+
$this->currVer = $this->getVerNum(FreeNom::VERSION);
104+
105+
return version_compare($this->latestVer, $this->currVer, '>');
106+
} catch (\Exception $e) {
107+
Log::error('检测升级出错:' . $e->getMessage());
108+
109+
return false;
110+
}
111+
}
112+
113+
/**
114+
* 此版本是否已推送过
115+
*
116+
* @param $ver
117+
*
118+
* @return bool
119+
*/
120+
public function isPushed($ver)
121+
{
122+
if (!file_exists($this->pushedVerFile)) {
123+
return false;
124+
}
125+
126+
$pushedVerFile = file_get_contents($this->pushedVerFile);
127+
128+
return stripos($pushedVerFile, $ver) !== false;
129+
}
130+
131+
/**
132+
* 记住版本号
133+
*
134+
* @param $ver
135+
*
136+
* @return bool
137+
*/
138+
public function rememberVer($ver)
139+
{
140+
return (bool)file_put_contents($this->pushedVerFile, $ver . "\n", FILE_APPEND);
141+
}
142+
143+
/**
144+
* 生成升级送信内容
145+
*
146+
* @return string
147+
*/
148+
public function genMsgContent()
149+
{
150+
$content = sprintf(
151+
"见信好,我们在 %s 发布了新版 FreeNom 续期工具 v%s,而你当前正在使用的版本为 v%s,你可以根据自己的实际需要决定是否升级到新版本。今次新版有以下更新或改进:\n\n",
152+
$this->friendlyDateFormat($this->releaseInfo['published_at'], 'UTC'),
153+
$this->latestVer,
154+
$this->currVer
155+
);
156+
157+
$content .= $this->releaseInfo['body'];
158+
159+
$content .= "\n\n" . '欲知更多信息,请访问:' . $this->releaseInfo['html_url'];
160+
161+
$content .= "\n\n" . '(本消息针对同一个新版只会推送一次,如果你不想收到新版本通知,将 .env 文件中的 NEW_VERSION_DETECTION 的值设为 0 即可)';
162+
163+
return $content;
164+
}
165+
166+
/**
167+
* 人类友好时间
168+
*
169+
* @param string $date 传入时间
170+
* @param null|string $timezone 传入时间的时区
171+
*
172+
* @return string
173+
*/
174+
public function friendlyDateFormat($date, $timezone = null)
175+
{
176+
try {
177+
$d = (new \DateTime($date, $timezone ? new \DateTimeZone($timezone) : null))->setTimezone(new \DateTimeZone('Asia/Shanghai'));
178+
179+
$time = $d->getTimestamp();
180+
$diff = time() - $time;
181+
182+
if ($diff < 86400) {
183+
if ($d->format('d') === date('d')) {
184+
return $diff < 60 ? '刚刚' : ($diff < 3600 ? floor($diff / 60) . '分钟前' : floor($diff / 3600) . '小时前');
185+
} else {
186+
return '昨天 ' . $d->format('H:i');
187+
}
188+
} else {
189+
return $d->format($d->format('Y') === date('Y') ? 'Y-m-d H:i' : 'Y-m-d');
190+
}
191+
} catch (\Exception $e) {
192+
Log::error('转人类友好时间出错:' . $e->getMessage());
193+
194+
return $date;
195+
}
196+
}
197+
198+
/**
199+
* @return bool
200+
*/
201+
public function handle()
202+
{
203+
try {
204+
if (!$this->needToUpgrade()) {
205+
return true;
206+
}
207+
208+
if ($this->isPushed($this->latestVer)) {
209+
return true;
210+
}
211+
212+
if (IS_SCF) {
213+
system_log(sprintf(
214+
'FreeNom 续期工具有新的版本可用,你当前版本为 v%s,最新版本为 v%s。关于新版的详细信息,请访问:%s',
215+
$this->currVer,
216+
$this->latestVer,
217+
$this->releaseInfo['html_url']
218+
));
219+
} else {
220+
system_log(sprintf(
221+
'<green>FreeNom 续期工具有新的版本可用,最新版本为 v%s(%s)</green>',
222+
$this->latestVer,
223+
$this->releaseInfo['html_url']
224+
));
225+
226+
$result = Message::send(
227+
$this->genMsgContent(),
228+
sprintf('主人,FreeNom 续期工具有新的版本(v%s)可用,新版相关情况已给到你', $this->latestVer),
229+
4
230+
);
231+
232+
if ($result) {
233+
$this->rememberVer($this->latestVer);
234+
system_log('有关新版的信息已送信给到你,请注意查收。');
235+
}
236+
}
237+
238+
return true;
239+
} catch (\Exception $e) {
240+
system_log('升级出错:' . $e->getMessage());
241+
242+
return false;
243+
}
244+
}
245+
246+
public function doUpgrade()
247+
{
248+
// TODO 自动升级
249+
// system_log('<green>恭喜,已完成升级。</green>');
250+
}
251+
}

app/helpers.php

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Luolongfei\Libs\Lang;
1616
use Luolongfei\Libs\PhpColor;
1717
use Luolongfei\App\Console\MigrateEnvFile;
18+
use Luolongfei\App\Console\Upgrade;
1819

1920
if (!function_exists('config')) {
2021
/**
@@ -251,6 +252,13 @@ function system_check()
251252
MigrateEnvFile::getInstance()->handle();
252253
}
253254

255+
// 是否有新版可用
256+
if (config('new_version_detection')) {
257+
Upgrade::getInstance()->handle();
258+
} else {
259+
system_log('由于你没有开启升级提醒功能,故无法在有新版本可用时第一时间收到通知。将 .env 文件中 NEW_VERSION_DETECTION 的值改为 1 即可重新开启相关功能。');
260+
}
261+
254262
if (!extension_loaded('curl')) {
255263
throw new LlfException(34520010);
256264
}

config.php

+2
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,6 @@
104104
'notice_freq' => (int)env('NOTICE_FREQ', 1), // 通知频率 0:仅当有续期操作的时候 1:每次执行
105105
'verify_ssl' => (bool)env('VERIFY_SSL'), // 请求时验证 SSL 证书行为,默认不验证,防止服务器证书过期或证书颁布者信息不全导致无法发出请求
106106
'debug' => (bool)env('DEBUG'),
107+
'freenom_proxy' => env('FREENOM_PROXY') ?: null, // FreeNom 代理,针对国内网络情况,可选择代理访问
108+
'new_version_detection' => (bool)env('NEW_VERSION_DETECTION'),
107109
];

index.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function customize_error_handler()
5151
{
5252
if (!is_null($error = error_get_last())) {
5353
Log::error('程序意外终止', $error);
54-
Message::send('具体情况我也不清楚,请查看服务器日志定位问题。', '主人,程序意外终止');
54+
Message::send('可能存在错误,这边收集到的错误信息为:' . json_encode($error, JSON_UNESCAPED_UNICODE), '主人,程序意外终止');
5555
}
5656
}
5757

libs/Connector/MessageGateway.php

+12
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,16 @@ public function check(string $content, array $data)
4242
throw new \Exception(lang('error_msg.100004'));
4343
}
4444
}
45+
46+
/**
47+
* 换行转 <br>
48+
*
49+
* @param string $content
50+
*
51+
* @return string
52+
*/
53+
public function newLine2Br(string $content)
54+
{
55+
return preg_replace("/\n/u", '<br>', $content);
56+
}
4557
}

libs/Connector/MessageServiceInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface MessageServiceInterface
1818
*
1919
* @param string $content
2020
* @param string $subject
21-
* @param integer $type 消息类型 1:普通消息 2:域名续期结果 3:无需续期,域名状态信件
21+
* @param integer $type 消息类型 1:普通消息 2:域名续期结果 3:无需续期,域名状态信件 4:升级通知
2222
* @param array $data
2323
* @param string|null $recipient
2424
* @param ...$params

0 commit comments

Comments
 (0)