-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
195 lines (153 loc) · 4.54 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
declare(strict_types=1);
/**
* image proxy configuration
*/
# this is very important!
ini_set("memory_limit", "256M");
# hosted image storage location
$imageRoot = "/var/www/pictures/";
# preshared key: must match the gazelle config
$presharedKey = "";
# user agent to use when downloading files
# https://techblog.willshouse.com/2012/01/03/most-common-user-agents/
$userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36";
# hmac hash algorithm to use
$algorithm = "sha3-512";
/** */
/**
* the proxy script itself
*/
if (empty($_GET["i"])) {
exit;
}
$imageUri = $_GET["i"];
$auth = rawurldecode($_GET["h"]);
$imageUriHash = hash($algorithm, $imageUri);
$imageDirectory = $imageRoot . substr($imageUriHash, 0, 2);
$imagePath = "{$imageDirectory}/{$imageUriHash}";
# deletion
if (!empty($_GET["d"])) {
$imageUri = $_GET["d"];
$imageUriHash = hash($algorithm, $imageUri);
if (base64_encode(hash_hmac($algorithm, $imageUri, strrev($presharedKey), true)) !== $auth) {
http_response_code(401);
echo "auth failure";
exit;
}
if (file_exists($imagePath)) {
unlink(realpath($imagePath));
http_response_code(200);
echo "success";
} else {
http_response_code(404);
echo "file doesn't exist";
}
exit;
}
# normal use
if (base64_encode(hash_hmac($algorithm, $imageUri, $presharedKey, true)) !== $auth) {
# authentication is incorrect: something other than the paired gazelle instance is attempting to use the host
serve("images/unauthorized.png", 401);
exit;
}
if (file_exists($imagePath)) {
serve($imagePath);
} else {
# the file is not cached: fetch it, cache it, and serve it
$imageData = @file_get_contents($imageUri, false, stream_context_create([
"http" => ["user_agent" => $userAgent],
"ssl" => ["verify_peer" => false]
]), 0, 134217728);
$contentType = contentType($imageData);
if ($contentType !== "application/octet-stream") {
if (!file_exists($imageDirectory)) {
mkdir($imageDirectory);
}
file_put_contents($imagePath, $imageData);
serve($imagePath);
} else {
serve("images/invalid.png", 415);
}
}
/** */
/**
* contentType
*
* Returns the content type of a file based on its first few bytes.
*
* @param string $data
* @return string
*
* @see https://en.wikipedia.org/wiki/List_of_file_signatures
* @see https://github.com/rosell-dk/image-mime-type-sniffer
*/
function contentType(string $data): string
{
# image/bmp
if (!strncmp($data, "BM", 2)) {
return "image/bmp";
}
# image/gif
if (!strncmp($data, "GIF", 3)) {
return "image/gif";
}
# image/jpeg
if (!strncmp($data, pack("H*", "FFD8FF"), 3)) {
return "image/jpeg";
}
# image/jxl
if (!strncmp($data, pack("H*", "0000000C4A584C200D0A870A"), 12) || !strncmp($data, pack("H*", "FF0A"), 2)) {
return "image/jxl";
}
# image/png
if (!strncmp($data, pack("H*", "89504E470D0A1A0A"), 8)) {
return "image/png";
}
# image/tiff
if (!strncmp($data, "II", 2) || !strncmp($data, "MM", 2)) {
return "image/tiff";
}
# image/webp
if (!strncmp($data, "RIFF", 4)) {
return "image/webp";
}
# video/webm
if (strlen($data) > 35 && !substr_compare($data, "webm", 31, 4)) {
return "video/webm";
}
# application/octet-stream
return "application/octet-stream";
}
/**
* serve
*
* @param string $path
* @param int $responseCode
* @return void
*/
function serve(string $path, int $responseCode = 200): void
{
$filesize = filesize($path);
$handle = fopen($path, "r");
$contentType = contentType(fread($handle, 100));
fclose($handle);
header("Accept-Ranges: bytes");
header("Content-Type: {$contentType}");
if (isset($_SERVER["HTTP_RANGE"])) {
preg_match("/bytes=(\d+)-(\d+)?/", $_SERVER["HTTP_RANGE"], $matches);
if ($matches[1] === "") {
$matches[1] = $filesize - intval($matches[2] ?? 0);
$matches[2] = $filesize;
}
$start = intval($matches[1] ?? 0);
$end = min(intval($matches[2] ?? $filesize), $filesize);
http_response_code(206);
header("Content-Range: bytes {$start}-{$end}/{$filesize}");
echo file_get_contents($path, false, null, $start, $end - $start);
} else {
http_response_code($responseCode);
header("Content-Length: {$filesize}");
readfile($path);
}
}