1
0

curlproxy.php 13 KB

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