Skip to content

Commit 996ce67

Browse files
committed
完善socks5
1 parent 3828cba commit 996ce67

10 files changed

+311
-293
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<dependency>
1414
<groupId>io.netty</groupId>
1515
<artifactId>netty-all</artifactId>
16-
<version>4.0.36.Final</version>
16+
<version>4.1.6.Final</version>
1717
</dependency>
1818
<dependency>
1919
<groupId>org.slf4j</groupId>

src/main/java/com/io2c/httpproxyserver/HttpProxyServer.java

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import com.io2c.httpproxyserver.container.Container;
44
import com.io2c.httpproxyserver.container.ContainerHelper;
5-
import com.io2c.httpproxyserver.handler.HttpProxyRequestHandler;
6-
import com.io2c.httpproxyserver.handler.HttpsCommandHandler;
7-
import com.io2c.httpproxyserver.handler.socks.HttpsSocksProxyChannelHandler;
8-
import com.io2c.httpproxyserver.handler.socks.RealServerChannelHandler;
5+
import com.io2c.httpproxyserver.handler.https.HttpProxyRequestHandler;
6+
import com.io2c.httpproxyserver.handler.https.HttpsCommandHandler;
7+
import com.io2c.httpproxyserver.handler.https.HttpsTunnelProxyChannelHandler;
8+
import com.io2c.httpproxyserver.handler.https.HttpsTunnelProxyRealServerChannelHandler;
9+
import com.io2c.httpproxyserver.handler.socks.Socks5CommandRequestHandler;
10+
import com.io2c.httpproxyserver.handler.socks.Socks5InitialRequestHandler;
11+
import com.io2c.httpproxyserver.handler.socks.Socks5PasswordAuthRequestHandler;
912
import io.netty.bootstrap.Bootstrap;
1013
import io.netty.bootstrap.ServerBootstrap;
1114
import io.netty.buffer.ByteBuf;
@@ -19,6 +22,10 @@
1922
import io.netty.handler.codec.http.HttpMethod;
2023
import io.netty.handler.codec.http.HttpRequest;
2124
import io.netty.handler.codec.http.HttpServerCodec;
25+
import io.netty.handler.codec.socksx.v5.Socks5CommandRequestDecoder;
26+
import io.netty.handler.codec.socksx.v5.Socks5InitialRequestDecoder;
27+
import io.netty.handler.codec.socksx.v5.Socks5PasswordAuthRequestDecoder;
28+
import io.netty.handler.codec.socksx.v5.Socks5ServerEncoder;
2229
import io.netty.handler.ssl.SslHandler;
2330
import io.netty.util.AttributeKey;
2431
import org.slf4j.Logger;
@@ -33,6 +40,7 @@
3340
import java.util.HashMap;
3441
import java.util.Map;
3542
import java.util.Properties;
43+
import java.util.concurrent.ExecutionException;
3644

