<?php
/**
 * This is SARBL whois and get proxy
 * (C) Benjamin Sonntag 2014-10
 * GPL-v3+ licensed
 * for more information, see https://www.sarbl.org/
 * 
 * requirements: 
 * PHP5.3+, php-curl (not mandatory)
 * ability to access the Internet: tcp/43,80,443, +DNS
 */


// List of allowed IP querying this service
// thanks to that, we basically don't check anything else...
$friends=array("91.194.61.194","2001:67c:288:1::194",
	       "91.194.60.170","2001:67c:288::170");

define("SARBL_WHOIS_VERSION","2.0~russian-flood");

if (!in_array($_SERVER["REMOTE_ADDR"],$friends)) {
  header("HTTP/1.0 403 Forbidden");
  echo "<h1>Access Forbidden</h1>\n<p>but <a href=\"https://www.sarbl.org/\">SARBL</a> whois-get-proxy seems to be working fine !</p>";
  exit();
}

// allowed commands below
header("Content-Type: text/plain");
switch ($_POST["command"]) {
case "version": // return a version number for this script
    echo SARBL_WHOIS_VERSION;
    exit();
case "whois": // return the whois information of a domain using a specific whois server, max 1MB
    whois($_POST["domain"],$_POST["server"]);
    exit();
case "get": // return an HTTP url content, just a simple GET, max size being 1MB
    get($_POST["url"]);
    exit();
}
echo "Command not found\n";
exit();


/** 
 * Launch a whois request for domain $dom on server $srv
 * uses fsockopen, limit to 1MB returned text
 * timeout at 3 seconds for both connect and gets
 * echoes one line with a number if something went wrong
 */
function whois($dom,$srv) {
    $srv=str_replace("/","",$srv);
    $f=fsockopen($srv,43,$errno,$errstr,3);
    if (!$f) {
        echo "1";
        return;
    }
    fputs($f,$dom."\r\n");
    stream_set_timeout($f, 3);
    $s="";
    while (!feof($f)) {
        $t=fgets($f,65536);
        $s.=$t;
        if (strlen($s)>=1024*1024) { // 1MB limit
            echo $s;
            fclose($f);
            return;
        }
        $info = stream_get_meta_data($f);
        if ($info['timed_out']) {
            echo $s;
            fclose($f);
            return;
        }
    }
    
    fclose($f);
    if (strlen($s)==0) {
        echo "2";
        return;
    }
    echo $s;
    return;
}


/**
 * get $url URL using HTTP or HTTPS (only) via cURL
 * uses CURL, fails if it's not installed
 * echoes a number only if an error occurred
 */
function get($url) {
    $useragent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
//    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    //  curl_setopt($ch, CURLOPT_POST, true);
    //  curl_setopt($ch, CURLOPT_POSTFIELDS, $POST);
    curl_setopt($ch, CURLOPT_HEADER, true);  
    $return = curl_exec($ch);
    if (curl_errno($ch)) {
        echo "0\n";
        echo curl_error($ch)."\n";
        return;
    }
    curl_close($ch);
    echo $return;
    return;
}

