Checking proxy anonymity in Java

433 views Asked by At

I need to determine level of proxy anonymity. I use following php script for detection:

<?php
if (!getenv('HTTP_X_FORWARDED_FOR')){
    echo "Anonymous";
    exit;
}
if ((!getenv('HTTP_X_FORWARDED_FOR')) && (!getenv('HTTP_VIA')) && (!getenv('HTTP_PROXY_CONNECTION')){
    echo "High Anonymous";
    exit;
}
echo "Unknown";
?>

and I use next code for page loading (java):

private static String load(ProxyWrapper proxyWrapper, URL url) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        SocketAddress socketAddress = null;
        try {
            socketAddress = new InetSocketAddress(proxyWrapper.getIp(), Integer.parseInt(proxyWrapper.getPort()));
        } catch (Exception e) {
            return null;
        }
        Proxy proxy = new Proxy(proxyWrapper.getType(), socketAddress);
        BufferedReader in = null;
        try {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
            connection.setConnectTimeout(proxyWrapper.getTimeout());
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                stringBuilder.append(inputLine + "\n");
            }
        } finally {
            if (in != null) {
                in.close();
            }
        }
        return stringBuilder.toString();
    }

My problem is that 90% of working anonymous proxies are recognized by script as "Unknown". For example if I load any ip determination service, it will show ip of proxy, that means it is anonymous. Could anyone tell me what is the problem?

1

There are 1 answers

0
Kim Yun Hee On

you should put your echo "Unknown"; into the else section. Otherwise it is always displayed.

  <?php
    if (!getenv('HTTP_X_FORWARDED_FOR')){
        echo "Anonymous";
        exit;
    }elseif ((!getenv('HTTP_X_FORWARDED_FOR')) && (!getenv('HTTP_VIA')) && (!getenv('HTTP_PROXY_CONNECTION')){
        echo "High Anonymous";
        exit;
    } else {
    echo "Unknown";
}
    ?>