Browse Source

Support range requests, general refactor

spaghetti 2 months ago
parent
commit
352665fd62
1 changed files with 66 additions and 36 deletions
  1. 66
    36
      index.php

+ 66
- 36
index.php View File

@@ -4,67 +4,97 @@
4 4
 define('IMG_ROOT', '/var/images/');
5 5
 // Pre-shared key - must match IMAGE_PSK in gazelle config
6 6
 define('PSK', 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
7
+// User agent to use when downloading files
8
+define('USER_AGENT', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36');
7 9
 
8 10
 ini_set('memory_limit', '256M');
9 11
 
10
-function image_type($Data) {
11
-  if (!strncmp($Data, pack('H*', '89504E47'), 4)) return 'png';
12
-  if (!strncmp($Data, pack('H*', 'FFD8'), 2)) return 'jpeg';
13
-  if (!strncmp($Data, 'GIF', 3)) return 'gif';
14
-  if (strlen($Data) > 35 && !substr_compare($Data, 'webm', 31, 4)) return 'webm';
15
-  if (!strncmp($Data, 'BM', 2)) return 'bmp';
16
-  if (!strncmp($Data, 'II', 2) || !strncmp($Data, 'MM', 2)) return 'tiff';
17
-  return '';
12
+function content_type($data) {
13
+  if (!strncmp($data, pack('H*', '89504E47'), 4)) return 'image/png';
14
+  if (!strncmp($data, pack('H*', 'FFD8'), 2)) return 'image/jpeg';
15
+  if (!strncmp($data, 'GIF', 3)) return 'image/gif';
16
+  if (strlen($data) > 35 && !substr_compare($data, 'webm', 31, 4)) return 'video/webm';
17
+  if (!strncmp($data, 'BM', 2)) return 'image/bmp';
18
+  if (!strncmp($data, 'II', 2) || !strncmp($data, 'MM', 2)) return 'image/tiff';
19
+  return 'application/octet-stream';
18 20
 }
19 21
 
20
-$ImgURL = $_GET['i'];
21
-$Auth = rawurldecode($_GET['h']);
22
-$ImgURLHash = hash('sha256', $ImgURL);
22
+function serve($path, $rescode = 200) {
23
+  $filesize = filesize($path);
24
+  $handle = fopen($path, "r");
25
+  $content_type = content_type(fread($handle, 100));
26
+  fclose($handle);
27
+  header('Accept-Ranges: bytes');
28
+  header("Content-Type: $content_type");
29
+  if (isset($_SERVER['HTTP_RANGE'])) {
30
+    preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
31
+    if ($matches[1] == "") {
32
+      $matches[1] = $filesize - intval($matches[2] ?? 0);
33
+      $matches[2] = $filesize;
34
+    }
35
+    $start = intval($matches[1] ?? 0);
36
+    $end = min(intval($matches[2] ?? $filesize), $filesize);
37
+    http_response_code(206);
38
+    header("Content-Range: bytes $start-$end/$filesize");
39
+    echo file_get_contents($path, false, null, $start, $end-$start);
40
+  } else {
41
+    http_response_code($rescode);
42
+    header("Content-Length: $filesize");
43
+    readfile($path);
44
+  }
45
+}
46
+
47
+if (empty($_GET['i'])) die();
48
+
49
+$img_url = $_GET['i'];
50
+$auth = rawurldecode($_GET['h']);
51
+$img_url_hash = hash('sha256', $img_url);
52
+$debug = !empty($_GET['debug']);
53
+
54
+$img_dir = IMG_ROOT.substr($img_url_hash, 0, 2);
55
+$img_path = $img_dir.'/'.$img_url_hash;
23 56
 
24 57
 // Deletion
25 58
 if (!empty($_GET['d'])) {
26
-  $ImgURL = $_GET['d'];
27
-  $ImgURLHash = hash('sha256', $ImgURL);
28
-  if (base64_encode(hash_hmac('sha256', $ImgURL, strrev(PSK), true)) != $Auth) {
59
+  $img_url = $_GET['d'];
60
+  $img_url_hash = hash('sha256', $img_url);
61
+  if (base64_encode(hash_hmac('sha256', $img_url, strrev(PSK), true)) != $auth) {
62
+    http_response_code(401);
29 63
     echo 'Auth failure';
30 64
     die();
31 65
   }
32
-  if (file_exists(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash)) {
33
-    unlink(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash);
66
+  if (file_exists($img_path)) {
67
+    unlink($img_path);
68
+    http_response_code(200);
34 69
     echo 'Success';
35 70
   } else {
71
+    http_response_code(404);
36 72
     echo 'File does not exist';
37 73
   }
38 74
   die();
39 75
 }
40 76
 
41 77
 // Normal use
42
-if (base64_encode(hash_hmac('sha256', $ImgURL, PSK, true)) != $Auth) {
78
+if (base64_encode(hash_hmac('sha256', $img_url, PSK, true)) != $auth) {
43 79
   // Authentication is incorrect. Something other than the paired Gazelle instance is attempting to use the host.
44
-  header('Content-type: image/png');
45
-  echo file_get_contents('imgs/unauthorized.png');
80
+  serve('imgs/unauthorized.png', 401);
46 81
   die();
47 82
 }
48
-if (file_exists(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash)) {
49
-  // The file is cached. Serve it.
50
-  $Img = file_get_contents(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash);
51
-  $FileType = image_type($Img);
52
-  header('Content-type: '.(($FileType=='webm')?'video':'image').'/'.$FileType);
53
-  echo $Img;
83
+if (file_exists($img_path)) {
84
+  serve($img_path);
54 85
 } else {
55 86
   // The file is not cached. Fetch it, cache it, and serve it.
56
-  $Img = @file_get_contents($ImgURL, 0, stream_context_create(['http' => ['user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.2987.133 Safari/537.36'], 'ssl' => ['verify_peer' => false]]), 0, 134217728);
57
-  $FileType = image_type($Img);
58
-  if (!empty($FileType)) {
59
-    if (!file_exists(IMG_ROOT.substr($ImgURLHash,0,2))) {
60
-      mkdir(IMG_ROOT.substr($ImgURLHash,0,2));
61
-    }
62
-    file_put_contents(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash, $Img);
63
-    header('Content-type: '.(($FileType=='webm')?'video':'image').'/'.$FileType);
64
-    echo $Img;
87
+  $img_data = @file_get_contents($img_url, 0, stream_context_create([
88
+    'http' => ['user_agent' => USER_AGENT],
89
+    'ssl' => ['verify_peer' => false]
90
+  ]), 0, 134217728);
91
+  $content_type = content_type($img_data);
92
+  if ($content_type != 'application/octet-stream') {
93
+    if (!file_exists($img_dir)) mkdir($img_dir);
94
+    file_put_contents($img_path, $img_data);
95
+    serve($img_path);
65 96
   } else {
66
-    header('Content-type: image/png');
67
-    echo file_get_contents('imgs/invalid.png');
97
+    serve('imgs/invalid.png', 415);
68 98
   }
69 99
 }
70 100
 ?>

Loading…
Cancel
Save