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
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

PHP 版本 >= 8.0.0

[Swoole](https://www.swoole.com/) 版本 >= 5.1.
[Swoole](https://www.swoole.com/) 版本 >= 5.1.0

以及对应版本的 [Zstd](https://github.com/kjdev/php-ext-zstd)

Expand Down Expand Up @@ -66,13 +66,20 @@ $config=[
"public_port"=> 4000,//服务端口
"CLUSTER_ID"=> "",
"CLUSTER_SECRET"=> "",
"byoc"=>false,
"byoc"=> false,
"certificates"=>[ //如果 byoc 关闭,以下设置默认禁用
"use-cert"=> false, //是否使用自己的证书
"cert"=> "/path/to/cert.crt",
"key"=> "/path/to/key.key",
],
],
"file"=> [
"cache_dir"=> "./cache",//缓存文件夹
"check"=> "size",//检查文件策略(hash:检查文件hash size:检查文件大小 exists:检查文件是否存在)
"database_dir"=> "./database",//访问数据数据库目录
],
"advanced"=> [
"Centerurl"=> "https://openbmclapi.bangbang93.com",//主控链接(不建议调整)
"keepalive"=> 60,//keepalive时间,秒为单位(不建议调整)
"MaxConcurrent"=> 30,//下载使用的线程
"Debug"=> false,//Debug开关
Expand All @@ -81,12 +88,13 @@ $config=[
```

## 📍 Todo
- [ ] Web仪表盘(主要)
- [ ] 支持上报错误 url(主要)
- [ ] 支持WebDAV
- [ ] 打包二进制文件
- [ ] 完善Log系统
- [ ] 添加注释
- [x] 可以正常上线使用
- [x] 插件系统
- [x] 完善Log系统
- [ ] 打包二进制文件 (延期:[原因](https://github.com/crazywhalecc/static-php-cli/issues/479))

## ❓ FAQ

Expand Down
9 changes: 8 additions & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
"public_port"=> 4000,//服务端口
"CLUSTER_ID"=> "",
"CLUSTER_SECRET"=> "",
"byoc"=>false,
"byoc"=> false,
"certificates"=>[ //如果 byoc 关闭,以下设置默认禁用
"use-cert"=> false, //是否使用自己的证书
"cert"=> "/path/to/cert.crt",
"key"=> "/path/to/key.key",
],
],
"file"=> [
"cache_dir"=> "./cache",//缓存文件夹
"check"=> "size",//检查文件策略(hash:检查文件hash size:检查文件大小 exists:检查文件是否存在)
"database_dir"=> "./database",//访问数据数据库目录
],
"advanced"=> [
"Centerurl"=> "https://openbmclapi.staging.bangbang93.com",//主控链接(不建议调整)
"keepalive"=> 60,//keepalive时间,秒为单位(不建议调整)
"MaxConcurrent"=> 30,//下载使用的线程
"Debug"=> false,//Debug开关
Expand Down
7 changes: 7 additions & 0 deletions inc/PluginInfoInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Plugin;

interface PluginInfoInterface {
public function getInfo();
public function main();
}
19 changes: 19 additions & 0 deletions inc/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
class api{
private static $config = [];
private static $info = ['enable'=>false,'isSynchronized'=>false,'uptime'=>0,'token'=>null];

public static function getconfig($newConfig = null) {
if (!is_null($newConfig)) {
self::$config = $newConfig;
}
return self::$config;
}

public static function getinfo($newinfo = null) {
if (!is_null($newinfo)) {
self::$info = $newinfo;
}
return self::$info;
}
}
64 changes: 30 additions & 34 deletions inc/cluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,25 @@ public function __construct($token,$version){
$this->version = $version;
}
public function getFileList() {
global $DOWNLOAD_DIR;
if (!file_exists($DOWNLOAD_DIR."/filecache")) {
mkdir($DOWNLOAD_DIR."/filecache",0777,true);
$download_dir = api::getconfig()['file']['cache_dir'];
if (!file_exists($download_dir."/filecache")) {
mkdir($download_dir."/filecache",0777,true);
}
$client = new Client(OPENBMCLAPIURL,443,true);
$client = new Client(OPENBMCLAPIURL['host'],OPENBMCLAPIURL['port'],OPENBMCLAPIURL['ssl']);
$client->set(['timeout' => -1]);
$client->setHeaders([
'Host' => OPENBMCLAPIURL,
'User-Agent' => 'openbmclapi-cluster/'.$this->version,
'Accept' => '*',
'Authorization' => "Bearer {$this->token}"
]);
mlog("Start FileList Download");
if (!$client->download('/openbmclapi/files',$DOWNLOAD_DIR.'/filecache/filelist.zstd')) {
mlog("FileList Download Failed",2);
mlog("Starting to download fileList");
if (!$client->get('/openbmclapi/files')) {
mlog("Failed to download fileList",2);
$client->close();
}
else{
mlog("FileList Download Success");
$this->compressedData = zstd_uncompress($client->body);
$client->close();
$this->compressedData = file_get_contents("compress.zstd://".$DOWNLOAD_DIR."/filecache/filelist.zstd");
}
$parser = new ParseFileList($this->compressedData);
$files = $parser->parse();
Expand All @@ -123,14 +121,14 @@ public function __construct($filesList = [], $maxConcurrent = 1) {
}

private function downloader(Swoole\Coroutine\Http\Client $client, $file,$bar) {
global $DOWNLOAD_DIR;
$filePath = $DOWNLOAD_DIR . '/' . substr($file->hash, 0, 2) . '/';
$download_dir = api::getconfig()['file']['cache_dir'];
$filePath = $download_dir . '/' . substr($file->hash, 0, 2) . '/';
if (!file_exists($filePath)) {
mkdir($filePath, 0777, true);
}
$savePath = $filePath . $file->hash;
$file->path = str_replace(' ', '%20', $file->path);
$downloader = $client->download($file->path,$DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
$downloader = $client->download($file->path,$download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
if (!$downloader) {
mlog("Error connecting to the main control:{$client->errMsg}",2);
return false;
Expand All @@ -152,7 +150,7 @@ private function downloader(Swoole\Coroutine\Http\Client $client, $file,$bar) {
'User-Agent' => USERAGENT,
'Accept' => '*/*',
]);
$downloader = $client->download($location_url['path'].'?'.($location_url['query']??''),$DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
$downloader = $client->download($location_url['path'].'?'.($location_url['query']??''),$download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
if (in_array($client->statusCode, [301, 302])) {
while(in_array($client->statusCode, [301, 302])){
$location_url = parse_url($client->getHeaders()['location']);
Expand All @@ -169,7 +167,7 @@ private function downloader(Swoole\Coroutine\Http\Client $client, $file,$bar) {
'User-Agent' => USERAGENT,
'Accept' => '*/*',
]);
$downloader = $client->download($location_url['path'].'?'.($location_url['query']??''),$DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
$downloader = $client->download($location_url['path'].'?'.($location_url['query']??''),$download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
}
if (!$downloader) {
echo PHP_EOL;
Expand All @@ -192,7 +190,7 @@ private function downloader(Swoole\Coroutine\Http\Client $client, $file,$bar) {
}
elseif($client->statusCode >= 400){
echo PHP_EOL;
mlog("Download Failed:{$client->statusCode} | {$file->path} | {$location_url['host']}:{$location_url['port']}",2);
mlog("{$file->path} Download Failed: {$client->statusCode} | {$location_url['host']}:{$location_url['port']}",2);
$bar->progress();
return false;
}
Expand All @@ -212,7 +210,7 @@ private function downloader(Swoole\Coroutine\Http\Client $client, $file,$bar) {

public function downloadFiles() {
$bar = new CliProgressBar(count($this->filesList));
$bar->setDetails("[Downloader]");
$bar->setDetails("[Downloader][线程数:{$this->maxConcurrent}]");
$bar->display();
foreach ($this->filesList as $file) {
global $shouldExit;
Expand All @@ -221,12 +219,11 @@ public function downloadFiles() {
}
$this->semaphore->push(true);
go(function () use ($file,$bar) {
$client = new Swoole\Coroutine\Http\Client('openbmclapi.bangbang93.com', 443, true);
$client = new Swoole\Coroutine\Http\Client(OPENBMCLAPIURL['host'],OPENBMCLAPIURL['port'],OPENBMCLAPIURL['ssl']);
$client->set([
'timeout' => -1
]);
$client->setHeaders([
'Host' => 'openbmclapi.bangbang93.com',
'User-Agent' => USERAGENT,
'Accept' => '*/*',
]);
Expand All @@ -247,24 +244,23 @@ public function downloadFiles() {
}

public function downloadnopoen($hash) {
global $DOWNLOAD_DIR;
global $tokendata;
$filePath = $DOWNLOAD_DIR . '/' . substr($hash, 0, 2) . '/';
$download_dir = api::getconfig()['file']['cache_dir'];
$tokenapi = api::getinfo();
$filePath = $download_dir . '/' . substr($hash, 0, 2) . '/';
if (!file_exists($filePath)) {
mkdir($filePath, 0777, true);
}
$filepath = "/openbmclapi/download/{$hash}?noopen=1";
$client = new Swoole\Coroutine\Http\Client('openbmclapi.bangbang93.com', 443, true);
$client = new Swoole\Coroutine\Http\Client(OPENBMCLAPIURL['host'],OPENBMCLAPIURL['port'],OPENBMCLAPIURL['ssl']);
$client->set([
'timeout' => -1
]);
$client->setHeaders([
'Host' => 'openbmclapi.bangbang93.com',
'User-Agent' => USERAGENT,
'Accept' => '*/*',
'Authorization' => "Bearer {$tokendata['token']}"
'Authorization' => "Bearer {$tokenapi['token']}"
]);
$downloader = $client->download($filepath,$DOWNLOAD_DIR.'/'.substr($hash, 0, 2).'/'.$hash);
$downloader = $client->download($filepath,$download_dir.'/'.substr($hash, 0, 2).'/'.$hash);
if (!$downloader) {
mlog("Error download to the main control:{$client->errMsg}",2);
return false;
Expand All @@ -291,14 +287,14 @@ public function FilesCheckerhash() {
$bar = new CliProgressBar(count($this->filesList));
$bar->setDetails("[FileCheck]");
$bar->display();
$download_dir = api::getconfig()['file']['cache_dir'];
foreach ($this->filesList as $file) {
global $shouldExit;
global $DOWNLOAD_DIR;
if ($shouldExit) {
return;
break;
}
if (!file_exists($DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash)){
if (!file_exists($download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash)){
$this->Missfile[] = new BMCLAPIFile(
$file->path,
$file->hash,
Expand All @@ -307,7 +303,7 @@ public function FilesCheckerhash() {
);
}
else{
if (hash_file('sha1',$DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash) != $file->hash) {
if (hash_file('sha1',$download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash) != $file->hash) {
$this->Missfile[] = new BMCLAPIFile(
$file->path,
$file->hash,
Expand All @@ -328,14 +324,14 @@ public function FilesCheckersize() {
$bar = new CliProgressBar(count($this->filesList));
$bar->setDetails("[FileCheck]");
$bar->display();
$download_dir = api::getconfig()['file']['cache_dir'];
foreach ($this->filesList as $file) {
global $shouldExit;
global $DOWNLOAD_DIR;
if ($shouldExit) {
return;
break;
}
if (!file_exists($DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash)){
if (!file_exists($download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash)){
$this->Missfile[] = new BMCLAPIFile(
$file->path,
$file->hash,
Expand All @@ -344,7 +340,7 @@ public function FilesCheckersize() {
);
}
else{
if (filesize($DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash) != $file->size) {
if (filesize($download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash) != $file->size) {
$this->Missfile[] = new BMCLAPIFile(
$file->path,
$file->hash,
Expand All @@ -365,14 +361,14 @@ public function FilesCheckerexists() {
$bar = new CliProgressBar(count($this->filesList));
$bar->setDetails("[FileCheck]");
$bar->display();
$download_dir = api::getconfig()['file']['cache_dir'];
foreach ($this->filesList as $file) {
global $shouldExit;
global $DOWNLOAD_DIR;
if ($shouldExit) {
return;
break;
}
if (!file_exists($DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash)){
if (!file_exists($download_dir.'/'.substr($file->hash, 0, 2).'/'.$file->hash)){
$this->Missfile[] = new BMCLAPIFile(
$file->path,
$file->hash,
Expand Down
Loading