2014-04-23 04:25:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2014 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.handler.codec.haproxy;
|
|
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
2014-06-21 06:54:18 +02:00
|
|
|
import io.netty.handler.codec.haproxy.HAProxyProxiedProtocol.AddressFamily;
|
2015-04-01 01:23:52 +02:00
|
|
|
import io.netty.util.ByteProcessor;
|
2014-06-21 06:54:18 +02:00
|
|
|
import io.netty.util.CharsetUtil;
|
2014-04-23 04:25:10 +02:00
|
|
|
import io.netty.util.NetUtil;
|
|
|
|
|
2016-09-28 23:04:07 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-04-23 04:25:10 +02:00
|
|
|
/**
|
|
|
|
* Message container for decoded HAProxy proxy protocol parameters
|
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
public final class HAProxyMessage {
|
2014-04-23 04:25:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Version 1 proxy protocol message for 'UNKNOWN' proxied protocols. Per spec, when the proxied protocol is
|
|
|
|
* 'UNKNOWN' we must discard all other header values.
|
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
private static final HAProxyMessage V1_UNKNOWN_MSG = new HAProxyMessage(
|
|
|
|
HAProxyProtocolVersion.V1, HAProxyCommand.PROXY, HAProxyProxiedProtocol.UNKNOWN, null, null, 0, 0);
|
2014-04-23 04:25:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Version 2 proxy protocol message for 'UNKNOWN' proxied protocols. Per spec, when the proxied protocol is
|
|
|
|
* 'UNKNOWN' we must discard all other header values.
|
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
private static final HAProxyMessage V2_UNKNOWN_MSG = new HAProxyMessage(
|
|
|
|
HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, HAProxyProxiedProtocol.UNKNOWN, null, null, 0, 0);
|
2014-04-23 04:25:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Version 2 proxy protocol message for local requests. Per spec, we should use an unspecified protocol and family
|
|
|
|
* for 'LOCAL' commands. Per spec, when the proxied protocol is 'UNKNOWN' we must discard all other header values.
|
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
private static final HAProxyMessage V2_LOCAL_MSG = new HAProxyMessage(
|
|
|
|
HAProxyProtocolVersion.V2, HAProxyCommand.LOCAL, HAProxyProxiedProtocol.UNKNOWN, null, null, 0, 0);
|
2014-04-23 04:25:10 +02:00
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
private final HAProxyProtocolVersion protocolVersion;
|
|
|
|
private final HAProxyCommand command;
|
|
|
|
private final HAProxyProxiedProtocol proxiedProtocol;
|
2014-04-23 04:25:10 +02:00
|
|
|
private final String sourceAddress;
|
|
|
|
private final String destinationAddress;
|
|
|
|
private final int sourcePort;
|
|
|
|
private final int destinationPort;
|
2016-09-28 23:04:07 +02:00
|
|
|
private final List<HAProxyTLV> tlvs;
|
2014-04-23 04:25:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new instance
|
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
private HAProxyMessage(
|
|
|
|
HAProxyProtocolVersion protocolVersion, HAProxyCommand command, HAProxyProxiedProtocol proxiedProtocol,
|
|
|
|
String sourceAddress, String destinationAddress, String sourcePort, String destinationPort) {
|
|
|
|
this(
|
|
|
|
protocolVersion, command, proxiedProtocol,
|
|
|
|
sourceAddress, destinationAddress, portStringToInt(sourcePort), portStringToInt(destinationPort));
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new instance
|
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
private HAProxyMessage(
|
|
|
|
HAProxyProtocolVersion protocolVersion, HAProxyCommand command, HAProxyProxiedProtocol proxiedProtocol,
|
|
|
|
String sourceAddress, String destinationAddress, int sourcePort, int destinationPort) {
|
2014-04-23 04:25:10 +02:00
|
|
|
|
2016-09-28 23:04:07 +02:00
|
|
|
this(protocolVersion, command, proxiedProtocol,
|
|
|
|
sourceAddress, destinationAddress, sourcePort, destinationPort, Collections.<HAProxyTLV>emptyList());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new instance
|
|
|
|
*/
|
|
|
|
private HAProxyMessage(
|
|
|
|
HAProxyProtocolVersion protocolVersion, HAProxyCommand command, HAProxyProxiedProtocol proxiedProtocol,
|
|
|
|
String sourceAddress, String destinationAddress, int sourcePort, int destinationPort,
|
|
|
|
List<HAProxyTLV> tlvs) {
|
|
|
|
|
2014-07-19 19:51:19 +02:00
|
|
|
if (proxiedProtocol == null) {
|
|
|
|
throw new NullPointerException("proxiedProtocol");
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
2014-07-19 19:51:19 +02:00
|
|
|
AddressFamily addrFamily = proxiedProtocol.addressFamily();
|
2014-04-23 04:25:10 +02:00
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
checkAddress(sourceAddress, addrFamily);
|
|
|
|
checkAddress(destinationAddress, addrFamily);
|
|
|
|
checkPort(sourcePort);
|
|
|
|
checkPort(destinationPort);
|
|
|
|
|
|
|
|
this.protocolVersion = protocolVersion;
|
|
|
|
this.command = command;
|
|
|
|
this.proxiedProtocol = proxiedProtocol;
|
|
|
|
this.sourceAddress = sourceAddress;
|
|
|
|
this.destinationAddress = destinationAddress;
|
|
|
|
this.sourcePort = sourcePort;
|
|
|
|
this.destinationPort = destinationPort;
|
2016-09-28 23:04:07 +02:00
|
|
|
this.tlvs = Collections.unmodifiableList(tlvs);
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* Decodes a version 2, binary proxy protocol header.
|
2014-04-23 04:25:10 +02:00
|
|
|
*
|
|
|
|
* @param header a version 2 proxy protocol header
|
2014-06-21 06:54:18 +02:00
|
|
|
* @return {@link HAProxyMessage} instance
|
2014-04-23 04:25:10 +02:00
|
|
|
* @throws HAProxyProtocolException if any portion of the header is invalid
|
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
static HAProxyMessage decodeHeader(ByteBuf header) {
|
2014-04-23 04:25:10 +02:00
|
|
|
if (header == null) {
|
2014-06-21 06:54:18 +02:00
|
|
|
throw new NullPointerException("header");
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (header.readableBytes() < 16) {
|
2014-06-21 06:54:18 +02:00
|
|
|
throw new HAProxyProtocolException(
|
|
|
|
"incomplete header: " + header.readableBytes() + " bytes (expected: 16+ bytes)");
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Per spec, the 13th byte is the protocol version and command byte
|
|
|
|
header.skipBytes(12);
|
|
|
|
final byte verCmdByte = header.readByte();
|
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
HAProxyProtocolVersion ver;
|
|
|
|
try {
|
|
|
|
ver = HAProxyProtocolVersion.valueOf(verCmdByte);
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
throw new HAProxyProtocolException(e);
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
if (ver != HAProxyProtocolVersion.V2) {
|
|
|
|
throw new HAProxyProtocolException("version 1 unsupported: 0x" + Integer.toHexString(verCmdByte));
|
|
|
|
}
|
2014-04-23 04:25:10 +02:00
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
HAProxyCommand cmd;
|
|
|
|
try {
|
|
|
|
cmd = HAProxyCommand.valueOf(verCmdByte);
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
throw new HAProxyProtocolException(e);
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
if (cmd == HAProxyCommand.LOCAL) {
|
2014-04-23 04:25:10 +02:00
|
|
|
return V2_LOCAL_MSG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Per spec, the 14th byte is the protocol and address family byte
|
2014-06-21 06:54:18 +02:00
|
|
|
HAProxyProxiedProtocol protAndFam;
|
|
|
|
try {
|
|
|
|
protAndFam = HAProxyProxiedProtocol.valueOf(header.readByte());
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
throw new HAProxyProtocolException(e);
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
if (protAndFam == HAProxyProxiedProtocol.UNKNOWN) {
|
2014-04-23 04:25:10 +02:00
|
|
|
return V2_UNKNOWN_MSG;
|
|
|
|
}
|
|
|
|
|
|
|
|
int addressInfoLen = header.readUnsignedShort();
|
|
|
|
|
|
|
|
String srcAddress;
|
|
|
|
String dstAddress;
|
|
|
|
int addressLen;
|
|
|
|
int srcPort = 0;
|
|
|
|
int dstPort = 0;
|
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
AddressFamily addressFamily = protAndFam.addressFamily();
|
2014-04-23 04:25:10 +02:00
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
if (addressFamily == AddressFamily.AF_UNIX) {
|
2014-04-23 04:25:10 +02:00
|
|
|
// unix sockets require 216 bytes for address information
|
|
|
|
if (addressInfoLen < 216 || header.readableBytes() < 216) {
|
|
|
|
throw new HAProxyProtocolException(
|
2014-06-21 06:54:18 +02:00
|
|
|
"incomplete UNIX socket address information: " +
|
|
|
|
Math.min(addressInfoLen, header.readableBytes()) + " bytes (expected: 216+ bytes)");
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
int startIdx = header.readerIndex();
|
2015-04-01 01:23:52 +02:00
|
|
|
int addressEnd = header.forEachByte(startIdx, 108, ByteProcessor.FIND_NUL);
|
2014-04-23 04:25:10 +02:00
|
|
|
if (addressEnd == -1) {
|
|
|
|
addressLen = 108;
|
|
|
|
} else {
|
|
|
|
addressLen = addressEnd - startIdx;
|
|
|
|
}
|
2014-06-21 06:54:18 +02:00
|
|
|
srcAddress = header.toString(startIdx, addressLen, CharsetUtil.US_ASCII);
|
2014-04-23 04:25:10 +02:00
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
startIdx += 108;
|
2014-04-23 04:25:10 +02:00
|
|
|
|
2015-04-01 01:23:52 +02:00
|
|
|
addressEnd = header.forEachByte(startIdx, 108, ByteProcessor.FIND_NUL);
|
2014-04-23 04:25:10 +02:00
|
|
|
if (addressEnd == -1) {
|
|
|
|
addressLen = 108;
|
|
|
|
} else {
|
|
|
|
addressLen = addressEnd - startIdx;
|
|
|
|
}
|
2014-06-21 06:54:18 +02:00
|
|
|
dstAddress = header.toString(startIdx, addressLen, CharsetUtil.US_ASCII);
|
2016-09-29 15:30:18 +02:00
|
|
|
// AF_UNIX defines that exactly 108 bytes are reserved for the address. The previous methods
|
|
|
|
// did not increase the reader index although we already consumed the information.
|
|
|
|
header.readerIndex(startIdx + 108);
|
2014-04-23 04:25:10 +02:00
|
|
|
} else {
|
2014-06-21 06:54:18 +02:00
|
|
|
if (addressFamily == AddressFamily.AF_IPv4) {
|
2014-04-23 04:25:10 +02:00
|
|
|
// IPv4 requires 12 bytes for address information
|
|
|
|
if (addressInfoLen < 12 || header.readableBytes() < 12) {
|
|
|
|
throw new HAProxyProtocolException(
|
2014-06-21 06:54:18 +02:00
|
|
|
"incomplete IPv4 address information: " +
|
|
|
|
Math.min(addressInfoLen, header.readableBytes()) + " bytes (expected: 12+ bytes)");
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
addressLen = 4;
|
2014-06-21 06:54:18 +02:00
|
|
|
} else if (addressFamily == AddressFamily.AF_IPv6) {
|
2014-04-23 04:25:10 +02:00
|
|
|
// IPv6 requires 36 bytes for address information
|
|
|
|
if (addressInfoLen < 36 || header.readableBytes() < 36) {
|
|
|
|
throw new HAProxyProtocolException(
|
2014-06-21 06:54:18 +02:00
|
|
|
"incomplete IPv6 address information: " +
|
|
|
|
Math.min(addressInfoLen, header.readableBytes()) + " bytes (expected: 36+ bytes)");
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
addressLen = 16;
|
|
|
|
} else {
|
|
|
|
throw new HAProxyProtocolException(
|
2017-04-19 22:37:03 +02:00
|
|
|
"unable to parse address information (unknown address family: " + addressFamily + ')');
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Per spec, the src address begins at the 17th byte
|
2017-04-19 22:37:03 +02:00
|
|
|
srcAddress = ipBytesToString(header, addressLen);
|
|
|
|
dstAddress = ipBytesToString(header, addressLen);
|
2014-04-23 04:25:10 +02:00
|
|
|
srcPort = header.readUnsignedShort();
|
|
|
|
dstPort = header.readUnsignedShort();
|
|
|
|
}
|
|
|
|
|
2016-09-28 23:04:07 +02:00
|
|
|
final List<HAProxyTLV> tlvs = readTlvs(header);
|
|
|
|
|
|
|
|
return new HAProxyMessage(ver, cmd, protAndFam, srcAddress, dstAddress, srcPort, dstPort, tlvs);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static List<HAProxyTLV> readTlvs(final ByteBuf header) {
|
|
|
|
HAProxyTLV haProxyTLV = readNextTLV(header);
|
|
|
|
if (haProxyTLV == null) {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
// In most cases there are less than 4 TLVs available
|
2019-01-22 16:07:26 +01:00
|
|
|
List<HAProxyTLV> haProxyTLVs = new ArrayList<>(4);
|
2016-09-28 23:04:07 +02:00
|
|
|
|
|
|
|
do {
|
|
|
|
haProxyTLVs.add(haProxyTLV);
|
|
|
|
if (haProxyTLV instanceof HAProxySSLTLV) {
|
|
|
|
haProxyTLVs.addAll(((HAProxySSLTLV) haProxyTLV).encapsulatedTLVs());
|
|
|
|
}
|
|
|
|
} while ((haProxyTLV = readNextTLV(header)) != null);
|
|
|
|
return haProxyTLVs;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static HAProxyTLV readNextTLV(final ByteBuf header) {
|
|
|
|
|
|
|
|
// We need at least 4 bytes for a TLV
|
|
|
|
if (header.readableBytes() < 4) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final byte typeAsByte = header.readByte();
|
|
|
|
final HAProxyTLV.Type type = HAProxyTLV.Type.typeForByteValue(typeAsByte);
|
|
|
|
|
|
|
|
final int length = header.readUnsignedShort();
|
|
|
|
switch (type) {
|
|
|
|
case PP2_TYPE_SSL:
|
|
|
|
final ByteBuf rawContent = header.retainedSlice(header.readerIndex(), length);
|
|
|
|
final ByteBuf byteBuf = header.readSlice(length);
|
|
|
|
final byte client = byteBuf.readByte();
|
|
|
|
final int verify = byteBuf.readInt();
|
|
|
|
|
|
|
|
if (byteBuf.readableBytes() >= 4) {
|
|
|
|
|
2019-01-22 16:07:26 +01:00
|
|
|
final List<HAProxyTLV> encapsulatedTlvs = new ArrayList<>(4);
|
2016-09-28 23:04:07 +02:00
|
|
|
do {
|
|
|
|
final HAProxyTLV haProxyTLV = readNextTLV(byteBuf);
|
|
|
|
if (haProxyTLV == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
encapsulatedTlvs.add(haProxyTLV);
|
|
|
|
} while (byteBuf.readableBytes() >= 4);
|
|
|
|
|
|
|
|
return new HAProxySSLTLV(verify, client, encapsulatedTlvs, rawContent);
|
|
|
|
}
|
|
|
|
return new HAProxySSLTLV(verify, client, Collections.<HAProxyTLV>emptyList(), rawContent);
|
|
|
|
// If we're not dealing with a SSL Type, we can use the same mechanism
|
|
|
|
case PP2_TYPE_ALPN:
|
|
|
|
case PP2_TYPE_AUTHORITY:
|
|
|
|
case PP2_TYPE_SSL_VERSION:
|
|
|
|
case PP2_TYPE_SSL_CN:
|
|
|
|
case PP2_TYPE_NETNS:
|
|
|
|
case OTHER:
|
|
|
|
return new HAProxyTLV(type, typeAsByte, header.readRetainedSlice(length));
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* Decodes a version 1, human-readable proxy protocol header.
|
2014-04-23 04:25:10 +02:00
|
|
|
*
|
|
|
|
* @param header a version 1 proxy protocol header
|
2014-06-21 06:54:18 +02:00
|
|
|
* @return {@link HAProxyMessage} instance
|
2014-04-23 04:25:10 +02:00
|
|
|
* @throws HAProxyProtocolException if any portion of the header is invalid
|
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
static HAProxyMessage decodeHeader(String header) {
|
2014-04-23 04:25:10 +02:00
|
|
|
if (header == null) {
|
2014-06-21 06:54:18 +02:00
|
|
|
throw new HAProxyProtocolException("header");
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 11:17:05 +02:00
|
|
|
String[] parts = header.split(" ");
|
2014-04-23 04:25:10 +02:00
|
|
|
int numParts = parts.length;
|
|
|
|
|
|
|
|
if (numParts < 2) {
|
|
|
|
throw new HAProxyProtocolException(
|
2014-06-21 06:54:18 +02:00
|
|
|
"invalid header: " + header + " (expected: 'PROXY' and proxied protocol values)");
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!"PROXY".equals(parts[0])) {
|
2014-06-21 06:54:18 +02:00
|
|
|
throw new HAProxyProtocolException("unknown identifier: " + parts[0]);
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
HAProxyProxiedProtocol protAndFam;
|
|
|
|
try {
|
|
|
|
protAndFam = HAProxyProxiedProtocol.valueOf(parts[1]);
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
throw new HAProxyProtocolException(e);
|
|
|
|
}
|
2014-04-23 04:25:10 +02:00
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
if (protAndFam != HAProxyProxiedProtocol.TCP4 &&
|
|
|
|
protAndFam != HAProxyProxiedProtocol.TCP6 &&
|
|
|
|
protAndFam != HAProxyProxiedProtocol.UNKNOWN) {
|
|
|
|
throw new HAProxyProtocolException("unsupported v1 proxied protocol: " + parts[1]);
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
if (protAndFam == HAProxyProxiedProtocol.UNKNOWN) {
|
2014-04-23 04:25:10 +02:00
|
|
|
return V1_UNKNOWN_MSG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numParts != 6) {
|
2014-06-21 06:54:18 +02:00
|
|
|
throw new HAProxyProtocolException("invalid TCP4/6 header: " + header + " (expected: 6 parts)");
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
return new HAProxyMessage(
|
|
|
|
HAProxyProtocolVersion.V1, HAProxyCommand.PROXY,
|
2014-04-23 04:25:10 +02:00
|
|
|
protAndFam, parts[2], parts[3], parts[4], parts[5]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert ip address bytes to string representation
|
|
|
|
*
|
|
|
|
* @param header buffer containing ip address bytes
|
|
|
|
* @param addressLen number of bytes to read (4 bytes for IPv4, 16 bytes for IPv6)
|
|
|
|
* @return string representation of the ip address
|
|
|
|
*/
|
2017-04-19 22:37:03 +02:00
|
|
|
private static String ipBytesToString(ByteBuf header, int addressLen) {
|
2014-04-23 04:25:10 +02:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
if (addressLen == 4) {
|
|
|
|
sb.append(header.readByte() & 0xff);
|
2014-06-21 06:54:18 +02:00
|
|
|
sb.append('.');
|
2014-04-23 04:25:10 +02:00
|
|
|
sb.append(header.readByte() & 0xff);
|
2014-06-21 06:54:18 +02:00
|
|
|
sb.append('.');
|
2014-04-23 04:25:10 +02:00
|
|
|
sb.append(header.readByte() & 0xff);
|
2014-06-21 06:54:18 +02:00
|
|
|
sb.append('.');
|
2014-04-23 04:25:10 +02:00
|
|
|
sb.append(header.readByte() & 0xff);
|
|
|
|
} else {
|
|
|
|
sb.append(Integer.toHexString(header.readUnsignedShort()));
|
2014-06-21 06:54:18 +02:00
|
|
|
sb.append(':');
|
2014-04-23 04:25:10 +02:00
|
|
|
sb.append(Integer.toHexString(header.readUnsignedShort()));
|
2014-06-21 06:54:18 +02:00
|
|
|
sb.append(':');
|
2014-04-23 04:25:10 +02:00
|
|
|
sb.append(Integer.toHexString(header.readUnsignedShort()));
|
2014-06-21 06:54:18 +02:00
|
|
|
sb.append(':');
|
2014-04-23 04:25:10 +02:00
|
|
|
sb.append(Integer.toHexString(header.readUnsignedShort()));
|
2014-06-21 06:54:18 +02:00
|
|
|
sb.append(':');
|
2014-04-23 04:25:10 +02:00
|
|
|
sb.append(Integer.toHexString(header.readUnsignedShort()));
|
2014-06-21 06:54:18 +02:00
|
|
|
sb.append(':');
|
2014-04-23 04:25:10 +02:00
|
|
|
sb.append(Integer.toHexString(header.readUnsignedShort()));
|
2014-06-21 06:54:18 +02:00
|
|
|
sb.append(':');
|
2014-04-23 04:25:10 +02:00
|
|
|
sb.append(Integer.toHexString(header.readUnsignedShort()));
|
2014-06-21 06:54:18 +02:00
|
|
|
sb.append(':');
|
2014-04-23 04:25:10 +02:00
|
|
|
sb.append(Integer.toHexString(header.readUnsignedShort()));
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert port to integer
|
|
|
|
*
|
2014-06-21 06:54:18 +02:00
|
|
|
* @param value the port
|
|
|
|
* @return port as an integer
|
2014-04-23 04:25:10 +02:00
|
|
|
* @throws HAProxyProtocolException if port is not a valid integer
|
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
private static int portStringToInt(String value) {
|
|
|
|
int port;
|
2014-04-23 04:25:10 +02:00
|
|
|
try {
|
2014-06-21 06:54:18 +02:00
|
|
|
port = Integer.parseInt(value);
|
2014-04-23 04:25:10 +02:00
|
|
|
} catch (NumberFormatException e) {
|
2014-06-21 06:54:18 +02:00
|
|
|
throw new HAProxyProtocolException("invalid port: " + value, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port <= 0 || port > 65535) {
|
|
|
|
throw new HAProxyProtocolException("invalid port: " + value + " (expected: 1 ~ 65535)");
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
2014-06-21 06:54:18 +02:00
|
|
|
|
|
|
|
return port;
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate an address (IPv4, IPv6, Unix Socket)
|
|
|
|
*
|
|
|
|
* @param address human-readable address
|
2014-06-21 06:54:18 +02:00
|
|
|
* @param addrFamily the {@link AddressFamily} to check the address against
|
2014-04-23 04:25:10 +02:00
|
|
|
* @throws HAProxyProtocolException if the address is invalid
|
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
private static void checkAddress(String address, AddressFamily addrFamily) {
|
2014-04-23 04:25:10 +02:00
|
|
|
if (addrFamily == null) {
|
2014-06-21 06:54:18 +02:00
|
|
|
throw new NullPointerException("addrFamily");
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-21 10:11:08 +02:00
|
|
|
switch (addrFamily) {
|
|
|
|
case AF_UNSPEC:
|
|
|
|
if (address != null) {
|
|
|
|
throw new HAProxyProtocolException("unable to validate an AF_UNSPEC address: " + address);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case AF_UNIX:
|
|
|
|
return;
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-21 10:11:08 +02:00
|
|
|
if (address == null) {
|
|
|
|
throw new NullPointerException("address");
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-21 10:11:08 +02:00
|
|
|
switch (addrFamily) {
|
|
|
|
case AF_IPv4:
|
|
|
|
if (!NetUtil.isValidIpV4Address(address)) {
|
|
|
|
throw new HAProxyProtocolException("invalid IPv4 address: " + address);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AF_IPv6:
|
|
|
|
if (!NetUtil.isValidIpV6Address(address)) {
|
|
|
|
throw new HAProxyProtocolException("invalid IPv6 address: " + address);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error();
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate a UDP/TCP port
|
|
|
|
*
|
|
|
|
* @param port the UDP/TCP port
|
|
|
|
* @throws HAProxyProtocolException if the port is out of range (0-65535 inclusive)
|
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
private static void checkPort(int port) {
|
2014-04-23 04:25:10 +02:00
|
|
|
if (port < 0 || port > 65535) {
|
2014-06-21 06:54:18 +02:00
|
|
|
throw new HAProxyProtocolException("invalid port: " + port + " (expected: 1 ~ 65535)");
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* Returns the {@link HAProxyProtocolVersion} of this {@link HAProxyMessage}.
|
2014-04-23 04:25:10 +02:00
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
public HAProxyProtocolVersion protocolVersion() {
|
|
|
|
return protocolVersion;
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* Returns the {@link HAProxyCommand} of this {@link HAProxyMessage}.
|
2014-04-23 04:25:10 +02:00
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
public HAProxyCommand command() {
|
2014-04-23 04:25:10 +02:00
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* Returns the {@link HAProxyProxiedProtocol} of this {@link HAProxyMessage}.
|
2014-04-23 04:25:10 +02:00
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
public HAProxyProxiedProtocol proxiedProtocol() {
|
|
|
|
return proxiedProtocol;
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* Returns the human-readable source address of this {@link HAProxyMessage}.
|
2014-04-23 04:25:10 +02:00
|
|
|
*/
|
|
|
|
public String sourceAddress() {
|
|
|
|
return sourceAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* Returns the human-readable destination address of this {@link HAProxyMessage}.
|
2014-04-23 04:25:10 +02:00
|
|
|
*/
|
|
|
|
public String destinationAddress() {
|
|
|
|
return destinationAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* Returns the UDP/TCP source port of this {@link HAProxyMessage}.
|
2014-04-23 04:25:10 +02:00
|
|
|
*/
|
|
|
|
public int sourcePort() {
|
|
|
|
return sourcePort;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* Returns the UDP/TCP destination port of this {@link HAProxyMessage}.
|
2014-04-23 04:25:10 +02:00
|
|
|
*/
|
|
|
|
public int destinationPort() {
|
|
|
|
return destinationPort;
|
|
|
|
}
|
2016-09-28 23:04:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of {@link HAProxyTLV} or an empty list if no TLVs are present.
|
|
|
|
* <p>
|
|
|
|
* TLVs are only available for the Proxy Protocol V2
|
|
|
|
*/
|
|
|
|
public List<HAProxyTLV> tlvs() {
|
|
|
|
return tlvs;
|
|
|
|
}
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|