From 810c81469e6f1460fed154e38ee5bd201964860c Mon Sep 17 00:00:00 2001 From: Mxmilu666 Date: Sun, 12 May 2024 14:38:01 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20=E5=B0=86http=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E6=94=BE=E5=88=B0=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=EF=BC=8C=E4=B8=BAapi=E5=81=9A=E5=87=86=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/server.php | 4 +-- main.php | 88 ++++++++++++++++++++++++++------------------------ 2 files changed, 47 insertions(+), 45 deletions(-) diff --git a/inc/server.php b/inc/server.php index 5221691..bd5f823 100644 --- a/inc/server.php +++ b/inc/server.php @@ -108,9 +108,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) { diff --git a/main.php b/main.php index cf1d661..3887911 100644 --- a/main.php +++ b/main.php @@ -4,15 +4,13 @@ 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.3.0'; const VERSION = '1.10.4'; global $DOWNLOAD_DIR; $DOWNLOAD_DIR = $config['file']['cache_dir']; const USERAGENT = 'openbmclapi-cluster/' . VERSION . ' ' . 'PHP-OpenBmclApi/'.PHPOBAVERSION; -const OPENBMCLAPIURL = 'openbmclapi.bangbang93.com'; +const OPENBMCLAPIURL = 'openbmclapi.staging.bangbang93.com'; $list = glob('inc/*.php'); foreach ($list as $file) { require $file; @@ -55,7 +53,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(); @@ -69,6 +109,7 @@ function registerSigintHandler() { elseif($config['file']['check'] == "exists"){ $Missfile = $FilesCheck->FilesCheckerexists(); } + //循环到没有Missfile这个变量 if (is_array($Missfile)){ mlog("缺失/损坏".count($Missfile)."个文件"); while(is_array($Missfile)){ @@ -100,44 +141,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']); } }); \ No newline at end of file From 541df39fa6c52e003006497ab0c66c94bebc8788 Mon Sep 17 00:00:00 2001 From: Mxmilu666 Date: Sat, 18 May 2024 13:18:43 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E8=84=91=E6=BA=A2=E8=A1=80=E5=86=99=E7=9A=84=E4=BC=98=E9=9B=85?= =?UTF-8?q?=E9=80=80=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/server.php | 5 +++-- inc/socketio.php | 13 +++++-------- main.php | 26 +++++++++++--------------- 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/inc/server.php b/inc/server.php index bd5f823..eb96a03 100644 --- a/inc/server.php +++ b/inc/server.php @@ -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; @@ -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); @@ -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); diff --git a/inc/socketio.php b/inc/socketio.php index 0ca339e..e6358a9 100644 --- a/inc/socketio.php +++ b/inc/socketio.php @@ -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; } @@ -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; @@ -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 { @@ -130,7 +127,7 @@ public function Getcert() { Swoole\Coroutine\System::sleep(1); $time++; } - if ($time = 30){ + if ($time == 30){ mlog("Getcert Connected Overtime",2); return(false); } @@ -146,7 +143,7 @@ public function ack($event,$data = "") { return($this->client->push('420'.$senddata)); } } - if ($time = 30){ + if ($time == 30){ mlog("[socket.io]ACK Connected Overtime",2); return(false); } diff --git a/main.php b/main.php index 3887911..2efa40a 100644 --- a/main.php +++ b/main.php @@ -21,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 From f98fd09b06db37fcaa11d6ac124d83ad9b387987 Mon Sep 17 00:00:00 2001 From: Mxmilu666 Date: Sat, 18 May 2024 13:19:55 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=8B=BC=E5=86=99=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.php b/main.php index 2efa40a..1b0daaa 100644 --- a/main.php +++ b/main.php @@ -5,12 +5,12 @@ declare(ticks=1) date_default_timezone_set('Asia/Shanghai'); require './config.php'; -const PHPOBAVERSION = '1.3.0'; +const PHPOBAVERSION = '1.5.0'; const VERSION = '1.10.4'; global $DOWNLOAD_DIR; $DOWNLOAD_DIR = $config['file']['cache_dir']; const USERAGENT = 'openbmclapi-cluster/' . VERSION . ' ' . 'PHP-OpenBmclApi/'.PHPOBAVERSION; -const OPENBMCLAPIURL = 'openbmclapi.staging.bangbang93.com'; +const OPENBMCLAPIURL = 'openbmclapi.bangbang93.com'; $list = glob('inc/*.php'); foreach ($list as $file) { require $file;