Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit 98dc447

Browse files
authored
Merge pull request #10 from AppleBlockTeam/dev
同步Dev
2 parents d165d10 + 77a8132 commit 98dc447

File tree

3 files changed

+63
-67
lines changed

3 files changed

+63
-67
lines changed

inc/server.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function startserver() {
2525
'ssl_cert_file' => './cert/'.$this->cert,
2626
'ssl_key_file' => './cert/'.$this->key,
2727
'open_http2_protocol' => true,
28+
'max_connection' => 10000,
2829
]);
2930
$server->handle('/', function ($request, $response) {
3031
$code = 404;
@@ -67,7 +68,7 @@ public function startserver() {
6768
$code = 206;
6869
$response->header('Content-Type', 'application/octet-stream');
6970
if(isset($request->header['name'])){
70-
$response->header('Content-Disposition', $allurl['name']);
71+
$response->header('Content-Disposition', 'attachment; filename='.$allurl['name']);
7172
}
7273
$response->header('x-bmclapi-hash', $downloadhash);
7374
$response->sendfile($filepath,$start_byte,$length);
@@ -82,7 +83,7 @@ public function startserver() {
8283
$code = 200;
8384
$response->header('Content-Type', 'application/octet-stream');
8485
if(isset($request->header['name'])){
85-
$response->header('Content-Disposition', $allurl['name']);
86+
$response->header('Content-Disposition', 'attachment; filename='.$allurl['name']);
8687
}
8788
$response->header('x-bmclapi-hash', $downloadhash);
8889
$response->sendfile($filepath);
@@ -108,9 +109,7 @@ public function startserver() {
108109
$url = $request->server['request_uri']."?".$request->server['query_string'];
109110
}
110111
//真的会有启动器不带ua的啊,太神奇了
111-
if(!isset($request->server['user-agent'])){
112-
$request->server['user-agent'] = "other";
113-
}
112+
$request->server['user-agent'] = $request->server['user-agent'] ?? "other";
114113
mlog(" Serve {$code} | {$request->server['remote_addr']} | {$request->server['server_protocol']} | {$url} | {$request->header['user-agent']};") ;
115114
});
116115
$server->handle('/measure', function ($request, $response) {

inc/socketio.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ public function connect() {
5050
}
5151
if ($code[0] == '41'){
5252
mlog("[socket.io]Close Connection");
53-
global $pid;
54-
posix_kill($pid, SIGINT);
53+
exits();
5554
$client->close();
5655
return;
5756
}
@@ -77,8 +76,7 @@ public function connect() {
7776
}
7877
elseif (isset($jsondata[0][1]) && $jsondata[0][1] == "0"){
7978
mlog("[socket.io]节点已掉线");
80-
global $pid;
81-
posix_kill($pid, SIGINT);
79+
exits();
8280
}
8381
elseif (isset($jsondata[0][1]) && $this->IsTime($jsondata[0][1])){
8482
global $kadata;
@@ -88,8 +86,7 @@ public function connect() {
8886
mlog("[socket.io]Got data {$jsondata[0][0]["message"]}");
8987
if (strpos($jsondata[0][0]["message"], "Error") !== false) {
9088
mlog("[socket.io]节点启用失败");
91-
global $pid;
92-
posix_kill($pid, SIGINT);
89+
exits();
9390
}
9491
}
9592
else {

main.php

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
use function Swoole\Timer;
55
declare(ticks=1)
66
date_default_timezone_set('Asia/Shanghai');
7-
set_time_limit(0);
8-
ini_set('memory_limit', '-1');
97
require './config.php';
108
const PHPOBAVERSION = '1.5.0';
119
const VERSION = '1.10.5';
@@ -23,23 +21,19 @@
2321
$enable = false;
2422
echo"OpenBmclApionPHP v". PHPOBAVERSION . "-" . VERSION . PHP_EOL;
2523
run(function()use ($config){
26-
//注册信号处理器
24+
//注册信号处理器、
25+
function exits() {
26+
global $shouldExit;
27+
global $tokentimerid;
28+
$shouldExit = true; // 设置退出标志
29+
Swoole\Timer::clear($tokentimerid);
30+
echo PHP_EOL;
31+
mlog("正在退出...");
32+
}
2733
function registerSigintHandler() {
28-
global $tokentimerid;
2934
$shouldExit = false; // 初始化为false
30-
Swoole\Process::signal(SIGINT, function ($signo) use ($tokentimerid) {
31-
try {
32-
global $shouldExit;
33-
$shouldExit = true; // 设置退出标志
34-
Swoole\Timer::clear($tokentimerid);
35-
echo PHP_EOL;
36-
mlog("正在退出...");
37-
exit();
38-
} catch (\Swoole\ExitException $e) {
39-
//var_dump($e->getMessage());
40-
//var_dump($e->getStatus() === 1);
41-
//var_dump($e->getFlags() === SWOOLE_EXIT_IN_COROUTINE);
42-
}
35+
Swoole\Process::signal(SIGINT, function ($signo){
36+
exits();
4337
});
4438
}
4539
//获取初次Token
@@ -55,7 +49,49 @@ function registerSigintHandler() {
5549
});
5650
registerSigintHandler();
5751
mlog("Timer start on ID{$tokentimerid}",1);
58-
52+
//建立socketio连接主控
53+
$socketio = new socketio(OPENBMCLAPIURL,$tokendata['token'],$config['advanced']['keepalive']);
54+
mlog("正在连接主控");
55+
Coroutine::create(function () use ($socketio){
56+
$socketio->connect();
57+
});
58+
Coroutine::sleep(1);
59+
//获取证书
60+
$socketio->ack("request-cert");
61+
Coroutine::sleep(1);
62+
$allcert = $socketio->Getcert();
63+
//写入证书并且是否损坏
64+
if (!file_exists('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt') && !file_exists('./cert/'.$config['cluster']['CLUSTER_ID'].'.key')) {
65+
mlog("正在获取证书");
66+
if (!file_exists("./cert")) {
67+
mkdir("./cert",0777,true);
68+
}
69+
mlog("已获取证书,到期时间{$allcert['0']['1']['expires']}");
70+
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt', 'w');
71+
$Writtencert = fwrite($cert, $allcert['0']['1']['cert']);
72+
fclose($cert);
73+
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.key', 'w');
74+
$Writtencert = fwrite($cert, $allcert['0']['1']['key']);
75+
fclose($cert);
76+
}
77+
$crt = file_get_contents('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt');
78+
if ($crt!== $allcert['0']['1']['cert']) {
79+
mlog("证书损坏/过期");
80+
mlog("已获取新的证书,到期时间{$allcert['0']['1']['expires']}");
81+
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt', 'w');
82+
$Writtencert = fwrite($cert, $allcert['0']['1']['cert']);
83+
fclose($cert);
84+
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.key', 'w');
85+
$Writtencert = fwrite($cert, $allcert['0']['1']['key']);
86+
fclose($cert);
87+
}
88+
//启动http服务器
89+
global $httpserver;
90+
$httpserver = new fileserver($config['cluster']['host'],$config['cluster']['port'],$config['cluster']['CLUSTER_ID'].'.crt',$config['cluster']['CLUSTER_ID'].'.key',$config['cluster']['CLUSTER_SECRET']);
91+
Coroutine::create(function () use ($config,$httpserver){
92+
$httpserver->startserver();
93+
});
94+
5995
//下载文件列表
6096
$cluster = new cluster($tokendata['token'],VERSION);
6197
$files = $cluster->getFileList();
@@ -69,6 +105,7 @@ function registerSigintHandler() {
69105
elseif($config['file']['check'] == "exists"){
70106
$Missfile = $FilesCheck->FilesCheckerexists();
71107
}
108+
//循环到没有Missfile这个变量
72109
if (is_array($Missfile)){
73110
mlog("缺失/损坏".count($Missfile)."个文件");
74111
while(is_array($Missfile)){
@@ -100,44 +137,7 @@ function registerSigintHandler() {
100137
}
101138
global $shouldExit;
102139
if (!is_array($Missfile) && !$shouldExit){//判断Missfile是否为空和是否是主动退出
103-
$socketio = new socketio(OPENBMCLAPIURL,$tokendata['token'],$config['advanced']['keepalive']);
104-
mlog("正在连接主控");
105-
Coroutine::create(function () use ($socketio){
106-
$socketio->connect();
107-
});
108-
Coroutine::sleep(1);
109-
$socketio->ack("request-cert");
110-
Coroutine::sleep(1);
111-
$allcert = $socketio->Getcert();
112-
if (!file_exists('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt') && !file_exists('./cert/'.$config['cluster']['CLUSTER_ID'].'.key')) {
113-
mlog("正在获取证书");
114-
if (!file_exists("./cert")) {
115-
mkdir("./cert",0777,true);
116-
}
117-
mlog("已获取证书,到期时间{$allcert['0']['1']['expires']}");
118-
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt', 'w');
119-
$Writtencert = fwrite($cert, $allcert['0']['1']['cert']);
120-
fclose($cert);
121-
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.key', 'w');
122-
$Writtencert = fwrite($cert, $allcert['0']['1']['key']);
123-
fclose($cert);
124-
}
125-
$crt = file_get_contents('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt');
126-
if ($crt!== $allcert['0']['1']['cert']) {
127-
mlog("证书损坏/过期");
128-
mlog("已获取新的证书,到期时间{$allcert['0']['1']['expires']}");
129-
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt', 'w');
130-
$Writtencert = fwrite($cert, $allcert['0']['1']['cert']);
131-
fclose($cert);
132-
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.key', 'w');
133-
$Writtencert = fwrite($cert, $allcert['0']['1']['key']);
134-
fclose($cert);
135-
}
136-
global $httpserver;
137-
$httpserver = new fileserver($config['cluster']['host'],$config['cluster']['port'],$config['cluster']['CLUSTER_ID'].'.crt',$config['cluster']['CLUSTER_ID'].'.key',$config['cluster']['CLUSTER_SECRET']);
138-
Coroutine::create(function () use ($config,$httpserver){
139-
$httpserver->startserver();
140-
});
140+
//enable节点
141141
$socketio->enable($config['cluster']['public_host'],$config['cluster']['public_port'],$config['cluster']['byoc']);
142142
}
143143
});

0 commit comments

Comments
 (0)