From 8b88da3ccdf89b53e207fedaa9e556d307345e42 Mon Sep 17 00:00:00 2001 From: hellcat <> Date: Thu, 3 Apr 2025 18:51:05 +0800 Subject: [PATCH] no message --- public/tests/hook.php | 4 +- src/Command/Cron.php | 6 +- src/Controllers/TestController.php | 4 +- src/Controllers/TomApi/Tg/HookController.php | 292 ++++++++++++++++++- src/Services/Cron.php | 131 ++++++++- src/Utils/Tools.php | 11 + 6 files changed, 440 insertions(+), 8 deletions(-) diff --git a/public/tests/hook.php b/public/tests/hook.php index 467540ef..16e7ebf6 100644 --- a/public/tests/hook.php +++ b/public/tests/hook.php @@ -35,7 +35,7 @@ $oHttp->aData = [ 'type' => 'private' ], 'date' => 1610000000, - "text" => "\/orderFindByUser 2", + "text" => "\/zhuanfaReset-youshi 2025-02-02", ] ]; @@ -70,7 +70,7 @@ $oHttp->aData = [ $r = $oHttp->send(); -var_dump($r); +echo($r); exit; diff --git a/src/Command/Cron.php b/src/Command/Cron.php index e5646517..17ef7a90 100755 --- a/src/Command/Cron.php +++ b/src/Command/Cron.php @@ -57,7 +57,7 @@ EOL; $jobs->cleanDb(); // $jobs->resetNodeBandwidth(); $jobs->resetDuckBandwidth(); - $jobs->bandwidthMerge(); + $jobs->reportBandWidth(); $jobs->resetFreeUserBandwidth(); $jobs->sendDailyTrafficReport(); @@ -138,6 +138,10 @@ EOL; $detect = new Detect(); $detect->ban(); } + + if ($minute === 0) { + $jobs->mergeBandWidth(); + } // Run email queue $jobs->processEmailQueue(); diff --git a/src/Controllers/TestController.php b/src/Controllers/TestController.php index 0ed43a77..8253ef53 100755 --- a/src/Controllers/TestController.php +++ b/src/Controllers/TestController.php @@ -29,7 +29,9 @@ final class TestController extends BaseController { $jobs = new CronService(); - $jobs->userCount(); + $jobs->mergeBandWidth(); + + $jobs->reportBandWidth(); exit; } diff --git a/src/Controllers/TomApi/Tg/HookController.php b/src/Controllers/TomApi/Tg/HookController.php index 075a2aa8..b1157873 100755 --- a/src/Controllers/TomApi/Tg/HookController.php +++ b/src/Controllers/TomApi/Tg/HookController.php @@ -10,12 +10,15 @@ use App\Models\Ticket; use App\Models\Node; use App\Models\User; use App\Models\Order; +use App\Models\Ducks; +use App\Models\Zhuanfas; use App\Services\Tg\ReportUser as ServiceTgReportUser; use GuzzleHttp\Exception\GuzzleException; use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Message\ResponseInterface; use Slim\Http\Response; use Slim\Http\ServerRequest; +use App\Utils\Tools; use App\Services\TomTool\Http; @@ -227,6 +230,292 @@ final class HookController } + // 添加duck + // /duckAdd jp01 + if (strpos($sText, '/duckAdd') === 0 || strpos($sText, '\/duckAdd') === 0) { + + list($sCmd, $sCode) = explode(" ", $sText, 2); + + try { + $oDucks = new Ducks(); + $oDucks->code = $sCode; + + if ($oDucks->save()) { + $sMsg = "成功"; + } else { + $sMsg = "失败"; + } + } catch (\Exception $e) { + $sMsg = json_encode($e->getMessage()); + } + + $this->sendMessage($sMsg); + + Http::response(200, 'ok'); + + } + + // 添加zhuanfa + // /zhuanfaAdd jp01 + if (strpos($sText, '/zhuanfaAdd') === 0 || strpos($sText, '\/zhuanfaAdd') === 0) { + + list($sCmd, $sCode) = explode(" ", $sText, 2); + + try { + $oZhuanfas = new Zhuanfas(); + $oZhuanfas->code = $sCode; + + if ($oZhuanfas->save()) { + $sMsg = "成功"; + } else { + $sMsg = "失败"; + } + } catch (\Exception $e) { + $sMsg = json_encode($e->getMessage()); + } + + $this->sendMessage($sMsg); + + Http::response(200, 'ok'); + + } + + // 删除duck + // /duckDel jp01 + if (strpos($sText, '/duckDel') === 0 || strpos($sText, '\/duckDel') === 0) { + + list($sCmd, $sCode) = explode(" ", $sText, 2); + + try { + $oDucks = Ducks::where("code", $sCode)->first(); + + if ($oDucks) { + // 尝试删除记录 + $deleted = $oDucks->delete(); + + if ($deleted) { + // 删除成功 + $sMsg = "成功"; + } else { + // 删除失败 + $sMsg = "失败"; + } + } else { + // 找不到记录 + $sMsg = "找不到记录"; + } + } catch (\Exception $e) { + // 捕获异常并返回错误信息 + $sMsg = json_encode($e->getMessage()); + } + + $this->sendMessage($sMsg); + + Http::response(200, 'ok'); + + } + + // 删除zhuanfa + // /zhuanfaDel jp01 + if (strpos($sText, '/zhuanfaDel') === 0 || strpos($sText, '\/zhuanfaDel') === 0) { + + list($sCmd, $sCode) = explode(" ", $sText, 2); + + try { + $oZhuanfas = Zhuanfas::where("code", $sCode)->first(); + + if ($oZhuanfas) { + // 尝试删除记录 + $deleted = $oZhuanfas->delete(); + + if ($deleted) { + // 删除成功 + $sMsg = "成功"; + } else { + // 删除失败 + $sMsg = "失败"; + } + } else { + // 找不到记录 + $sMsg = "找不到记录"; + } + } catch (\Exception $e) { + // 捕获异常并返回错误信息 + $sMsg = json_encode($e->getMessage()); + } + + $this->sendMessage($sMsg); + + Http::response(200, 'ok'); + + } + + // 修改duck + // /duckOption-end_date-jp01 2025_02_02 + // /duckOption-max_width-jp01 1.66#tb + if (strpos($sText, '/duckOption') === 0 || strpos($sText, '\/duckOption') === 0) { + + list($sCmd, $sSet) = explode(" ", $sText, 2); + list(, $sField, $sCode) = explode("-", $sCmd, 3); + @list($iWidth, $sWidthType) = explode("#", $sSet, 2); + + if ($sWidthType == 'tb') { + $sSet = Tools::tomTbToB($iWidth); + } + + if ($sWidthType == 'gb') { + $sSet = Tools::tomGbToB($iWidth); + } + + $sMsg = '?'; + try { + + $oDuck = Ducks::where('code', $sCode)->first(); + + if (!$oDuck) { + $sMsg = "没有这个code"; + } else { + $oDuck->$sField = $sSet; + if ($oDuck->save()) { + $sMsg = "成功"; // 保存成功 + } else { + $sMsg = "失败"; // 保存失败 + } + } + + } catch (\Illuminate\Database\QueryException $e) { + + $sMsg = json_encode($e->getMessage()); + + } + + $this->sendMessage($sMsg); + + Http::response(200, 'ok'); + + } + + // 修改zhuanfa + // /zhuanfaOption-end_date-jp01 2025_02_02 + // /zhuanfaOption-max_width-jp01 1.66#tb + if (strpos($sText, '/zhuanfaOption') === 0 || strpos($sText, '\/zhuanfaOption') === 0) { + + list($sCmd, $sSet) = explode(" ", $sText, 2); + list(, $sField, $sCode) = explode("-", $sCmd, 3); + @list($iWidth, $sWidthType) = explode("#", $sSet, 2); + + if ($sWidthType == 'tb') { + $sSet = Tools::tomTbToB($iWidth); + } + + if ($sWidthType == 'gb') { + $sSet = Tools::tomGbToB($iWidth); + } + + $sMsg = '?'; + try { + + $oZhuanfas = Zhuanfas::where('code', $sCode)->first(); + + if (!$oZhuanfas) { + $sMsg = "没有这个code"; + } else { + $oZhuanfas->$sField = $sSet; + if ($oZhuanfas->save()) { + $sMsg = "成功"; // 保存成功 + } else { + $sMsg = "失败"; // 保存失败 + } + } + + } catch (\Illuminate\Database\QueryException $e) { + + $sMsg = json_encode($e->getMessage()); + + } + + $this->sendMessage($sMsg); + + Http::response(200, 'ok'); + + } + + // 转发重置 + // /zhuanfaReset-youshi 2025-02-02 + if (strpos($sText, '/zhuanfaReset') === 0 || strpos($sText, '\/zhuanfaReset') === 0) { + + list($sCmd, $sDate) = explode(" ", $sText, 2); + list(, $sCode) = explode("-", $sCmd, 2); + + $sMsg = '?'; + try { + + $oZhuanfas = Zhuanfas::where('code', $sCode)->first(); + + if (!$oZhuanfas) { + $sMsg = "没有这个code"; + } else { + $oZhuanfas->use_width = 0; + $oZhuanfas->end_date = $sDate; + if ($oZhuanfas->save()) { + $sMsg = "成功"; // 保存成功 + } else { + $sMsg = "失败"; // 保存失败 + } + } + + } catch (\Illuminate\Database\QueryException $e) { + + $sMsg = json_encode($e->getMessage()); + + } + + $this->sendMessage($sMsg); + + Http::response(200, 'ok'); + + } + + // duck list + // /duckList + if (strpos($sText, '/duckList') === 0 || strpos($sText, '\/duckList') === 0) { + + $aDuckList = Ducks::all()->toArray(); + + $aMsgData = []; + foreach ($aDuckList as $k => $v) { + $v['use_width'] = Tools::flowToTB($v['use_width']).'t'; + $v['max_width'] = Tools::flowToTB($v['max_width']).'t'; + $v['today_width'] = Tools::flowToGB($v['today_width']).'g'; + $aMsgData[] = $v; + } + + $this->sendMessage(json_encode($aMsgData, JSON_PRETTY_PRINT)); + + Http::response(200, 'ok'); + + } + + // zhuanfa list + // /zhuanfaList + if (strpos($sText, '/zhuanfaList') === 0 || strpos($sText, '\/zhuanfaList') === 0) { + + $aZhuanfaList = Zhuanfas::all()->toArray(); + + $aMsgData = []; + foreach ($aZhuanfaList as $k => $v) { + $v['use_width'] = Tools::flowToTB($v['use_width'])."t"; + $v['max_width'] = Tools::flowToTB($v['max_width']).'t'; + $v['today_width'] = Tools::flowToGB($v['today_width']).'g'; + $aMsgData[] = $v; + } + + $this->sendMessage(json_encode($aMsgData, JSON_PRETTY_PRINT)); + + Http::response(200, 'ok'); + + } + // 节点设置变更 // /nodeOption_name_1 haha if (strpos($sText, '/nodeOption_') === 0 || strpos($sText, '\/nodeOption_') === 0) { @@ -391,7 +680,8 @@ final class HookController private function sendMessage($sText) { if ($_ENV['is_loc'] === true) { - var_dump($sText); + echo "
";
+ echo($sText);
} else {
$url = "https://api.telegram.org/bot$this->sApiToken/sendMessage?chat_id=$this->sApiChatId&text=" . urlencode($sText);
file_get_contents($url);
diff --git a/src/Services/Cron.php b/src/Services/Cron.php
index 3950b400..f6ce88f3 100755
--- a/src/Services/Cron.php
+++ b/src/Services/Cron.php
@@ -586,7 +586,134 @@ final class Cron
// echo Tools::toDateTime(time()) . ' 重设机器流量完成' . PHP_EOL;
// }
- public static function bandwidthMerge(): void
+ public static function mergeBandWidth(): void
+ {
+
+ $oNodeAll = Node::all();
+
+ $aZhuanfaFlowNow = [];
+ $aDuckFlowNow = [];
+ $sMsg = "";
+
+ // 统计node流量
+ foreach ($oNodeAll as $oNodeRow) {
+
+ if ($oNodeRow->node_bandwidth <= 1242880) {
+
+ $sMsg .= $oNodeRow->name."1小时消耗流量不足1mb,可能存在异常。\n";
+
+ }
+
+ @$aZhuanfaFlowNow[$oNodeRow->zhuanfa_code] += $oNodeRow->node_bandwidth;
+ @$aDuckFlowNow[$oNodeRow->duck_code] += $oNodeRow->node_bandwidth;
+
+ }
+
+ // 入库zhunafa
+ foreach ($aZhuanfaFlowNow as $k => $v) {
+
+ if ($v) {
+ Zhuanfas::where('code', $k)->increment('today_width', $v);
+ Zhuanfas::where('code', $k)->increment('use_width', $v);
+ }
+
+ }
+
+ // 入库duck
+ foreach ($aDuckFlowNow as $k => $v) {
+
+ if ($v) {
+ Ducks::where('code', $k)->increment('today_width', $v);
+ Ducks::where('code', $k)->increment('use_width', $v);
+ }
+
+ }
+
+ // 清空node流量
+ Node::query()->update(['node_bandwidth' => 0]);
+
+ if ($sMsg) {
+// echo $sMsg;
+ (new Telegram())->send(0, $sMsg);
+ }
+
+ }
+
+ public static function reportBandWidth(): void
+ {
+
+ $oZhuanfas = new Zhuanfas();
+ $oDucks = new Ducks();
+
+ // 流量报告,转发
+ $oTgSendFormat = new TgSendFormat();
+ $oTgSendFormat->sTitleEmoji = "⚡️⚡️⚡️⚡️⚡️⚡️";
+ $oTgSendFormat->sTitle = "# 转发流量";
+ $today = new DateTime();
+
+ $oZhuanfasAll = $oZhuanfas->all();
+ foreach ($oZhuanfasAll as $oZhuanfasRow) {
+
+ $targetDate = new DateTime($oZhuanfasRow->end_date);
+ $interval = $today->diff($targetDate);
+
+ $oTgSendFormat->rowAdd(
+ $oZhuanfasRow->name,
+ $oZhuanfasRow->today_width,
+ $oZhuanfasRow->use_width,
+ $oZhuanfasRow->max_width,
+ $interval->days
+ );
+
+ }
+
+ $sTgReport = $oTgSendFormat->typeA();
+
+// var_dump($sTgReport);
+ (new Telegram())->send(0, $sTgReport);
+ Zhuanfas::query()->update(['today_width' => 0]);
+
+ // 流量报告,机器
+ $oTgSendFormat = new TgSendFormat();
+ $oTgSendFormat->sTitleEmoji = "\n\n";
+ $oTgSendFormat->sTitle = "# 机器流量";
+
+ $oDucksAll = $oDucks->orderBy('code')->get();
+ foreach ($oDucksAll as $oDucksRow) {
+
+ if ($oDucksRow->width_reset_day == 0) {
+ $iWidthResetDay = 0;
+ } if ($oDucksRow->width_reset_day > date('d')) {
+ $iWidthResetDay = $oDucksRow->width_reset_day - date('d');
+ } else {
+ $date = new DateTime();
+ $date->modify('first day of next month');
+ $date->setDate((int)$date->format('Y'), (int)$date->format('m'), $oDucksRow->width_reset_day);
+ $sEndDate = $date;
+
+ $currentDate = new DateTime();
+ $interval = $currentDate->diff($sEndDate);
+ $iWidthResetDay = $interval->days;
+ }
+
+ $oTgSendFormat->rowAdd(
+ $oDucksRow->code,
+ $oDucksRow->today_width,
+ $oDucksRow->use_width,
+ $oDucksRow->max_width,
+ $iWidthResetDay
+ );
+
+ }
+
+ $sTgReport = $oTgSendFormat->typeA();
+
+// var_dump($sTgReport);
+ (new Telegram())->send(0, $sTgReport);
+ Ducks::query()->update(['today_width' => 0]);
+ }
+
+ public static function reportBandWidth_back(): void
{
$oNode = new Node();
$oZhuanfas = new Zhuanfas();
@@ -630,7 +757,6 @@ final class Cron
$sTgReport = $oTgSendFormat->typeA();
-// var_dump($sTgReport);
(new Telegram())->send(0, $sTgReport);
// 流量报告,机器
@@ -668,7 +794,6 @@ final class Cron
$sTgReport = $oTgSendFormat->typeA();
-// var_dump($sTgReport);
(new Telegram())->send(0, $sTgReport);
}
diff --git a/src/Utils/Tools.php b/src/Utils/Tools.php
index 4df48858..72346b60 100755
--- a/src/Utils/Tools.php
+++ b/src/Utils/Tools.php
@@ -158,6 +158,17 @@ final class Tools
{
return (int) $traffic * 1073741824;
}
+
+ public static function tomTbToB($traffic)
+ {
+ return bcmul($traffic, '1099511627776', 0); // 0 表示没有小数位
+ }
+
+ public static function tomGbToB($traffic)
+ {
+ return bcmul($traffic, '1073741824', 0); // 0 表示没有小数位
+ }
+
public static function flowToMB($traffic): float
{