// Create proxy connector.
NioSocketConnector socketConnector = new NioSocketConnector(Runtime
.getRuntime().availableProcessors() + 1);
ProxyConnector connector = new ProxyConnector(socketConnector);
// Set connect timeout.
connector.setConnectTimeoutMillis(5000);
URL url = new URL(args[2]);
int port = url.getPort() == -1 ? url.getDefaultPort() : url.getPort();
ProxyRequest req = null;
if (args.length == 4) {
if ("SOCKS4".equals(args[3])) {
req = new SocksProxyRequest(
SocksProxyConstants.SOCKS_VERSION_4,
SocksProxyConstants.ESTABLISH_TCPIP_STREAM,
new InetSocketAddress(url.getHost(), port), USER);
} else if ("SOCKS4a".equals(args[3])) {
req = new SocksProxyRequest(
SocksProxyConstants.ESTABLISH_TCPIP_STREAM, url
.getHost(), port, USER);
} else if ("SOCKS5".equals(args[3])) {
req = new SocksProxyRequest(
SocksProxyConstants.SOCKS_VERSION_5,
SocksProxyConstants.ESTABLISH_TCPIP_STREAM,
new InetSocketAddress(url.getHost(), port), USER);
((SocksProxyRequest) req).setPassword(PWD);
((SocksProxyRequest) req)
.setServiceKerberosName(Socks5GSSAPITestServer.SERVICE_NAME);
} else {
req = createHttpProxyRequest(args[2]);
}
} else {
req = createHttpProxyRequest(args[2]);
}
ProxyIoSession proxyIoSession = new ProxyIoSession(
new InetSocketAddress(args[0], Integer.parseInt(args[1])), req);
// Tests modifying authentication order preferences. First algorithm in list available on server
// will be used for authentication.
List<HttpAuthenticationMethods> l = new ArrayList<HttpAuthenticationMethods>();
l.add(HttpAuthenticationMethods.DIGEST);
l.add(HttpAuthenticationMethods.BASIC);
proxyIoSession.setPreferedOrder(l);
connector.setProxyIoSession(proxyIoSession);
socketConnector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 5);
connector.getFilterChain().addLast("logger", new LoggingFilter());
// This command is sent when using a socks proxy to request a page from the web server.
String cmd = "GET " + url.toExternalForm() + " HTTP/1.0"
+ HttpProxyConstants.CRLF + HttpProxyConstants.CRLF;
connector.setHandler(new ClientSessionHandler(cmd));
IoSession session;
for (;;) {
try {
ConnectFuture future = connector.connect();
future.awaitUninterruptibly();
session = future.getSession();
break;
} catch (RuntimeIoException e) {
System.err.println("Failed to connect. Retrying in 5 secs ...");
Thread.sleep(5000);
}
}
// Wait until done
if (session != null) {
session.getCloseFuture().awaitUninterruptibly();
}
connector.dispose();
System.exit(0);
}