1
1
package top .meethigher .proxy .http ;
2
2
3
3
import io .vertx .core .AsyncResult ;
4
- import io .vertx .core .Future ;
5
4
import io .vertx .core .Handler ;
6
5
import io .vertx .core .Vertx ;
7
6
import io .vertx .core .http .*;
@@ -339,18 +338,11 @@ public ReverseHttpProxy host(String host) {
339
338
}
340
339
341
340
public void start () {
342
- httpServer .requestHandler (router ).exceptionHandler (e -> log .error ("request failed" , e ));
343
- Future <HttpServer > listenFuture = httpServer .listen (sourcePort , sourceHost );
344
-
345
- Handler <AsyncResult <HttpServer >> asyncResultHandler = ar -> {
346
- if (ar .succeeded ()) {
347
- log .info ("{} started on {}:{}" , name , sourceHost , sourcePort );
348
- } else {
349
- log .error ("{} start failed" , name , ar .cause ());
350
-
351
- }
352
- };
353
- listenFuture .onComplete (asyncResultHandler );
341
+ httpServer .requestHandler (router )
342
+ .exceptionHandler (e -> log .error ("{} socket errors happening before the HTTP connection" , name , e ))
343
+ .listen (sourcePort , sourceHost )
344
+ .onFailure (e -> log .error ("{} start failed" , name , e ))
345
+ .onSuccess (v -> log .info ("{} started on {}:{}" , name , sourceHost , sourcePort ));
354
346
}
355
347
356
348
public void stop () {
@@ -635,10 +627,14 @@ protected Handler<AsyncResult<HttpClientRequest>> connectHandler(RoutingContext
635
627
setContextData (ctx , INTERNAL_CLIENT_REMOTE_ADDR , connection .remoteAddress ().toString ());
636
628
log .debug ("target {} -- {} connected" , getContextData (ctx , INTERNAL_CLIENT_LOCAL_ADDR ), getContextData (ctx , INTERNAL_CLIENT_REMOTE_ADDR ));
637
629
638
- connection .closeHandler (v -> {
639
- setContextData (ctx , INTERNAL_CLIENT_CONNECTION_OPEN , false );
640
- log .debug ("target {} -- {} closed" , getContextData (ctx , INTERNAL_CLIENT_LOCAL_ADDR ), getContextData (ctx , INTERNAL_CLIENT_REMOTE_ADDR ));
641
- });
630
+ // 由于内部都是使用pipe来进行数据传输,所以exceptionHandler肯定是都重新注册过了,参考{@code io.vertx.core.streams.impl.PipeImpl.PipeImpl }
631
+ // 但如果还没进入pipe前,连接出现异常,那么就会触发此处的exceptionHandler。https://github.com/meethigher/tcp-reverse-proxy/issues/18
632
+ connection .exceptionHandler (e ->
633
+ log .error ("target {} -- {} exception occurred" , getContextData (ctx , INTERNAL_CLIENT_LOCAL_ADDR ), getContextData (ctx , INTERNAL_CLIENT_REMOTE_ADDR ), e ))
634
+ .closeHandler (v -> {
635
+ setContextData (ctx , INTERNAL_CLIENT_CONNECTION_OPEN , false );
636
+ log .debug ("target {} -- {} closed" , getContextData (ctx , INTERNAL_CLIENT_LOCAL_ADDR ), getContextData (ctx , INTERNAL_CLIENT_REMOTE_ADDR ));
637
+ });
642
638
643
639
644
640
// 复制请求头。复制的过程中忽略逐跳标头
@@ -675,15 +671,21 @@ protected Handler<RoutingContext> routingContextHandler(HttpClient httpClient) {
675
671
return ctx -> {
676
672
// 暂停流读取
677
673
ctx .request ().pause ();
678
-
679
674
HttpConnection connection = ctx .request ().connection ();
680
675
setContextData (ctx , INTERNAL_SERVER_REMOTE_ADDR , connection .remoteAddress ().toString ());
681
676
setContextData (ctx , INTERNAL_SERVER_LOCAL_ADDR , connection .localAddress ().toString ());
677
+ log .debug ("source {} -- {} connected" , getContextData (ctx , INTERNAL_SERVER_LOCAL_ADDR ), getContextData (ctx , INTERNAL_SERVER_REMOTE_ADDR ));
678
+ // 由于内部都是使用pipe来进行数据传输,所以exceptionHandler肯定是都重新注册过了,参考{@code io.vertx.core.streams.impl.PipeImpl.PipeImpl }
679
+ // 但如果还没进入pipe前,连接出现异常,那么就会触发此处的exceptionHandler。https://github.com/meethigher/tcp-reverse-proxy/issues/18
680
+ connection .exceptionHandler (e -> log .error ("source {} -- {} exception occurred" , getContextData (ctx , INTERNAL_SERVER_LOCAL_ADDR ), getContextData (ctx , INTERNAL_SERVER_REMOTE_ADDR ), e ))
681
+ .closeHandler (v -> {
682
+ setContextData (ctx , INTERNAL_SERVER_CONNECTION_OPEN , false );
683
+ log .debug ("source {} -- {} closed" , getContextData (ctx , INTERNAL_SERVER_LOCAL_ADDR ), getContextData (ctx , INTERNAL_SERVER_REMOTE_ADDR ));
684
+ });
682
685
// 记录请求开始时间
683
686
setContextData (ctx , INTERNAL_SEND_TIMESTAMP , System .currentTimeMillis ());
684
687
// 记录连接状态
685
688
setContextData (ctx , INTERNAL_SERVER_CONNECTION_OPEN , true );
686
- log .debug ("source {} -- {} connected" , getContextData (ctx , INTERNAL_SERVER_LOCAL_ADDR ), getContextData (ctx , INTERNAL_SERVER_REMOTE_ADDR ));
687
689
688
690
// vertx的uri()是包含query参数的。而path()才是我们常说的不带有query的uri
689
691
// route不是线程安全的。route里的metadata应以路由为单元存储,而不是以请求为单元存储。一个路由会有很多请求。
@@ -708,10 +710,6 @@ protected Handler<RoutingContext> routingContextHandler(HttpClient httpClient) {
708
710
requestOptions .setMethod (ctx .request ().method ());
709
711
requestOptions .setFollowRedirects (getContextData (ctx , P_FOLLOW_REDIRECTS ) != null && Boolean .parseBoolean (getContextData (ctx , P_FOLLOW_REDIRECTS ).toString ()));
710
712
711
- connection .closeHandler (v -> {
712
- setContextData (ctx , INTERNAL_SERVER_CONNECTION_OPEN , false );
713
- log .debug ("source {} -- {} closed" , getContextData (ctx , INTERNAL_SERVER_LOCAL_ADDR ), getContextData (ctx , INTERNAL_SERVER_REMOTE_ADDR ));
714
- });
715
713
716
714
// 如果跨域由代理服务接管,那么针对跨域使用的OPTIONS预检请求,就由代理服务接管,而不经过实际的后端服务
717
715
if (HttpMethod .OPTIONS .name ().equalsIgnoreCase (ctx .request ().method ().name ()) &&
0 commit comments