curlproxy.php 13 KB

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