From 9032ea0405de41a1cdc06734fc0cd5050c3fe674 Mon Sep 17 00:00:00 2001 From: net909 Date: Mon, 28 Jul 2025 20:30:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E5=90=88=E8=AE=A1=E5=88=92=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=EF=BC=8C=E6=96=B0=E5=A2=9E=E8=AE=BF=E9=97=AEURL?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E7=9A=84=E8=AE=A1=E5=88=92=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/command/Certtask.php | 11 ++- app/command/Opiptask.php | 33 -------- app/controller/Optimizeip.php | 3 + app/controller/System.php | 37 ++++++++ app/service/CertTaskService.php | 19 +++-- app/service/ExpireNoticeService.php | 2 + app/service/OptimizeService.php | 10 +++ app/utils/CheckUtils.php | 6 +- app/view/cert/certset.html | 10 +-- app/view/common/layout.html | 5 +- app/view/dmonitor/overview.html | 2 +- app/view/domain/expire_notice.html | 8 +- app/view/optimizeip/opipset.html | 19 ++++- app/view/system/cronset.html | 126 ++++++++++++++++++++++++++++ config/app.php | 2 +- config/console.php | 1 - route/app.php | 2 + 17 files changed, 231 insertions(+), 65 deletions(-) delete mode 100644 app/command/Opiptask.php create mode 100644 app/view/system/cronset.html diff --git a/app/command/Certtask.php b/app/command/Certtask.php index 443e5a3..74331a5 100644 --- a/app/command/Certtask.php +++ b/app/command/Certtask.php @@ -12,7 +12,9 @@ use think\console\input\Option; use think\console\Output; use think\facade\Db; use think\facade\Config; +use app\service\OptimizeService; use app\service\CertTaskService; +use app\service\ExpireNoticeService; class Certtask extends Command { @@ -20,7 +22,7 @@ class Certtask extends Command { // 指令配置 $this->setName('certtask') - ->setDescription('证书申请与部署任务'); + ->setDescription('SSL证书续签与部署、域名到期提醒、CF优选IP更新'); } protected function execute(Input $input, Output $output) @@ -28,6 +30,11 @@ class Certtask extends Command $res = Db::name('config')->cache('configs', 0)->column('value', 'key'); Config::set($res, 'sys'); - (new CertTaskService())->execute(); + $res = (new OptimizeService())->execute(); + if (!$res) { + (new CertTaskService())->execute(); + (new ExpireNoticeService())->task(); + } + echo 'done'.PHP_EOL; } } diff --git a/app/command/Opiptask.php b/app/command/Opiptask.php deleted file mode 100644 index df60bed..0000000 --- a/app/command/Opiptask.php +++ /dev/null @@ -1,33 +0,0 @@ -setName('opiptask') - ->setDescription('CF优选IP任务'); - } - - protected function execute(Input $input, Output $output) - { - $res = Db::name('config')->cache('configs', 0)->column('value', 'key'); - Config::set($res, 'sys'); - - (new OptimizeService())->execute(); - } -} diff --git a/app/controller/Optimizeip.php b/app/controller/Optimizeip.php index b044308..3b33cd4 100644 --- a/app/controller/Optimizeip.php +++ b/app/controller/Optimizeip.php @@ -20,6 +20,9 @@ class Optimizeip extends BaseController if (empty($key)) { continue; } + if ($key == 'optimize_ip_min' && intval($value) < 10) { + return json(['code' => -1, 'msg' => '自动更新时间间隔不能小于10分钟']); + } config_set($key, $value); Cache::delete('configs'); } diff --git a/app/controller/System.php b/app/controller/System.php index 3828641..727b490 100644 --- a/app/controller/System.php +++ b/app/controller/System.php @@ -7,6 +7,9 @@ use Exception; use think\facade\Db; use think\facade\View; use think\facade\Cache; +use app\service\OptimizeService; +use app\service\CertTaskService; +use app\service\ExpireNoticeService; class System extends BaseController { @@ -107,4 +110,38 @@ class System extends BaseController } return json(['code' => 0]); } + + public function cronset() + { + if (!checkPermission(2)) return $this->alert('error', '无权限'); + if (config_get('cron_key') === null) { + config_set('cron_key', random(10)); + Cache::delete('configs'); + } + View::assign('is_user_www', isset($_SERVER['USER']) && $_SERVER['USER'] == 'www'); + View::assign('siteurl', request()->root(true)); + return View::fetch(); + } + + public function cron() + { + if (function_exists("set_time_limit")) { + @set_time_limit(0); + } + if (function_exists("ignore_user_abort")) { + @ignore_user_abort(true); + } + if (isset($_SERVER['HTTP_USER_AGENT']) && str_contains($_SERVER['HTTP_USER_AGENT'], 'Baiduspider')) exit; + $key = input('get.key', ''); + $cron_key = config_get('cron_key'); + if (config_get('cron_type', '0') != '1' || empty($cron_key)) exit('未开启当前方式'); + if ($key != $cron_key) exit('访问密钥错误'); + + $res = (new OptimizeService())->execute(); + if (!$res) { + (new CertTaskService())->execute(); + (new ExpireNoticeService())->task(); + } + echo 'success!'; + } } \ No newline at end of file diff --git a/app/service/CertTaskService.php b/app/service/CertTaskService.php index 2d96015..e2bf9d1 100644 --- a/app/service/CertTaskService.php +++ b/app/service/CertTaskService.php @@ -11,15 +11,17 @@ class CertTaskService public function execute() { - $this->execute_deploy(); - $this->execute_order(); - (new ExpireNoticeService())->task(); - config_set('certtask_time', date("Y-m-d H:i:s")); - echo 'done'.PHP_EOL; + if ($this->execute_deploy()) { + config_set('certdeploy_time', date("Y-m-d H:i:s")); + } + if ($this->execute_order()) { + config_set('certtask_time', date("Y-m-d H:i:s")); + } } private function execute_order() { + echo '开始执行SSL证书签发任务...'.PHP_EOL; $days = config_get('cert_renewdays', 7); $list = Db::name('cert_order')->field('id,aid,status,issend')->whereRaw('status NOT IN (3,4) AND (retrytime IS NULL OR retrytime date('Y-m-d H:i:s', time() + $days * 86400)])->select(); //print_r($list);exit; @@ -55,6 +57,7 @@ class CertTaskService if ($failcount >= 3) break; sleep(1); } + return true; } private function execute_deploy() @@ -64,14 +67,15 @@ class CertTaskService $hour = date('H'); if($start <= $end){ if($hour < $start || $hour > $end){ - echo '不在部署任务运行时间范围内'.PHP_EOL; return; + echo '不在部署任务运行时间范围内'.PHP_EOL; return false; } }else{ if($hour < $start && $hour > $end){ - echo '不在部署任务运行时间范围内'.PHP_EOL; return; + echo '不在部署任务运行时间范围内'.PHP_EOL; return false; } } + echo '开始执行SSL证书部署任务...'.PHP_EOL; $list = Db::name('cert_deploy')->field('id,status,issend')->whereRaw('active=1 AND status IN (0,-1) AND (retrytime IS NULL OR retrytimeselect(); //print_r($list);exit; $count = 0; @@ -95,5 +99,6 @@ class CertTaskService if ($count >= 3) break; sleep(1); } + return true; } } diff --git a/app/service/ExpireNoticeService.php b/app/service/ExpireNoticeService.php index e5fd2e2..e541896 100644 --- a/app/service/ExpireNoticeService.php +++ b/app/service/ExpireNoticeService.php @@ -26,6 +26,8 @@ class ExpireNoticeService public function task() { + echo '开始执行域名到期提醒任务...' . PHP_EOL; + config_set('domain_expire_time', date("Y-m-d H:i:s")); $count = $this->refreshDomainList(); if ($count > 0) return; diff --git a/app/service/OptimizeService.php b/app/service/OptimizeService.php index 5ceac45..1f243d4 100644 --- a/app/service/OptimizeService.php +++ b/app/service/OptimizeService.php @@ -96,7 +96,15 @@ class OptimizeService //批量执行优选任务 public function execute() { + $minute = config_get('optimize_ip_min', '30'); + $last = config_get('optimize_ip_time', null, true); + if ($last && strtotime($last) > time() - $minute * 60) { + return false; + } $list = Db::name('optimizeip')->where('active', 1)->select(); + if (empty($list)) { + return false; + } echo '开始执行IP优选任务,共获取到'.count($list).'个待执行任务'."\n"; foreach ($list as $row) { try { @@ -108,6 +116,8 @@ class OptimizeService echo '优选任务'.$row['id'].'执行失败:'.$e->getMessage()."\n"; } } + config_set('optimize_ip_time', date("Y-m-d H:i:s")); + return true; } //执行单个优选任务 diff --git a/app/utils/CheckUtils.php b/app/utils/CheckUtils.php index bcd18b5..44b94b5 100644 --- a/app/utils/CheckUtils.php +++ b/app/utils/CheckUtils.php @@ -126,7 +126,11 @@ class CheckUtils return ['status' => false, 'errmsg' => 'Invalid IP address', 'usetime' => 0]; } $timeout = 1; - exec('ping -c 1 -w '.$timeout.' '.$target, $output, $return_var); + if (str_contains($target, ':')) { + exec('ping -6 -c 1 -w '.$timeout.' '.$target, $output, $return_var); + } else { + exec('ping -c 1 -w '.$timeout.' '.$target, $output, $return_var); + } if (!empty($output[1])) { if (strpos($output[1], '毫秒') !== false) { $usetime = getSubstr($output[1], '时间=', ' 毫秒'); diff --git a/app/view/cert/certset.html b/app/view/cert/certset.html index e54d1ac..6b97073 100644 --- a/app/view/cert/certset.html +++ b/app/view/cert/certset.html @@ -3,14 +3,6 @@ {block name="main"}
-
-

计划任务说明

-
-

  • 计划任务:将以下命令添加到计划任务,1分钟1次
  • -

    cd {:app()->getRootPath()} && php think certtask

    -

  • 上次运行时间:{:config_get('certtask_time', '未运行', true)}
  • -
    -

    自动续签设置

    @@ -28,7 +20,7 @@
    diff --git a/app/view/common/layout.html b/app/view/common/layout.html index d428e58..3b65dea 100644 --- a/app/view/common/layout.html +++ b/app/view/common/layout.html @@ -153,10 +153,10 @@
  • 自动部署账户
  • 自动部署任务
  • CNAME代理
  • -
  • 计划任务设置
  • +
  • 自动续签设置
  • -
  • +
  • 系统设置 @@ -165,6 +165,7 @@
      +
    • 计划任务
    • 登录设置
    • 通知设置
    • 代理设置
    • diff --git a/app/view/dmonitor/overview.html b/app/view/dmonitor/overview.html index 5fec815..bdeff4a 100644 --- a/app/view/dmonitor/overview.html +++ b/app/view/dmonitor/overview.html @@ -160,7 +160,7 @@

      1、php需要安装swoole组件

      2、在命令行执行以下命令启动进程:

      cd {:app()->getRootPath()} && php think dmtask

      -

      3、也可以使用进程守护管理器,添加守护进程,运行目录:{:app()->getRootPath()},启动命令:php think dmtask

      +

      3、也可以使用进程守护管理器,添加守护进程。
      运行目录:{:app()->getRootPath()}
      启动命令:php think dmtask

  • diff --git a/app/view/domain/expire_notice.html b/app/view/domain/expire_notice.html index 2455e0a..91b8778 100644 --- a/app/view/domain/expire_notice.html +++ b/app/view/domain/expire_notice.html @@ -35,12 +35,8 @@ - - -
    -

    计划任务说明

    -
    -

    支持域名到期提醒+域名列表到期时间自动刷新。与SSL证书共用计划任务,不需要单独添加计划任务。

    查看计划任务说明

    +
    diff --git a/app/view/optimizeip/opipset.html b/app/view/optimizeip/opipset.html index 63cac5d..9199c3f 100644 --- a/app/view/optimizeip/opipset.html +++ b/app/view/optimizeip/opipset.html @@ -16,8 +16,7 @@

  • 不支持对CloudFlare里的域名添加优选,必须使用其他DNS服务商。需开通Cloudflare for SaaS,且域名使用CNAME的方式解析到CloudFlare。
  • 数据接口:wetest.vip 数据接口支持CloudFlare、CloudFront、EdgeOne;HostMonit 只支持CloudFlare。
  • 接口密钥:默认o1zrmHAF为免费KEY可永久免费使用。
  • -

  • 计划任务:将以下命令添加到计划任务,周期设置为15分钟以上
  • -

    cd {:app()->getRootPath()} && php think opiptask

    +

  • 自动更新:可查看计划任务设置

  • @@ -43,6 +42,22 @@ +
    +

    自动更新设置

    +
    +
    +
    + +
    +
    +
    +
    + +
    +
    +
    +
    +
    diff --git a/app/view/system/cronset.html b/app/view/system/cronset.html new file mode 100644 index 0000000..6b8c960 --- /dev/null +++ b/app/view/system/cronset.html @@ -0,0 +1,126 @@ +{extend name="common/layout" /} +{block name="title"}计划任务{/block} +{block name="main"} +
    +
    +
    +

    计划任务说明

    +
    +{if config_get('cron_type', '0') == '1'} +

  • 需定时访问以下URL,频率;1分钟1次
  • +

    {$siteurl}/cron?key={:config_get('cron_key')}

    +{else} +

  • 将以下Shell命令添加到计划任务,频率;1分钟1次
  • +

    cd {:app()->getRootPath()} && php think certtask

    + {if $is_user_www}

  • 注:计划任务执行用户必须选择www用户
  • {/if} +

  • 采用Docker镜像部署的会自动添加计划任务,无需手动添加。
  • +{/if} +
    +
    + +
    +

    计划任务设置

    +
    +
    +
    + +
    +
    +
    + +
    +
    +
    +
    + +
    +
    +
    +
    + +
    + +
    +

    计划任务运行状态

    + + + + + + + + + + + + + + + + + + + + + + + + + +
    任务名称上次运行时间
    SSL证书续签{:config_get('certtask_time', '未运行', true)}
    SSL证书部署{:config_get('certdeploy_time', '未运行', true)}
    域名到期提醒{:config_get('domain_expire_time', '未运行', true)}
    CF优选IP更新{:config_get('optimize_ip_time', '未运行', true)}
    +
    + +
    +
    +{/block} +{block name="script"} + + +{/block} \ No newline at end of file diff --git a/config/app.php b/config/app.php index 556ea32..bb2c457 100644 --- a/config/app.php +++ b/config/app.php @@ -31,7 +31,7 @@ return [ 'show_error_msg' => true, 'exception_tmpl' => \think\facade\App::getAppPath() . 'view/exception.tpl', - 'version' => '1038', + 'version' => '1039', 'dbversion' => '1033' ]; diff --git a/config/console.php b/config/console.php index 2cf352c..7158f6f 100644 --- a/config/console.php +++ b/config/console.php @@ -6,7 +6,6 @@ return [ // 指令定义 'commands' => [ 'dmtask' => 'app\command\Dmtask', - 'opiptask' => 'app\command\Opiptask', 'certtask' => 'app\command\Certtask', 'reset' => 'app\command\Reset', ], diff --git a/route/app.php b/route/app.php index 795b663..1630a46 100644 --- a/route/app.php +++ b/route/app.php @@ -29,6 +29,7 @@ Route::get('/logout', 'auth/logout'); Route::any('/quicklogin', 'auth/quicklogin'); Route::any('/dmtask/status', 'dmonitor/status'); Route::any('/optimizeip/status', 'optimizeip/status'); +Route::get('/cron', 'system/cron'); Route::group(function () { Route::any('/', 'index/index'); @@ -120,6 +121,7 @@ Route::group(function () { Route::get('/system/tgbottest', 'system/tgbottest'); Route::get('/system/webhooktest', 'system/webhooktest'); Route::post('/system/proxytest', 'system/proxytest'); + Route::get('/system/cronset', 'system/cronset'); })->middleware(CheckLogin::class) ->middleware(ViewOutput::class);