I'm developing a tool which includes forcing all network traffic of application to go across a socks proxy in Java. For old Socket API, I can just set system properties "-DsocksProxyHost=my-host -DsocksProxyPort=my-port", but it doesn't work with NIO.
I tried a solution:
I wrote an NIO SocketChannel, called "ProxySocketChannel" which extends SocketChannel. It contains socks connection and other socks proxy logic. But when I run it, I got an "IllegalSelectorException" in this line of code in "SelectorImpl.register":
if (!(ch instanceof SelChImpl))
throw new IllegalSelectorException();
sun.nio.ch.SelChImpl is package visible so I can't access it. I tried a tricky solution: I put my "ProxySocketChannel" in package "sun.nio.ch". The compilation passed, but I got an error when running:
java.lang.IllegalAccessError: class sun.nio.ch.ProxySocketChannel cannot access its superinterface sun.nio.ch.SelChImpl
I don't know why the class sun.nio.ch.ProxySocketChannel with package sun.nio.ch still can not access sun.nio.ch.SelChImpl. I think there is some protection for JDK built-in classes. Is there a way to access it?
My JDK Version is 1.6.0_65.
Otherwise, is there a way to setup socks proxy for NIO without change existing code?