如何在Java NIO中配置socks代理 [英] How to config socks proxy in Java NIO

查看:200
本文介绍了如何在Java NIO中配置socks代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个工具,其中包括强制应用程序的所有网络流量穿过Ja​​va中的socks代理。对于旧的Socket API,我可以设置系统属性-DsocksProxyHost = my-host -DsocksProxyPort = my-port,但它不适用于NIO。

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:

我写了一个名为ProxySocketChannel的NIO SocketChannel,它扩展了SocketChannel。它包含socks连接和其他socks代理逻辑。但是当我运行它时,我在SelectorImpl.register中的这行代码中得到了一个IllegalSelectorException:

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包是可见的,所以我无法访问它。我尝试了一个棘手的解决方案:我将ProxySocketChannel放在包sun.nio.ch中。编译通过,但运行时遇到错误:

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

我不知道为什么太阳级。包sun.nio.ch的nio.ch.ProxySocketChannel仍然无法访问sun.nio.ch.SelChImpl。我认为JDK内置类有一些保护。有没有办法访问它?

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?

我的JDK版本是1.6.0_65。

My JDK Version is 1.6.0_65.

否则,是否有在不更改现有代码的情况下为NIO设置socks代理的方法?

Otherwise, is there a way to setup socks proxy for NIO without change existing code?

推荐答案

这非常困难。几年前我写了一个 SSLSocketChannel 类,最后我不得不写自己的 SSLSelectorProvider SSLSelector ,以及 SSLSelectionKey 类。您不能只在现有基础架构中添加 SocketChannel - 派生类:它专门用于防止它。

This is seriously difficult. I wrote an SSLSocketChannel class some years ago and I ended up having to write my own SSLSelectorProvider, SSLSelector, and SSLSelectionKey classes as well. You can't just add a SocketChannel-derived class into the existing infrastructure: it is specifically designed to prevent it.

这篇关于如何在Java NIO中配置socks代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