Skip to content

Instantly share code, notes, and snippets.

@ServOKio
Created March 21, 2022 02:06
Show Gist options
  • Save ServOKio/ab4c0beca026d11ab24fd2afabc1f4ce to your computer and use it in GitHub Desktop.
Save ServOKio/ab4c0beca026d11ab24fd2afabc1f4ce to your computer and use it in GitHub Desktop.
Our .php proxy
<?php
header("Access-Control-Allow-Origin: *");
$baseURL = "http://site.net/api.php"; //For example http://mysite/proxy.php
$request = $_GET["reg"];
if ($request == "get_info") {
$url = $_GET["url"];
$ch = curl_init();
$headers = array(
'cache-control: max-age=0',
'upgrade-insecure-requests: 1',
'user-agent: Mozilla/5.0 (compatible; ServOKioBot/1.0; + https://servokio.ru/bots)',
'sec-fetch-user: ?1',
'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'accept-encoding: deflate, br',
'accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
$html = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$c_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$t_time = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
$c_time = curl_getinfo($ch, CURLINFO_CONNECT_TIME);
if ($http_code == 200) {
if($url !== curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)){
$PROXY = $baseURL."?reg=get_info&url=";
$url_new = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
header("HTTP/1.1 301 Moved Permanently");
header('Location: ' . $PROXY . $url_new);
}
$file_array = explode("\r\n\r\n", $html, 2);
$image = imagecreatefromstring($file_array[1]);
$result = array(
'statscode' => 1,
'message' => 'ok',
'contentType' => $c_type,
'httpCode' => $http_code,
'totalTime' => $t_time,
'connectTime' => $c_time,
'width' => imagesx($image),
'height' => imagesy($image),
'url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL),
);
curl_close($ch_new);
imagedestroy($image);
header("Content-Type: application/json; charset=utf-8");
echo json_encode($result, JSON_UNESCAPED_UNICODE);
} else if($http_code == 302){
$PROXY = $baseURL."?reg=get_info&url=";
$url_new = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
header("HTTP/1.1 301 Moved Permanently");
header('Location: ' . $PROXY . $url_new);
} else {
curl_close($ch);
$result = array(
'statscode' => 1,
'message' => 'ok',
'httpCode' => $http_code,
);
header("Content-Type: application/json; charset=utf-8");
echo json_encode($result, JSON_UNESCAPED_UNICODE);
}
} else if ($request == "get_content") {
$url = $_GET["url"];
$u18chan = $_GET["u18chan"] == 'true' ? true : false;
$ch = curl_init();
$headers = array(
'cache-control: max-age=0',
'upgrade-insecure-requests: 1',
'user-agent: Mozilla/5.0 (compatible; ServOKioBot/1.0; + https://servokio.ru/bots)',
'sec-fetch-user: ?1',
'accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
);
if($u18chan) array_push($headers, 'Cookie: fap_mode=on');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$body = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 200) {
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($body, 0, $headerSize);
$header = getHeaders($header);
$body = substr($body, $headerSize);
curl_close($ch);
print_r($body);
} else if($http_code == 302 || $http_code == 301){
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close($body);
$header = substr($body, 0, $headerSize);
preg_match("!\r\n(?:Location|URI): *(.*?) *\r\n!", $header, $matches);
$url_new = $matches[1];
$ch_new = curl_init();
curl_setopt($ch_new, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch_new, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_new, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch_new, CURLOPT_HEADER, true);
curl_setopt($ch_new, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch_new, CURLOPT_URL, $url_new);
$body_new = curl_exec($ch_new);
$http_code_new = curl_getinfo($ch_new, CURLINFO_HTTP_CODE);
if ($http_code_new == 200) {
$headerSize_new = curl_getinfo($ch_new, CURLINFO_HEADER_SIZE);
$body_new = substr($body_new, $headerSize_new);
curl_close($ch_new);
print_r($body_new);
} else {
curl_close($ch_new);
echo "no ".$http_code_new;
}
} else {
curl_close($ch);
echo "no ".$http_code." ".$url;
}
} else if ($request == "get_image") {
$url = $_GET["url"];
if (
isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) or
isset($_SERVER['HTTP_IF_NONE_MATCH'])
) {
header('HTTP/1.1 304 Not Modified');
exit;
}
$PROXY = $baseURL."?reg=get_image&url=";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; ServOKioBot/1.0; + https://servokio.ru/bots)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 12800);
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function ($DownloadSize, $Downloaded, $UploadSize, $Uploaded) {
return ($Downloaded > 104857600) ? 1 : 0;
}); # max 500kb
$out = curl_exec($ch);
curl_close($ch);
if (strlen($out) === 0) {
header("HTTP/1.1 403 Forbidden ");
echo strlen($out);
exit;
}
$file_array = explode("\r\n\r\n", $out, 2);
$header_array = explode("\r\n", $file_array[0]);
foreach ($header_array as $header_value) {
$header_pieces = explode(': ', $header_value);
if (count($header_pieces) == 2) {
$headers[$header_pieces[0]] = trim($header_pieces[1]);
}
}
if (array_key_exists('Location', $headers)) {
$newurl = urlencode($headers['Location']);
header("HTTP/1.1 301 Moved Permanently");
header('Location: ' . $PROXY . $newurl);
} else {
if (array_key_exists('Content-Type', $headers)) {
$ct = $headers['Content-Type'];
if (preg_match('#image/png|image/.*icon|image/jpe?g|image/gif#', $ct) !== 1) {
header('HTTP/1.1 404 Not Found');
exit;
}
header('Content-Type: ' . $ct);
}
if (array_key_exists('Content-Length', $headers))
header('Content-Length: ' . $headers['Content-Length']);
if (array_key_exists('Expires', $headers))
header('Expires: ' . $headers['Expires']);
if (array_key_exists('Cache-Control', $headers))
header('Cache-Control: ' . $headers['Cache-Control']);
if (array_key_exists('Last-Modified', $headers))
header('Last-Modified: ' . $headers['Last-Modified']);
echo $file_array[1];
}
} else if ($request == "get_video") {
$url = $_GET["url"];
if (
isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) or
isset($_SERVER['HTTP_IF_NONE_MATCH'])
) {
header('HTTP/1.1 304 Not Modified');
exit;
}
$PROXY = $baseURL."?reg=get_video&url=" . $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; ServOKioBot/1.0; + https://servokio.ru/bots)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 12800);
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function ($DownloadSize, $Downloaded, $UploadSize, $Uploaded) {
return ($Downloaded > 104857600) ? 1 : 0;
}); # max 500kb
$out = curl_exec($ch);
curl_close($ch);
if (strlen($out) === 0) {
header("HTTP/1.1 403 Forbidden ");
echo strlen($out);
exit;
}
$file_array = explode("\r\n\r\n", $out, 2);
$header_array = explode("\r\n", $file_array[0]);
foreach ($header_array as $header_value) {
$header_pieces = explode(': ', $header_value);
if (count($header_pieces) == 2) {
$headers[$header_pieces[0]] = trim($header_pieces[1]);
}
}
if (array_key_exists('Location', $headers)) {
$newurl = urlencode($headers['Location']);
header("HTTP/1.1 301 Moved Permanently");
header('Location: ' . $PROXY . $newurl);
} else {
if (array_key_exists('Content-Type', $headers)) {
$ct = $headers['Content-Type'];
if (preg_match('#video/mp4|video/webm#', $ct) !== 1) {
header('HTTP/1.1 404 Not Found');
exit;
}
header('Content-Type: ' . $ct);
}
if (array_key_exists('Content-Length', $headers))
header('Content-Length: ' . $headers['Content-Length']);
if (array_key_exists('Expires', $headers))
header('Expires: ' . $headers['Expires']);
if (array_key_exists('Cache-Control', $headers))
header('Cache-Control: ' . $headers['Cache-Control']);
if (array_key_exists('Last-Modified', $headers))
header('Last-Modified: ' . $headers['Last-Modified']);
echo $file_array[1];
}
} else if ($request == "get_server_info") {
$result = array(
'statscode' => 1,
'message' => 'ok',
'server' => array(
'disk_free_space' => disk_free_space("/"),
'disk_total_space' => disk_total_space("/"),
'ram_free_space' => memory_get_usage(),
'ram_total_space' => memory_get_peak_usage(),
'cpu_usage' => sys_getloadavg()[0]
)
);
header("Content-Type: application/json; charset=utf-8");
echo json_encode($result, JSON_UNESCAPED_UNICODE);
} else if ($request == "hi") {
$result = array(
'statscode' => 1,
'message' => 'ok'
);
}
function getHeaders($respHeaders){
$headers = array();
$headerText = substr($respHeaders, 0, strpos($respHeaders, "\r\n\r\n"));
foreach (explode("\r\n", $headerText) as $i => $line) {
if ($i === 0) {
$headers['http_code'] = $line;
} else {
list($key, $value) = explode(': ', $line);
$headers[$key] = $value;
}
}
return $headers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment