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

Commit 49cf2e2

Browse files
committed
✨ 初步支持Http服务器启用/measure生成
1 parent cd4290d commit 49cf2e2

File tree

4 files changed

+132
-9
lines changed

4 files changed

+132
-9
lines changed

config.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
<?php
22
$config=[
3-
"CLUSTER_ID"=> "",
4-
"CLUSTER_SECRET"=> "",
3+
"cluster"=> [
4+
"host"=> "0.0.0.0",//本地IP
5+
"port"=> 4000,//本地端口
6+
"public_host"=> "0.0.0.0",//服务地址
7+
"public_port"=> 4000,//服务端口
8+
"CLUSTER_ID"=> "",
9+
"CLUSTER_SECRET"=> "",
10+
],
511
"cache_dir"=> "./cache",//缓存文件夹
612
"MaxConcurrent"=> 10,//下载使用的线程
713
"Debug"=> false,//Debug开关

inc/server.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
use Swoole\Coroutine\Http\Server;
3+
4+
class fileserver {
5+
private $host;
6+
private $port;
7+
private $cert;
8+
private $key;
9+
private $server;
10+
private $dir;
11+
public function __construct($host,$port,$cert,$key,$dir) {
12+
$this->host = $host;
13+
$this->port = $port;
14+
$this->cert = $cert;
15+
$this->key = $key;
16+
$this->dir = $dir;
17+
}
18+
19+
public function startserver() {
20+
$this->server = $server = new Server($this->host, $this->port, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
21+
$server->set([
22+
'ssl_cert_file' => './cert/'.$this->cert,
23+
'ssl_key_file' => './cert/'.$this->key,
24+
'open_http2_protocol' => true,
25+
]);
26+
$server->handle('/', function ($request, $response) {
27+
$code = 404;
28+
$response->status($code);
29+
$response->header('Content-Type', 'text/html; charset=utf-8');
30+
$response->end("<title>Error</title><pre>404 Not Found</pre>");
31+
if(!isset($request->server['query_string'])){
32+
$url = $request->server['request_uri'];
33+
}
34+
else{
35+
$url = $request->server['request_uri']."?".$request->server['query_string'];
36+
}
37+
mlog(" Serve {$code} | {$request->server['remote_addr']} | {$request->server['server_protocol']} | {$url} | {$request->header['user-agent']};") ;
38+
});
39+
$server->handle('/download', function ($request, $response) {
40+
$response->end("Test");
41+
echo "download";
42+
mlog(" Serve {$code} | {$request->server['remote_addr']} | {$request->server['server_protocol']} | {$request->server['request_uri']}?{$request->server['query_string']} | {$request->header['user-agent']};") ;
43+
});
44+
$server->handle('/measure', function ($request, $response) {
45+
$measuresize = str_replace('/measure/', '', $request->server['request_uri']);
46+
echo $measuresize;
47+
if (!file_exists($this->dir.'/measure')) {
48+
mkdir($this->dir.'/measure',0777,true);
49+
}
50+
if(is_numeric($measuresize)){
51+
if (!file_exists($this->dir.'/measure/'.$measuresize)) {
52+
$file = fopen($this->dir.'/measure/'.$measuresize, 'w+');
53+
$bytesToWrite = $measuresize * 1048576;
54+
$fillChar = str_repeat("\0", 1024);
55+
while ($bytesToWrite > 0) {
56+
$chunkSize = min(1024, $bytesToWrite);
57+
fwrite($file, substr($fillChar, 0, $chunkSize));
58+
$bytesToWrite -= $chunkSize;
59+
}
60+
fclose($file);
61+
}
62+
$code = 200;
63+
$response->header('Content-Type', 'application/octet-stream');
64+
$response->sendfile($this->dir.'/measure/'.$measuresize);
65+
}
66+
else{
67+
$code = 404;
68+
$response->status($code);
69+
$response->header('Content-Type', 'text/html; charset=utf-8');
70+
$response->end("<title>Error</title><pre>404 Not Found</pre>");
71+
}
72+
if(!isset($request->server['query_string'])){
73+
$url = $request->server['request_uri'];
74+
}
75+
else{
76+
$url = $request->server['request_uri']."?".$request->server['query_string'];
77+
}
78+
mlog(" Serve {$code} | {$request->server['remote_addr']} | {$request->server['server_protocol']} | {$url} | {$request->header['user-agent']};") ;
79+
});
80+
mlog("Start Http Server on {$this->host}:{$this->port}");
81+
$server->start();
82+
}
83+
public function stopserver() {
84+
mlog("Stop Http Server");
85+
$this->server->shutdown();
86+
}
87+
}

inc/socketio.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,22 @@ public function connect() {
5454
}
5555
if ($code[0] == '430'){
5656
$jsondata = json_decode(substr($data, strlen($code[0])),true);
57-
$this->certdata = $jsondata;
57+
mlog("[socket.io]Got data {$jsondata[0][0]["message"]}");
5858
//mlog("[socket.io]Got MESSAGE {$data}",1);
5959
}
60+
if ($code[0] == '423'){
61+
$data = substr($data, strlen($code[0]));
62+
mlog("[socket.io]Got data {$data}");
63+
}
6064
//var_dump($data);
6165
Coroutine::sleep(0.1);
6266
}
6367
global $shouldExit;
68+
global $httpserver;
6469
if ($shouldExit) {
6570
mlog("[socket.io]Close Connection");
6671
$client->close();
72+
$httpserver->stopserver();
6773
return;
6874
}
6975
}
@@ -99,4 +105,22 @@ public function ack($event,$data = "") {
99105
public function Getclient() {
100106
return $this->client;
101107
}
108+
public function enable($host,$port) {
109+
if (in_array($host, ["0.0.0.0", "127.0.0.1"])){
110+
$host = preg_replace('/\R/', '', file_get_contents('http://ip.3322.net'));
111+
}
112+
mlog("正在enable节点");
113+
$data = [
114+
'host' => $host,
115+
'port' => $port,
116+
'version' => VERSION,
117+
'byoc' => false,
118+
'noFastEnable' => false,
119+
'flavor' =>[
120+
'runtime' => 'PHP-'.substr(PHP_VERSION,0,3).'/'.php_uname('s'),
121+
'storage' => 'file'
122+
]
123+
];
124+
$this->ack("enable",$data);
125+
}
102126
}

