2
2
3
3
import com .io2c .httpproxyserver .container .Container ;
4
4
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 ;
9
12
import io .netty .bootstrap .Bootstrap ;
10
13
import io .netty .bootstrap .ServerBootstrap ;
11
14
import io .netty .buffer .ByteBuf ;
19
22
import io .netty .handler .codec .http .HttpMethod ;
20
23
import io .netty .handler .codec .http .HttpRequest ;
21
24
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 ;
22
29
import io .netty .handler .ssl .SslHandler ;
23
30
import io .netty .util .AttributeKey ;
24
31
import org .slf4j .Logger ;
33
40
import java .util .HashMap ;
34
41
import java .util .Map ;
35
42
import java .util .Properties ;
43
+ import java .util .concurrent .ExecutionException ;
36
44
37
45
/**
38
46
* @author fei.feng
@@ -81,7 +89,8 @@ public void start() {
81
89
initProxyClient (proxyClientBootstrap , workerGroup );
82
90
initHttpProxyServer (httpServerBootstrap , proxyClientBootstrap , bossGroup , workerGroup );
83
91
initHttpsProxyServer (httpsServerBootstrap , proxyClientBootstrap , bossGroup , workerGroup );
84
- initHttpsSocksProxyServer ();
92
+ initHttpsTunnelProxyServer ();
93
+ initSocks5ProxyServer ();
85
94
try {
86
95
httpServerBootstrap .bind (configuration .getProperty ("server.bind" ), Integer .parseInt (configuration .getProperty (CONFIG_SERVER_PORT_KEY ))).get ();
87
96
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 {
162
171
});
163
172
}
164
173
165
- private void initHttpsSocksProxyServer () {
174
+ /**
175
+ * https隧道代理其他协议端口
176
+ */
177
+ private void initHttpsTunnelProxyServer () {
166
178
ServerBootstrap serverBootstrap = new ServerBootstrap ();
167
179
final Bootstrap proxyClientBootstrap = new Bootstrap ();
168
180
proxyClientBootstrap .channel (NioSocketChannel .class );
169
181
proxyClientBootstrap .group (workerGroup ).handler (new ChannelInitializer <SocketChannel >() {
170
182
171
183
@ Override
172
184
public void initChannel (SocketChannel ch ) {
173
- ch .pipeline ().addLast (new RealServerChannelHandler ());
185
+ ch .pipeline ().addLast (new HttpsTunnelProxyRealServerChannelHandler ());
174
186
}
175
187
});
176
188
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
178
190
final Map <Integer , String > portMap = new HashMap <>();
179
191
final SSLContext sslContext = new SslContextCreator ().initSSLContext (configuration .getProperty ("server.https.jksPath" ),
180
192
configuration .getProperty ("server.https.keyStorePassword" ), configuration .getProperty ("server.https.keyManagerPassword" ));
@@ -197,7 +209,7 @@ public void initChannel(SocketChannel ch) {
197
209
}
198
210
ch .attr (connectInfoAttributeKey ).set (ipPort );
199
211
pipeline .addLast ("ssl" , createSslHandler (sslContext ));
200
- pipeline .addLast (new HttpsSocksProxyChannelHandler (proxyClientBootstrap ));
212
+ pipeline .addLast (new HttpsTunnelProxyChannelHandler (proxyClientBootstrap ));
201
213
}
202
214
});
203
215
@@ -213,12 +225,52 @@ public void initChannel(SocketChannel ch) {
213
225
portMap .put (Integer .parseInt (itemArr [0 ]), itemArr [1 ]);
214
226
try {
215
227
serverBootstrap .bind ("0.0.0.0" , Integer .parseInt (itemArr [0 ])).get ();
228
+ LOG .info ("HTTPS通道绑定 {}->{}" , itemArr [0 ], itemArr [1 ]);
216
229
} catch (Exception e ) {
217
230
throw new RuntimeException (e );
218
231
}
219
232
}
233
+ }
220
234
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 >() {
221
241
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
+ }
222
274
}
223
275
224
276
private ChannelHandler createSslHandler (SSLContext sslContext ) {
0 commit comments