curlproxy.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. <?php
  2. class proxy{
  3. public const req_web = 0;
  4. public const req_image = 1;
  5. public function __construct($cache = true){
  6. $this->cache = $cache;
  7. }
  8. public function do404(){
  9. http_response_code(404);
  10. header("Content-Type: image/png");
  11. $handle = fopen("lib/img404.png", "r");
  12. echo fread($handle, filesize("lib/img404.png"));
  13. fclose($handle);
  14. die();
  15. return;
  16. }
  17. public function getabsoluteurl($path, $relative){
  18. if($this->validateurl($path)){
  19. return $path;
  20. }
  21. if(substr($path, 0, 2) == "//"){
  22. return "https:" . $path;
  23. }
  24. $url = null;
  25. $relative = parse_url($relative);
  26. $url = $relative["scheme"] . "://";
  27. if(
  28. isset($relative["user"]) &&
  29. isset($relative["pass"])
  30. ){
  31. $url .= $relative["user"] . ":" . $relative["pass"] . "@";
  32. }
  33. $url .= $relative["host"];
  34. if(isset($relative["path"])){
  35. $relative["path"] = explode(
  36. "/",
  37. $relative["path"]
  38. );
  39. unset($relative["path"][count($relative["path"]) - 1]);
  40. $relative["path"] = implode("/", $relative["path"]);
  41. $url .= $relative["path"];
  42. }
  43. if(
  44. strlen($path) !== 0 &&
  45. $path[0] !== "/"
  46. ){
  47. $url .= "/";
  48. }
  49. $url .= $path;
  50. return $url;
  51. }
  52. public function validateurl($url){
  53. $url_parts = parse_url($url);
  54. // check if required parts are there
  55. if(
  56. !isset($url_parts["scheme"]) ||
  57. !(
  58. $url_parts["scheme"] == "http" ||
  59. $url_parts["scheme"] == "https"
  60. ) ||
  61. !isset($url_parts["host"])
  62. ){
  63. return false;
  64. }
  65. $ip =
  66. str_replace(
  67. ["[", "]"], // handle ipv6
  68. "",
  69. $url_parts["host"]
  70. );
  71. // if its not an IP
  72. if(!filter_var($ip, FILTER_VALIDATE_IP)){
  73. // resolve domain's IP
  74. $ip = gethostbyname($url_parts["host"] . ".");
  75. }
  76. // check if its localhost
  77. if(
  78. filter_var(
  79. $ip,
  80. FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
  81. ) === false
  82. ){
  83. return false;
  84. }
  85. return true;
  86. }
  87. public function get($url, $reqtype = self::req_web, $acceptallcodes = false, $referer = null, $redirectcount = 0){
  88. if($redirectcount === 5){
  89. throw new Exception("Too many redirects");
  90. }
  91. if($url == "https://i.imgur.com/removed.png"){
  92. throw new Exception("Encountered imgur 404");
  93. }
  94. // sanitize URL
  95. if($this->validateurl($url) === false){
  96. throw new Exception("Invalid URL");
  97. }
  98. $this->clientcache();
  99. $curl = curl_init();
  100. curl_setopt($curl, CURLOPT_URL, $url);
  101. curl_setopt($curl, CURLOPT_ENCODING, ""); // default encoding
  102. curl_setopt($curl, CURLOPT_HEADER, 1);
  103. switch($reqtype){
  104. case self::req_web:
  105. curl_setopt(
  106. $curl,
  107. CURLOPT_HTTPHEADER,
  108. [
  109. "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0",
  110. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  111. "Accept-Language: en-US,en;q=0.5",
  112. "Accept-Encoding: gzip, deflate",
  113. "DNT: 1",
  114. "Connection: keep-alive",
  115. "Upgrade-Insecure-Requests: 1",
  116. "Sec-Fetch-Dest: document",
  117. "Sec-Fetch-Mode: navigate",
  118. "Sec-Fetch-Site: none",
  119. "Sec-Fetch-User: ?1"
  120. ]
  121. );
  122. break;
  123. case self::req_image:
  124. if($referer === null){
  125. $referer = explode("/", $url, 4);
  126. array_pop($referer);
  127. $referer = implode("/", $referer);
  128. }
  129. curl_setopt(
  130. $curl,
  131. CURLOPT_HTTPHEADER,
  132. [
  133. "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0",
  134. "Accept: image/avif,image/webp,*/*",
  135. "Accept-Language: en-US,en;q=0.5",
  136. "Accept-Encoding: gzip, deflate",
  137. "DNT: 1",
  138. "Connection: keep-alive",
  139. "Referer: {$referer}"
  140. ]
  141. );
  142. break;
  143. }
  144. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  145. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
  146. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  147. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
  148. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  149. // limit size of payloads
  150. curl_setopt($curl, CURLOPT_BUFFERSIZE, 1024);
  151. curl_setopt($curl, CURLOPT_NOPROGRESS, false);
  152. curl_setopt(
  153. $curl,
  154. CURLOPT_PROGRESSFUNCTION,
  155. function($downloadsize, $downloaded, $uploadsize, $uploaded
  156. ){
  157. // if $downloaded exceeds 100MB, fuck off
  158. return ($downloaded > 100000000) ? 1 : 0;
  159. });
  160. $body = curl_exec($curl);
  161. if(curl_errno($curl)){
  162. throw new Exception(curl_error($curl));
  163. }
  164. curl_close($curl);
  165. $headers = [];
  166. $http = null;
  167. while(true){
  168. $header = explode("\n", $body, 2);
  169. $body = $header[1];
  170. if($http === null){
  171. // http/1.1 200 ok
  172. $header = explode("/", $header[0], 2);
  173. $header = explode(" ", $header[1], 3);
  174. $http = [
  175. "version" => (float)$header[0],
  176. "code" => (int)$header[1]
  177. ];
  178. continue;
  179. }
  180. if(trim($header[0]) == ""){
  181. // reached end of headers
  182. break;
  183. }
  184. $header = explode(":", $header[0], 2);
  185. // malformed headers
  186. if(count($header) !== 2){ continue; }
  187. $headers[strtolower(trim($header[0]))] = trim($header[1]);
  188. }
  189. // check http code
  190. if(
  191. $http["code"] >= 300 &&
  192. $http["code"] <= 309
  193. ){
  194. // redirect
  195. if(!isset($headers["location"])){
  196. throw new Exception("Broken redirect");
  197. }
  198. $redirectcount++;
  199. return $this->get($this->getabsoluteurl($headers["location"], $url), $reqtype, $acceptallcodes, $referer, $redirectcount);
  200. }else{
  201. if(
  202. $acceptallcodes === false &&
  203. $http["code"] > 300
  204. ){
  205. throw new Exception("Remote server returned an error code! ({$http["code"]})");
  206. }
  207. }
  208. // check if data is okay
  209. switch($reqtype){
  210. case self::req_image:
  211. $format = false;
  212. if(isset($headers["content-type"])){
  213. if($headers["content-type"] == "text/html"){
  214. throw new Exception("Server returned an html document instead of image");
  215. }
  216. $tmp = explode(";", $headers["content-type"]);
  217. for($i=0; $i<count($tmp); $i++){
  218. if(
  219. preg_match(
  220. '/^image\/([^ ]+)/i',
  221. $tmp[$i],
  222. $match
  223. )
  224. ){
  225. $format = strtolower($match[1]);
  226. if(substr($format, 0, 2) == "x-"){
  227. $format = substr($format, 2);
  228. }
  229. break;
  230. }
  231. }
  232. }
  233. return [
  234. "http" => $http,
  235. "format" => $format,
  236. "headers" => $headers,
  237. "body" => $body
  238. ];
  239. break;
  240. default:
  241. return [
  242. "http" => $http,
  243. "headers" => $headers,
  244. "body" => $body
  245. ];
  246. break;
  247. }
  248. return;
  249. }
  250. public function stream_linear_image($url, $referer = null){
  251. $this->stream($url, $referer, "image");
  252. }
  253. public function stream_linear_audio($url, $referer = null){
  254. $this->stream($url, $referer, "audio");
  255. }
  256. private function stream($url, $referer, $format){
  257. $this->url = $url;
  258. $this->format = $format;
  259. // sanitize URL
  260. if($this->validateurl($url) === false){
  261. throw new Exception("Invalid URL");
  262. }
  263. $this->clientcache();
  264. $curl = curl_init();
  265. // set headers
  266. if($referer === null){
  267. $referer = explode("/", $url, 4);
  268. array_pop($referer);
  269. $referer = implode("/", $referer);
  270. }
  271. switch($format){
  272. case "image":
  273. curl_setopt(
  274. $curl,
  275. CURLOPT_HTTPHEADER,
  276. [
  277. "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0",
  278. "Accept: image/avif,image/webp,*/*",
  279. "Accept-Language: en-US,en;q=0.5",
  280. "Accept-Encoding: gzip, deflate, br",
  281. "DNT: 1",
  282. "Connection: keep-alive",
  283. "Referer: {$referer}"
  284. ]
  285. );
  286. break;
  287. case "audio":
  288. curl_setopt(
  289. $curl,
  290. CURLOPT_HTTPHEADER,
  291. [
  292. "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0",
  293. "Accept: audio/webm,audio/ogg,audio/wav,audio/*;q=0.9,application/ogg;q=0.7,video/*;q=0.6,*/*;q=0.5",
  294. "Accept-Language: en-US,en;q=0.5",
  295. "Accept-Encoding: gzip, deflate, br",
  296. "DNT: 1",
  297. "Connection: keep-alive",
  298. "Referer: {$referer}"
  299. ]
  300. );
  301. break;
  302. }
  303. // follow redirects
  304. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  305. curl_setopt($curl, CURLOPT_MAXREDIRS, 5);
  306. curl_setopt($curl, CURLOPT_AUTOREFERER, 5);
  307. // set url
  308. curl_setopt($curl, CURLOPT_URL, $url);
  309. curl_setopt($curl, CURLOPT_ENCODING, ""); // default encoding
  310. // timeout + disable ssl
  311. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
  312. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  313. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
  314. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  315. curl_setopt(
  316. $curl,
  317. CURLOPT_WRITEFUNCTION,
  318. function($c, $data){
  319. if(curl_getinfo($c, CURLINFO_HTTP_CODE) !== 200){
  320. throw new Exception("Serber returned a non-200 code");
  321. }
  322. echo $data;
  323. return strlen($data);
  324. }
  325. );
  326. $this->empty_header = false;
  327. $this->cont = false;
  328. $this->headers_tmp = [];
  329. $this->headers = [];
  330. curl_setopt(
  331. $curl,
  332. CURLOPT_HEADERFUNCTION,
  333. function($c, $header){
  334. $head = trim($header);
  335. $len = strlen($head);
  336. if($len === 0){
  337. $this->empty_header = true;
  338. $this->headers_tmp = [];
  339. }else{
  340. $this->empty_header = false;
  341. $this->headers_tmp[] = $head;
  342. }
  343. foreach($this->headers_tmp as $h){
  344. // parse headers
  345. $h = explode(":", $h, 2);
  346. if(count($h) !== 2){
  347. if(curl_getinfo($c, CURLINFO_HTTP_CODE) !== 200){
  348. // not HTTP 200, probably a redirect
  349. $this->cont = false;
  350. }else{
  351. $this->cont = true;
  352. }
  353. // is HTTP 200, just ignore that line
  354. continue;
  355. }
  356. $this->headers[strtolower(trim($h[0]))] = trim($h[1]);
  357. }
  358. if(
  359. $this->cont &&
  360. $this->empty_header
  361. ){
  362. // get content type
  363. if(isset($this->headers["content-type"])){
  364. $filetype = explode("/", $this->headers["content-type"]);
  365. if(strtolower($filetype[0]) != $this->format){
  366. throw new Exception("Resource is not an {$this->format} (Found {$filetype[0]} instead)");
  367. }
  368. }else{
  369. throw new Exception("Resource is not an {$this->format} (no Content-Type)");
  370. }
  371. header("Content-Type: {$this->format}/{$filetype[1]}");
  372. // give payload size
  373. if(isset($this->headers["content-length"])){
  374. header("Content-Length: {$this->headers["content-length"]}");
  375. }
  376. // give filename
  377. $this->getfilenameheader($this->headers, $this->url, $filetype[1]);
  378. }
  379. return strlen($header);
  380. }
  381. );
  382. curl_exec($curl);
  383. if(curl_errno($curl)){
  384. throw new Exception(curl_error($curl));
  385. }
  386. curl_close($curl);
  387. }
  388. public function getfilenameheader($headers, $url, $filetype = "jpg"){
  389. // get filename from content-disposition header
  390. if(isset($headers["content-disposition"])){
  391. preg_match(
  392. '/filename=([^;]+)/',
  393. $headers["content-disposition"],
  394. $filename
  395. );
  396. if(isset($filename[1])){
  397. header("Content-Disposition: filename=" . $filename[1] . "." . $filetype);
  398. return;
  399. }
  400. }
  401. // get filename from URL
  402. $filename = parse_url($url, PHP_URL_PATH);
  403. if($filename === null){
  404. // everything failed! rename file to domain name
  405. header("Content-Disposition: filename=" . parse_url($url, PHP_URL_HOST) . "." . $filetype);
  406. return;
  407. }
  408. // remove extension from filename
  409. $filename =
  410. explode(
  411. ".",
  412. basename($filename)
  413. );
  414. if(count($filename) > 1){
  415. array_pop($filename);
  416. }
  417. $filename = implode(".", $filename);
  418. header("Content-Disposition: inline; filename=" . $filename . "." . $filetype);
  419. return;
  420. }
  421. public function getimageformat($payload, &$imagick){
  422. $finfo = new finfo(FILEINFO_MIME_TYPE);
  423. $format = $finfo->buffer($payload["body"]);
  424. if($format === false){
  425. if($payload["format"] === false){
  426. header("X-Error: Could not parse format");
  427. $this->favicon404();
  428. }
  429. $format = $payload["format"];
  430. }else{
  431. $format_tmp = explode("/", $format, 2);
  432. if($format_tmp[0] == "image"){
  433. $format_tmp = strtolower($format_tmp[1]);
  434. if(substr($format_tmp, 0, 2) == "x-"){
  435. $format_tmp = substr($format_tmp, 2);
  436. }
  437. $format = $format_tmp;
  438. }
  439. }
  440. switch($format){
  441. case "tiff": $format = "gif"; break;
  442. case "vnd.microsoft.icon": $format = "ico"; break;
  443. case "icon": $format = "ico"; break;
  444. case "svg+xml": $format = "svg"; break;
  445. }
  446. $imagick = new Imagick();
  447. if(
  448. !in_array(
  449. $format,
  450. array_map("strtolower", $imagick->queryFormats())
  451. )
  452. ){
  453. // format could not be found, but imagemagick can
  454. // sometimes detect it? shit's fucked
  455. $format = false;
  456. }
  457. return $format;
  458. }
  459. public function clientcache(){
  460. if($this->cache === false){
  461. return;
  462. }
  463. header("Last-Modified: Thu, 01 Oct 1970 00:00:00 GMT");
  464. $headers = getallheaders();
  465. if(
  466. isset($headers["If-Modified-Since"]) ||
  467. isset($headers["If-Unmodified-Since"])
  468. ){
  469. http_response_code(304); // 304: Not Modified
  470. die();
  471. }
  472. }
  473. }