no message
This commit is contained in:
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
* Copyright 2025 mingliqiye
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile NetworkAddress.java
|
||||
* LastUpdate 2025-09-09 08:37:33
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.network;
|
||||
|
||||
import com.mingliqiye.utils.string.StringUtil;
|
||||
import java.io.Serializable;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.regex.Pattern;
|
||||
import lombok.Getter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 网络地址类,用于表示一个网络地址(IP或域名),并提供相关操作。
|
||||
* 支持IPv4和IPv6地址的解析与验证。
|
||||
*
|
||||
* @author MingLiPro
|
||||
*/
|
||||
public class NetworkAddress implements Serializable {
|
||||
|
||||
/**
|
||||
* IPv6标识
|
||||
*/
|
||||
public static int IPV6 = 6;
|
||||
|
||||
/**
|
||||
* IPv4标识
|
||||
*/
|
||||
public static int IPV4 = 4;
|
||||
|
||||
/**
|
||||
* IPv4地址正则表达式
|
||||
*/
|
||||
static String IPV4REG =
|
||||
"^((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2" +
|
||||
"(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}$";
|
||||
|
||||
/**
|
||||
* 编译后的IPv4地址匹配模式
|
||||
*/
|
||||
private static final Pattern IPV4_PATTERN = Pattern.compile(IPV4REG);
|
||||
|
||||
/**
|
||||
* IPv6地址正则表达式
|
||||
*/
|
||||
static String IPV6REG =
|
||||
"^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|" +
|
||||
"^(::([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})$" +
|
||||
"|" +
|
||||
"^(::)$|" +
|
||||
"^([0-9a-fA-F]{1,4}::([0-9a-fA-F]{1,4}:){0,5}[0-9a-fA-F]{1,4})$|" +
|
||||
"^(([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4})$|" +
|
||||
"^(([0-9a-fA-F]{1,4}:){6}(([0-9]{1,3}\\.){3}[0-9]{1,3}))$|" +
|
||||
"^::([fF]{4}:)?(([0-9]{1,3}\\.){3}[0-9]{1,3})$";
|
||||
|
||||
/**
|
||||
* 编译后的IPv6地址匹配模式
|
||||
*/
|
||||
private static final Pattern IPV6_PATTERN = Pattern.compile(IPV6REG);
|
||||
|
||||
/**
|
||||
* IP地址类型:4 表示 IPv4,6 表示 IPv6
|
||||
*/
|
||||
@Getter
|
||||
private int IPv;
|
||||
|
||||
/**
|
||||
* IP地址字符串
|
||||
*/
|
||||
@Getter
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* 域名(如果输入的是域名)
|
||||
*/
|
||||
private String domain;
|
||||
|
||||
/**
|
||||
* 标识是否是域名解析来的IP
|
||||
*/
|
||||
private boolean isdom;
|
||||
|
||||
/**
|
||||
* 构造方法,根据传入的字符串判断是IP地址还是域名,并进行相应处理。
|
||||
*
|
||||
* @param domip 可能是IP地址或域名的字符串
|
||||
*/
|
||||
NetworkAddress(String domip) {
|
||||
try {
|
||||
// 尝试将输入识别为IP地址
|
||||
IPv = testIp(domip);
|
||||
ip = domip;
|
||||
} catch (NetworkException e) {
|
||||
try {
|
||||
// 如果不是有效IP,则尝试作为域名解析
|
||||
String ips = getHostIp(domip);
|
||||
IPv = testIp(ips);
|
||||
ip = ips;
|
||||
isdom = true;
|
||||
domain = domip;
|
||||
} catch (UnknownHostException ex) {
|
||||
throw new NetworkException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 静态工厂方法,创建 NetworkAddress 实例。
|
||||
*
|
||||
* @param domip 可能是IP地址或域名的字符串
|
||||
* @return 新建的 NetworkAddress 实例
|
||||
*/
|
||||
public static NetworkAddress of(String domip) {
|
||||
return new NetworkAddress(domip);
|
||||
}
|
||||
|
||||
/**
|
||||
* 静态工厂方法,通过 InetAddress 创建 NetworkAddress 实例。
|
||||
*
|
||||
* @param inetAddress InetAddress 对象
|
||||
* @return 新建的 NetworkAddress 实例
|
||||
*/
|
||||
public static NetworkAddress of(InetAddress inetAddress) {
|
||||
return new NetworkAddress(inetAddress.getHostAddress());
|
||||
}
|
||||
|
||||
/**
|
||||
* 从DNS服务器解析域名获取对应的IP地址。
|
||||
*
|
||||
* @param domain 域名
|
||||
* @return 解析出的第一个IP地址
|
||||
* @throws UnknownHostException 如果域名无法解析
|
||||
*/
|
||||
public static String getHostIp(@NotNull String domain)
|
||||
throws UnknownHostException {
|
||||
InetAddress[] addresses = InetAddress.getAllByName(domain.trim());
|
||||
return addresses[0].getHostAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测给定字符串是否为有效的IPv4或IPv6地址。
|
||||
*
|
||||
* @param ip 要检测的IP地址字符串
|
||||
* @return 4 表示IPv4,6 表示IPv6
|
||||
* @throws NetworkException 如果IP格式无效
|
||||
*/
|
||||
public static int testIp(String ip) {
|
||||
if (ip == null) {
|
||||
throw new NetworkException("IP地址不能为null");
|
||||
}
|
||||
String trimmedIp = ip.trim();
|
||||
|
||||
// 判断是否匹配IPv4格式
|
||||
if (IPV4_PATTERN.matcher(trimmedIp).matches()) {
|
||||
return IPV4;
|
||||
}
|
||||
|
||||
// 判断是否匹配IPv6格式
|
||||
if (IPV6_PATTERN.matcher(trimmedIp).matches()) {
|
||||
return IPV6;
|
||||
}
|
||||
|
||||
// 不符合任一格式时抛出异常
|
||||
throw new NetworkException(
|
||||
StringUtil.format("[{}] 不是有效的IPv4或IPv6地址", ip)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前 NetworkAddress 转换为 InetAddress 对象。
|
||||
*
|
||||
* @return InetAddress 对象
|
||||
*/
|
||||
public InetAddress toInetAddress() {
|
||||
try {
|
||||
return InetAddress.getByName(ip != null ? ip : domain);
|
||||
} catch (UnknownHostException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 NetworkAddress 的字符串表示形式。
|
||||
*
|
||||
* @return 字符串表示
|
||||
*/
|
||||
public String toString() {
|
||||
return isdom
|
||||
? StringUtil.format(
|
||||
"NetworkAddress(IP='{}',type='{}'," + "domain='{}')",
|
||||
ip,
|
||||
IPv,
|
||||
domain
|
||||
)
|
||||
: StringUtil.format("NetworkAddress(IP='{}',type='{}')", ip, IPv);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* Copyright 2025 mingliqiye
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile NetworkEndpoint.java
|
||||
* LastUpdate 2025-09-09 08:37:33
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.network;
|
||||
|
||||
import com.mingliqiye.utils.string.StringUtil;
|
||||
import java.io.Serializable;
|
||||
import java.net.InetSocketAddress;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* IP和端口聚集类,用于封装网络地址与端口信息。
|
||||
* 该类提供了与InetSocketAddress之间的相互转换功能。
|
||||
*
|
||||
* @author MingLiPro
|
||||
* @see InetSocketAddress
|
||||
*/
|
||||
public class NetworkEndpoint implements Serializable {
|
||||
|
||||
@Getter
|
||||
private final NetworkAddress networkAddress;
|
||||
|
||||
@Getter
|
||||
private final NetworkPort networkPort;
|
||||
|
||||
/**
|
||||
* 构造函数,使用指定的网络地址和端口创建NetworkEndpoint实例。
|
||||
*
|
||||
* @param networkAddress 网络地址对象
|
||||
* @param networkPort 网络端口对象
|
||||
* @see NetworkAddress
|
||||
* @see NetworkPort
|
||||
*/
|
||||
private NetworkEndpoint(
|
||||
NetworkAddress networkAddress,
|
||||
NetworkPort networkPort
|
||||
) {
|
||||
this.networkAddress = networkAddress;
|
||||
this.networkPort = networkPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据给定的InetSocketAddress对象创建NetworkEndpoint实例。
|
||||
*
|
||||
* @param address InetSocketAddress对象
|
||||
* @return 新建的NetworkEndpoint实例
|
||||
* @see InetSocketAddress
|
||||
*/
|
||||
public static NetworkEndpoint of(InetSocketAddress address) {
|
||||
return new NetworkEndpoint(
|
||||
new NetworkAddress(address.getHostString()),
|
||||
new NetworkPort(address.getPort())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据主机名或IP字符串和端口号创建NetworkEndpoint实例。
|
||||
*
|
||||
* @param s 主机名或IP地址字符串
|
||||
* @param i 端口号
|
||||
* @return 新建的NetworkEndpoint实例
|
||||
*/
|
||||
public static NetworkEndpoint of(String s, Integer i) {
|
||||
NetworkAddress networkAddress = new NetworkAddress(s);
|
||||
NetworkPort networkPort = new NetworkPort(i);
|
||||
return new NetworkEndpoint(networkAddress, networkPort);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据"host:port"格式的字符串创建NetworkEndpoint实例。
|
||||
* 例如:"127.0.0.1:8080"
|
||||
*
|
||||
* @param s "host:port"格式的字符串
|
||||
* @return 新建的NetworkEndpoint实例
|
||||
*/
|
||||
public static NetworkEndpoint of(String s) {
|
||||
// 查找最后一个冒号的位置,以支持IPv6地址中的冒号
|
||||
int lastColonIndex = s.lastIndexOf(':');
|
||||
return of(
|
||||
s.substring(0, lastColonIndex),
|
||||
Integer.parseInt(s.substring(lastColonIndex + 1))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前NetworkEndpoint转换为InetSocketAddress对象。
|
||||
*
|
||||
* @return 对应的InetSocketAddress对象
|
||||
* @see InetSocketAddress
|
||||
*/
|
||||
public InetSocketAddress toInetSocketAddress() {
|
||||
return new InetSocketAddress(
|
||||
networkAddress.toInetAddress(),
|
||||
networkPort.getPort()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前NetworkEndpoint转换为"host:port"格式的字符串。
|
||||
* 例如:"127.0.0.1:25563"
|
||||
*
|
||||
* @return 格式化后的字符串
|
||||
*/
|
||||
public String toHostPortString() {
|
||||
return StringUtil.format(
|
||||
"{}:{}",
|
||||
networkAddress.getIp(),
|
||||
networkPort.getPort()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回NetworkEndpoint的详细字符串表示形式。
|
||||
* 格式:NetworkEndpoint(IP=...,Port=...,Endpoint=...)
|
||||
*
|
||||
* @return 包含详细信息的字符串
|
||||
*/
|
||||
public String toString() {
|
||||
return StringUtil.format(
|
||||
"NetworkEndpoint(IP={},Port={},Endpoint={})",
|
||||
networkAddress.getIp(),
|
||||
networkPort.getPort(),
|
||||
toHostPortString()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主机名或IP地址字符串。
|
||||
*
|
||||
* @return 主机名或IP地址
|
||||
*/
|
||||
public String getHost() {
|
||||
return networkAddress.getIp();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取端口号。
|
||||
*
|
||||
* @return 端口号
|
||||
*/
|
||||
public Integer getPort() {
|
||||
return networkPort.getPort();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2025 mingliqiye
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile NetworkException.java
|
||||
* LastUpdate 2025-09-09 08:37:33
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.network;
|
||||
|
||||
/**
|
||||
* 网络异常类,用于处理网络相关的运行时异常
|
||||
*
|
||||
* @author MingLiPro
|
||||
*/
|
||||
public class NetworkException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* 构造一个带有指定详细消息的网络异常
|
||||
*
|
||||
* @param message 异常的详细消息
|
||||
*/
|
||||
public NetworkException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造一个网络异常,指定原因异常
|
||||
*
|
||||
* @param e 导致此异常的原因异常
|
||||
*/
|
||||
public NetworkException(Exception e) {
|
||||
super(e);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2025 mingliqiye
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile NetworkPort.java
|
||||
* LastUpdate 2025-09-09 08:37:33
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.network;
|
||||
|
||||
import com.mingliqiye.utils.string.StringUtil;
|
||||
import java.io.Serializable;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 网络端口类
|
||||
*
|
||||
* @author MingLiPro
|
||||
*/
|
||||
public class NetworkPort implements Serializable {
|
||||
|
||||
@Getter
|
||||
private final int port;
|
||||
|
||||
/**
|
||||
* 构造函数,创建一个网络端口对象
|
||||
*
|
||||
* @param port 端口号,必须在0-65535范围内
|
||||
*/
|
||||
public NetworkPort(int port) {
|
||||
testPort(port);
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证端口号是否合法
|
||||
*
|
||||
* @param port 待验证的端口号
|
||||
* @throws NetworkException 当端口号不在合法范围(0-65535)内时抛出异常
|
||||
*/
|
||||
public static void testPort(int port) {
|
||||
// 验证端口号范围是否在0-65535之间
|
||||
if (!(0 <= port && 65535 >= port)) {
|
||||
throw new NetworkException(
|
||||
StringUtil.format("{} 不是正确的端口号", port)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user