generated from mingliqiye/lib-tem
This commit is contained in:
parent
006d7404e1
commit
76f2581a9f
@ -1,12 +1,14 @@
|
||||
|
||||
import java.security.MessageDigest
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
plugins {
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
|
||||
plugins {
|
||||
id("java")
|
||||
id("java-library")
|
||||
id("maven-publish")
|
||||
id("com.github.johnrengelman.shadow") version ("8.1.1")
|
||||
}
|
||||
}
|
||||
|
||||
val GROUPSID = project.properties["GROUPSID"] as String
|
||||
val VERSIONS = project.properties["VERSIONS"] as String
|
||||
@ -22,6 +24,8 @@ group = GROUPSID
|
||||
version = VERSIONS
|
||||
|
||||
val libDir = rootDir.resolve("build").resolve("libs")
|
||||
val publicationsDir =
|
||||
rootDir.resolve("build").resolve("publications").resolve("mavenJava")
|
||||
|
||||
java {
|
||||
withSourcesJar()
|
||||
@ -32,7 +36,9 @@ dependencies {
|
||||
testImplementation(platform("org.junit:junit-bom:5.10.0"))
|
||||
testImplementation("org.junit.jupiter:junit-jupiter")
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
implementation("org.checkerframework:checker-qual:3.43.0")
|
||||
implementation("com.mingliqiye:string-utilts:1.0.4")
|
||||
implementation("org.jetbrains:annotations:24.0.0")
|
||||
annotationProcessor("org.jetbrains:annotations:24.0.0")
|
||||
}
|
||||
|
||||
tasks.test {
|
||||
@ -49,15 +55,15 @@ private fun generateHash(file: File, string: String): String {
|
||||
}
|
||||
}
|
||||
|
||||
private fun getHash(file: File) {
|
||||
private fun getHash(outpath: String, file: File) {
|
||||
val md5 = generateHash(file, "MD5")
|
||||
val sha1 = generateHash(file, "SHA-1")
|
||||
val sha256 = generateHash(file, "SHA-256")
|
||||
val sha512 = generateHash(file, "SHA-512")
|
||||
val md5f = File(libDir, file.getName() + ".md5")
|
||||
val sha1f = File(libDir, file.getName() + ".sha1")
|
||||
val sha256f = File(libDir, file.getName() + ".sha256")
|
||||
val sha512f = File(libDir, file.getName() + ".sha512")
|
||||
val md5f = File(outpath, file.getName() + ".md5")
|
||||
val sha1f = File(outpath, file.getName() + ".sha1")
|
||||
val sha256f = File(outpath, file.getName() + ".sha256")
|
||||
val sha512f = File(outpath, file.getName() + ".sha512")
|
||||
md5f.writeText(md5)
|
||||
sha1f.writeText(sha1)
|
||||
sha256f.writeText(sha256)
|
||||
@ -125,11 +131,13 @@ tasks.register("build-jar") {
|
||||
dependsOn(tasks["sourcesJar"])
|
||||
dependsOn(tasks["generatePomFileForMavenJavaPublication"])
|
||||
dependsOn(tasks["generateMetadataFileForMavenJavaPublication"])
|
||||
/*doLast {
|
||||
getHash(File(libDir, jarName))
|
||||
getHash(File(libDir, fatJarName))
|
||||
getHash(File(libDir, srcJarName))
|
||||
}*/
|
||||
doLast {
|
||||
getHash(libDir.toString(),File(libDir, jarName))
|
||||
getHash(libDir.toString(),File(libDir, fatJarName))
|
||||
getHash(libDir.toString(),File(libDir, srcJarName))
|
||||
getHash(publicationsDir.toString(),File(publicationsDir, "module.json"))
|
||||
getHash(publicationsDir.toString(),File(publicationsDir, "pom-default.xml"))
|
||||
}
|
||||
}
|
||||
components {
|
||||
withType<AdhocComponentWithVariants>().configureEach {
|
||||
@ -155,3 +163,4 @@ publishing {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
GROUPSID=com.mingliqiye
|
||||
ARTIFACTID=socket-utilts
|
||||
VERSIONS=0.1
|
||||
MAINCLASS=com.mingliqiye.Main
|
||||
ARTIFACTID=network-endpoint
|
||||
VERSIONS=1.0.2
|
||||
MAINCLASS=com.mingliqiye
|
||||
JDKVERSIONS=1.8
|
||||
|
@ -12,6 +12,7 @@
|
||||
"license": "ISC",
|
||||
"packageManager": "pnpm@10.4.1",
|
||||
"devDependencies": {
|
||||
"prettier-plugin-java": "^2.6.8"
|
||||
"prettier-plugin-java": "^2.7.1",
|
||||
"prettier": "^3.6.2"
|
||||
}
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
rootProject.name = "socket-utilts"
|
||||
rootProject.name = "network-endpoint"
|
||||
|
@ -1,13 +0,0 @@
|
||||
package com.mingliqiye;
|
||||
|
||||
public class Main {
|
||||
/**
|
||||
* @param args []
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
System.out.print("Hello and welcome!");
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
System.out.println("i = " + i);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
package com.mingliqiye.network.endpoint;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.mingliqiye.utils.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 网络地址类
|
||||
*/
|
||||
|
||||
public class NetworkAddress implements Serializable {
|
||||
|
||||
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}$";
|
||||
|
||||
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})$";
|
||||
|
||||
private static final Pattern IPV4_PATTERN = Pattern.compile(IPV4REG);
|
||||
private static final Pattern IPV6_PATTERN = Pattern.compile(IPV6REG);
|
||||
|
||||
public static int IPV6 = 6;
|
||||
public static int IPV4 = 4;
|
||||
|
||||
private int IPv;
|
||||
private String ip;
|
||||
private String domain;
|
||||
private boolean isdom;
|
||||
|
||||
NetworkAddress(String domip) {
|
||||
try {
|
||||
IPv = testIp(domip);
|
||||
ip = domip;
|
||||
} catch (NetworkException e) {
|
||||
try {
|
||||
String ips = getHostIp(domip);
|
||||
IPv = testIp(ips);
|
||||
ip = ips;
|
||||
isdom = true;
|
||||
domain = domip;
|
||||
} catch (UnknownHostException ex) {
|
||||
throw new NetworkException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static NetworkAddress of(String domip) {
|
||||
return new NetworkAddress(domip);
|
||||
}
|
||||
public static NetworkAddress of(InetAddress inetAddress) {
|
||||
return new NetworkAddress(inetAddress.getHostAddress());
|
||||
}
|
||||
public InetAddress toInetAddress(){
|
||||
try {
|
||||
return InetAddress.getByName(domain);
|
||||
} catch (UnknownHostException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从DNS服务器解析域名
|
||||
*
|
||||
* @param domain 域名
|
||||
* @return IP地址
|
||||
* @throws UnknownHostException 没找到对应的IP
|
||||
*/
|
||||
public static String getHostIp(@NotNull String domain) throws UnknownHostException {
|
||||
InetAddress[] addresses = InetAddress.getAllByName(domain.trim());
|
||||
return addresses[0].getHostAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测IP地址类型
|
||||
*
|
||||
* @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();
|
||||
if (IPV4_PATTERN.matcher(trimmedIp).matches()) {
|
||||
return 4;
|
||||
}
|
||||
if (IPV6_PATTERN.matcher(trimmedIp).matches()) {
|
||||
return 6;
|
||||
}
|
||||
throw new NetworkException(
|
||||
StringUtil.format("[{}] 不是有效的IPv4或IPv6地址", ip));
|
||||
}
|
||||
|
||||
public int getIPv() {
|
||||
return IPv;
|
||||
}
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return isdom?StringUtil.format("NetworkAddress(IP='{}',type='{}'," +
|
||||
"domain='{}')",ip,IPv,domain):
|
||||
StringUtil.format("NetworkAddress(IP='{}',type='{}')",ip,IPv);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,98 @@
|
||||
package com.mingliqiye.network.endpoint;
|
||||
|
||||
import com.mingliqiye.utils.StringUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
/**
|
||||
* IP和端口聚集类
|
||||
*
|
||||
* @see InetSocketAddress
|
||||
*/
|
||||
public class NetworkEndpoint implements Serializable {
|
||||
|
||||
private final NetworkAddress networkAddress;
|
||||
private final NetworkPort networkPort;
|
||||
|
||||
/**
|
||||
* @param networkAddress 地址
|
||||
* @param networkPort 端口
|
||||
* @see NetworkAddress
|
||||
* @see NetworkPort
|
||||
*/
|
||||
private NetworkEndpoint(
|
||||
NetworkAddress networkAddress, NetworkPort networkPort) {
|
||||
this.networkAddress = networkAddress;
|
||||
this.networkPort = networkPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换到 InetSocketAddress
|
||||
*
|
||||
* @return InetSocketAddress
|
||||
* @see InetSocketAddress
|
||||
*/
|
||||
public InetSocketAddress toInetSocketAddress() {
|
||||
return new InetSocketAddress(networkAddress.toInetAddress(),
|
||||
networkPort.getPort());
|
||||
}
|
||||
|
||||
/**
|
||||
* 从InetSocketAddress转换
|
||||
*
|
||||
* @param address InetSocketAddress
|
||||
* @return NetworkEndpoint
|
||||
* @see InetSocketAddress
|
||||
*/
|
||||
public static NetworkEndpoint of(InetSocketAddress address) {
|
||||
return new NetworkEndpoint(new NetworkAddress(address.getHostString()),
|
||||
new NetworkPort(address.getPort()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 127.0.0.1:25563<br/>
|
||||
*
|
||||
* @return String({ ip } : { port })
|
||||
*/
|
||||
public String toHostPortString() {
|
||||
return StringUtil.format("{}:{}", networkAddress.getIp(),
|
||||
networkPort.getPort());
|
||||
}
|
||||
|
||||
public static NetworkEndpoint of(String s, Integer i) {
|
||||
NetworkAddress networkAddress = new NetworkAddress(s);
|
||||
NetworkPort networkPort = new NetworkPort(i);
|
||||
return new NetworkEndpoint(networkAddress, networkPort);
|
||||
}
|
||||
|
||||
public static NetworkEndpoint of(String s) {
|
||||
int lastColonIndex = s.lastIndexOf(':');
|
||||
return of(s.substring(0, lastColonIndex),
|
||||
Integer.parseInt(s.substring(lastColonIndex + 1)));
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
return StringUtil.format("NetworkEndpoint(IP={},Port={},Endpoint={})",
|
||||
networkAddress.getIp(), networkPort.getPort(),
|
||||
toHostPortString());
|
||||
}
|
||||
|
||||
public NetworkAddress getNetworkAddress() {
|
||||
return networkAddress;
|
||||
}
|
||||
|
||||
public NetworkPort getNetworkPort() {
|
||||
return networkPort;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return networkAddress.getIp();
|
||||
}
|
||||
|
||||
public Integer getPort() {
|
||||
return networkPort.getPort();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.mingliqiye.network.endpoint;
|
||||
|
||||
public class NetworkException extends RuntimeException {
|
||||
public NetworkException(String message) {
|
||||
super(message);
|
||||
}
|
||||
public NetworkException(Exception e){
|
||||
super(e);
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.mingliqiye.network.endpoint;
|
||||
|
||||
import com.mingliqiye.utils.StringUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 网络端口类
|
||||
*/
|
||||
|
||||
public class NetworkPort implements Serializable {
|
||||
|
||||
private final int port;
|
||||
|
||||
public static void testPort(int port) {
|
||||
if (!(0 <= port && 65535 >= port)) {
|
||||
throw new NetworkException(
|
||||
StringUtil.format("{} 不是正确的端口号", port));
|
||||
}
|
||||
}
|
||||
|
||||
public NetworkPort(int port) {
|
||||
testPort(port);
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user