3745
/**
3846
* @author fei.feng
@@ -81,7 +89,8 @@ public void start() {
8189
initProxyClient(proxyClientBootstrap, workerGroup);
8290
initHttpProxyServer(httpServerBootstrap, proxyClientBootstrap, bossGroup, workerGroup);
8391
initHttpsProxyServer(httpsServerBootstrap, proxyClientBootstrap, bossGroup, workerGroup);
84-
initHttpsSocksProxyServer();
92+
initHttpsTunnelProxyServer();
93+
initSocks5ProxyServer();
8594
try {
8695
httpServerBootstrap.bind(configuration.getProperty("server.bind"), Integer.parseInt(configuration.getProperty(CONFIG_SERVER_PORT_KEY))).get();
8796
LOG.info("http proxy server started on port {}, bind {}", configuration.getProperty(CONFIG_SERVER_PORT_KEY), configuration.getProperty("server.bind"));
@@ -162,19 +171,22 @@ public void initChannel(SocketChannel ch) throws Exception {
162171
});
163172
}
164173

165-
private void initHttpsSocksProxyServer() {
174+
/**
175+
* https隧道代理其他协议端口
176+
*/
177+
private void initHttpsTunnelProxyServer() {
166178
ServerBootstrap serverBootstrap = new ServerBootstrap();
167179
final Bootstrap proxyClientBootstrap = new Bootstrap();
168180
proxyClientBootstrap.channel(NioSocketChannel.class);
169181
proxyClientBootstrap.group(workerGroup).handler(new ChannelInitializer<SocketChannel>() {
170182

171183
@Override
172184
public void initChannel(SocketChannel ch) {
173-
ch.pipeline().addLast(new RealServerChannelHandler());
185+
ch.pipeline().addLast(new HttpsTunnelProxyRealServerChannelHandler());
174186
}
175187
});
176188

177-
String configStr = configuration.getProperty("server.https.proxy.config");//port->ip:port,port->ip:port
189+
String configStr = configuration.getProperty("https.tunnel.config");//port->ip:port,port->ip:port
178190
final Map<Integer, String> portMap = new HashMap<>();
179191
final SSLContext sslContext = new SslContextCreator().initSSLContext(configuration.getProperty("server.https.jksPath"),
180192
configuration.getProperty("server.https.keyStorePassword"), configuration.getProperty("server.https.keyManagerPassword"));
@@ -197,7 +209,7 @@ public void initChannel(SocketChannel ch) {
197209
}
198210
ch.attr(connectInfoAttributeKey).set(ipPort);
199211
pipeline.addLast("ssl", createSslHandler(sslContext));
200-
pipeline.addLast(new HttpsSocksProxyChannelHandler(proxyClientBootstrap));
212+
pipeline.addLast(new HttpsTunnelProxyChannelHandler(proxyClientBootstrap));
201213
}
202214
});
203215

@@ -213,12 +225,52 @@ public void initChannel(SocketChannel ch) {
213225
portMap.put(Integer.parseInt(itemArr[0]), itemArr[1]);
214226
try {
215227
serverBootstrap.bind("0.0.0.0", Integer.parseInt(itemArr[0])).get();
228+
LOG.info("HTTPS通道绑定 {}->{}", itemArr[0], itemArr[1]);
216229
} catch (Exception e) {
217230
throw new RuntimeException(e);
218231
}
219232
}
233+
}
220234

235+
/**
236+
* socks5协议
237+
*/
238+
private void initSocks5ProxyServer() {
239+
ServerBootstrap serverBootstrap = new ServerBootstrap();
240+
serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
221241

242+
@Override
243+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
244+
LOG.error("exceptionCaught", cause);
245+
super.exceptionCaught(ctx, cause);
246+
}
247+
248+
@Override
249+
public void initChannel(SocketChannel ch) {
250+
//Socks5MessagByteBuf
251+
ch.pipeline().addLast(Socks5ServerEncoder.DEFAULT);
252+
//sock5 init
253+
ch.pipeline().addLast(new Socks5InitialRequestDecoder());
254+
//sock5 init
255+
ch.pipeline().addLast(new Socks5InitialRequestHandler(configuration));
256+
if ("true".equals(configuration.getProperty("auth.socks5"))) {
257+
ch.pipeline().addLast(new Socks5PasswordAuthRequestDecoder());
258+
ch.pipeline().addLast(new Socks5PasswordAuthRequestHandler(configuration));
259+
}
260+
//socks connection
261+
ch.pipeline().addLast(new Socks5CommandRequestDecoder());
262+
//Socks connection
263+
ch.pipeline().addLast(new Socks5CommandRequestHandler(bossGroup));
264+
}
265+
});
266+
String bind = configuration.getProperty("server.socks5.bind");
267+
String port = configuration.getProperty("server.socks5.port");
268+
try {
269+
serverBootstrap.bind(bind, Integer.parseInt(port)).get();
270+
LOG.info("绑定socks5端口 {}:{}", bind, port);
271+
} catch (Exception e) {
272+
e.printStackTrace();
273+
}
222274
}
223275

