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

Commit 9e2d51a

Browse files
committed
♻️ 把大部分的global变量移到class里面了,方便插件和api调用
1 parent c006de8 commit 9e2d51a

File tree

7 files changed

+103
-45
lines changed

7 files changed

+103
-45
lines changed

inc/api.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
class api{
3+
private static $config = [];
4+
private static $info = ['enable'=>false,'isSynchronized'=>true];
5+
private static $server;
6+
7+
public static function getconfig($newConfig = null) {
8+
if (!is_null($newConfig)) {
9+
self::$config = $newConfig;
10+
}
11+
return self::$config;
12+
}
13+
14+
public static function getinfo($newinfo = null) {
15+
if (!is_null($newinfo)) {
16+
self::$info = $newinfo;
17+
}
18+
return self::$info;
19+
}
20+
public static function getserver($newserver = null) {
21+
if (!is_null($newserver)) {
22+
self::$server = $newserver;
23+
}
24+
return self::$server;
25+
}
26+
}

inc/cluster.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ public function __construct($token,$version){
8383
$this->version = $version;
8484
}
8585
public function getFileList() {
86-
global $DOWNLOAD_DIR;
87-
if (!file_exists($DOWNLOAD_DIR."/filecache")) {
88-
mkdir($DOWNLOAD_DIR."/filecache",0777,true);
86+
$download_dir = api::getconfig()['file']['cache_dir'];
87+
if (!file_exists($download_dir."/filecache")) {
88+
mkdir($download_dir."/filecache",0777,true);
8989
}
9090
$client = new Client(OPENBMCLAPIURL,443,true);
9191
$client->set(['timeout' => -1]);
@@ -122,14 +122,14 @@ public function __construct($filesList = [], $maxConcurrent = 1) {
122122
}
123123

124124
private function downloader(Swoole\Coroutine\Http\Client $client, $file,$bar) {
125-
global $DOWNLOAD_DIR;
126-
$filePath = $DOWNLOAD_DIR . '/' . substr($file->hash, 0, 2) . '/';
125+
$download_dir = api::getconfig()['file']['cache_dir'];
126+
$filePath = $download_dir . '/' . substr($file->hash, 0, 2) . '/';
127127
if (!file_exists($filePath)) {
128128
mkdir($filePath, 0777, true);
129129
}
130130
$savePath = $filePath . $file->hash;
131131
$file->path = str_replace(' ', '%20', $file->path);
132-
$downloader = $client->download($file->path,$DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
132+
$downloader = $client->download($file->path,$download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
133133
if (!$downloader) {
134134
mlog("Error connecting to the main control:{$client->errMsg}",2);
135135
return false;
@@ -151,7 +151,7 @@ private function downloader(Swoole\Coroutine\Http\Client $client, $file,$bar) {
151151
'User-Agent' => USERAGENT,
152152
'Accept' => '*/*',
153153
]);
154-
$downloader = $client->download($location_url['path'].'?'.($location_url['query']??''),$DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
154+
$downloader = $client->download($location_url['path'].'?'.($location_url['query']??''),$download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
155155
if (in_array($client->statusCode, [301, 302])) {
156156
while(in_array($client->statusCode, [301, 302])){
157157
$location_url = parse_url($client->getHeaders()['location']);
@@ -168,7 +168,7 @@ private function downloader(Swoole\Coroutine\Http\Client $client, $file,$bar) {
168168
'User-Agent' => USERAGENT,
169169
'Accept' => '*/*',
170170
]);
171-
$downloader = $client->download($location_url['path'].'?'.($location_url['query']??''),$DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
171+
$downloader = $client->download($location_url['path'].'?'.($location_url['query']??''),$download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
172172
}
173173
if (!$downloader) {
174174
echo PHP_EOL;
@@ -191,7 +191,7 @@ private function downloader(Swoole\Coroutine\Http\Client $client, $file,$bar) {
191191
}
192192
elseif($client->statusCode >= 400){
193193
echo PHP_EOL;
194-
mlog("Download Failed:{$client->statusCode} | {$file->path} | {$location_url['host']}:{$location_url['port']}",2);
194+
mlog("{$file->path} Download Failed: {$client->statusCode} | {$location_url['host']}:{$location_url['port']}",2);
195195
$bar->progress();
196196
return false;
197197
}
@@ -211,7 +211,7 @@ private function downloader(Swoole\Coroutine\Http\Client $client, $file,$bar) {
211211

212212
public function downloadFiles() {
213213
$bar = new CliProgressBar(count($this->filesList));
214-
$bar->setDetails("[Downloader]");
214+
$bar->setDetails("[Downloader][线程数:{$this->maxConcurrent}]");
215215
$bar->display();
216216
foreach ($this->filesList as $file) {
217217
global $shouldExit;
@@ -246,9 +246,9 @@ public function downloadFiles() {
246246
}
247247

248248
public function downloadnopoen($hash) {
249-
global $DOWNLOAD_DIR;
249+
$download_dir = api::getconfig()['file']['cache_dir'];
250250
global $tokendata;
251-
$filePath = $DOWNLOAD_DIR . '/' . substr($hash, 0, 2) . '/';
251+
$filePath = $download_dir . '/' . substr($hash, 0, 2) . '/';
252252
if (!file_exists($filePath)) {
253253
mkdir($filePath, 0777, true);
254254
}
@@ -263,7 +263,7 @@ public function downloadnopoen($hash) {
263263
'Accept' => '*/*',
264264
'Authorization' => "Bearer {$tokendata['token']}"
265265
]);
266-
$downloader = $client->download($filepath,$DOWNLOAD_DIR.'/'.substr($hash, 0, 2).'/'.$hash);
266+
$downloader = $client->download($filepath,$download_dir.'/'.substr($hash, 0, 2).'/'.$hash);
267267
if (!$downloader) {
268268
mlog("Error download to the main control:{$client->errMsg}",2);
269269
return false;
@@ -290,14 +290,14 @@ public function FilesCheckerhash() {
290290
$bar = new CliProgressBar(count($this->filesList));
291291
$bar->setDetails("[FileCheck]");
292292
$bar->display();
293+
$download_dir = api::getconfig()['file']['cache_dir'];
293294
foreach ($this->filesList as $file) {
294295
global $shouldExit;
295-
global $DOWNLOAD_DIR;
296296
if ($shouldExit) {
297297
return;
298298
break;
299299
}
300-
if (!file_exists($DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash)){
300+
if (!file_exists($download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash)){
301301
$this->Missfile[] = new BMCLAPIFile(
302302
$file->path,
303303
$file->hash,
@@ -306,7 +306,7 @@ public function FilesCheckerhash() {
306306
);
307307
}
308308
else{
309-
if (hash_file('sha1',$DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash) != $file->hash) {
309+
if (hash_file('sha1',$download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash) != $file->hash) {
310310
$this->Missfile[] = new BMCLAPIFile(
311311
$file->path,
312312
$file->hash,
@@ -327,14 +327,14 @@ public function FilesCheckersize() {
327327
$bar = new CliProgressBar(count($this->filesList));
328328
$bar->setDetails("[FileCheck]");
329329
$bar->display();
330+
$download_dir = api::getconfig()['file']['cache_dir'];
330331
foreach ($this->filesList as $file) {
331332
global $shouldExit;
332-
global $DOWNLOAD_DIR;
333333
if ($shouldExit) {
334334
return;
335335
break;
336336
}
337-
if (!file_exists($DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash)){
337+
if (!file_exists($download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash)){
338338
$this->Missfile[] = new BMCLAPIFile(
339339
$file->path,
340340
$file->hash,
@@ -343,7 +343,7 @@ public function FilesCheckersize() {
343343
);
344344
}
345345
else{
346-
if (filesize($DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash) != $file->size) {
346+
if (filesize($download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash) != $file->size) {
347347
$this->Missfile[] = new BMCLAPIFile(
348348
$file->path,
349349
$file->hash,
@@ -364,14 +364,14 @@ public function FilesCheckerexists() {
364364
$bar = new CliProgressBar(count($this->filesList));
365365
$bar->setDetails("[FileCheck]");
366366
$bar->display();
367+
$download_dir = api::getconfig()['file']['cache_dir'];
367368
foreach ($this->filesList as $file) {
368369
global $shouldExit;
369-
global $DOWNLOAD_DIR;
370370
if ($shouldExit) {
371371
return;
372372
break;
373373
}
374-
if (!file_exists($DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash)){
374+
if (!file_exists($download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash)){
375375
$this->Missfile[] = new BMCLAPIFile(
376376
$file->path,
377377
$file->hash,

inc/mlog.class.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,5 @@ function mlog($content, $type = 0, $minimalFormat = false)
4646

4747
function isDebugMode()
4848
{
49-
global $config;
50-
return isset($config['advanced']['Debug']) && $config['advanced']['Debug'];
49+
return isset(api::getconfig()['advanced']['Debug']) && api::getconfig()['advanced']['Debug'];
5150
}

inc/server.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ class fileserver {
1010
private $dir;
1111
private $secret;
1212
public function __construct($host,$port,$cert,$key,$secret) {
13-
global $DOWNLOAD_DIR;
1413
$this->host = $host;
1514
$this->port = $port;
1615
$this->cert = $cert;
1716
$this->key = $key;
18-
$this->dir = $DOWNLOAD_DIR;
17+
$this->dir = api::getconfig()['file']['cache_dir'];
1918
$this->secret = $secret;
2019
}
2120

@@ -57,8 +56,7 @@ public function startserver() {
5756
}
5857
$length = $end_byte - $start_byte + 1;
5958
$fileSize = filesize($filepath);
60-
global $enable;
61-
if ($enable){
59+
if (api::getinfo()['enable']){
6260
global $kacounters;
6361
$kacounters->incr('1','hits');
6462
$kacounters->incr('1','bytes',$length);
@@ -72,7 +70,7 @@ public function startserver() {
7270
$response->sendfile($filepath,$start_byte,$length);
7371
}
7472
else{
75-
global $enable;
73+
$enable = api::getinfo()['enable'];
7674
if ($enable){
7775
global $kacounters;
7876
$kacounters->incr('1','hits');
@@ -155,6 +153,36 @@ public function startserver() {
155153
}
156154
mlog(" Serve {$code} | {$request->server['remote_addr']} | {$request->server['server_protocol']} | {$url} | {$request->header['user-agent']};") ;
157155
});
156+
157+
$server->handle('/api/cluster', function ($request, $response) {
158+
$type = $request->server['request_uri'] ? substr($request->server['request_uri'], strlen('/api/cluster') + 1) : '';
159+
if($type === "type"){
160+
$code = 200;
161+
$response->header('Content-Type: application/json; charset=utf-8');
162+
163+
$response->end("hello");
164+
}
165+
elseif($type === "status"){
166+
167+
}
168+
elseif($type === "info"){
169+
170+
}
171+
else{
172+
$code = 403;
173+
$response->status($code);
174+
$response->header('Content-Type', 'text/html; charset=utf-8');
175+
$response->end("<title>Error</title><pre>Forbidden</pre>");
176+
}
177+
if(!isset($request->server['query_string'])){
178+
$url = $request->server['request_uri'];
179+
}
180+
else{
181+
$url = $request->server['request_uri']."?".$request->server['query_string'];
182+
}
183+
mlog(" Serve {$code} | {$request->server['remote_addr']} | {$request->server['server_protocol']} | {$url} | {$request->header['user-agent']};") ;
184+
});
185+
158186
mlog("Start Http Server on {$this->host}:{$this->port}");
159187
$server->start();
160188
}

inc/socketio.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class socketio {
88
private $data;
99
private $certdata;
1010
private $kattl;
11-
private $rekeepalive;
11+
private $rekeepalive = 1;
1212
private $Connected = false;
1313
public function __construct($url,$token,$kattl) {
1414
$this->url = $url;
@@ -61,7 +61,7 @@ public function connect() {
6161
$this->certdata = $jsondata;
6262
}
6363
elseif (isset($jsondata[0][1]) && $jsondata[0][1] == "1"){
64-
global $enable;
64+
$enable = api::getinfo()['enable'];
6565
$enable = true;
6666
mlog("节点已启用 Let's Goooooo!");
6767
global $kacounters;
@@ -76,7 +76,7 @@ public function connect() {
7676
});
7777
}
7878
elseif (isset($jsondata[0][1]) && $jsondata[0][1] == "0"){
79-
if($this->rekeepalive < 3){
79+
if($this->rekeepalive <= 3){
8080
mlog("Keep-Alive失败,正在重试({$this->rekeepalive}/3)");
8181
global $kadata;
8282
$this->ack("keep-alive",$kadata);
@@ -87,7 +87,7 @@ public function connect() {
8787
}
8888
}
8989
elseif (isset($jsondata[0][1]) && $this->IsTime($jsondata[0][1])){
90-
$this->rekeepalive = 0;
90+
$this->rekeepalive = 1;
9191
global $kadata;
9292
mlog(" Keep-alive success: hits={$kadata['hits']} bytes={$kadata['bytes']} Time={$jsondata[0][1]}");
9393
}
@@ -115,9 +115,9 @@ public function connect() {
115115
//var_dump($data);
116116
}
117117
global $shouldExit;
118-
global $httpserver;
118+
$httpserver = api::getserver();
119119
if ($shouldExit) {
120-
global $enable;
120+
$enable = api::getinfo()['enable'];
121121
if($enable){
122122
Swoole\Timer::clear($katimeid);
123123
}
@@ -195,7 +195,7 @@ public function IsTime($inputString) {
195195
}
196196

197197
public function disable() {
198-
global $enable;
198+
$enable = api::getinfo()['enable'];
199199
if ($enable){
200200
$this->ack("disable");
201201
Coroutine::sleep(2);

inc/webapi.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
class webapi{
3+
public function gettoken() {
4+
5+
}
6+
}

main.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@
44
use function Swoole\Timer;
55
date_default_timezone_set('Asia/Shanghai');
66
require './config.php';
7-
const PHPOBAVERSION = '1.6.0';
8-
const VERSION = '1.10.6';
9-
global $DOWNLOAD_DIR;
10-
$DOWNLOAD_DIR = $config['file']['cache_dir'];
11-
const USERAGENT = 'openbmclapi-cluster/' . VERSION . ' ' . 'PHP-OpenBmclApi/'.PHPOBAVERSION;
12-
const OPENBMCLAPIURL = 'openbmclapi.bangbang93.com';
137
$list = glob('inc/*.php');
148
foreach ($list as $file) {
159
require $file;
1610
}
17-
global $enable;
18-
$enable = false;
11+
api::getconfig($config);
12+
const PHPOBAVERSION = '1.6.0';
13+
const VERSION = '1.10.6';
14+
$download_dir = api::getconfig()['file']['cache_dir'];
15+
const USERAGENT = 'openbmclapi-cluster/' . VERSION . ' ' . 'PHP-OpenBmclApi/'.PHPOBAVERSION;
16+
const OPENBMCLAPIURL = 'openbmclapi.staging.bangbang93.com';
1917
mlog("OpenBmclApi on PHP v". PHPOBAVERSION . "-" . VERSION,0,true);
20-
run(function()use ($config){
18+
run(function(){
19+
$config = api::getconfig();
2120
//注册信号处理器、
2221
function exits() {
2322
global $shouldExit;
@@ -84,11 +83,11 @@ function registerSigintHandler() {
8483
fclose($cert);
8584
}
8685
//启动http服务器
87-
global $httpserver;
8886
$httpserver = new fileserver($config['cluster']['host'],$config['cluster']['port'],$config['cluster']['CLUSTER_ID'].'.crt',$config['cluster']['CLUSTER_ID'].'.key',$config['cluster']['CLUSTER_SECRET']);
8987
Coroutine::create(function () use ($config,$httpserver){
9088
$httpserver->startserver();
9189
});
90+
api::getserver($httpserver);
9291

9392
//下载文件列表
9493
$cluster = new cluster($tokendata['token'],VERSION);

0 commit comments

Comments
 (0)