SocketFactory and java.nio
Hi guys,
i have a serious problem with java.nio / java.net.
I'm writing a library which provides an abstraction about the details how a connection is established. This includes that when certain settings are set, a relayed connection should be returned. The library is based on java.nio.
The problem: When I return a SocketChannel which is uses a relay host, its socket().getRemoteSocketAddress()-method returns the relay hosts address. BUT I want it to return the address of the target, namely the guy behind the relay host.
First try:
I wanted to use the higher-level abstraction "SocketFactory" and adapt Socket, so that the getRemoteAddress()-method returns the real target, not the relay host. Anyway, there seems to be no way to plug in this abstraction [my custom SocketFactory] in the newIO channel techniques. Am I correct that the SocketChannel can't be customized to create its sockets using a specified SocketFactory?
Second try:
I tried to implement my own SelectorProvider by delegating the calls to the default provider, adapting SocketChannel and overwriting its socket()-method [so that the returned socket is wrapped by my socket which returns the target, not the relay]. Anyway, things get more complicated... it is not possible to adapt the socketchannel, because the WindowsSelector casts it to a WindowsSocketChannel [or similar stuff] which, of course, results in a cast exception => not possible to adapt SocketChannel. All in all... bad try to create a custom SelectorProvider.
Third try:
I tried to implement my own CustomSocketImpl and a SocketImplFactory and set them to the default classes... anyway, thats insane too, since I have to implement plain sockets again, because i cant adapt the existing one because its not public. Far too complicated.. it would be fine to use those classes and just override one single method, but noooooo.... private :(.
Workaround:
Just dont call socket().getRemoteSocketAddress(). Create a class which checks out whether the socket owns a relayed connection and return the proper address (which we registered before). Of course this is possible, but it has the obvious disadvantage that the resulting code can't be plugged in in any application without checking for these calls [since the socket just returnes the wrong address]. Seems to be a bad solution, because the returned channels are inconsistent, I dont like that.
Anyone has some nice idea how to realize that? The workaround seems to be ugly, is there no better way?
[I just got an idea: Does java.nio has SOCKS support? If yes, is it possible to set a proxy for one requested Channel? I think I could use SOCKS as relay service and it seems to be inplemented in java.net.Socket]
Message was edited by:
Matthias__

