Files
cfn/app/Http/Controllers/Api/FreenodeController.php
T
2026-01-04 14:51:30 +08:00

282 lines
8.2 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
//use voku\helper\AntiXSS;
use App\Models\ArticleList as ArticleList;
use App\Services\TomTool\HttpV2;
use App\Models\Option as ModelOption;
use App\Services\TomTool\Telegram\Slave as TeleSlave;
class FreenodeController extends Controller
{
static $sEnding = [
"clash" => ".yaml",
"v2ray" => ".txt",
];
static $aCountry = [
"美国",
"美国",
"日本",
"日本",
"新加坡",
"新加坡",
"香港",
"香港",
"台湾",
"台湾",
"越南",
"越南",
"俄罗斯",
"新西兰",
"瑞士",
"荷兰",
"冰岛",
"爱尔兰",
"乌克兰",
"泰国",
"阿根廷",
"葡萄牙",
"美国",
"日本",
"新加坡",
"台湾",
"香港",
"韩国",
"德国",
"土耳其",
"法国",
"加拿大",
"加拿大",
];
public function receive(Request $request)
{
$aData = $request->all();
$sData = json_encode($aData);
$sFnDir = "sub";
$sDateCode = date("Ymd");
$aResult = [];
foreach ($aData as $sFnClient => $sFnContent) {
$sFnFilePath = $sFnDir."/".$sDateCode."-".$sFnClient.$this->endingByClient($sFnClient);
file_put_contents($sFnFilePath, $sFnContent);
$aResult[$sFnClient] = "以更新:".env("APP_URL")."/".$sFnFilePath;
}
$this->saveArticleHtml();
TeleSlave::notify()->enableSlaveQueue(false)->setMsgA($aResult)->send();
return ["is_done" => true];
}
private function saveArticleHtml()
{
$sDateCode = date("Ymd");
//// test
// ArticleList::where("code", $sDateCode)->delete();
//// test end
$oArticleList = ArticleList::where("code", $sDateCode)->first();
if ($oArticleList) {
return false;
}
$this->_saveArticle($sDateCode);
$sHtmlUrl = $this->_saveHtml($sDateCode);
TeleSlave::log()->enableSlaveQueue(false)->send("新fn文章:".$sHtmlUrl);
self::_delLast();
$this->mainHtmlRe();
return $sHtmlUrl;
}
private function mainHtmlRe()
{
@unlink("index.html");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, env("APP_URL"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_exec($ch);
}
private function _delLast()
{
$iLastTime = strtotime("+365 day"); // 这边保存1年
// $sLastDate = date("Y-m-d", $iLastTime);
$sLastDateCode = date("Ymd", $iLastTime);
// 删除file
foreach (self::$sEnding as $sClient => $sEnding) {
$sFilePath = "sub/".$sLastDateCode."-".$sClient.$sEnding;
if (file_exists($sFilePath)) {
if (unlink($sFilePath)) {
// echo " - 文件删除成功".$sFilePath."\n";
} else {
$sMsg = " - 文件删除失败 -> ".$sFilePath."\n";
TeleSlave::warn()->send($sMsg);
}
}
}
// 删除html
$sHtmlPath = "fn/".$sLastDateCode.".html";
if (file_exists($sHtmlPath)) {
if (unlink($sHtmlPath)) {
// echo " - 文件删除成功".$sHtmlPath."\n";
} else {
$sMsg = " - 文件删除失败 -> ".$sHtmlPath."\n";
TeleSlave::warn()->send($sMsg);
}
}
}
private function _saveHtml($sDateCode)
{
$sFreenodeNewUrl = env("APP_URL")."/fn/single/".$sDateCode;
$sFreeNodePath = "fn/".$sDateCode.".html";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sFreenodeNewUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$sFreeNodeNewView = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 获取 HTTP 响应代码
if ($httpCode !== 200) {
ArticleList::where("code", $sDateCode)->delete();
$sMsg = "fn站saveHtml时发生了 $httpCode 错误 -> ".$sFreenodeNewUrl;
TeleSlave::warn()->enableSlaveQueue(false)->send($sMsg);
echo $sMsg." -> ".$sFreeNodeNewView;
exit;
}
curl_close($ch);
file_put_contents($sFreeNodePath, $sFreeNodeNewView);
$sHtmlUrl = env("APP_URL")."/fn/".$sDateCode.".html";
return $sHtmlUrl;
}
private function endingByClient($sFnClient)
{
if (isset(self::$sEnding[$sFnClient])) {
$sEnding = self::$sEnding[$sFnClient];
} else {
$sEnding = ".none";
}
return $sEnding;
}
private function _saveArticle($sDateCode)
{
$sTime = time();
$sTimePrev = strtotime("-1 day");
$sDateCode = date("Ymd", $sTime);
$sDateCodePrev = date("Ymd", $sTimePrev);
$aOption = ModelOption::_all();
//// 图片
$iPrevImgId = ArticleList::where("code", $sDateCodePrev)->first()?->img_id;
if ($iPrevImgId >= env('article_img_count')) {
$iImgId = 1;
} else {
$iImgId = $iPrevImgId + 1;
}
//// 地区
$iCountryLimit = rand(5, 12);
$aCountryKey = array_rand(self::$aCountry, $iCountryLimit);
$aCountry = [];
foreach ($aCountryKey as $sCountryKey) {
$aCountry[] = self::$aCountry[$sCountryKey];
}
$sNodeZone = implode("、", $aCountry);
//// 标题
$aTitlePool = [
date('Y', $sTime).$aOption["freenode_title"]
];
$iTitlePoolRandKey = array_rand($aTitlePool);
$sTitle = $aTitlePool[$iTitlePoolRandKey];
//// 内容
$aOptionFnClient = explode(",", $aOption["freenode_client"]);
$iNodeSpeed = rand((int) env("freenode_speed_min"), (int) env("freenode_speed_max"));
$iNodeCount = rand((int) env("freenode_count_min"), (int) env("freenode_count_max"));
$sContent = "
<p>本次更新共".$iNodeCount."个可用节点,最高速度".$iNodeSpeed."M/S。</p>
<p>覆盖".$sNodeZone."等多个区域。</p>
<p>复制下方的v2ray/Clash订阅链接,在客户端添加即可正常使用。<br />
";
// 副标题
$sTitleSub = "
本次更新共".$iNodeCount."个可用节点,最高速度".$iNodeSpeed."M/S。
覆盖".$sNodeZone."等多个区域。
复制下方的v2ray/Clash订阅链接,在客户端添加即可正常使用。
";
// 标签
$aTagPool = [
'[1][2][3][4][5][6][7][8][11]',
'[11][3][1][2][5][6][4][8][7]',
'[3][2][1][4][7][8][6][5][4]',
'[4][3][1][2][5][8][7][6][5]',
];
$iTagPoolRandKey = array_rand($aTagPool);
$sTag = $aTagPool[$iTagPoolRandKey];
$oFreenoodList = new ArticleList();
$oFreenoodList->img_id = $iImgId;
$oFreenoodList->date_str = date('n月j日', $sTime);
$oFreenoodList->created_at = $sTime;
$oFreenoodList->title = $sTitle;
$oFreenoodList->code = $sDateCode;
$oFreenoodList->tags = $sTag;
$oFreenoodList->type = 'node';
$oFreenoodList->content = $sContent;
$oFreenoodList->title_sub = $sTitleSub;
$r = $oFreenoodList->save();
if ($r != true) {
$sMsg = " - article insert - fail.";
echo $sMsg;
TeleSlave::warn()->enableSlaveQueue(false)->send($sMsg);
}
return true;
}
}