224276
private ChannelHandler createSslHandler(SSLContext sslContext) {

src/main/java/com/io2c/httpproxyserver/handler/HttpProxyRequestHandler.java renamed to src/main/java/com/io2c/httpproxyserver/handler/https/HttpProxyRequestHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
package com.io2c.httpproxyserver.handler;
1+
package com.io2c.httpproxyserver.handler.https;
22

33
import com.io2c.httpproxyserver.HttpProxyServer;
44
import io.netty.bootstrap.Bootstrap;
55
import io.netty.channel.*;
66
import io.netty.handler.codec.http.*;
77

88
import java.net.URI;
9-
import java.net.URLEncoder;
109
import java.util.LinkedList;
1110
import java.util.Properties;
1211
import java.util.Queue;

src/main/java/com/io2c/httpproxyserver/handler/HttpsCommandHandler.java renamed to src/main/java/com/io2c/httpproxyserver/handler/https/HttpsCommandHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.io2c.httpproxyserver.handler;
1+
package com.io2c.httpproxyserver.handler.https;
22

33
import com.io2c.httpproxyserver.HttpProxyServer;
44
import io.netty.bootstrap.Bootstrap;

src/main/java/com/io2c/httpproxyserver/handler/socks/HttpsSocksProxyChannelHandler.java renamed to src/main/java/com/io2c/httpproxyserver/handler/https/HttpsTunnelProxyChannelHandler.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
1-
package com.io2c.httpproxyserver.handler.socks;
1+
package com.io2c.httpproxyserver.handler.https;
22

33
import com.io2c.httpproxyserver.HttpProxyServer;
44
import io.netty.bootstrap.Bootstrap;
55
import io.netty.buffer.ByteBuf;
66
import io.netty.channel.*;
7-
import io.netty.handler.codec.http.DefaultFullHttpResponse;
8-
import io.netty.handler.codec.http.FullHttpResponse;
9-
import io.netty.handler.codec.http.HttpResponseStatus;
10-
import io.netty.handler.codec.http.HttpVersion;
117
import org.slf4j.Logger;
128
import org.slf4j.LoggerFactory;
139

14-
import java.util.concurrent.atomic.AtomicLong;
15-
1610
/**
1711
* 处理服务端 channel.
1812
*/
19-
public class HttpsSocksProxyChannelHandler extends SimpleChannelInboundHandler<ByteBuf> {
13+
public class HttpsTunnelProxyChannelHandler extends SimpleChannelInboundHandler<ByteBuf> {
2014

21-
private static Logger logger = LoggerFactory.getLogger(HttpsSocksProxyChannelHandler.class);
15+
private static Logger logger = LoggerFactory.getLogger(HttpsTunnelProxyChannelHandler.class);
2216

2317
private Bootstrap proxyClientBootstrap;
2418

25-
public HttpsSocksProxyChannelHandler(Bootstrap proxyClientBootstrap) {
19+
public HttpsTunnelProxyChannelHandler(Bootstrap proxyClientBootstrap) {
2620
this.proxyClientBootstrap = proxyClientBootstrap;
2721
}
2822

src/main/java/com/io2c/httpproxyserver/handler/socks/RealServerChannelHandler.java renamed to src/main/java/com/io2c/httpproxyserver/handler/https/HttpsTunnelProxyRealServerChannelHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.io2c.httpproxyserver.handler.socks;
1+
package com.io2c.httpproxyserver.handler.https;
22

33
import com.io2c.httpproxyserver.HttpProxyServer;
44
import io.netty.buffer.ByteBuf;
@@ -12,9 +12,9 @@
1212
/**
1313
* 处理服务端 channel.
1414
*/
15-
public class RealServerChannelHandler extends SimpleChannelInboundHandler<ByteBuf> {
15+
public class HttpsTunnelProxyRealServerChannelHandler extends SimpleChannelInboundHandler<ByteBuf> {
1616

17-
private static Logger logger = LoggerFactory.getLogger(RealServerChannelHandler.class);
17+
private static Logger logger = LoggerFactory.getLogger(HttpsTunnelProxyRealServerChannelHandler.class);
1818

1919
@Override
2020
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf buf) throws Exception {

0 commit comments

Comments
 (0)