@@ -8,12 +8,14 @@ class fileserver {
8
8
private $ key ;
9
9
private $ server ;
10
10
private $ dir ;
11
- public function __construct ($ host ,$ port ,$ cert ,$ key ,$ dir ) {
11
+ private $ secret ;
12
+ public function __construct ($ host ,$ port ,$ cert ,$ key ,$ dir ,$ secret ) {
12
13
$ this ->host = $ host ;
13
14
$ this ->port = $ port ;
14
15
$ this ->cert = $ cert ;
15
16
$ this ->key = $ key ;
16
17
$ this ->dir = $ dir ;
18
+ $ this ->secret = $ secret ;
17
19
}
18
20
19
21
public function startserver () {
@@ -53,21 +55,32 @@ public function startserver() {
53
55
if (!file_exists ($ this ->dir .'/measure ' )) {
54
56
mkdir ($ this ->dir .'/measure ' ,0777 ,true );
55
57
}
58
+ if (isset ($ request ->server ['query_string ' ])){
56
59
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 );
65
72
}
66
- fclose ($ file );
73
+ $ code = 200 ;
74
+ $ response ->header ('Content-Type ' , 'application/octet-stream ' );
75
+ $ response ->sendfile ($ this ->dir .'/measure/ ' .$ measuresize );
67
76
}
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
+ }
71
84
}
72
85
else {
73
86
$ code = 404 ;
@@ -90,4 +103,19 @@ public function stopserver() {
90
103
mlog ("Stop Http Server " );
91
104
$ this ->server ->shutdown ();
92
105
}
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
+ }
93
121
}
0 commit comments