139 lines
4.5 KiB
PHP
139 lines
4.5 KiB
PHP
<?php
|
||
|
||
namespace App\Services\Tg\Hook\Mod;
|
||
|
||
use App\Models\Node as ModelNode;
|
||
use App\Services\Tg\Hook\Dog;
|
||
use App\Services\TomTool\Http;
|
||
use Illuminate\Support\Facades\DB;
|
||
use App\Utils\Tools;
|
||
|
||
class Node extends Base {
|
||
|
||
private $oDog;
|
||
|
||
public function __construct($aUpdate)
|
||
{
|
||
|
||
$this->oDog = new Dog(new ModelNode());
|
||
$this->oDog->_setDogName("node"); // 必须,me用,正常就是类名,只不过后期没准改名,所以就这里写死
|
||
// $this->oDog->_setPrimaryKey("code"); // 可选,默认id
|
||
// $this->oDog->_setStrField(["content"]); // 可选,指定长文本字段
|
||
$this->oDog->_setJsonField(["custom_config", "dynamic_rate_config"]);
|
||
$this->oDog->_setHideField(["gfw_block", "dynamic_rate_type", "dynamic_rate_config", "password", "online_user_date_last"]);
|
||
$this->oDog->_setInFormat(["node_bandwidth_limit", "node_bandwidth"]);
|
||
$this->oDog->_setOutFormat([
|
||
"node_heartbeat" => "date",
|
||
"node_bandwidth_limit" => "gb",
|
||
"node_bandwidth" => "gb"
|
||
]);
|
||
}
|
||
|
||
public function __call($sName, $aArg)
|
||
{
|
||
|
||
return $this->oDog->$sName($aArg[0]);
|
||
|
||
}
|
||
|
||
public function fly($aParam)
|
||
{
|
||
// $sCmd = $aParam["cmd"] ?? "";
|
||
|
||
$oServers = ModelNode::where("type", 2)->orderBy("order_group")->get();
|
||
|
||
//// 取出pwd
|
||
$aPwds = [];
|
||
foreach ($oServers as $oServer) {
|
||
$aChildPwds = ModelNode::where("pid", $oServer->id)->pluck("password")->toArray();
|
||
$aPwds[$oServer->id] = $aChildPwds;
|
||
}
|
||
|
||
//// 清空旧node
|
||
ModelNode::where("type", 1)->delete();
|
||
|
||
//// new分身
|
||
$aNodes = [];
|
||
foreach ($oServers as $oServer) {
|
||
for ($i=0; $i<$oServer->fly; $i++) {
|
||
$oNode = $oServer->replicate();
|
||
$oNode->pid = $oServer->id;
|
||
$aNodes[$oServer->order_group][] = $oNode;
|
||
}
|
||
}
|
||
|
||
//// 打乱、命名、落库
|
||
$aNodeNameCodeCount = [];
|
||
$iDone = 0;
|
||
$iNodeId = 1001;
|
||
foreach ($aNodes as $iOrderGroup => $aNodeOrderGroups) {
|
||
shuffle($aNodeOrderGroups);
|
||
foreach ($aNodeOrderGroups as $oNode) {
|
||
if (preg_match('/\$([^\$]+)\$/', $oNode->name, $aMatches)) {
|
||
$sNodeNameCode = $aMatches[1];
|
||
} else {
|
||
continue;
|
||
}
|
||
|
||
if (!isset($aNodeNameCodeCount[$sNodeNameCode])) {
|
||
$aNodeNameCodeCount[$sNodeNameCode] = 1;
|
||
}
|
||
|
||
$iNodeNum = $aNodeNameCodeCount[$sNodeNameCode]++;
|
||
$iNodeNum = sprintf("%02d", $iNodeNum);
|
||
|
||
$oNode->id = $iNodeId;
|
||
$oNode->name = preg_replace('/\$.*?\$/', $iNodeNum, $oNode->name);
|
||
$oNode->type = 1;
|
||
$oNode->node_bandwidth = 0;
|
||
$oNode->node_bandwidth_limit = 0;
|
||
$oNode->bandwidthlimit_resetday = 0;
|
||
$oNode->duck_code = str_replace('server', 'node', $oNode->duck_code);
|
||
$oNode->duck_group = str_replace('server', 'node', $oNode->duck_group);
|
||
$oNode->is_shadow = 1;
|
||
$oNode->password = array_pop($aPwds[$oNode->pid]) ?? Tools::genRandomChar(32);
|
||
$iDone += $oNode->save();
|
||
$iNodeId++;
|
||
}
|
||
}
|
||
|
||
return "搞定,生成".$iDone."节点";
|
||
}
|
||
|
||
public function clone($aParam)
|
||
{
|
||
|
||
// list(, $iNodeId) = explode(" ", $sText, 2);
|
||
|
||
$iNodeId = $aParam['aArg'][1];
|
||
|
||
$oHttp = new Http();
|
||
|
||
$oHttp->sMethod = "post";
|
||
$oHttp->sToUrl = $_ENV['baseUrl']."/api/tg/node/".$iNodeId."/copy";
|
||
$oHttp->bIsJson = true;
|
||
$oHttp->aData = [
|
||
'chatId' => $aParam['aOther']['chatId'],
|
||
'fromId' => $aParam['aOther']['fromId']
|
||
];
|
||
|
||
$r = @json_decode($oHttp->send(), true);
|
||
|
||
if ($r["ret"] == 1) {
|
||
// $sMessage = "成功";
|
||
} else {
|
||
$sMessage = "失败";
|
||
return $sMessage;
|
||
}
|
||
|
||
$id = ModelNode::where("type", 1)->orderBy("id", "desc")->first()?->id;
|
||
|
||
$aParam = [];
|
||
$aParam["aArg"][1] = $id;
|
||
|
||
return $this->find($aParam);
|
||
|
||
}
|
||
|
||
}
|