xtuhcy / socks5-netty Goto Github PK
View Code? Open in Web Editor NEW基于netty实现的socks5代理
License: MIT License
基于netty实现的socks5代理
License: MIT License
2020-10-25 15:03:57 [nioEventLoopGroup-4-6] DEBUG com.geccocrawler.socks5.handler.ss5.Socks5InitialRequestHandler -初始化ss5连接 : DefaultSocks5InitialRequest(decoderResult: failure(io.netty.handler.codec.DecoderException: unsupported version: 67 (expected: 5)), authMethods: [NO_AUTH(0)]) 2020-10-25 15:03:57 [nioEventLoopGroup-4-6] DEBUG com.geccocrawler.socks5.handler.ss5.Socks5InitialRequestHandler -不是ss5协议: {} io.netty.handler.codec.DecoderException: unsupported version: 67 (expected: 5) at io.netty.handler.codec.socksx.v5.Socks5InitialRequestDecoder.decode(Socks5InitialRequestDecoder.java:55) at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:501) at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) at io.netty.handler.logging.LoggingHandler.channelRead(LoggingHandler.java:271) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) at io.netty.handler.traffic.AbstractTrafficShapingHandler.channelRead(AbstractTrafficShapingHandler.java:513) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.lang.Thread.run(Thread.java:748)
下载运行的时候,填写代理,浏览器访问报错
wrapper | The proxy service is not installed - The specified service does not exist as an installed service. (0x424)
感谢作者提供了一个很好的学习 netty socks5 的项目。
之前一直想写一个代理,以了解http原理,最终目的是达到大陆到国外的不可描述的目的。作者的项目是很好的sslocal-加密 的项目。
拿到这个项目,看了很久之后感觉作者写的很完善,没什么好自己动手修改的地方。
然后就看作者的具体实现。用jdk9带的 java misson control 工具查看了一下项目的内存情况。发现当长时间运行,产生多个与远端的连接时,程序占用的内存非常大,而且使用jmc的full gc工具也无法进行垃圾回收。具体情况可以见附图。
看jmc中的线程信息时,发现:出现了350多个类nioEventLoopGroup-350-1的runnable的线程。
结合学习的netty知识判断和对您源码的阅读,判断是在Socks5CommandRequestHandler.channelRead0()
方法中的EventLoopGroup bossGroup=new NioEventLoopGroup();
语句导致的。这句话导致——每次需要代理新建到远程服务器的连接时,都创建了一个NioEventLoopGroup。而且经过上面fullGC的测试,这些NioEventLoopGroup还不能被回收。
netty实战告诉我,服务器端有两个NioEventLoopGroup,客户端有一个NioEventLoopGroup。
所以我设想,只使用一个NioEventLoopGroup固定的处理所有对远程的channel。
我做出了如下修改
在Socks5CommandRequestHandler
中增加:
private EventLoopGroup bossGroup;
public Socks5CommandRequestHandler(ProxyServer proxyServer) {
bossGroup=proxyServer.getBossGroup();
}
即在ProxyServer中创建一个单独的NioEventLoopGroup,在Socks5CommandRequestHandler,导入这个NioEventLoopGroup,用以管理与远程的连接。
在JMC的线程页面中显示只有3个NioEventLoopGroup(编号分别位2、3、4)。当执行full gc时,内存占用降低到13.8M。这个数据非常稳定,13.8M。
这是第一次在github上提交issue。也是第一次利用自己的知识解决java内存相关的问题。感觉很宝贵。
I got Exception:
It's strange that:
I visited url https://www.baidu.com, it works well,
but when I visited https://www.google.com,
it throws an exception:
Exception in thread "main" java.net.ProtocolException: unexpected end of stream
at okhttp3.internal.http1.Http1Codec$FixedLengthSource.read(Http1Codec.java:398)
at okio.RealBufferedSource.read(RealBufferedSource.java:46)
at okio.RealBufferedSource.exhausted(RealBufferedSource.java:56)
at okio.InflaterSource.refill(InflaterSource.java:101)
at okio.InflaterSource.read(InflaterSource.java:62)
at okio.GzipSource.read(GzipSource.java:80)
at okio.Buffer.writeAll(Buffer.java:1005)
at okio.RealBufferedSource.readString(RealBufferedSource.java:190)
at okhttp3.ResponseBody.string(ResponseBody.java:175)
at com.geccocrawler.socks5.test.HttpProxyClient.main(HttpProxyClient.java:42)
Here was my code:
user, password, host, port, I'm sure it's correct:
public static void main(String[] args) throws Exception {
final String user = "x";
final String password = "y";
Proxy proxyTest = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("115.137.1.15", 6080));
java.net.Authenticator.setDefault(new java.net.Authenticator()
{
private PasswordAuthentication authentication = new PasswordAuthentication(user, password.toCharArray());
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return authentication;
}
});
OkHttpClient client = new OkHttpClient.Builder().proxy(proxyTest).build();
Request request = new Request.Builder().url("https://www.google.com").build();
Response response = client.newCall(request).execute();
System.out.println(response.code());
System.out.println(response.body().string());
client.dispatcher().executorService().shutdown();
client.connectionPool().evictAll();
}
pxoxy.bat start
the proxy service is not installed -指定的服务未安装
有基于netty的 socks5 client吗
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.