Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netty and proxy server in an applet

Tags:

netty

We use Netty in a Java applet to talk to a tomcat server. This works perfectly.

I am able to get the proxy information using the helper class posted below.

My question is how do I supply the proxy server information to Netty?

More information:

We have a user who has configured a proxy server in Internet Explorer. The web page and Java applet get served up, but when Netty tries to connect, it bypasses the proxy server and does a direct connection; this fails because the user has a firewall set up to prevent direct connections -- all client connections must go through the proxy server.

Helper code to get proxy information:

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

public class ProxyConfig {

    private static final String JAVA_NET_USE_SYSTEM_PROXIES = "java.net.useSystemProxies";
    private static String host;
    private static int port;
    private static String protUrlStr = null;

    public static void init(URL url) {
        // http://10.0.1.136
        protUrlStr = url.getProtocol() + "://" + url.getHost();
        System.setProperty(JAVA_NET_USE_SYSTEM_PROXIES, "true");
        Proxy proxy = getProxy();
        if(proxy != null) {
            InetSocketAddress addr = (InetSocketAddress) proxy.address();
            if(addr != null) {
                host = addr.getHostName();
                port = addr.getPort();
                System.setProperty(JAVA_NET_USE_SYSTEM_PROXIES, "false");
                System.setProperty("http.proxyHost", host);
                System.setProperty("http.proxyPort", port + "");
            }
        }
        System.setProperty(JAVA_NET_USE_SYSTEM_PROXIES, "false");
    }

    public static String getHost() {
        return host;
    }

    public static int getPort() {
        return port;
    }

    private static Proxy getProxy() {
        List<Proxy> l = null;
        try {
            ProxySelector def = ProxySelector.getDefault();
            System.out.println("Getting List<Proxy> for URL = " + protUrlStr);
            l = def.select(new URI(protUrlStr));
            ProxySelector.setDefault(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(l != null) {
            for(Iterator<Proxy> iter = l.iterator(); iter.hasNext();) {
                java.net.Proxy proxy = iter.next();
                return proxy;
            }
        }
        return null;
    }
}
like image 393
ForceRs Avatar asked Jan 17 '13 15:01

ForceRs


1 Answers

Thanks for a very detailed question. Both HTTP and SOCKS proxy support has not been integrated into Netty yet. Netty 3.6.2 has support for SOCKS but it is only provided as a codec, so you need to adapt a little bit to make it work transparently.

The current lack of proper proxy support is related to how Java NIO works, although Netty is definitely capable of integrating proxy support to its core. If someone is willing to contribute, the Netty project team will be very happy to review the contribution and accept it.

If you are going to send an HTTP request only with Netty, you might want to give a dedicated HTTP client library based on Netty which supports proxies, such as AsyncHttpClient.

like image 50
trustin Avatar answered Sep 28 '22 02:09

trustin