@@ -23,47 +23,51 @@ public class ProxyProtocolHandler extends ChannelInboundHandlerAdapter {
2323 @ Override
2424 public void channelRead (ChannelHandlerContext ctx , Object msg ) throws Exception {
2525 if (msg instanceof HAProxyMessage ) {
26- HAProxyMessage message = ((HAProxyMessage ) msg );
27- if (message .command () == HAProxyCommand .PROXY ) {
28- final String realAddress = message .sourceAddress ();
29- final int realPort = message .sourcePort ();
26+ try {
27+ HAProxyMessage message = ((HAProxyMessage ) msg );
28+ if (message .command () == HAProxyCommand .PROXY ) {
29+ final String realAddress = message .sourceAddress ();
30+ final int realPort = message .sourcePort ();
3031
31- final InetSocketAddress socketAddr = new InetSocketAddress (realAddress , realPort );
32+ final InetSocketAddress socketAddr = new InetSocketAddress (realAddress , realPort );
3233
33- Connection connection = ((Connection ) ctx .channel ().pipeline ().get ("packet_handler" ));
34- SocketAddress proxyAddress = connection .getRemoteAddress ();
34+ Connection connection = ((Connection ) ctx .channel ().pipeline ().get ("packet_handler" ));
35+ SocketAddress proxyAddress = connection .getRemoteAddress ();
3536
36- if (!ProxyProtocolSupport .whitelistedIPs .isEmpty ()) {
37- if (proxyAddress instanceof InetSocketAddress ) {
38- InetSocketAddress proxySocketAddress = ((InetSocketAddress ) proxyAddress );
39- boolean isWhitelistedIP = false ;
37+ if (!ProxyProtocolSupport .whitelistedIPs .isEmpty ()) {
38+ if (proxyAddress instanceof InetSocketAddress ) {
39+ InetSocketAddress proxySocketAddress = ((InetSocketAddress ) proxyAddress );
40+ boolean isWhitelistedIP = false ;
4041
41- for (CIDRMatcher matcher : ProxyProtocolSupport .whitelistedIPs ) {
42- if (matcher .matches (proxySocketAddress .getAddress ())) {
43- isWhitelistedIP = true ;
44- break ;
42+ for (CIDRMatcher matcher : ProxyProtocolSupport .whitelistedIPs ) {
43+ if (matcher .matches (proxySocketAddress .getAddress ())) {
44+ isWhitelistedIP = true ;
45+ break ;
46+ }
4547 }
46- }
4748
48- if (!isWhitelistedIP ) {
49- if (ctx .channel ().isOpen ()) {
50- ctx .disconnect ();
51- ProxyProtocolSupport .warnLogger .accept ("Blocked proxy IP: " + proxySocketAddress + " when tried to connect!" );
49+ if (!isWhitelistedIP ) {
50+ if (ctx .channel ().isOpen ()) {
51+ ctx .disconnect ();
52+ ProxyProtocolSupport .warnLogger .accept ("Blocked proxy IP: " + proxySocketAddress + " when tried to connect!" );
53+ }
54+ return ;
5255 }
53- return ;
56+ } else {
57+ ProxyProtocolSupport .warnLogger .accept ("**********************************************************************" );
58+ ProxyProtocolSupport .warnLogger .accept ("* Detected other SocketAddress than InetSocketAddress! *" );
59+ ProxyProtocolSupport .warnLogger .accept ("* Please report it with logs to mod author to provide compatibility! *" );
60+ ProxyProtocolSupport .warnLogger .accept ("* https://github.com/PanSzelescik/proxy-protocol-support/issues *" );
61+ ProxyProtocolSupport .warnLogger .accept ("**********************************************************************" );
62+ ProxyProtocolSupport .warnLogger .accept (proxyAddress .getClass ().toString ());
63+ ProxyProtocolSupport .warnLogger .accept (proxyAddress .toString ());
5464 }
55- } else {
56- ProxyProtocolSupport .warnLogger .accept ("**********************************************************************" );
57- ProxyProtocolSupport .warnLogger .accept ("* Detected other SocketAddress than InetSocketAddress! *" );
58- ProxyProtocolSupport .warnLogger .accept ("* Please report it with logs to mod author to provide compatibility! *" );
59- ProxyProtocolSupport .warnLogger .accept ("* https://github.com/PanSzelescik/proxy-protocol-support/issues *" );
60- ProxyProtocolSupport .warnLogger .accept ("**********************************************************************" );
61- ProxyProtocolSupport .warnLogger .accept (proxyAddress .getClass ().toString ());
62- ProxyProtocolSupport .warnLogger .accept (proxyAddress .toString ());
6365 }
64- }
6566
66- ((ProxyProtocolAddressSetter ) connection ).setAddress (socketAddr );
67+ ((ProxyProtocolAddressSetter ) connection ).setAddress (socketAddr );
68+ }
69+ } catch (Exception e ) {
70+ ProxyProtocolSupport .exceptionLogger .accept ("Error while handling HAProxyMessage!" , e );
6771 }
6872 } else {
6973 super .channelRead (ctx , msg );
0 commit comments