main.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function registerSigintHandler() {
4040
});
4141
}
4242
//获取初次Token
43-
$token = new token($config['CLUSTER_ID'],$config['CLUSTER_SECRET'],VERSION);
43+
$token = new token($config['cluster']['CLUSTER_ID'],$config['cluster']['CLUSTER_SECRET'],VERSION);
4444
$tokendata = $token->gettoken();
4545
mlog("GetToken:".$tokendata['token'],1);
4646
mlog("TokenTTL:".$tokendata['upttl'],1);
@@ -50,6 +50,7 @@ function registerSigintHandler() {
5050
$tokendata = $token->gettoken();
5151
mlog("GetNewToken:".$tokendata['token'],1);
5252
});
53+
global $socketio;
5354
registerSigintHandler();
5455
mlog("Timer start on ID{$timerId}",1);
5556

@@ -87,9 +88,7 @@ function registerSigintHandler() {
8788
$socketio->connect();
8889
});
8990
Coroutine::sleep(1);
90-
if (file_exists('./cert/'.$config['CLUSTER_ID'].'.crt') && file_exists('./cert/'.$config['CLUSTER_ID'].'.key')) {
91-
92-
} else {
91+
if (!file_exists('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt') && !file_exists('./cert/'.$config['cluster']['CLUSTER_ID'].'.key')) {
9392
mlog("正在获取证书");
9493
if (!file_exists("./cert")) {
9594
mkdir("./cert",0777,true);
@@ -98,12 +97,19 @@ function registerSigintHandler() {
9897
Coroutine::sleep(1);
9998
$allcert = $socketio->Getcert();
10099
mlog("已获取证书,到期时间{$allcert['0']['1']['expires']}");
101-
$cert = fopen('./cert/'.$config['CLUSTER_ID'].'.crt', 'w');
100+
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.crt', 'w');
102101
$Writtencert = fwrite($cert, $allcert['0']['1']['cert']);
103102
fclose($cert);
104-
$cert = fopen('./cert/'.$config['CLUSTER_ID'].'.key', 'w');
103+
$cert = fopen('./cert/'.$config['cluster']['CLUSTER_ID'].'.key', 'w');
105104
$Writtencert = fwrite($cert, $allcert['0']['1']['key']);
106105
fclose($cert);
107106
}
107+
global $httpserver;
108+
global $DOWNLOAD_DIR;
109+
$httpserver = new fileserver($config['cluster']['host'],$config['cluster']['port'],$config['cluster']['CLUSTER_ID'].'.crt',$config['cluster']['CLUSTER_ID'].'.key',$DOWNLOAD_DIR);
110+
Coroutine::create(function () use ($config,$httpserver){
111+
$httpserver->startserver();
112+
});
113+
$socketio->enable($config['cluster']['public_host'],$config['cluster']['public_port']);
108114
}
109115
});

0 commit comments

Comments
 (0)