22
33import com .io2c .httpproxyserver .container .Container ;
44import 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 ;
912import io .netty .bootstrap .Bootstrap ;
1013import io .netty .bootstrap .ServerBootstrap ;
1114import io .netty .buffer .ByteBuf ;
1922import io .netty .handler .codec .http .HttpMethod ;
2023import io .netty .handler .codec .http .HttpRequest ;
2124import 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 ;
2229import io .netty .handler .ssl .SslHandler ;
2330import io .netty .util .AttributeKey ;
2431import org .slf4j .Logger ;
3340import java .util .HashMap ;
3441import java .util .Map ;
3542import 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 ) {
0 commit comments