ref[move]: move NetUtils

This commit is contained in:
godotg committed 2023-09-02 20:54:55 +08:00
1 parent ff3e06c123
commit 00ac69dca2
48 files changed
+54 -71

No files matched your search

@@ -13,9 +13,9 @@
package com.zfoo.net.config.model;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.util.NetUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.util.net.HostAndPort;
import com.zfoo.util.net.NetUtils;
import java.util.List;
import java.util.Objects;
@@ -16,6 +16,7 @@ package com.zfoo.net.consumer.registry;
import com.zfoo.event.manager.EventBus;
import com.zfoo.net.NetContext;
import com.zfoo.net.consumer.event.ConsumerStartEvent;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.tcp.TcpClient;
import com.zfoo.net.core.tcp.TcpServer;
import com.zfoo.net.session.Session;
@@ -24,13 +25,8 @@ import com.zfoo.protocol.collection.ArrayUtils;
import com.zfoo.protocol.collection.concurrent.ConcurrentArrayList;
import com.zfoo.protocol.collection.concurrent.ConcurrentHashSet;
import com.zfoo.protocol.exception.ExceptionUtils;
import com.zfoo.protocol.util.AssertionUtils;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.protocol.util.*;
import com.zfoo.scheduler.manager.SchedulerBus;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.util.concurrent.FastThreadLocalThread;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
@@ -19,7 +19,6 @@ import com.zfoo.net.session.Session;
import com.zfoo.protocol.exception.ExceptionUtils;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.epoll.Epoll;
@@ -15,7 +15,6 @@ package com.zfoo.net.core;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.epoll.Epoll;
@@ -0,0 +1,119 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed 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 com.zfoo.net.core;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.util.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author godotg
* @version 3.0
*/
public class HostAndPort {
private String host;
private int port;
public static HostAndPort valueOf(String host, int port) {
HostAndPort hostAndPort = new HostAndPort();
hostAndPort.host = host;
hostAndPort.port = port;
return hostAndPort;
}
/**
* @param hostAndPort example -> localhost:port
*/
public static HostAndPort valueOf(String hostAndPort) {
var split = hostAndPort.trim().split(StringUtils.COLON_REGEX);
return valueOf(split[0].trim(), Integer.parseInt(split[1].trim()));
}
/**
* @param hostAndPorts example -> localhost:port,localhost:port,localhost:port
*/
public static List<HostAndPort> toHostAndPortList(String hostAndPorts) {
if (StringUtils.isEmpty(hostAndPorts)) {
return Collections.emptyList();
}
var hostAndPortSplits = hostAndPorts.split(StringUtils.COMMA_REGEX);
var hostAndPortList = new ArrayList<HostAndPort>();
for (var hostAndPort : hostAndPortSplits) {
hostAndPortList.add(valueOf(hostAndPort));
}
return hostAndPortList;
}
public static List<HostAndPort> toHostAndPortList(Collection<String> list) {
if (CollectionUtils.isEmpty(list)) {
return Collections.emptyList();
}
var hostAndPortList = new ArrayList<HostAndPort>();
list.forEach(it -> hostAndPortList.addAll(toHostAndPortList(it)));
return hostAndPortList;
}
public static String toHostAndPortListStr(Collection<HostAndPort> list) {
var urlList = list.stream()
.map(it -> it.toHostAndPortStr())
.collect(Collectors.toList());
return StringUtils.joinWith(StringUtils.COMMA, urlList.toArray());
}
public String toHostAndPortStr() {
return StringUtils.format("{}:{}", this.host.trim(), this.port);
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
HostAndPort that = (HostAndPort) o;
return port == that.port && Objects.equals(host, that.host);
}
@Override
public int hashCode() {
return Objects.hash(host, port);
}
@Override
public String toString() {
return StringUtils.format("[{}]", toHostAndPortStr());
}
}
@@ -14,12 +14,12 @@
package com.zfoo.net.core.gateway;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.GatewayRouteHandler;
import com.zfoo.net.handler.codec.tcp.TcpCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.net.session.Session;
import com.zfoo.protocol.IPacket;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.lang.Nullable;
@@ -14,13 +14,13 @@
package com.zfoo.net.core.gateway;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.GatewayRouteHandler;
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.net.session.Session;
import com.zfoo.protocol.IPacket;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
@@ -14,13 +14,13 @@
package com.zfoo.net.core.gateway;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.GatewayRouteHandler;
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.net.session.Session;
import com.zfoo.protocol.IPacket;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
@@ -14,11 +14,11 @@
package com.zfoo.net.core.http;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.http.HttpCodecHandler;
import com.zfoo.net.packet.DecodedPacketInfo;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpObjectAggregator;
@@ -14,12 +14,12 @@
package com.zfoo.net.core.jprotobuf;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.GatewayRouteHandler;
import com.zfoo.net.handler.codec.jprotobuf.JProtobufTcpCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.net.session.Session;
import com.zfoo.protocol.IPacket;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.lang.Nullable;
@@ -14,10 +14,10 @@
package com.zfoo.net.core.jprotobuf;
import com.zfoo.net.core.AbstractClient;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ClientRouteHandler;
import com.zfoo.net.handler.codec.jprotobuf.JProtobufTcpCodecHandler;
import com.zfoo.net.handler.idle.ClientIdleHandler;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
@@ -14,10 +14,10 @@
package com.zfoo.net.core.jprotobuf;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.jprotobuf.JProtobufTcpCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
@@ -14,10 +14,10 @@
package com.zfoo.net.core.json;
import com.zfoo.net.core.AbstractClient;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ClientRouteHandler;
import com.zfoo.net.handler.codec.json.JsonWebSocketCodecHandler;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
@@ -14,10 +14,10 @@
package com.zfoo.net.core.json;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.json.JsonWebSocketCodecHandler;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
@@ -14,10 +14,10 @@
package com.zfoo.net.core.tcp;
import com.zfoo.net.core.AbstractClient;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ClientRouteHandler;
import com.zfoo.net.handler.codec.tcp.TcpCodecHandler;
import com.zfoo.net.handler.idle.ClientIdleHandler;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
@@ -14,10 +14,10 @@
package com.zfoo.net.core.tcp;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.tcp.TcpCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
@@ -15,12 +15,12 @@ package com.zfoo.net.core.udp;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.AbstractClient;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.BaseRouteHandler;
import com.zfoo.net.handler.ClientRouteHandler;
import com.zfoo.net.handler.codec.udp.UdpCodecHandler;
import com.zfoo.net.session.Session;
import com.zfoo.protocol.exception.ExceptionUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
@@ -13,9 +13,9 @@
package com.zfoo.net.core.udp;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.udp.UdpCodecHandler;
import com.zfoo.util.net.HostAndPort;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
@@ -14,10 +14,10 @@
package com.zfoo.net.core.websocket;
import com.zfoo.net.core.AbstractClient;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ClientRouteHandler;
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
@@ -14,10 +14,10 @@
package com.zfoo.net.core.websocket;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
@@ -14,31 +14,22 @@
package com.zfoo.net.core.websocket;
import com.zfoo.net.core.AbstractClient;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ClientRouteHandler;
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.net.handler.idle.ClientIdleHandler;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig;
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler;
import io.netty.handler.codec.http.websocketx.WebSocketVersion;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.NoSuchAlgorithmException;
@@ -14,12 +14,11 @@
package com.zfoo.net.core.websocket;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.handler.GatewayRouteHandler;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
@@ -0,0 +1,479 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed 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 com.zfoo.net.util;
import com.zfoo.protocol.collection.ArrayUtils;
import com.zfoo.protocol.util.AssertionUtils;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.util.EnumUtils;
import org.springframework.lang.Nullable;
import java.io.IOException;
import java.io.OutputStream;
import java.net.*;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* 网络相关工具
*
* @author godotg
* @version 3.0
*/
public class NetUtils {
public final static String LOCAL_LOOPBACK_IP = "127.0.0.1";
/**
* 默认最小端口,1024
*/
public static final int PORT_RANGE_MIN = 1024;
/**
* 默认最大端口,65535
*/
public static final int PORT_RANGE_MAX = 0xFFFF;
/**
* IP v4
*/
public final static Pattern IPV4 = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
/**
* IP v6
*/
public final static Pattern IPV6 = Pattern.compile("(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))");
/**
* 根据long值获取ip v4地址
*
* @param longIP IP的long表示形式
* @return IP V4 地址
*/
public static String longToIpv4(long longIP) {
final StringBuilder sb = new StringBuilder();
// 直接右移24位
sb.append((longIP / 1_000_000_000));
sb.append(".");
// 将高8位置0,然后右移16位
sb.append(longIP / 1_000_000 % 1_000);
sb.append(".");
sb.append(longIP / 1_000 % 1_000);
sb.append(".");
sb.append(longIP % 1_000);
return sb.toString();
}
/**
* 根据ip地址计算出long型的数据
*
* @param strIP IP V4 地址
* @return long值
*/
public static long ipv4ToLong(String strIP) {
if (isValidAddress(strIP)) {
long[] ip = new long[4];
// 先找到IP地址字符串中.的位置
int position1 = strIP.indexOf(".");
int position2 = strIP.indexOf(".", position1 + 1);
int position3 = strIP.indexOf(".", position2 + 1);
// 将每个.之间的字符串转换成整型
ip[0] = Long.parseLong(strIP.substring(0, position1));
ip[1] = Long.parseLong(strIP.substring(position1 + 1, position2));
ip[2] = Long.parseLong(strIP.substring(position2 + 1, position3));
ip[3] = Long.parseLong(strIP.substring(position3 + 1));
return (ip[0] * 1_000_000_000) + (ip[1] * 1_000_000) + (ip[2] * 1_000) + ip[3];
}
return 0;
}
public static boolean isValidAddress(String address) {
return IPV4.matcher(address).matches() || IPV6.matcher(address).matches();
}
// 同时支持ipv4和ipv6
public static long ipToLong(String ip) {
var splits = ip.split(StringUtils.PERIOD_REGEX);
var longs = new long[splits.length];
for (var i = 0; i < splits.length; i++) {
var b = Long.parseLong(splits[i]);
longs[i] = b;
}
var longList = ArrayUtils.toList(longs);
Collections.reverse(longList);
longs = ArrayUtils.longToArray(longList);
var value = 0L;
for (var i = 0; i < longs.length; i++) {
var b = longs[i];
var shift = i * 8;
value = value | (b << shift);
}
return value;
}
public static String longToIp(long ipLong) {
var longs = new long[8];
for (var i = 0; i < longs.length; i++) {
var shift = i * 8;
var mask = 0xFFL << shift;
longs[i] = (ipLong & mask) >> shift;
}
var longList = ArrayUtils.toList(longs);
Collections.reverse(longList);
var ip = StringUtils.joinWith(".", longList.toArray());
if (ip.startsWith("0.0.0.0.")) {
return StringUtils.substringAfterLast(ip, "0.0.0.0.");
}
return ip;
}
/**
* 检测本地端口可用性<br>
* 来自org.springframework.util.SocketUtils
*
* @param port 被检测的端口
* @return 是否可用
*/
public static boolean isAvailablePort(int port) {
if (!isValidPort(port)) {
// 给定的IP未在指定端口范围中
return false;
}
// try-with-resources会自动调用close方法
try (ServerSocket ss = new ServerSocket(port, 0, getLocalhost())) {
return true;
} catch (Exception e) {
return false;
}
}
/**
* 是否为有效的端口<br>
* 此方法并不检查端口是否被占用
*
* @param port 端口号
* @return 是否有效
*/
public static boolean isValidPort(int port) {
// 有效端口是0~65535
return port >= 0 && port <= PORT_RANGE_MAX;
}
/**
* 查找1024~65535范围内的可用端口<br>
* 此方法只检测给定范围内的随机一个端口,检测65535-1024次<br>
*
* @return 可用的端口
*/
public static int getAvailablePort() {
return getAvailablePort(PORT_RANGE_MIN);
}
/**
* 查找指定范围内的可用端口,最大值为65535<br>
* 此方法只检测给定范围内的随机一个端口,检测65535-minPort次<br>
*
* @param minPort 端口最小值(包含)
* @return 可用的端口
*/
public static int getAvailablePort(int minPort) {
return getAvailablePort(minPort, PORT_RANGE_MAX);
}
/**
* 查找指定范围内的可用端口<br>
* 此方法只检测给定范围内的随机一个端口,检测maxPort-minPort次<br>
*
* @param minPort 端口最小值(包含)
* @param maxPort 端口最大值(包含)
* @return 可用的端口
*/
public static int getAvailablePort(int minPort, int maxPort) {
for (int i = minPort; i <= maxPort; i++) {
if (isAvailablePort(i)) {
return i;
}
}
throw new IllegalArgumentException(StringUtils
.format("Could not find an available port in the range [{}, {}] after {} attempts", minPort, maxPort, maxPort - minPort));
}
/**
* 获取多个本地可用端口<br>
*
* @param minPort 端口最小值(包含)
* @param maxPort 端口最大值(包含)
* @return 可用的端口
*/
public static TreeSet<Integer> getAvailablePorts(int numRequested, int minPort, int maxPort) {
final TreeSet<Integer> availablePorts = new TreeSet<>();
int attemptCount = 0;
while ((++attemptCount <= numRequested + 100) && availablePorts.size() < numRequested) {
availablePorts.add(getAvailablePort(minPort, maxPort));
}
if (availablePorts.size() != numRequested) {
throw new IllegalArgumentException(StringUtils
.format("Could not find {} available ports in the range [{}, {}]", numRequested, minPort, maxPort));
}
return availablePorts;
}
/**
* 判定是否为内网IP<br>
* 私有IP:A类 10.0.0.0-10.255.255.255 B类 172.16.0.0-172.31.255.255 C类 192.168.0.0-192.168.255.255 当然,还有127这个网段是环回地址
*
* @param ipAddress IP地址
* @return 是否为内网IP
*/
public static boolean isInnerIP(String ipAddress) {
long ipNum = NetUtils.ipv4ToLong(ipAddress);
long aBegin = NetUtils.ipv4ToLong("10.0.0.0");
long aEnd = NetUtils.ipv4ToLong("10.255.255.255");
long bBegin = NetUtils.ipv4ToLong("172.16.0.0");
long bEnd = NetUtils.ipv4ToLong("172.31.255.255");
long cBegin = NetUtils.ipv4ToLong("192.168.0.0");
long cEnd = NetUtils.ipv4ToLong("192.168.255.255");
boolean isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd) || ipAddress.equals(LOCAL_LOOPBACK_IP);
return isInnerIp;
}
/**
* 指定IP的long是否在指定范围内
*
* @param userIp 用户IP
* @param begin 开始IP
* @param end 结束IP
* @return 是否在范围内
*/
private static boolean isInner(long userIp, long begin, long end) {
return (userIp >= begin) && (userIp <= end);
}
/**
* 通过域名得到IP
*
* @param hostName HOST
* @return ip address or hostName if UnknownHostException
*/
public static String getIpByHost(String hostName) {
try {
return InetAddress.getByName(hostName).getHostAddress();
} catch (UnknownHostException e) {
return hostName;
}
}
/**
* 获取本机所有网卡
*/
public static Set<NetworkInterface> getAllNetworkInterface() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
return EnumUtils.enumerationToSet(networkInterfaces);
} catch (SocketException e) {
return Collections.emptySet();
}
}
/**
* 获取所有满足过滤条件的本地IP地址对象
*
* @return 过滤后的地址对象列表
*/
public static Set<InetAddress> getAllAddress() {
var networkInterfaces = getAllNetworkInterface();
var ipSet = new LinkedHashSet<InetAddress>();
networkInterfaces.forEach(it -> {
final Enumeration<InetAddress> inetAddresses = it.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
final InetAddress inetAddress = inetAddresses.nextElement();
if (Objects.nonNull(inetAddress)) {
ipSet.add(inetAddress);
}
}
});
return ipSet;
}
/**
* 获得本机的IPv4地址列表<br>
* 返回的IP列表有序,按照系统设备顺序
*/
public static Set<String> localIpv4s() {
var localAddressList = getAllAddress().stream()
.filter(it -> it instanceof Inet4Address).collect(Collectors.toSet());
return toIpList(localAddressList);
}
/**
* 获得本机的IPv6地址列表<br>
* 返回的IP列表有序,按照系统设备顺序
*/
public static Set<String> localIpv6s() {
var localAddressList = getAllAddress().stream()
.filter(it -> it instanceof Inet6Address).collect(Collectors.toSet());
return toIpList(localAddressList);
}
/**
* 地址列表转换为IP地址列表
*/
public static Set<String> toIpList(Set<InetAddress> addressList) {
var ipSet = new LinkedHashSet<String>();
for (InetAddress address : addressList) {
ipSet.add(address.getHostAddress());
}
return ipSet;
}
/**
* 获得本机的IP地址列表(包括Ipv4和Ipv6)<br>
*/
public static Set<String> localIps() {
var localAddressList = getAllAddress();
return toIpList(localAddressList);
}
/**
* 获取本机网卡IP地址,这个地址为所有网卡中非回路地址的第一个<br>
* 如果获取失败调用 {@link InetAddress#getLocalHost()}方法获取。<br>
* 此方法不会抛出异常,获取失败将返回<code>null</code><br>
* <p>
* 参考:http://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java
*
* @return 本机网卡IP地址,获取失败返回空字符串
*/
public static String getLocalhostStr() {
InetAddress localhost = getLocalhost();
if (null != localhost) {
return localhost.getHostAddress();
}
return StringUtils.EMPTY;
}
/**
* 获取本机网卡IP地址,规则如下:
*
* <pre>
* 1. 查找所有网卡地址,必须非回路(loopback)地址、非局域网地址(siteLocal)、IPv4地址
* 2. 如果无满足要求的地址,调用 {@link InetAddress#getLocalHost()} 获取地址
* </pre>
*
* @return 本机网卡IP地址,获取失败返回<code>null</code>
*/
@Nullable
public static InetAddress getLocalhost() {
var address = getAllAddress().stream()
.filter(it -> !it.isLoopbackAddress()
// 非地区本地地址,指10.0.0.0 ~ 10.255.255.255、172.16.0.0 ~ 172.31.255.255、192.168.0.0 ~ 192.168.255.255
&& it.isSiteLocalAddress()
// 需为IPV4地址
&& it instanceof Inet4Address
&& !it.getHostAddress().contains(":"))
.findFirst();
if (address.isPresent()) {
return address.get();
}
try {
return InetAddress.getLocalHost();
} catch (UnknownHostException e) {
return null;
}
}
/**
* 获得本机MAC地址
*
* @return 本机MAC地址
*/
public static String getLocalMacAddress() {
return getMacAddress(getLocalhost());
}
/**
* 获得指定地址信息中的MAC地址,使用分隔符“-”
*
* @param inetAddress {@link InetAddress}
* @return MAC地址,用-分隔
*/
public static String getMacAddress(InetAddress inetAddress) {
return getMacAddress(inetAddress, "-");
}
/**
* 获得指定地址信息中的MAC地址
*
* @param inetAddress {@link InetAddress}
* @param separator 分隔符,推荐使用“-”或者“:”
* @return MAC地址,用-分隔
*/
public static String getMacAddress(InetAddress inetAddress, String separator) {
AssertionUtils.notNull(inetAddress);
byte[] mac;
try {
mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
} catch (SocketException e) {
throw new IllegalArgumentException(e);
}
if (null != mac) {
final StringBuilder sb = new StringBuilder();
String s;
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append(separator);
}
// 字节转换为整数
s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length() == 1 ? 0 + s : s);
}
return sb.toString();
}
return null;
}
/**
* 使用普通Socket发送数据
*
* @param host Server主机
* @param port Server端口
* @param data 数据
* @throws IOException IO异常
*/
public static void netCat(String host, int port, byte[] data) throws IOException {
OutputStream out = null;
try {
Socket socket = new Socket(host, port);
out = socket.getOutputStream();
out.write(data);
out.flush();
} finally {
IOUtils.closeIO(out);
}
}
}
@@ -18,8 +18,8 @@ import com.zfoo.net.config.model.ConsumerModule;
import com.zfoo.net.config.model.ProviderConfig;
import com.zfoo.net.config.model.ProviderModule;
import com.zfoo.net.consumer.registry.RegisterVO;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.protocol.registration.ProtocolModule;
import com.zfoo.util.net.HostAndPort;
import io.netty.util.NetUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -13,10 +13,10 @@
package com.zfoo.net.core.csharp;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.tcp.TcpServer;
import com.zfoo.net.session.SessionUtils;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -14,13 +14,13 @@
package com.zfoo.net.core.gateway;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.tcp.TcpClient;
import com.zfoo.net.packet.gateway.GatewayToProviderRequest;
import com.zfoo.net.packet.gateway.GatewayToProviderResponse;
import com.zfoo.net.session.SessionUtils;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
@@ -13,13 +13,13 @@
package com.zfoo.net.core.http;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.packet.DecodedPacketInfo;
import com.zfoo.net.packet.http.HttpHelloRequest;
import com.zfoo.net.router.attachment.HttpAttachment;
import com.zfoo.protocol.exception.RunException;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.junit.Ignore;
@@ -14,10 +14,10 @@
package com.zfoo.net.core.jprotobuf.client;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.jprotobuf.JProtobufTcpClient;
import com.zfoo.net.packet.jprotobuf.JProtobufHelloRequest;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
@@ -13,9 +13,9 @@
package com.zfoo.net.core.jprotobuf.server;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.jprotobuf.JProtobufTcpServer;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -14,10 +14,10 @@
package com.zfoo.net.core.json.client;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.json.JsonWebsocketClient;
import com.zfoo.net.packet.json.JsonHelloRequest;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig;
import org.junit.Ignore;
import org.junit.Test;
@@ -13,9 +13,9 @@
package com.zfoo.net.core.json.server;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.json.JsonWebsocketServer;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -14,10 +14,10 @@
package com.zfoo.net.core.tcp.client;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.tcp.TcpClient;
import com.zfoo.net.packet.tcp.TcpHelloRequest;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
@@ -13,9 +13,9 @@
package com.zfoo.net.core.tcp.server;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.tcp.TcpServer;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -13,12 +13,12 @@
package com.zfoo.net.core.tcpAsync.client;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.tcp.TcpClient;
import com.zfoo.net.packet.tcp.AsyncMessAnswer;
import com.zfoo.net.packet.tcp.AsyncMessAsk;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
@@ -14,12 +14,12 @@
package com.zfoo.net.core.tcpAsync.client;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.tcp.TcpClient;
import com.zfoo.net.packet.tcp.AsyncMessAnswer;
import com.zfoo.net.packet.tcp.AsyncMessAsk;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
@@ -13,9 +13,9 @@
package com.zfoo.net.core.tcpAsync.server;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.tcp.TcpServer;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -13,12 +13,12 @@
package com.zfoo.net.core.tcpSync.client;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.tcp.TcpClient;
import com.zfoo.net.packet.tcp.SyncMessAnswer;
import com.zfoo.net.packet.tcp.SyncMessAsk;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
@@ -14,12 +14,12 @@
package com.zfoo.net.core.tcpSync.client;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.tcp.TcpClient;
import com.zfoo.net.packet.tcp.SyncMessAnswer;
import com.zfoo.net.packet.tcp.SyncMessAsk;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
@@ -13,9 +13,9 @@
package com.zfoo.net.core.tcpSync.server;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.tcp.TcpServer;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -13,11 +13,11 @@
package com.zfoo.net.core.udp.client;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.udp.UdpClient;
import com.zfoo.net.packet.udp.UdpHelloRequest;
import com.zfoo.net.router.attachment.UdpAttachment;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -12,9 +12,9 @@
package com.zfoo.net.core.udp.server;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.udp.UdpServer;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -14,10 +14,10 @@
package com.zfoo.net.core.websocket.client;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.websocket.WebsocketClient;
import com.zfoo.net.packet.websocket.WebsocketHelloRequest;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig;
import org.junit.Ignore;
import org.junit.Test;
@@ -13,9 +13,9 @@
package com.zfoo.net.core.websocket.server;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.websocket.WebsocketServer;
import com.zfoo.protocol.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed 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 com.zfoo.net.util;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
/**
* @author godotg
* @version 3.0
*/
public class NetUtilsTest {
@Test
public void localhostTest() {
var localHostStr = NetUtils.getLocalhostStr();
var localHost = NetUtils.getLocalhost();
Assert.assertEquals(localHostStr, localHost.getHostAddress());
}
@Test
public void ipv4Test() {
var ipLong = NetUtils.ipv4ToLong(NetUtils.LOCAL_LOOPBACK_IP);
var ipStr = NetUtils.longToIpv4(ipLong);
Assert.assertEquals(ipStr, NetUtils.LOCAL_LOOPBACK_IP);
}
@Test
public void isInnerIpTest() {
Assert.assertTrue(NetUtils.isInnerIP(NetUtils.LOCAL_LOOPBACK_IP));
}
@Ignore
@Test
public void isNetCatTest() throws IOException {
NetUtils.netCat(NetUtils.LOCAL_LOOPBACK_IP, 9000, new byte[]{1, 2, 3});
}
@Ignore
@Test
public void isUsableLocalPortTest() {
System.out.println(NetUtils.isAvailablePort(2181));
System.out.println(NetUtils.getAvailablePort(2181));
}
@Ignore
@Test
public void getAllNetworkInterfaceTest() {
var set = NetUtils.getAllNetworkInterface();
set.stream().forEach(it -> System.out.println(it));
}
@Ignore
@Test
public void getAllAddressTest() {
var set = NetUtils.getAllAddress();
set.stream().forEach(it -> System.out.println(it));
}
@Ignore
@Test
public void localIpv4sTest() {
var set = NetUtils.localIpv4s();
set.stream().forEach(it -> System.out.println(it));
}
}