0

I am trying to make a class to help me manage proxies in a program. I want to use apaches http client to check if the proxy is live and if it is get what time zone it is in. The class currently has dependencies on apaches http client and org.json. Whenever I try with an authenticated proxy it outputs this :

Checking proxy global.711proxy.com:10000
Proxy check failed: Remote host terminated the handshake
Proxy is dead or not in USA. Timezone: null

Here is my class. Everything else is working as intended.

import org.json.JSONObject;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.client5.http.classic.methods.HttpGet;

public class Proxy {
    private String host;
    private int port;
    private String username;
    private String password;
    private boolean isAuth;
    private boolean isLive;
    private String timezone;

    public Proxy(String proxyString) {
        parseProxy(proxyString);
        checkProxy();
    }

    private void parseProxy(String proxyString) {
        String[] parts = proxyString.split(":");
        if (parts.length < 2 || parts.length > 4) {
            throw new IllegalArgumentException("Invalid proxy format. Use host:port or host:port:user:pass");
        }

        this.host = parts[0];
        this.port = Integer.parseInt(parts[1]);
        
        if (parts.length == 4) {
            this.username = parts[2];
            this.password = parts[3];
            this.isAuth = true;
        } else {
            this.isAuth = false;
        }
    }

    private void checkProxy() {
        try {
            System.out.println("Checking proxy " + host + ":" + port);
            
            // Define target and proxy hosts
            final HttpHost target = new HttpHost("https", "worldtimeapi.org", 443);
            final HttpHost proxy = new HttpHost("http", host, port);

            // Build client based on authentication needs
            final CloseableHttpClient httpClient;
            if (isAuth) {
                // With authentication
                BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
                credsProvider.setCredentials(
                    new AuthScope(host, port),
                    new UsernamePasswordCredentials(username, password.toCharArray())
                );
                
                httpClient = HttpClients.custom()
                    .setProxy(proxy)
                    .setDefaultCredentialsProvider(credsProvider)
                    .build();
            } else {
                // Without authentication
                httpClient = HttpClients.custom()
                    .setProxy(proxy)
                    .build();
            }

            try {
                final HttpGet request = new HttpGet("/api/ip");
                
                httpClient.execute(target, request, response -> {
                    this.isLive = (response.getCode() >= 200 && response.getCode() < 400);
                    
                    if (this.isLive) {
                        String jsonStr = EntityUtils.toString(response.getEntity());
                        JSONObject json = new JSONObject(jsonStr);
                        this.timezone = json.getString("timezone");
                        System.out.println("Proxy check successful. Timezone: " + timezone);
                    }
                    
                    return null;
                });
            } finally {
                httpClient.close();
            }
            
        } catch (Exception e) {
            System.err.println("Proxy check failed: " + e.getMessage());
            this.isLive = false;
            this.timezone = null;
        }
    }
    
    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public boolean isAuth() {
        return isAuth;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public boolean isLive() {
        return isLive;
    }
    
    public String getTimezone() {
        return timezone;
    }

    public void recheckLive() {
        checkProxy();
    }

    @Override
    public String toString() {
        return String.format("Proxy{host='%s', port=%d, auth=%b, live=%b}", 
            host, port, isAuth, isLive);
    }
}
4
  • Hi! Haven't you tried the proxy URLs with another client, such as cURL? $ curl cloudflare.com/cdn-cgi/trace -x http://<user:password>@proxy.soax.com:5000 Also, you can try online proxy checks like this one datascrape.tech/tools/proxychick Commented Dec 31, 2024 at 11:32
  • If everything is good with the proxy URLs and the alternative client returns correct replies from the target - I suggest taking a look at your requests in plain text. Wireshark can help with that Commented Dec 31, 2024 at 11:33
  • I checked proxys are live. Api works from curl too with curl "http://worldtimeapi.org/api/ip" Commented Dec 31, 2024 at 16:24
  • The error message sounds like SSL issue. Try to connect to other target URL. If the target resource terminates the handshake, you need to validate your SSL client behavior. I recommend using Wireshark/tcpdump to capture the traffic; there might be an exact error code from the SSL server. Commented Jan 1 at 14:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.