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

Commit 1970f36

Browse files
committed
✨ 支持check_sign
1 parent e67ac46 commit 1970f36

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

inc/server.php

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ class fileserver {
88
private $key;
99
private $server;
1010
private $dir;
11-
public function __construct($host,$port,$cert,$key,$dir) {
11+
private $secret;
12+
public function __construct($host,$port,$cert,$key,$dir,$secret) {
1213
$this->host = $host;
1314
$this->port = $port;
1415
$this->cert = $cert;
1516
$this->key = $key;
1617
$this->dir = $dir;
18+
$this->secret = $secret;
1719
}
1820

1921
public function startserver() {
@@ -53,21 +55,32 @@ public function startserver() {
5355
if (!file_exists($this->dir.'/measure')) {
5456
mkdir($this->dir.'/measure',0777,true);
5557
}
58+
if(isset($request->server['query_string'])){
5659
if(is_numeric($measuresize)){
57-
if (!file_exists($this->dir.'/measure/'.$measuresize)) {
58-
$file = fopen($this->dir.'/measure/'.$measuresize, 'w+');
59-
$bytesToWrite = $measuresize * 1048576;
60-
$fillChar = str_repeat("\0", 1024);
61-
while ($bytesToWrite > 0) {
62-
$chunkSize = min(1024, $bytesToWrite);
63-
fwrite($file, substr($fillChar, 0, $chunkSize));
64-
$bytesToWrite -= $chunkSize;
60+
parse_str($request->server['query_string'], $allurl);
61+
if ($this->check_sign($request->server['request_uri'], $this->secret, $allurl['s'], $allurl['e'])){
62+
if (!file_exists($this->dir.'/measure/'.$measuresize)) {
63+
$file = fopen($this->dir.'/measure/'.$measuresize, 'w+');
64+
$bytesToWrite = $measuresize * 1048576;
65+
$fillChar = str_repeat("\0", 1024);
66+
while ($bytesToWrite > 0) {
67+
$chunkSize = min(1024, $bytesToWrite);
68+
fwrite($file, substr($fillChar, 0, $chunkSize));
69+
$bytesToWrite -= $chunkSize;
70+
}
71+
fclose($file);
6572
}
66-
fclose($file);
73+
$code = 200;
74+
$response->header('Content-Type', 'application/octet-stream');
75+
$response->sendfile($this->dir.'/measure/'.$measuresize);
6776
}
68-
$code = 200;
69-
$response->header('Content-Type', 'application/octet-stream');
70-
$response->sendfile($this->dir.'/measure/'.$measuresize);
77+
else{
78+
$code = 403;
79+
$response->status($code);
80+
$response->header('Content-Type', 'text/html; charset=utf-8');
81+
$response->end("<title>Error</title><pre>Forbidden</pre>");
82+
}
83+
}
7184
}
7285
else{
7386
$code = 404;
@@ -90,4 +103,19 @@ public function stopserver() {
90103
mlog("Stop Http Server");
91104
$this->server->shutdown();
92105
}
106+
//你问我这段函数为什么要放在server里面? 因为只有server需要check_sign(
107+
public function check_sign(string $hash, string $secret, string $s, string $e): bool {
108+
try {
109+
$t = intval($e, 36);
110+
} catch (\Exception $ex) {
111+
return false;
112+
}
113+
114+
$sha1 = hash_init('sha1');
115+
hash_update($sha1, $secret);
116+
hash_update($sha1, $hash);
117+
hash_update($sha1, $e);
118+
$computedSignature = rtrim(strtr(base64_encode(hash_final($sha1,true)), '+/', '-_'), '=');
119+
return ($computedSignature === $s && time() * 1000 <= $t);
120+
}
93121
}

main.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function registerSigintHandler() {
106106
}
107107
global $httpserver;
108108
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);
109+
$httpserver = new fileserver($config['cluster']['host'],$config['cluster']['port'],$config['cluster']['CLUSTER_ID'].'.crt',$config['cluster']['CLUSTER_ID'].'.key',$DOWNLOAD_DIR,$config['cluster']['CLUSTER_SECRET']);
110110
Coroutine::create(function () use ($config,$httpserver){
111111
$httpserver->startserver();
112112
});

0 commit comments

Comments
 (0)