Code cleanup.
Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
parent
04060ce567
commit
d25fa7d20c
|
@ -39,10 +39,8 @@ import org.eclipse.jetty.util.TypeUtil;
|
||||||
import org.eclipse.jetty.util.log.Log;
|
import org.eclipse.jetty.util.log.Log;
|
||||||
import org.eclipse.jetty.util.log.Logger;
|
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
|
* <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>
|
* 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
|
public class ProxyConnectionFactory extends AbstractConnectionFactory
|
||||||
{
|
{
|
||||||
|
private static final Logger LOG = Log.getLogger(ProxyConnectionFactory.class);
|
||||||
public static final String TLS_VERSION = "TLS_VERSION";
|
public static final String TLS_VERSION = "TLS_VERSION";
|
||||||
|
|
||||||
private static final Logger LOG = Log.getLogger(ProxyConnectionFactory.class);
|
|
||||||
private final String _next;
|
private final String _next;
|
||||||
private int _maxProxyHeader = 1024;
|
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
|
* on the connector as the next protocol
|
||||||
*/
|
*/
|
||||||
public ProxyConnectionFactory()
|
public ProxyConnectionFactory()
|
||||||
|
@ -201,7 +199,6 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
|
||||||
fillInterested();
|
fillInterested();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private boolean parse(ByteBuffer buffer)
|
private boolean parse(ByteBuffer buffer)
|
||||||
{
|
{
|
||||||
// parse fields
|
// parse fields
|
||||||
|
@ -239,7 +236,6 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
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};
|
private static final byte[] MAGIC = new byte[]{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A};
|
||||||
|
|
||||||
public class ProxyProtocolV2Connection extends AbstractConnection
|
public class ProxyProtocolV2Connection extends AbstractConnection
|
||||||
|
@ -336,8 +339,7 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
|
||||||
private final int _length;
|
private final int _length;
|
||||||
private final ByteBuffer _buffer;
|
private final ByteBuffer _buffer;
|
||||||
|
|
||||||
protected ProxyProtocolV2Connection(EndPoint endp, Connector connector, String next,ByteBuffer buffer)
|
protected ProxyProtocolV2Connection(EndPoint endp, Connector connector, String next, ByteBuffer buffer) throws IOException
|
||||||
throws IOException
|
|
||||||
{
|
{
|
||||||
super(endp, connector.getExecutor());
|
super(endp, connector.getExecutor());
|
||||||
_connector = connector;
|
_connector = connector;
|
||||||
|
@ -355,9 +357,11 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
|
||||||
// uint8_t fam; /* protocol family and address */
|
// uint8_t fam; /* protocol family and address */
|
||||||
// uint16_t len; /* number of following bytes part of the header */
|
// uint16_t len; /* number of following bytes part of the header */
|
||||||
// };
|
// };
|
||||||
for (int i=0;i<MAGIC.length;i++)
|
for (byte magic : MAGIC)
|
||||||
if (buffer.get()!=MAGIC[i])
|
{
|
||||||
|
if (buffer.get() != magic)
|
||||||
throw new IOException("Bad PROXY protocol v2 signature");
|
throw new IOException("Bad PROXY protocol v2 signature");
|
||||||
|
}
|
||||||
|
|
||||||
int versionAndCommand = 0xff & buffer.get();
|
int versionAndCommand = 0xff & buffer.get();
|
||||||
if ((versionAndCommand & 0xf0) != 0x20)
|
if ((versionAndCommand & 0xf0) != 0x20)
|
||||||
|
@ -367,19 +371,33 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
|
||||||
int transportAndFamily = 0xff & buffer.get();
|
int transportAndFamily = 0xff & buffer.get();
|
||||||
switch (transportAndFamily >> 4)
|
switch (transportAndFamily >> 4)
|
||||||
{
|
{
|
||||||
case 0: _family=Family.UNSPEC; break;
|
case 0:
|
||||||
case 1: _family=Family.INET; break;
|
_family = Family.UNSPEC;
|
||||||
case 2: _family=Family.INET6; break;
|
break;
|
||||||
case 3: _family=Family.UNIX; break;
|
case 1:
|
||||||
|
_family = Family.INET;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
_family = Family.INET6;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
_family = Family.UNIX;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new IOException("Bad PROXY protocol v2 family");
|
throw new IOException("Bad PROXY protocol v2 family");
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (0xf & transportAndFamily)
|
switch (0xf & transportAndFamily)
|
||||||
{
|
{
|
||||||
case 0: _transport=Transport.UNSPEC; break;
|
case 0:
|
||||||
case 1: _transport=Transport.STREAM; break;
|
_transport = Transport.UNSPEC;
|
||||||
case 2: _transport=Transport.DGRAM; break;
|
break;
|
||||||
|
case 1:
|
||||||
|
_transport = Transport.STREAM;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
_transport = Transport.DGRAM;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new IOException("Bad PROXY protocol v2 family");
|
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))
|
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));
|
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));
|
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;
|
_buffer = _length > 0 ? BufferUtil.allocate(_length) : BufferUtil.EMPTY_BUFFER;
|
||||||
|
@ -425,15 +443,13 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
next();
|
||||||
}
|
}
|
||||||
catch (Throwable x)
|
catch (Throwable x)
|
||||||
{
|
{
|
||||||
LOG.warn("PROXY error for " + getEndPoint(), x);
|
LOG.warn("PROXY error for " + getEndPoint(), x);
|
||||||
close();
|
close();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void next()
|
private void next()
|
||||||
|
@ -472,7 +488,6 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
|
||||||
dst = Inet4Address.getByAddress(addr);
|
dst = Inet4Address.getByAddress(addr);
|
||||||
sp = _buffer.getChar();
|
sp = _buffer.getChar();
|
||||||
dp = _buffer.getChar();
|
dp = _buffer.getChar();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -492,14 +507,12 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Extract Addresses
|
// Extract Addresses
|
||||||
InetSocketAddress remote = new InetSocketAddress(src, sp);
|
InetSocketAddress remote = new InetSocketAddress(src, sp);
|
||||||
InetSocketAddress local = new InetSocketAddress(dst, dp);
|
InetSocketAddress local = new InetSocketAddress(dst, dp);
|
||||||
ProxyEndPoint proxyEndPoint = new ProxyEndPoint(endPoint, remote, local);
|
ProxyEndPoint proxyEndPoint = new ProxyEndPoint(endPoint, remote, local);
|
||||||
endPoint = proxyEndPoint;
|
endPoint = proxyEndPoint;
|
||||||
|
|
||||||
|
|
||||||
// Any additional info?
|
// Any additional info?
|
||||||
while (_buffer.hasRemaining())
|
while (_buffer.hasRemaining())
|
||||||
{
|
{
|
||||||
|
@ -522,7 +535,6 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int client = 0xff & value[i++];
|
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)
|
while (i < value.length)
|
||||||
{
|
{
|
||||||
int ssl_type = 0xff & value[i++];
|
int ssl_type = 0xff & value[i++];
|
||||||
|
@ -559,7 +571,6 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
|
||||||
if (LOG.isDebugEnabled())
|
if (LOG.isDebugEnabled())
|
||||||
LOG.debug("{} {}", getEndPoint(), proxyEndPoint.toString());
|
LOG.debug("{} {}", getEndPoint(), proxyEndPoint.toString());
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
@ -572,7 +583,6 @@ public class ProxyConnectionFactory extends AbstractConnectionFactory
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class ProxyEndPoint extends AttributesMap implements EndPoint
|
public static class ProxyEndPoint extends AttributesMap implements EndPoint
|
||||||
{
|
{
|
||||||
private final EndPoint _endp;
|
private final EndPoint _endp;
|
||||||
|
|
Loading…
Reference in New Issue