Code cleanup.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2018-04-27 10:12:09 +02:00
parent 04060ce567
commit d25fa7d20c
1 changed files with 185 additions and 175 deletions

View File

@ -39,10 +39,8 @@ import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/* ------------------------------------------------------------ */
/**
* ConnectionFactory for the PROXY Protocol.
* <p>ConnectionFactory for the PROXY Protocol.</p>
* <p>This factory can be placed in front of any other connection factory
* to process the proxy v1 or v2 line before the normal protocol handling</p>
*
@ -50,14 +48,14 @@ import org.eclipse.jetty.util.log.Logger;
*/
public class ProxyConnectionFactory extends AbstractConnectionFactory
{
private static final Logger LOG = Log.getLogger(ProxyConnectionFactory.class);
public static final String TLS_VERSION = "TLS_VERSION";
private static final Logger LOG = Log.getLogger(ProxyConnectionFactory.class);
private final String _next;
private int _maxProxyHeader = 1024;
/* ------------------------------------------------------------ */
/** Proxy Connection Factory that uses the next ConnectionFactory
/**
* Proxy Connection Factory that uses the next ConnectionFactory
* on the connector as the next protocol
*/
public ProxyConnectionFactory()
@ -201,7 +199,6 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
fillInterested();
}
private boolean parse(ByteBuffer buffer)
{
// parse fields
@ -239,7 +236,6 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
return false;
}
}
return true;
}
@ -321,9 +317,16 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
}
}
private enum Family
{
UNSPEC, INET, INET6, UNIX
}
private enum Transport
{
UNSPEC, STREAM, DGRAM
}
enum Family { UNSPEC, INET, INET6, UNIX };
enum Transport { UNSPEC, STREAM, DGRAM };
private static final byte[] MAGIC = new byte[]{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A};
public class ProxyProtocolV2Connection extends AbstractConnection
@ -336,8 +339,7 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
private final int _length;
private final ByteBuffer _buffer;
protected ProxyProtocolV2Connection(EndPoint endp, Connector connector, String next,ByteBuffer buffer)
throws IOException
protected ProxyProtocolV2Connection(EndPoint endp, Connector connector, String next, ByteBuffer buffer) throws IOException
{
super(endp, connector.getExecutor());
_connector = connector;
@ -355,9 +357,11 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
// uint8_t fam; /* protocol family and address */
// uint16_t len; /* number of following bytes part of the header */
// };
for (int i=0;i<MAGIC.length;i++)
if (buffer.get()!=MAGIC[i])
for (byte magic : MAGIC)
{
if (buffer.get() != magic)
throw new IOException("Bad PROXY protocol v2 signature");
}
int versionAndCommand = 0xff & buffer.get();
if ((versionAndCommand & 0xf0) != 0x20)
@ -367,19 +371,33 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
int transportAndFamily = 0xff & buffer.get();
switch (transportAndFamily >> 4)
{
case 0: _family=Family.UNSPEC; break;
case 1: _family=Family.INET; break;
case 2: _family=Family.INET6; break;
case 3: _family=Family.UNIX; break;
case 0:
_family = Family.UNSPEC;
break;
case 1:
_family = Family.INET;
break;
case 2:
_family = Family.INET6;
break;
case 3:
_family = Family.UNIX;
break;
default:
throw new IOException("Bad PROXY protocol v2 family");
}
switch (0xf & transportAndFamily)
{
case 0: _transport=Transport.UNSPEC; break;
case 1: _transport=Transport.STREAM; break;
case 2: _transport=Transport.DGRAM; break;
case 0:
_transport = Transport.UNSPEC;
break;
case 1:
_transport = Transport.STREAM;
break;
case 2:
_transport = Transport.DGRAM;
break;
default:
throw new IOException("Bad PROXY protocol v2 family");
}
@ -389,7 +407,7 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
if (!_local && (_family == Family.UNSPEC || _family == Family.UNIX || _transport != Transport.STREAM))
throw new IOException(String.format("Unsupported PROXY protocol v2 mode 0x%x,0x%x", versionAndCommand, transportAndFamily));
if (_length>_maxProxyHeader)
if (_length > getMaxProxyHeader())
throw new IOException(String.format("Unsupported PROXY protocol v2 mode 0x%x,0x%x,0x%x", versionAndCommand, transportAndFamily, _length));
_buffer = _length > 0 ? BufferUtil.allocate(_length) : BufferUtil.EMPTY_BUFFER;
@ -425,15 +443,13 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
return;
}
}
next();
}
catch (Throwable x)
{
LOG.warn("PROXY error for " + getEndPoint(), x);
close();
return;
}
next();
}
private void next()
@ -472,7 +488,6 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
dst = Inet4Address.getByAddress(addr);
sp = _buffer.getChar();
dp = _buffer.getChar();
break;
}
@ -492,14 +507,12 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
throw new IllegalStateException();
}
// Extract Addresses
InetSocketAddress remote = new InetSocketAddress(src, sp);
InetSocketAddress local = new InetSocketAddress(dst, dp);
ProxyEndPoint proxyEndPoint = new ProxyEndPoint(endPoint, remote, local);
endPoint = proxyEndPoint;
// Any additional info?
while (_buffer.hasRemaining())
{
@ -522,7 +535,6 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
{
int i = 0;
int client = 0xff & value[i++];
int verify = ((0xff & value[i++])<<24) + ((0xff & value[i++])<<16) + ((0xff & value[i++])<<8) + (0xff&value[i++]);
while (i < value.length)
{
int ssl_type = 0xff & value[i++];
@ -559,7 +571,6 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
if (LOG.isDebugEnabled())
LOG.debug("{} {}", getEndPoint(), proxyEndPoint.toString());
}
catch (Exception e)
{
@ -572,7 +583,6 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
}
}
public static class ProxyEndPoint extends AttributesMap implements EndPoint
{
private final EndPoint _endp;