function gen_http_request($url) { (yield 'notdone' => [$url, 'Just Loaded']); // Always yield to start with to help with queueing up requests $url = trim($url); if (!$url) { (yield 'error' => [$url, 'No URL was provided']); } $started_at = time(); $parsed_url = parse_url($url); $parsed_url['path'] = $parsed_url['path'] ?: '/'; $request = "GET {$parsed_url['path']} HTTP/1.0\r\n" . "Host: {$parsed_url['host']}\r\n" . "User-Agent: Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136\r\n" . "Connection: close\r\n" . "Accept: */*\r\n" . "pragma: no-cache\r\n" . "dnt: 1\r\n" . "\r\n"; // GET THE IP $ip = false; $ip_gen = gen_get_ip($parsed_url['host']); while (!$ip && $ip_gen->key()) { $ip_gen->next(); list($domain, $result) = $ip_gen->current(); switch ($ip_gen->key()) { case 'notdone': (yield 'notdone' => [$url, 'Waiting on DNS']); continue; case 'error': (yield 'error' => [$url, 'DNS resolution failed']); continue; case 'result': $ip = $result; break; } } //var_dump( "Found the IP for $url it's $ip" ); $connect_str = 'https' == $parsed_url['scheme'] ? "ssl://{$ip}:443" : "tcp://{$ip}:80"; $timeout = 15; $context = stream_context_create(array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true))); $socket = new D_AsyncSocket($connect_str, $timeout, $context); if ($socket->error()) { (yield 'error' => [$url, "Connect failed with " . $socket->error()]); } do { if (time() - $started_at > 15) { //echo "Giving up $url"; unset($socket); (yield 'error' => [$url, 'Giving Up']); } if ($socket->error()) { //echo "Error on $url\n"; (yield 'error' => [$url, 'URL Hung up on us']); } if (!$socket->ready_for_write()) { //echo "[$url] waiting for socket connect\n"; (yield 'notdone' => [$url, 'waiting for socket connect']); continue; } $wrote = $socket->write($request, strlen($request)); $request = substr($request, $wrote); } while (!$socket->error() && $request); $data = ''; while (!$socket->error() && !$socket->eof()) { if (time() - $started_at > 15) { //echo "Giving up $url"; (yield 'error' => [$url, 'Giving Up']); } if (!$socket->ready_for_read()) { //echo "[$url] waiting for data\n"; (yield 'notdone' => [$url, 'waiting for data']); continue; } $data .= $socket->read(8192); } unset($socket); if (!$data) { //echo "[$url] No data\n"; (yield 'error' => [$url, 'no data']); } list($headers, $body) = parse_http_response($data); $result = ['headers' => $headers, 'body' => $body, 'resolved_ip' => $ip]; (yield 'result' => [$url, $result]); }
function netsock($host, $param = '', $port = 80, $advanced = false, $useragent = false) { if (!($fp = @fsockopen($host, $port, $errno, $errstr, 10))) { return false; } $call = 'GET ' . $param . " HTTP/1.1\r\n"; if ($useragent !== false) { $call .= 'User-Agent: ' . $useragent . "\r\n"; } $call .= "Connection: Close\r\n\r\n"; $response = ''; @fputs($fp, $call); while (!feof($fp)) { $response .= @fgets($fp, 8192); } @fclose($fp); if ($advanced) { $response = parse_http_response($response); } else { $response = ltrim(substr($response, strpos($response, "\r\n\r\n"))); } return $response; }
<?php require_once 'functions.php'; require_once 'session.php'; rtgui_session_start(); $ip = $_REQUEST['ip']; if (!ip2long($ip)) { die(json_encode(array('ip' => $ip, 'error' => 'Invalid IP'))); } if (is_array($_SESSION["ip-{$ip}"])) { die(json_encode($_SESSION["ip-{$ip}"])); } // What hostip.info returns happens to look a lot like HTTP headers $url = "http://api.hostip.info/get_html.php?ip={$ip}"; $info = parse_http_response(file_get_contents($url)); $info = $info[0]; $info['hostname'] = gethostbyaddr($ip); $info['country_short'] = preg_replace('@^[^(]+\\((.*)\\)[^)]*$@', '\\1', $info['country']); foreach (array('city', 'country', 'country_short') as $key) { if (stristr($info[$key], 'unknown') !== false) { $info[$key] = 'unknown'; } } $info['location'] = $info['city'] . ', ' . $info['country_short']; $_SESSION["ip-{$ip}"] = $info; print json_encode($info);
function do_xmlrpc($request) { global $scgi_host, $scgi_port, $scgi_timeout; if ($response = scgi_send($scgi_host, $scgi_port, $request, $scgi_timeout)) { $response = parse_http_response($response); $content = str_replace('i8', 'double', $response[1]); return xmlrpc_decode(utf8_encode($content)); } else { die('<h1>ERROR: Cannot connect to rtorrent</h1>'); } }
error_reporting($err_level); print base64_encode(json_encode($to_add)); break; case 'process_url': if (!function_exists('curl_init')) { json_error('The required PHP CURL extension is not present.'); } $c = curl_init($r_data); curl_setopt_array($c, array(CURLOPT_HEADER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_FAILONERROR => true)); if ($cookies_file) { curl_setopt($c, CURLOPT_COOKIEFILE, $cookies_file); } $r = curl_exec($c); if ($r !== false) { curl_close($c); $response = parse_http_response($r); $content = $response[1]; $filename = basename(parse_url($r_data, PHP_URL_PATH)); if (!preg_match('@\\.torrent$@i', $filename)) { $filename .= '.torrent'; } $mime_type = ''; foreach ($response[0] as $header => $value) { switch (strtolower($header)) { case 'content-disposition': preg_match('@filename="?([^"]+)"?$@', $value, $matches); if (count($matches) > 1) { $filename = $matches[1]; } break; case 'content-type':