dd/app/Services/Cron/FreenodeSync.php
2026-05-15 13:14:32 +08:00

122 lines
4.5 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Services\Cron;
use App\Models\SiteMap as ModelSiteMap;
use App\Services\TomTool\Telegram\Slave as TeleSlave;
use App\Services\TomTool\HttpV2;
use Illuminate\Support\Facades\Http;
final class FreenodeSync
{
static function run()
{
echo "\nFeenodeSync::run() - 开始 \n";
$sDirBase = $_ENV["dir_base"];
$oFnSiteList = ModelSiteMap::where("group_code", "fn_a")->get();
$aResultAll = [];
foreach ($oFnSiteList as $oFnSite) {
$aSiteConfig = json_decode($oFnSite->config, true);
$aSiteConfigFnClient = $aSiteConfig["freenode_client"] ?? [];
$aSiteApi = json_decode($oFnSite->api_path, true);
$sSiteApiFreenodeReceive = $aSiteApi["freenode_receive"] ?? "";
$aFnContent = [];
foreach ($aSiteConfigFnClient as $aSiteConfigFnClientRow) {
$sFilePath = $sDirBase."public/freenode/merge/".$oFnSite->code."/".$aSiteConfigFnClientRow."/".date("Y-m-d").".txt";
if (file_exists($sFilePath)) {
$aFnContent[$aSiteConfigFnClientRow] = file_get_contents($sFilePath);
} else {
$sMsg = "freenodeSync的run没有当天文件".$sFilePath;
TeleSlave::warn()->send($sMsg);
}
}
$sApiUrl = "https://".$oFnSite->url."/".$sSiteApiFreenodeReceive;
$sResult = HttpV2::make($sApiUrl, "post")->setDataA($aFnContent)->enableJsonSend()->send();
$aResult = json_decode($sResult, true);
if (!isset($aResult["is_done"]) || $aResult["is_done"] != true) {
$sMsg = "freenodeSync::同步失败 ->".$oFnSite->name."->".$sResult;
echo $sMsg;
TeleSlave::warn()->setMsgS($sMsg)->send();
continue;
}
}
echo " - ok \n";
}
static function nodehub()
{
echo "\nFeenodeSync::nodehub() - 开始 \n";
$sDirBase = $_ENV["dir_base"];
$oFnSiteList = ModelSiteMap::whereIn("group_code", ["fn_b", "fn_a"])->get();
$oSiteNodehub = ModelSiteMap::where("code", "nodehub")->first();
$aSiteNodehubConfig = json_decode($oSiteNodehub->config ?? '', true);
$aFreenodeClient = $aSiteNodehubConfig["freenode_client"] ?? [];
$aNodehubApis = json_decode($oSiteNodehub->api_path, true);
$sSiteApiFreenodeReceive = $aNodehubApis["freenode_receive"] ?? "";
$aResultAll = [];
foreach ($oFnSiteList as $oFnSite) {
// $aSiteConfig = json_decode($oFnSite->config, true);
// $aSiteConfigFnClient = $aSiteConfig["freenode_client"] ?? [];
// $aSiteConfigFnClient = ["clash", "v2ray"];
// $aSiteApi = json_decode($oFnSite->api_path, true);
// $sSiteApiFreenodeReceive = $aSiteApi["freenode_receive"] ?? "";
$aData = [];
$aData["site_code_short"] = $oFnSite->code_short;
foreach ($aFreenodeClient as $aSiteConfigFnClientRow) {
$sFilePath = $sDirBase."public/freenode/merge/".$oFnSite->code."/".$aSiteConfigFnClientRow."/".date("Y-m-d").".txt";
if (!file_exists($sFilePath)) {
$sMsg = "freenodeSync的run没有当天文件".$sFilePath;
TeleSlave::warn()->send($sMsg);
echo "\n - ".$sMsg;
continue;
}
$aData["raw"][$aSiteConfigFnClientRow] = file_get_contents($sFilePath);
}
$sApiUrl = "https://".$oSiteNodehub->url."/".$sSiteApiFreenodeReceive;
// tomd($sApiUrl, 1);
$sResult = HttpV2::make($sApiUrl, "post")->setDataA($aData)->enableJsonSend()->send();
$aResult = json_decode($sResult, true);
if (!isset($aResult["isDone"]) || $aResult["isDone"] != true) {
$sMsg = "freenodeSync::同步失败 ->".$oFnSite->name."->".$sResult;
echo $sMsg;
TeleSlave::warn()->setMsgS($sMsg)->send();
continue;
}
if (isset($aResult["sMsg"])) {
$sMsg = json_encode($aResult["sMsg"]);
echo $sMsg;
// TeleSlave::warn()->setMsgS($sMsg)->send();
}
echo "\n nodehub";
}
echo " - 完成 \n";
}
}