private static boolean isValidIpV4Address(CharSequence ip, int from, int toExcluded) { return ip instanceof String ? isValidIpV4Address((String) ip, from, toExcluded) : ip instanceof AsciiString ? isValidIpV4Address((AsciiString) ip, from, toExcluded) : isValidIpV4Address0(ip, from, toExcluded); }
/** * Takes a {@link String} and parses it to see if it is a valid IPV4 address. * * @return true, if the string represents an IPV4 address in dotted * notation, false otherwise */ public static boolean isValidIpV4Address(String ip) { return isValidIpV4Address(ip, 0, ip.length()); }
/** * Takes a {@link CharSequence} and parses it to see if it is a valid IPV4 address. * * @return true, if the string represents an IPV4 address in dotted * notation, false otherwise */ public static boolean isValidIpV4Address(CharSequence ip) { return isValidIpV4Address(ip, 0, ip.length()); }
private static boolean isValidIpV4Address(CharSequence ip, int from, int toExcluded) { return ip instanceof String ? isValidIpV4Address((String) ip, from, toExcluded) : ip instanceof AsciiString ? isValidIpV4Address((AsciiString) ip, from, toExcluded) : isValidIpV4Address0(ip, from, toExcluded); }
/** * Takes a {@link String} and parses it to see if it is a valid IPV4 address. * * @return true, if the string represents an IPV4 address in dotted * notation, false otherwise */ public static boolean isValidIpV4Address(String ip) { return isValidIpV4Address(ip, 0, ip.length()); }
private static boolean isValidIpV4Address(CharSequence ip, int from, int toExcluded) { return ip instanceof String ? isValidIpV4Address((String) ip, from, toExcluded) : ip instanceof AsciiString ? isValidIpV4Address((AsciiString) ip, from, toExcluded) : isValidIpV4Address0(ip, from, toExcluded); }
/** * Takes a {@link CharSequence} and parses it to see if it is a valid IPV4 address. * * @return true, if the string represents an IPV4 address in dotted * notation, false otherwise */ public static boolean isValidIpV4Address(CharSequence ip) { return isValidIpV4Address(ip, 0, ip.length()); }
/** * Takes a {@link CharSequence} and parses it to see if it is a valid IPV4 address. * * @return true, if the string represents an IPV4 address in dotted * notation, false otherwise */ public static boolean isValidIpV4Address(CharSequence ip) { return isValidIpV4Address(ip, 0, ip.length()); }
/** * Takes a {@link String} and parses it to see if it is a valid IPV4 address. * * @return true, if the string represents an IPV4 address in dotted * notation, false otherwise */ public static boolean isValidIpV4Address(String ip) { return isValidIpV4Address(ip, 0, ip.length()); }
/** * Validate that the given hostname can be used in SNI extension. */ static boolean isValidHostNameForSNI(String hostname) { return hostname != null && hostname.indexOf('.') > 0 && !hostname.endsWith(".") && !NetUtil.isValidIpV4Address(hostname) && !NetUtil.isValidIpV6Address(hostname); }
/** * Validate that the given hostname can be used in SNI extension. */ static boolean isValidHostNameForSNI(String hostname) { return hostname != null && hostname.indexOf('.') > 0 && !hostname.endsWith(".") && !NetUtil.isValidIpV4Address(hostname) && !NetUtil.isValidIpV6Address(hostname); }
/** * Creates an byte[] based on an ipAddressString. No error handling is performed here. */ public static byte[] createByteArrayFromIpAddressString(String ipAddressString) { if (isValidIpV4Address(ipAddressString)) { return validIpV4ToBytes(ipAddressString); } if (isValidIpV6Address(ipAddressString)) { if (ipAddressString.charAt(0) == '[') { ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1); } int percentPos = ipAddressString.indexOf('%'); if (percentPos >= 0) { ipAddressString = ipAddressString.substring(0, percentPos); } return getIPv6ByName(ipAddressString, true); } return null; }
/** * Creates a new instance. * * @param status the status of the response * @param dstAddr the {@code DSTIP} field of the response * @param dstPort the {@code DSTPORT} field of the response */ public DefaultSocks4CommandResponse(Socks4CommandStatus status, String dstAddr, int dstPort) { if (status == null) { throw new NullPointerException("cmdStatus"); } if (dstAddr != null) { if (!NetUtil.isValidIpV4Address(dstAddr)) { throw new IllegalArgumentException( "dstAddr: " + dstAddr + " (expected: a valid IPv4 address)"); } } if (dstPort < 0 || dstPort > 65535) { throw new IllegalArgumentException("dstPort: " + dstPort + " (expected: 0~65535)"); } this.status = status; this.dstAddr = dstAddr; this.dstPort = dstPort; }
/** * Creates an byte[] based on an ipAddressString. No error handling is performed here. */ public static byte[] createByteArrayFromIpAddressString(String ipAddressString) { if (isValidIpV4Address(ipAddressString)) { return validIpV4ToBytes(ipAddressString); } if (isValidIpV6Address(ipAddressString)) { if (ipAddressString.charAt(0) == '[') { ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1); } int percentPos = ipAddressString.indexOf('%'); if (percentPos >= 0) { ipAddressString = ipAddressString.substring(0, percentPos); } return getIPv6ByName(ipAddressString, true); } return null; }
/** * Creates an {@link AddressResolverGroup} which always returns the specified {@code ip} address * for any hostname. */ public static MockAddressResolverGroup always(String ip) { requireNonNull(ip, "ip"); checkArgument(NetUtil.isValidIpV4Address(ip) || NetUtil.isValidIpV6Address(ip), "not an IP address: %s", ip); try { final InetAddress resolved = InetAddress.getByName(ip); return new MockAddressResolverGroup(unused -> resolved); } catch (UnknownHostException e) { throw new IllegalArgumentException(e); } }
/** * Returns a {@link Predicate} which returns {@code true} if the given {@link InetAddress} is in the * range of a <a href="https://tools.ietf.org/html/rfc4632">Classless Inter-domain Routing (CIDR)</a> block. * * @param baseAddress the base {@link InetAddress} of a CIDR notation * @param subnetMask the subnet mask, e.g. {@code 255.255.255.0} */ public static Predicate<InetAddress> ofCidr(InetAddress baseAddress, String subnetMask) { requireNonNull(baseAddress, "baseAddress"); requireNonNull(subnetMask, "subnetMask"); checkArgument(NetUtil.isValidIpV4Address(subnetMask), "subnetMask: %s (expected: an IPv4 address string)", subnetMask); final int maskBits = toMaskBits(subnetMask); return ofCidr(baseAddress, maskBits, maskBits + 96); }
/** * Creates an byte[] based on an ipAddressString. No error handling is performed here. */ public static byte[] createByteArrayFromIpAddressString(String ipAddressString) { if (isValidIpV4Address(ipAddressString)) { return validIpV4ToBytes(ipAddressString); } if (isValidIpV6Address(ipAddressString)) { if (ipAddressString.charAt(0) == '[') { ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1); } int percentPos = ipAddressString.indexOf('%'); if (percentPos >= 0) { ipAddressString = ipAddressString.substring(0, percentPos); } return getIPv6ByName(ipAddressString, true); } return null; }
private static Endpoint create(String host, int port) { requireNonNull(host, "host"); if (NetUtil.isValidIpV4Address(host)) { return new Endpoint(host, host, port, DEFAULT_WEIGHT, HostType.IPv4_ONLY); } if (NetUtil.isValidIpV6Address(host)) { final String ipV6Addr; if (host.charAt(0) == '[') { // Strip surrounding '[' and ']'. ipV6Addr = host.substring(1, host.length() - 1); } else { ipV6Addr = host; } return new Endpoint(ipV6Addr, ipV6Addr, port, DEFAULT_WEIGHT, HostType.IPv6_ONLY); } return new Endpoint(InternetDomainName.from(host).toString(), null, port, DEFAULT_WEIGHT, HostType.HOSTNAME_ONLY); }
private void sendConnectCommand(ChannelHandlerContext ctx) throws Exception { InetSocketAddress raddr = destinationAddress(); Socks5AddressType addrType; String rhost; if (raddr.isUnresolved()) { addrType = Socks5AddressType.DOMAIN; rhost = raddr.getHostString(); } else { rhost = raddr.getAddress().getHostAddress(); if (NetUtil.isValidIpV4Address(rhost)) { addrType = Socks5AddressType.IPv4; } else if (NetUtil.isValidIpV6Address(rhost)) { addrType = Socks5AddressType.IPv6; } else { throw new ProxyConnectException( exceptionMessage("unknown address type: " + StringUtil.simpleClassName(rhost))); } } ctx.pipeline().replace(decoderName, decoderName, new Socks5CommandResponseDecoder()); sendToProxyServer(new DefaultSocks5CommandRequest(Socks5CommandType.CONNECT, addrType, rhost, raddr.getPort())); } }
@Override protected void encode(ChannelHandlerContext ctx, Socks4CommandRequest msg, ByteBuf out) throws Exception { out.writeByte(msg.version().byteValue()); out.writeByte(msg.type().byteValue()); out.writeShort(msg.dstPort()); if (NetUtil.isValidIpV4Address(msg.dstAddr())) { out.writeBytes(NetUtil.createByteArrayFromIpAddressString(msg.dstAddr())); ByteBufUtil.writeAscii(out, msg.userId()); out.writeByte(0); } else { out.writeBytes(IPv4_DOMAIN_MARKER); ByteBufUtil.writeAscii(out, msg.userId()); out.writeByte(0); ByteBufUtil.writeAscii(out, msg.dstAddr()); out.writeByte(0); } } }