Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions inc/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function startserver() {
'ssl_cert_file' => './cert/'.$this->cert,
'ssl_key_file' => './cert/'.$this->key,
'open_http2_protocol' => true,
'max_connection' => 10000,
]);
$server->handle('/', function ($request, $response) {
$code = 404;
Expand Down Expand Up @@ -67,7 +68,7 @@ public function startserver() {
$code = 206;
$response->header('Content-Type', 'application/octet-stream');
if(isset($request->header['name'])){
$response->header('Content-Disposition', $allurl['name']);
$response->header('Content-Disposition', 'attachment; filename='.$allurl['name']);
}
$response->header('x-bmclapi-hash', $downloadhash);
$response->sendfile($filepath,$start_byte,$length);
Expand All @@ -82,7 +83,7 @@ public function startserver() {
$code = 200;
$response->header('Content-Type', 'application/octet-stream');
if(isset($request->header['name'])){
$response->header('Content-Disposition', $allurl['name']);
$response->header('Content-Disposition', 'attachment; filename='.$allurl['name']);
}
$response->header('x-bmclapi-hash', $downloadhash);
$response->sendfile($filepath);
Expand All @@ -108,9 +109,7 @@ public function startserver() {
$url = $request->server['request_uri']."?".$request->server['query_string'];
}
//真的会有启动器不带ua的啊,太神奇了
if(!isset($request->server['user-agent'])){
$request->server['user-agent'] = "other";
}
$request->server['user-agent'] = $request->server['user-agent'] ?? "other";
mlog(" Serve {$code} | {$request->server['remote_addr']} | {$request->server['server_protocol']} | {$url} | {$request->header['user-agent']};") ;
});
$server->handle('/measure', function ($request, $response) {
Expand Down
9 changes: 3 additions & 6 deletions inc/socketio.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public function connect() {
}
if ($code[0] == '41'){
mlog("[socket.io]Close Connection");
global $pid;
posix_kill($pid, SIGINT);
exits();
$client->close();
return;
}
Expand All @@ -77,8 +76,7 @@ public function connect() {
}
elseif (isset($jsondata[0][1]) && $jsondata[0][1] == "0"){
mlog("[socket.io]节点已掉线");
global $pid;
posix_kill($pid, SIGINT);
exits();
}
elseif (isset($jsondata[0][1]) && $this->IsTime($jsondata[0][1])){
global $kadata;
Expand All @@ -88,8 +86,7 @@ public function connect() {
mlog("[socket.io]Got data {$jsondata[0][0]["message"]}");
if (strpos($jsondata[0][0]["message"], "Error") !== false) {
mlog("[socket.io]节点启用失败");
global $pid;
posix_kill($pid, SIGINT);
exits();
}
}
else {
Expand Down
112 changes: 56 additions & 56 deletions main.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
use function Swoole\Timer;
declare(ticks=1)
date_default_timezone_set('Asia/Shanghai');
set_time_limit(0);
ini_set('memory_limit', '-1');
require './config.php';
const PHPOBAVERSION = '1.5.0';
const VERSION = '1.10.5';
Expand All @@ -23,23 +21,19 @@
$enable = false;
echo"OpenBmclApionPHP v". PHPOBAVERSION . "-" . VERSION . PHP_EOL;
run(function()use ($config){
//注册信号处理器
//注册信号处理器、
function exits() {
global $shouldExit;
global $tokentimerid;
$shouldExit = true; // 设置退出标志
Swoole\Timer::clear($tokentimerid);
echo PHP_EOL;
mlog("正在退出...");
}
function registerSigintHandler() {
global $tokentimerid;
$shouldExit = false; // 初始化为false
Swoole\Process::signal(SIGINT, function ($signo) use ($tokentimerid) {
try {
global $shouldExit;
$shouldExit = true; // 设置退出标志
Swoole\Timer::clear($tokentimerid);
echo PHP_EOL;
mlog("正在退出...");
exit();
} catch (\Swoole\ExitException $e) {
//var_dump($e->getMessage());
//var_dump($e->getStatus() === 1);
//var_dump($e->getFlags() === SWOOLE_EXIT_IN_COROUTINE);
}
Swoole\Process::signal(SIGINT, function ($signo){
exits();
});
}
//获取初次Token
Expand All @@ -55,7 +49,49 @@ function registerSigintHandler() {
});
registerSigintHandler();
mlog("Timer start on ID{$tokentimerid}",1);

//建立socketio连接主控
$socketio = new socketio(OPENBMCLAPIURL,$tokendata['token'],$config['advanced']['keepalive']);
mlog("正在连接主控");
Coroutine::create(function () use ($socketio){
$socketio->connect();
});
Coroutine::sleep(1);
//获取证书
$socketio->ack("request-cert");
Coroutine::sleep(1);
$allcert = $socketio->Getcert();
//写入证书并且是否损坏
if (!file_exists('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt') && !file_exists('./cert/'.$config['cluster']['CLUSTER_ID'].'.key')) {
mlog("正在获取证书");
if (!file_exists("./cert")) {
mkdir("./cert",0777,true);
}
mlog("已获取证书,到期时间{$allcert['0']['1']['expires']}");
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt', 'w');
$Writtencert = fwrite($cert, $allcert['0']['1']['cert']);
fclose($cert);
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.key', 'w');
$Writtencert = fwrite($cert, $allcert['0']['1']['key']);
fclose($cert);
}
$crt = file_get_contents('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt');
if ($crt!== $allcert['0']['1']['cert']) {
mlog("证书损坏/过期");
mlog("已获取新的证书,到期时间{$allcert['0']['1']['expires']}");
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt', 'w');
$Writtencert = fwrite($cert, $allcert['0']['1']['cert']);
fclose($cert);
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.key', 'w');
$Writtencert = fwrite($cert, $allcert['0']['1']['key']);
fclose($cert);
}
//启动http服务器
global $httpserver;
$httpserver = new fileserver($config['cluster']['host'],$config['cluster']['port'],$config['cluster']['CLUSTER_ID'].'.crt',$config['cluster']['CLUSTER_ID'].'.key',$config['cluster']['CLUSTER_SECRET']);
Coroutine::create(function () use ($config,$httpserver){
$httpserver->startserver();
});

//下载文件列表
$cluster = new cluster($tokendata['token'],VERSION);
$files = $cluster->getFileList();
Expand All @@ -69,6 +105,7 @@ function registerSigintHandler() {
elseif($config['file']['check'] == "exists"){
$Missfile = $FilesCheck->FilesCheckerexists();
}
//循环到没有Missfile这个变量
if (is_array($Missfile)){
mlog("缺失/损坏".count($Missfile)."个文件");
while(is_array($Missfile)){
Expand Down Expand Up @@ -100,44 +137,7 @@ function registerSigintHandler() {
}
global $shouldExit;
if (!is_array($Missfile) && !$shouldExit){//判断Missfile是否为空和是否是主动退出
$socketio = new socketio(OPENBMCLAPIURL,$tokendata['token'],$config['advanced']['keepalive']);
mlog("正在连接主控");
Coroutine::create(function () use ($socketio){
$socketio->connect();
});
Coroutine::sleep(1);
$socketio->ack("request-cert");
Coroutine::sleep(1);
$allcert = $socketio->Getcert();
if (!file_exists('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt') && !file_exists('./cert/'.$config['cluster']['CLUSTER_ID'].'.key')) {
mlog("正在获取证书");
if (!file_exists("./cert")) {
mkdir("./cert",0777,true);
}
mlog("已获取证书,到期时间{$allcert['0']['1']['expires']}");
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt', 'w');
$Writtencert = fwrite($cert, $allcert['0']['1']['cert']);
fclose($cert);
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.key', 'w');
$Writtencert = fwrite($cert, $allcert['0']['1']['key']);
fclose($cert);
}
$crt = file_get_contents('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt');
if ($crt!== $allcert['0']['1']['cert']) {
mlog("证书损坏/过期");
mlog("已获取新的证书,到期时间{$allcert['0']['1']['expires']}");
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt', 'w');
$Writtencert = fwrite($cert, $allcert['0']['1']['cert']);
fclose($cert);
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.key', 'w');
$Writtencert = fwrite($cert, $allcert['0']['1']['key']);
fclose($cert);
}
global $httpserver;
$httpserver = new fileserver($config['cluster']['host'],$config['cluster']['port'],$config['cluster']['CLUSTER_ID'].'.crt',$config['cluster']['CLUSTER_ID'].'.key',$config['cluster']['CLUSTER_SECRET']);
Coroutine::create(function () use ($config,$httpserver){
$httpserver->startserver();
});
//enable节点
$socketio->enable($config['cluster']['public_host'],$config['cluster']['public_port'],$config['cluster']['byoc']);
}
});