dd/app/Services/Cron/FreenodeCrawl.php
2025-12-18 16:01:13 +08:00

230 lines
7.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace App\Services\Cron;
use App\Models\CrawlFreenode as ModelCrawlFreenode;
use App\Services\TomTool\Telegram\Slave as TeleSlave;
//use Symfony\Component\Yaml\Yaml;
final class FreenodeCrawl
{
// 自有系
// 必然获取到但是密码会变暂定每天8点变意味着8袋内03分获取的是新密码
private static function getFile___my($aFrom)
{
$aFileContent = [];
foreach ($aFrom["url_file"] as $k => $sFileName) {
$sUrlSend = "https://".$aFrom["url_base"].$aFrom["url_node"].$sFileName;
$sFileContent = self::curl($sUrlSend) ?? "";
if ($sFileContent) {
$aFileContent[] = [
"type" => $k,
"file_name" => $sFileName,
"file_content" => $sFileContent
];
}
}
return $aFileContent;
}
// stairnode系
// ripaojiedian系stairnode本源
// 这个可能会没有,没有就不更新
private static function getFile__A($aFrom)
{
$aFileContent = [];
foreach ($aFrom["url_file"] as $k => $v) {
$sFileName = str_replace("{{Ymd}}", date("Ymd"), $v);
$sUrlSend = "http://".$aFrom["url_base"].$aFrom["url_node"].$sFileName;
$sFileContent = self::curl($sUrlSend) ?? "";
if (!$sFileContent) {
return [];
}
if ($k == "v2ray") {
$sFileContent = base64_decode($sFileContent);
}
if ($sFileContent) {
$aFileContent[] = [
"type" => $k,
"file_name" => $sFileName,
"file_content" => $sFileContent
];
}
}
return $aFileContent;
}
// v2rayshare系
// 这必然有,和下一天一致说明没更新(但是好像改了,现在它是一次生成很多天)
private static function getFile__B($aFrom)
{
$aFileContent = [];
$sTime = time();
$sTimeNext = strtotime("+1 day", $sTime);
$sUrlBase = $aFrom["url_base"];
$sUrlNode = $aFrom["url_node"];
$sUrlNode = str_replace("{{Y}}", date("Y"), $sUrlNode);
$sUrlNode = str_replace("{{m}}", date("m"), $sUrlNode);
$sUrlNodeNext = str_replace("{{Y}}", date("Y", $sTimeNext), $sUrlNode);
$sUrlNodeNext = str_replace("{{m}}", date("m", $sTimeNext), $sUrlNode);
foreach ($aFrom["url_file"] as $k => $v) {
$sUrlFile = str_replace("{{Ymd}}", date("Ymd"), $v);
$sUrlFileNext = str_replace("{{Ymd}}", date("Ymd"), $v);
$sHouzhui = ".".$k;
if ($k == "v2ray") {
$sHouzhui = ".txt";
} else if ($k == "clash") {
$sHouzhui = ".yaml";
}
$sFileName = date("Ymd")."-".$k.$sHouzhui;
$sUrlSend = "https://".$sUrlBase.$sUrlNode.$sUrlFile;
$sUrlSendNext = "https://".$sUrlBase.$sUrlNodeNext.$sUrlFileNext;
$sFileContent = self::curl($sUrlSend) ?? "";
// $sFileContentNext = self::curl($sUrlSendNext) ?? "";
if (!$sFileContent) {
return [];
}
if ($k == "v2ray") {
$sFileContent = base64_decode($sFileContent);
}
$sFileContentMd5 = md5($sFileContent);
// $sFileContentMd5Next = md5($sFileContentNext);
// if ($sFileContentMd5 != $sFileContentMd5Next) {
$aFileContent[] = [
"type" => $k,
"file_name" => $sFileName,
"file_content" => $sFileContent
];
// }
}
return $aFileContent;
}
private static function fileRealUpd($iId, $sType, $sFileName)
{
$oFrom = ModelCrawlFreenode::where("id", $iId)->first();
$aFileReal = json_decode($oFrom->file_real ?? "[]", true);
$aFileReal[$sType] = $sFileName;
$oFrom->file_real = $aFileReal;
return $oFrom->save();
}
public static function run()
{
echo "\nCrawlFreenode::caseA - 开始\n";
$aFromList = ModelCrawlFreenode::where("status", 1)->get()->toArray();
foreach ($aFromList as $aFrom) {
$aFrom["url_file"] = json_decode($aFrom["url_file"], true);
$sMethod = "getFile__".$aFrom["group"];
$aFileList = self::$sMethod($aFrom); // 没有就返回空数组所以不用判断直接foreach
foreach ($aFileList as $sFileRow) {
$sType = $sFileRow["type"];
$sHouzhui = "";
if ($sType == "v2ray") {
$sHouzhui = ".txt";
} else if ($sType == "clash") {
$sHouzhui = ".yaml";
}
$sFileName = date("Ymd")."-".$sType.$sHouzhui;
$sFileContent = $sFileRow["file_content"];
$sDir = "freenode/from/".$aFrom["name"];
$sPath = $sDir."/".$sFileName;
if (!is_dir($sDir)) {
mkdir($sDir, 0777, true);
}
// if ($sType == "v2ray") {
// $sFileContent = base64_decode($sFileContent);
// }
file_put_contents($sPath, $sFileContent);
self::fileRealUpd($aFrom["id"], $sType, $sFileName);
echo "- ".$aFrom["name"]."::".$sFileName."\n";
}
}
}
private static function curl($sUrl)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回内容而不是直接输出
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 如果是 https可以关闭证书验证测试用
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 关闭主机名验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 关闭主机名验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$sContent = curl_exec($ch);
if ($sContent === false) {
$error = curl_error($ch);
$errno = curl_errno($ch);
$sMsg = "cURL error ({$errno}): {$error}";
echo $sMsg;
TeleSlave::log()->send($sMsg);
}
curl_close($ch);
return $sContent;
}
private static function fileGet($sUrl)
{
$sContent = file_get_contents($sUrl);
return $sContent;
}
}