From b5e4c7ca6e8803b1bb8c2ed538238c2bba720557 Mon Sep 17 00:00:00 2001 From: jianguo Date: Tue, 14 May 2024 15:12:21 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E6=9B=B4=E6=96=B0Pom=E8=BF=9E=E5=B8=A6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=85=B6=E4=BB=96=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?graalVM=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 86 ++++++++- src/main/java/org/aohe/Main.java | 24 ++- .../aohe/core/sane/utils/RemoteConfig.java | 11 +- .../core/sane/utils/SaneSessionUtils.java | 2 +- src/main/java/org/aohe/core/utils/IpUtil.java | 165 ++++++++++++++++++ .../core/{sane => }/utils/SystemUtils.java | 2 +- src/main/resources/config.setting | 1 + src/main/resources/logback.xml | 41 +++++ 8 files changed, 325 insertions(+), 7 deletions(-) create mode 100644 src/main/java/org/aohe/core/utils/IpUtil.java rename src/main/java/org/aohe/core/{sane => }/utils/SystemUtils.java (97%) create mode 100644 src/main/resources/config.setting create mode 100644 src/main/resources/logback.xml diff --git a/pom.xml b/pom.xml index f9976ab..4db2500 100644 --- a/pom.xml +++ b/pom.xml @@ -12,9 +12,25 @@ 21 21 UTF-8 + + 21 + 23.1.0 + 0.10.1 + org.aohe.Main + + + commons-net + commons-net + 3.10.0 + + + org.apache.logging.log4j + log4j-to-slf4j + 2.23.1 + com.googlecode.jfreesane jfreesane @@ -42,6 +58,74 @@ 1.18.30 - + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + + + org.apache.maven.plugins + maven-assembly-plugin + + ${project.build.finalName} + + + ${start-class} + + + + jar-with-dependencies + + false + + + + make-assembly + package + + single + + + + + + org.graalvm.buildtools + native-maven-plugin + ${native.maven.plugin.version} + + false + false + ${project.artifactId} + ${start-class} + + --no-fallback + -H:-CheckToolchain + + + + + true + + + + true + + + + + compile-no-fork + + package + + + + + \ No newline at end of file diff --git a/src/main/java/org/aohe/Main.java b/src/main/java/org/aohe/Main.java index 06bf688..c6e2386 100644 --- a/src/main/java/org/aohe/Main.java +++ b/src/main/java/org/aohe/Main.java @@ -1,7 +1,29 @@ package org.aohe; +import java.io.IOException; +import java.util.List; + +import au.com.southsky.jfreesane.SaneDevice; +import au.com.southsky.jfreesane.SaneException; +import lombok.extern.slf4j.Slf4j; +import org.aohe.core.sane.utils.SaneSessionUtils; +import org.aohe.core.utils.SystemUtils; + +@Slf4j public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + log.info(" 当前系统类型: {}", SystemUtils.getOsName()); + List deviceList; + try { + deviceList = SaneSessionUtils.getSaneDrivers(); + } catch (IOException | SaneException e) { + log.info(" 获取驱动报错 {}", e.getMessage()); + throw new RuntimeException(e); + } + for (SaneDevice sane : deviceList){ + log.info(sane.getName()); + } + + } } \ No newline at end of file diff --git a/src/main/java/org/aohe/core/sane/utils/RemoteConfig.java b/src/main/java/org/aohe/core/sane/utils/RemoteConfig.java index 5cba167..475d815 100644 --- a/src/main/java/org/aohe/core/sane/utils/RemoteConfig.java +++ b/src/main/java/org/aohe/core/sane/utils/RemoteConfig.java @@ -17,9 +17,14 @@ public class RemoteConfig { } public static RemoteConfig getInstance(){ - String ip = new Setting("config.setting").get("ip"); - if(ip != null){ - return new RemoteConfig(ip); + String setIp; + try { + setIp = new Setting("config.setting").get("ip"); + }catch (Exception e){ + setIp = "127.0.0.1"; + } + if(setIp != null){ + return new RemoteConfig(setIp); } return new RemoteConfig(); } diff --git a/src/main/java/org/aohe/core/sane/utils/SaneSessionUtils.java b/src/main/java/org/aohe/core/sane/utils/SaneSessionUtils.java index 8eb0813..9830442 100644 --- a/src/main/java/org/aohe/core/sane/utils/SaneSessionUtils.java +++ b/src/main/java/org/aohe/core/sane/utils/SaneSessionUtils.java @@ -35,7 +35,7 @@ public class SaneSessionUtils { if (saneSession == null) { synchronized(lock){ if (saneSession == null) { - log.info("jFreeSaneConfig:" + JSON.toJSONString(remoteConfig)); + log.info("jFreeSaneConfig:{}", JSON.toJSONString(remoteConfig)); InetAddress address = InetAddress.getByName(remoteConfig.getIp()); saneSession = SaneSession.withRemoteSane(address); log.info("初始化saneSession完成,状态:{}", saneSession != null); diff --git a/src/main/java/org/aohe/core/utils/IpUtil.java b/src/main/java/org/aohe/core/utils/IpUtil.java new file mode 100644 index 0000000..4c35a88 --- /dev/null +++ b/src/main/java/org/aohe/core/utils/IpUtil.java @@ -0,0 +1,165 @@ +package org.aohe.core.utils; + + +import lombok.extern.slf4j.Slf4j; + +import java.util.HashMap; + +@Slf4j +public class IpUtil { + + + /** + * 输出二进制 + * @param n + */ + public static void binaryToDecimal(long n){ + for(int i = 63;i >= 0; i--) { + System.out.print(n >>> i & 1); + } + System.out.println("---->"); + } + /** + * 把 xx.xx.xx.xx 类型的转为 long 类型的 + * + * @param ip + * @return + */ + public static Long getIpFromString(String ip) + { + // 进制 2^8 = 256 共四段 + long ipLong = 0L; + String ipTemp = ip; + ipLong = Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.'))); + ipTemp = ipTemp.substring(ipTemp.indexOf('.') + 1); + ipLong = ipLong * 256 + + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.'))); + ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1); + ipLong = ipLong * 256 + + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.'))); + ipTemp = ipTemp.substring(ipTemp.indexOf('.') + 1); + ipLong = ipLong * 256 + Long.parseLong(ipTemp); + return ipLong; + } + /** + * 把 long 类型的 Ip 转为一般 Ip 类型:xx.xx.xx.xx + * + * @param ip + * @return + */ + public static String getIpFromLong(Long ip) + { + String s1 = String.valueOf((ip & 4278190080L) / 16777216L); // 255.0.0.0 ~ 1.0.0.0 截取第一个八位 + String s2 = String.valueOf((ip & 16711680L) / 65536L); // 0.255.0.0 ~ 0.1.0.0 截取第二个八位 + String s3 = String.valueOf((ip & 65280L) / 256L); // 0.0.255.0 ~ 0.0.1.0 截取第三个八位 + String s4 = String.valueOf(ip & 255L); // 0.0.0.255 ~ 0.0.0.1 截取第四个八位 + return s1 + "." + s2 + "." + s3 + "." + s4; + } + public static class NetInfo{ + private static final long VALIDATE_DATA = Long.MAX_VALUE>>31; + private static final HashMap NET_MASK = new HashMap<>(); + private static String IP_REGEX = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"; + // ip + private String ipStr; + private Long ip; + // 掩码 + private String maskStr; + private Long mask; + // 网段 + private String segmentStr; + private Long segment; + // 广播地址 + private String broadcastStr; + private Long broadcast; + // 开始 ip + private String startIpStr; + private Long startIp; + // 结束 ip + private String endIpStr; + private Long endIp; + // 可用 ip 数 + private Long count; + // 初始化掩码 + static{ + NET_MASK.put(32,getIpFromLong(VALIDATE_DATA)); + for(int i=1;i<32;i++) { + NET_MASK.put(32-i,getIpFromLong(VALIDATE_DATA << i & VALIDATE_DATA)); + } + } + public NetInfo(String ipStr, String maskStr) { + this.ipStr = ipStr; + this.maskStr = maskStr; + checkParam(); + init(); + } + public NetInfo(String ipStr, int mask) { + this(ipStr,NET_MASK.get(mask)); + } + public void checkParam(){ + if(maskStr == null + || "".equals(maskStr.trim()) + || !NET_MASK.containsValue(maskStr)){ + throw new RuntimeException("掩码无效"); + } + if(ipStr == null || "".equals(ipStr.trim())){ + throw new RuntimeException("ip 地址为空"); + } + // 判断 ip 地址是否与正则表达式匹配 + if (!ipStr.matches(IP_REGEX)) { + throw new RuntimeException("IP 地址不合规范"); + } + } + public void init(){ + this.ip = getIpFromString(ipStr); + this.mask = getIpFromString(maskStr); + // 网段 = ip & 掩码 + this.segment = this.ip & this.mask; + this.segmentStr = getIpFromLong(this.segment); + // 广播地址 网段末尾用 1 补齐 + this.broadcast = this.segment | (~this.mask & VALIDATE_DATA); + binaryToDecimal(broadcast); + this.broadcastStr = getIpFromLong(this.broadcast); + // 开始地址 网段+1 + this.startIp = this.segment +1; + this.startIpStr = getIpFromLong(this.startIp); + // 结束地址 广播地址-1 + this.endIp = this.broadcast -1; + this.endIpStr = getIpFromLong(this.endIp); + /*for( long i = startIp;i< endIp;i++){ + System.out.println(getIpFromLong(i + 1)); + }*/ + this.count = this.broadcast - this.startIp; + } + + public String getIp() { + return ipStr; + } + + public String getMask() { + return maskStr; + } + + public String getSegment() { + return segmentStr; + } + + public String getBroadcast() { + return broadcastStr; + } + + public String getStartIp() { + return startIpStr; + } + + public String getEndIp() { + return endIpStr; + } + + public Long getCount() { + return count; + } + } +} + diff --git a/src/main/java/org/aohe/core/sane/utils/SystemUtils.java b/src/main/java/org/aohe/core/utils/SystemUtils.java similarity index 97% rename from src/main/java/org/aohe/core/sane/utils/SystemUtils.java rename to src/main/java/org/aohe/core/utils/SystemUtils.java index 5cb6ca0..1630389 100644 --- a/src/main/java/org/aohe/core/sane/utils/SystemUtils.java +++ b/src/main/java/org/aohe/core/utils/SystemUtils.java @@ -1,4 +1,4 @@ -package org.aohe.core.sane.utils; +package org.aohe.core.utils; public class SystemUtils { diff --git a/src/main/resources/config.setting b/src/main/resources/config.setting new file mode 100644 index 0000000..5deec15 --- /dev/null +++ b/src/main/resources/config.setting @@ -0,0 +1 @@ +ip = 127.0.0.1 \ No newline at end of file diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..518b540 --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + ${log.pattern} + + + + + + + + + ${log.path}/scanService.%d{yyyy-MM-dd}.log + + 60 + 20GB + + + ${log.pattern} + + + + + + + + + + + + + + +