diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/sensorhub-core/pom.xml b/sensorhub-core/pom.xml index ae68ea8..2a77c72 100644 --- a/sensorhub-core/pom.xml +++ b/sensorhub-core/pom.xml @@ -58,7 +58,7 @@ org.bouncycastle bcprov-jdk15to18 - 1.64 + 1.71 @@ -75,14 +75,6 @@ 0.0.1-SNAPSHOT - - - org.reflections - reflections - 0.9.10 - - - diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/sensorhub-core/pom.xml b/sensorhub-core/pom.xml index ae68ea8..2a77c72 100644 --- a/sensorhub-core/pom.xml +++ b/sensorhub-core/pom.xml @@ -58,7 +58,7 @@ org.bouncycastle bcprov-jdk15to18 - 1.64 + 1.71 @@ -75,14 +75,6 @@ 0.0.1-SNAPSHOT - - - org.reflections - reflections - 0.9.10 - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java deleted file mode 100644 index 80c3458..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.casic.missiles.codec; - -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import lombok.extern.slf4j.Slf4j; -import org.reflections.Reflections; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -@Slf4j -public class ClassUtil { - -// public static List get(Class clazz){ -// List abstractPreprocessings = new ArrayList<>(); -// try{ -// String pk = clazz.getPackage().getName(); -// String path = pk.replace('.', '/'); -// //获取该路径下所有类 -// Reflections reflections = new Reflections(path); -// //获取继承了ISuperClass的所有类 -// Set> classSet = reflections.getSubTypesOf(clazz); -// -// for (Class clazz : classSet) { -// // 实例化获取到的类 -// T obj = clazz.newInstance(); -// // TODO 自己的处理逻辑 -// abstractPreprocessings.add(obj); -// } -// }catch (IllegalAccessException iae){ -// -// }catch (InstantiationException iae){ -// -// } -// return abstractPreprocessings; -// } - -} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/sensorhub-core/pom.xml b/sensorhub-core/pom.xml index ae68ea8..2a77c72 100644 --- a/sensorhub-core/pom.xml +++ b/sensorhub-core/pom.xml @@ -58,7 +58,7 @@ org.bouncycastle bcprov-jdk15to18 - 1.64 + 1.71 @@ -75,14 +75,6 @@ 0.0.1-SNAPSHOT - - - org.reflections - reflections - 0.9.10 - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java deleted file mode 100644 index 80c3458..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.casic.missiles.codec; - -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import lombok.extern.slf4j.Slf4j; -import org.reflections.Reflections; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -@Slf4j -public class ClassUtil { - -// public static List get(Class clazz){ -// List abstractPreprocessings = new ArrayList<>(); -// try{ -// String pk = clazz.getPackage().getName(); -// String path = pk.replace('.', '/'); -// //获取该路径下所有类 -// Reflections reflections = new Reflections(path); -// //获取继承了ISuperClass的所有类 -// Set> classSet = reflections.getSubTypesOf(clazz); -// -// for (Class clazz : classSet) { -// // 实例化获取到的类 -// T obj = clazz.newInstance(); -// // TODO 自己的处理逻辑 -// abstractPreprocessings.add(obj); -// } -// }catch (IllegalAccessException iae){ -// -// }catch (InstantiationException iae){ -// -// } -// return abstractPreprocessings; -// } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java deleted file mode 100644 index 3658376..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.casic.missiles.codec.predecodec; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - -import java.util.List; - -public class AbstractPreprocessing extends ByteToMessageDecoder { - - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { - list.add(byteBuf); - } -} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/sensorhub-core/pom.xml b/sensorhub-core/pom.xml index ae68ea8..2a77c72 100644 --- a/sensorhub-core/pom.xml +++ b/sensorhub-core/pom.xml @@ -58,7 +58,7 @@ org.bouncycastle bcprov-jdk15to18 - 1.64 + 1.71 @@ -75,14 +75,6 @@ 0.0.1-SNAPSHOT - - - org.reflections - reflections - 0.9.10 - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java deleted file mode 100644 index 80c3458..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.casic.missiles.codec; - -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import lombok.extern.slf4j.Slf4j; -import org.reflections.Reflections; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -@Slf4j -public class ClassUtil { - -// public static List get(Class clazz){ -// List abstractPreprocessings = new ArrayList<>(); -// try{ -// String pk = clazz.getPackage().getName(); -// String path = pk.replace('.', '/'); -// //获取该路径下所有类 -// Reflections reflections = new Reflections(path); -// //获取继承了ISuperClass的所有类 -// Set> classSet = reflections.getSubTypesOf(clazz); -// -// for (Class clazz : classSet) { -// // 实例化获取到的类 -// T obj = clazz.newInstance(); -// // TODO 自己的处理逻辑 -// abstractPreprocessings.add(obj); -// } -// }catch (IllegalAccessException iae){ -// -// }catch (InstantiationException iae){ -// -// } -// return abstractPreprocessings; -// } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java deleted file mode 100644 index 3658376..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.casic.missiles.codec.predecodec; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - -import java.util.List; - -public class AbstractPreprocessing extends ByteToMessageDecoder { - - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { - list.add(byteBuf); - } -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java deleted file mode 100644 index ae08584..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; - -import java.nio.charset.Charset; -import java.util.List; - -/** - * @description: 将从接口取到的数据编码 - * @author: cz - * @create: 2023-05-04 15:15 - **/ -@Slf4j -@Component -public class AepPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public AepPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public AepPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析 预处理 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - }else { - out.add(msg); - } - } - -} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/sensorhub-core/pom.xml b/sensorhub-core/pom.xml index ae68ea8..2a77c72 100644 --- a/sensorhub-core/pom.xml +++ b/sensorhub-core/pom.xml @@ -58,7 +58,7 @@ org.bouncycastle bcprov-jdk15to18 - 1.64 + 1.71 @@ -75,14 +75,6 @@ 0.0.1-SNAPSHOT - - - org.reflections - reflections - 0.9.10 - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java deleted file mode 100644 index 80c3458..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.casic.missiles.codec; - -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import lombok.extern.slf4j.Slf4j; -import org.reflections.Reflections; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -@Slf4j -public class ClassUtil { - -// public static List get(Class clazz){ -// List abstractPreprocessings = new ArrayList<>(); -// try{ -// String pk = clazz.getPackage().getName(); -// String path = pk.replace('.', '/'); -// //获取该路径下所有类 -// Reflections reflections = new Reflections(path); -// //获取继承了ISuperClass的所有类 -// Set> classSet = reflections.getSubTypesOf(clazz); -// -// for (Class clazz : classSet) { -// // 实例化获取到的类 -// T obj = clazz.newInstance(); -// // TODO 自己的处理逻辑 -// abstractPreprocessings.add(obj); -// } -// }catch (IllegalAccessException iae){ -// -// }catch (InstantiationException iae){ -// -// } -// return abstractPreprocessings; -// } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java deleted file mode 100644 index 3658376..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.casic.missiles.codec.predecodec; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - -import java.util.List; - -public class AbstractPreprocessing extends ByteToMessageDecoder { - - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { - list.add(byteBuf); - } -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java deleted file mode 100644 index ae08584..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; - -import java.nio.charset.Charset; -import java.util.List; - -/** - * @description: 将从接口取到的数据编码 - * @author: cz - * @create: 2023-05-04 15:15 - **/ -@Slf4j -@Component -public class AepPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public AepPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public AepPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析 预处理 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - }else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java deleted file mode 100644 index 6ab1368..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import java.nio.charset.Charset; -import java.util.List; - -@Slf4j -@Component -public class NbPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public NbPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public NbPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析,预处理base编码 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - } else { - out.add(msg); - } - } - -} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/sensorhub-core/pom.xml b/sensorhub-core/pom.xml index ae68ea8..2a77c72 100644 --- a/sensorhub-core/pom.xml +++ b/sensorhub-core/pom.xml @@ -58,7 +58,7 @@ org.bouncycastle bcprov-jdk15to18 - 1.64 + 1.71 @@ -75,14 +75,6 @@ 0.0.1-SNAPSHOT - - - org.reflections - reflections - 0.9.10 - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java deleted file mode 100644 index 80c3458..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.casic.missiles.codec; - -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import lombok.extern.slf4j.Slf4j; -import org.reflections.Reflections; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -@Slf4j -public class ClassUtil { - -// public static List get(Class clazz){ -// List abstractPreprocessings = new ArrayList<>(); -// try{ -// String pk = clazz.getPackage().getName(); -// String path = pk.replace('.', '/'); -// //获取该路径下所有类 -// Reflections reflections = new Reflections(path); -// //获取继承了ISuperClass的所有类 -// Set> classSet = reflections.getSubTypesOf(clazz); -// -// for (Class clazz : classSet) { -// // 实例化获取到的类 -// T obj = clazz.newInstance(); -// // TODO 自己的处理逻辑 -// abstractPreprocessings.add(obj); -// } -// }catch (IllegalAccessException iae){ -// -// }catch (InstantiationException iae){ -// -// } -// return abstractPreprocessings; -// } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java deleted file mode 100644 index 3658376..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.casic.missiles.codec.predecodec; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - -import java.util.List; - -public class AbstractPreprocessing extends ByteToMessageDecoder { - - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { - list.add(byteBuf); - } -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java deleted file mode 100644 index ae08584..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; - -import java.nio.charset.Charset; -import java.util.List; - -/** - * @description: 将从接口取到的数据编码 - * @author: cz - * @create: 2023-05-04 15:15 - **/ -@Slf4j -@Component -public class AepPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public AepPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public AepPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析 预处理 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - }else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java deleted file mode 100644 index 6ab1368..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import java.nio.charset.Charset; -import java.util.List; - -@Slf4j -@Component -public class NbPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public NbPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public NbPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析,预处理base编码 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - } else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java index 21c7b07..9f9e279 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java @@ -40,8 +40,6 @@ */ @Autowired private SensorhubProperties sensorhubProperties; - @Autowired - private SensorhubServerChannelInitialHandler sensorhubHandler; public SensorhubServer(SensorhubProperties sensorhubProperties) { this.sensorhubProperties = sensorhubProperties; @@ -76,7 +74,7 @@ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) // 设置EventLoopGroup .channel(NioServerSocketChannel.class) // 指明新的Channel的类型 - .childHandler(sensorhubHandler) // 指定ChannelHandler + .childHandler(new SensorhubServerChannelInitialHandler()) // 指定ChannelHandler .option(ChannelOption.SO_BACKLOG, 1024) // 设置的ServerChannel的一些选项 .childOption(ChannelOption.SO_KEEPALIVE, true); // 当SO_KEEPALIVE=true的时候,服务端可以探测客户端的连接是否还存活着,如果客户端关闭了,那么服务端的连接可以关闭掉,释放资源 // 绑定端口,开始接收进来的连接 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/sensorhub-core/pom.xml b/sensorhub-core/pom.xml index ae68ea8..2a77c72 100644 --- a/sensorhub-core/pom.xml +++ b/sensorhub-core/pom.xml @@ -58,7 +58,7 @@ org.bouncycastle bcprov-jdk15to18 - 1.64 + 1.71 @@ -75,14 +75,6 @@ 0.0.1-SNAPSHOT - - - org.reflections - reflections - 0.9.10 - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java deleted file mode 100644 index 80c3458..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.casic.missiles.codec; - -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import lombok.extern.slf4j.Slf4j; -import org.reflections.Reflections; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -@Slf4j -public class ClassUtil { - -// public static List get(Class clazz){ -// List abstractPreprocessings = new ArrayList<>(); -// try{ -// String pk = clazz.getPackage().getName(); -// String path = pk.replace('.', '/'); -// //获取该路径下所有类 -// Reflections reflections = new Reflections(path); -// //获取继承了ISuperClass的所有类 -// Set> classSet = reflections.getSubTypesOf(clazz); -// -// for (Class clazz : classSet) { -// // 实例化获取到的类 -// T obj = clazz.newInstance(); -// // TODO 自己的处理逻辑 -// abstractPreprocessings.add(obj); -// } -// }catch (IllegalAccessException iae){ -// -// }catch (InstantiationException iae){ -// -// } -// return abstractPreprocessings; -// } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java deleted file mode 100644 index 3658376..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.casic.missiles.codec.predecodec; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - -import java.util.List; - -public class AbstractPreprocessing extends ByteToMessageDecoder { - - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { - list.add(byteBuf); - } -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java deleted file mode 100644 index ae08584..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; - -import java.nio.charset.Charset; -import java.util.List; - -/** - * @description: 将从接口取到的数据编码 - * @author: cz - * @create: 2023-05-04 15:15 - **/ -@Slf4j -@Component -public class AepPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public AepPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public AepPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析 预处理 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - }else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java deleted file mode 100644 index 6ab1368..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import java.nio.charset.Charset; -import java.util.List; - -@Slf4j -@Component -public class NbPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public NbPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public NbPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析,预处理base编码 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - } else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java index 21c7b07..9f9e279 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java @@ -40,8 +40,6 @@ */ @Autowired private SensorhubProperties sensorhubProperties; - @Autowired - private SensorhubServerChannelInitialHandler sensorhubHandler; public SensorhubServer(SensorhubProperties sensorhubProperties) { this.sensorhubProperties = sensorhubProperties; @@ -76,7 +74,7 @@ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) // 设置EventLoopGroup .channel(NioServerSocketChannel.class) // 指明新的Channel的类型 - .childHandler(sensorhubHandler) // 指定ChannelHandler + .childHandler(new SensorhubServerChannelInitialHandler()) // 指定ChannelHandler .option(ChannelOption.SO_BACKLOG, 1024) // 设置的ServerChannel的一些选项 .childOption(ChannelOption.SO_KEEPALIVE, true); // 当SO_KEEPALIVE=true的时候,服务端可以探测客户端的连接是否还存活着,如果客户端关闭了,那么服务端的连接可以关闭掉,释放资源 // 绑定端口,开始接收进来的连接 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java index 827744f..24a6fef 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java @@ -1,15 +1,13 @@ package com.casic.missiles.netty.handler; -import com.casic.missiles.codec.SensorhubCodec; +import com.casic.missiles.util.ClassUtil; import com.casic.missiles.codec.SensorhubDecoder; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; +import com.casic.missiles.predecodec.AbstractPreprocessing; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import java.util.List; @@ -17,21 +15,18 @@ * @author cz * @date 2023-06-05 */ -@Component public class SensorhubServerChannelInitialHandler extends ChannelInitializer { - @Autowired - private List abstractPreprocessingList; - @Override protected void initChannel(SocketChannel socketChannel) { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); + //添加前置处理器 + ClassUtil classUtil=new ClassUtil(); + List abstractPreprocessingList=classUtil.getSubClassList(AbstractPreprocessing.class); for (AbstractPreprocessing abstractPreprocessing:abstractPreprocessingList) { pipeline.addLast(abstractPreprocessing); } -// pipeline.addLast(new LengthFieldBasedFrameDecoder(1024*100, 0, 2, 0, 2)); - pipeline.addLast("codec", new SensorhubCodec()); pipeline.addLast(new SensorhubDecoder()); pipeline.addLast(new SensorhubServerHandler()); // 心跳续约 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/sensorhub-core/pom.xml b/sensorhub-core/pom.xml index ae68ea8..2a77c72 100644 --- a/sensorhub-core/pom.xml +++ b/sensorhub-core/pom.xml @@ -58,7 +58,7 @@ org.bouncycastle bcprov-jdk15to18 - 1.64 + 1.71 @@ -75,14 +75,6 @@ 0.0.1-SNAPSHOT - - - org.reflections - reflections - 0.9.10 - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java deleted file mode 100644 index 80c3458..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.casic.missiles.codec; - -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import lombok.extern.slf4j.Slf4j; -import org.reflections.Reflections; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -@Slf4j -public class ClassUtil { - -// public static List get(Class clazz){ -// List abstractPreprocessings = new ArrayList<>(); -// try{ -// String pk = clazz.getPackage().getName(); -// String path = pk.replace('.', '/'); -// //获取该路径下所有类 -// Reflections reflections = new Reflections(path); -// //获取继承了ISuperClass的所有类 -// Set> classSet = reflections.getSubTypesOf(clazz); -// -// for (Class clazz : classSet) { -// // 实例化获取到的类 -// T obj = clazz.newInstance(); -// // TODO 自己的处理逻辑 -// abstractPreprocessings.add(obj); -// } -// }catch (IllegalAccessException iae){ -// -// }catch (InstantiationException iae){ -// -// } -// return abstractPreprocessings; -// } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java deleted file mode 100644 index 3658376..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.casic.missiles.codec.predecodec; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - -import java.util.List; - -public class AbstractPreprocessing extends ByteToMessageDecoder { - - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { - list.add(byteBuf); - } -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java deleted file mode 100644 index ae08584..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; - -import java.nio.charset.Charset; -import java.util.List; - -/** - * @description: 将从接口取到的数据编码 - * @author: cz - * @create: 2023-05-04 15:15 - **/ -@Slf4j -@Component -public class AepPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public AepPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public AepPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析 预处理 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - }else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java deleted file mode 100644 index 6ab1368..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import java.nio.charset.Charset; -import java.util.List; - -@Slf4j -@Component -public class NbPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public NbPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public NbPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析,预处理base编码 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - } else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java index 21c7b07..9f9e279 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java @@ -40,8 +40,6 @@ */ @Autowired private SensorhubProperties sensorhubProperties; - @Autowired - private SensorhubServerChannelInitialHandler sensorhubHandler; public SensorhubServer(SensorhubProperties sensorhubProperties) { this.sensorhubProperties = sensorhubProperties; @@ -76,7 +74,7 @@ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) // 设置EventLoopGroup .channel(NioServerSocketChannel.class) // 指明新的Channel的类型 - .childHandler(sensorhubHandler) // 指定ChannelHandler + .childHandler(new SensorhubServerChannelInitialHandler()) // 指定ChannelHandler .option(ChannelOption.SO_BACKLOG, 1024) // 设置的ServerChannel的一些选项 .childOption(ChannelOption.SO_KEEPALIVE, true); // 当SO_KEEPALIVE=true的时候,服务端可以探测客户端的连接是否还存活着,如果客户端关闭了,那么服务端的连接可以关闭掉,释放资源 // 绑定端口,开始接收进来的连接 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java index 827744f..24a6fef 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java @@ -1,15 +1,13 @@ package com.casic.missiles.netty.handler; -import com.casic.missiles.codec.SensorhubCodec; +import com.casic.missiles.util.ClassUtil; import com.casic.missiles.codec.SensorhubDecoder; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; +import com.casic.missiles.predecodec.AbstractPreprocessing; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import java.util.List; @@ -17,21 +15,18 @@ * @author cz * @date 2023-06-05 */ -@Component public class SensorhubServerChannelInitialHandler extends ChannelInitializer { - @Autowired - private List abstractPreprocessingList; - @Override protected void initChannel(SocketChannel socketChannel) { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); + //添加前置处理器 + ClassUtil classUtil=new ClassUtil(); + List abstractPreprocessingList=classUtil.getSubClassList(AbstractPreprocessing.class); for (AbstractPreprocessing abstractPreprocessing:abstractPreprocessingList) { pipeline.addLast(abstractPreprocessing); } -// pipeline.addLast(new LengthFieldBasedFrameDecoder(1024*100, 0, 2, 0, 2)); - pipeline.addLast("codec", new SensorhubCodec()); pipeline.addLast(new SensorhubDecoder()); pipeline.addLast(new SensorhubServerHandler()); // 心跳续约 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java new file mode 100644 index 0000000..27c2838 --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java @@ -0,0 +1,179 @@ +package com.casic.missiles.parser.safe.impl; + +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.crypto.symmetric.SM4; +import cn.hutool.crypto.symmetric.SymmetricCrypto; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.ByteBufUtil; +import lombok.extern.slf4j.Slf4j; +import org.bouncycastle.crypto.engines.SM4Engine; +import org.bouncycastle.crypto.params.KeyParameter; +import org.bouncycastle.util.encoders.Hex; + +import static cn.hutool.crypto.Mode.ECB; +import static cn.hutool.crypto.Padding.NoPadding; + +/** + * SM4算法工具类 + */ +@Slf4j +public class Sm4 { + + //加解密的字节快大小 + public static final int BlockSize = 16; + + /** + * SM4ECB加密算法,对内 + * + * @param in 待加密字节数组 + * @param keyBytes 密钥 + * @return + */ + private static byte[] ecb_encrypt(byte[] in, byte[] keyBytes) { + + + SM4Engine sm4Engine = new SM4Engine(); + sm4Engine.init(true, new KeyParameter(keyBytes)); + byte[] out = new byte[in.length]; + int times = in.length / BlockSize; + for (int i = 0; i < times; i++) { + sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); + } + return out; + } + + + /** + * SM4ECB加密算法 + * + * @param in 待加密字节数组 + * @param keyBytes 密钥 + * @return + */ + public static String EcbEncrypt(byte[] in, byte[] keyBytes) { + byte[] out = ecb_encrypt(in, keyBytes); + String cipher = Hex.toHexString(out); + return cipher; + } + + + /** + * hutool 解密SM4 + * + * @param encodeContent + * @return + */ + public static String decode(String encodeContent) { + String key = "24ad182507479a5da3ad94d9d78ea203"; + try { + SymmetricCrypto sm4 = new SM4(ECB, NoPadding, key.getBytes()); + String decryptStr = sm4.decryptStr(encodeContent, CharsetUtil.CHARSET_UTF_8); + return decryptStr; + } catch (Exception e) { + e.printStackTrace(); + log.error("SM4解密失败"); + } + return null; + } + + + /** + * SM4的ECB加密算法 + * + * @param content 待加密字符串, hex字符串 + * @param key 密钥 + * @return + */ + public static String EcbEncrypt(String content, String key) { + byte[] in = Hex.decode(content); + byte[] keyBytes = Hex.decode(key); + + String cipher = EcbEncrypt(in, keyBytes); + return cipher; + } + + /** + * SM4的ECB解密算法,对内 + * + * @param in 输入的密文字符串 + * @param keyBytes 密钥 + * @return + */ + private static byte[] ecb_decrypt(byte[] in, byte[] keyBytes) { + SM4Engine sm4Engine = new SM4Engine(); + sm4Engine.init(false, new KeyParameter(keyBytes)); + int inLen = in.length; + byte[] out = new byte[inLen]; + + int times = inLen / BlockSize; + + for (int i = 0; i < times; i++) { + sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); + } + + return out; + } + + /** + * SM4的ECB解密算法,对外 + * + * @param in 输入的密文字节数组 + * @param keyBytes 密钥 + * @return + */ + public static String EcbDecrypt(byte[] in, byte[] keyBytes) { + byte[] out = ecb_decrypt(in, keyBytes); + String plain = Hex.toHexString(out); + return plain; + } + + /** + * SM4的ECB解密算法 + * + * @param cipher 输入的密文字符串 + * @param key 密钥 + * @return + */ + public static String EcbDecrypt(String cipher, String key) { + byte[] in = Hex.decode(cipher); + byte[] keyBytes = Hex.decode(key); + + String plain = EcbDecrypt(in, keyBytes); + return plain; + } + + /** + * 主函数 + * + * @param args + */ + public static void main(String[] args) { + byte[] ciphertext = { + (byte) 0xE9, 0x1C, (byte) 0xCD, 0x53, 0x02, (byte) 0xD7, 0x0C, (byte) 0x9B, (byte) 0x96, 0x30, 0x62, (byte) 0xE8, (byte) 0xFB, (byte) 0xEF, (byte) 0xB9, (byte) 0xFF, + (byte) 0xDE, 0x19, (byte) 0x80, 0x35, 0x3E, (byte) 0x90, 0x4F, 0x2F, 0x12, 0x2B, 0x19, (byte) 0x8B, 0x6C, 0x7A, (byte) 0x85, (byte) 0xA1, + (byte) 0xB1, (byte) 0x86, (byte) 0xA3, 0x7A, 0x46, 0x0C, 0x2F, (byte) 0x94, 0x4F, 0x7F, 0x2C, 0x56, 0x14, (byte) 0xF3, (byte) 0x89, (byte) 0xC3, + 0x3F, 0x20, (byte) 0xF9, (byte) 0x87, (byte) 0xA1, 0x60, (byte) 0xD9, (byte) 0xA8, (byte) 0xFD, (byte) 0xD7, (byte) 0x9B, 0x4B, 0x26, 0x06, 0x30, 0x44}; + ByteBuf in = ByteBufAllocator.DEFAULT.heapBuffer(); + in.writeBytes(ciphertext); + + byte[] keyByte = { + 0x24, (byte) 0xad, 0x18, 0x25, 0x07, 0x47, (byte) 0x9a, 0x5d, (byte) 0xa3, (byte) 0xad, (byte) 0x94, (byte) 0xd9, (byte) 0xd7, (byte) 0x8e, (byte) 0xa2, 0x03 + }; + ByteBuf key = ByteBufAllocator.DEFAULT.heapBuffer(); + key.writeBytes(ciphertext); + String resCipherData = decode(ByteBufUtil.hexDump(in)); + ByteBuf out = ByteBufAllocator.DEFAULT.heapBuffer(); +// out.writeBytes(resCipherData); +// System.out.println(hexBytes); + System.out.println(resCipherData.toString()); +// String resPlainData = EcbDecrypt(resCipherData, key); +// System.out.println("原始明文:" + data); +// System.out.println("解密后明文:" + resPlainData); + } + +} + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/sensorhub-core/pom.xml b/sensorhub-core/pom.xml index ae68ea8..2a77c72 100644 --- a/sensorhub-core/pom.xml +++ b/sensorhub-core/pom.xml @@ -58,7 +58,7 @@ org.bouncycastle bcprov-jdk15to18 - 1.64 + 1.71 @@ -75,14 +75,6 @@ 0.0.1-SNAPSHOT - - - org.reflections - reflections - 0.9.10 - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java deleted file mode 100644 index 80c3458..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.casic.missiles.codec; - -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import lombok.extern.slf4j.Slf4j; -import org.reflections.Reflections; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -@Slf4j -public class ClassUtil { - -// public static List get(Class clazz){ -// List abstractPreprocessings = new ArrayList<>(); -// try{ -// String pk = clazz.getPackage().getName(); -// String path = pk.replace('.', '/'); -// //获取该路径下所有类 -// Reflections reflections = new Reflections(path); -// //获取继承了ISuperClass的所有类 -// Set> classSet = reflections.getSubTypesOf(clazz); -// -// for (Class clazz : classSet) { -// // 实例化获取到的类 -// T obj = clazz.newInstance(); -// // TODO 自己的处理逻辑 -// abstractPreprocessings.add(obj); -// } -// }catch (IllegalAccessException iae){ -// -// }catch (InstantiationException iae){ -// -// } -// return abstractPreprocessings; -// } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java deleted file mode 100644 index 3658376..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.casic.missiles.codec.predecodec; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - -import java.util.List; - -public class AbstractPreprocessing extends ByteToMessageDecoder { - - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { - list.add(byteBuf); - } -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java deleted file mode 100644 index ae08584..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; - -import java.nio.charset.Charset; -import java.util.List; - -/** - * @description: 将从接口取到的数据编码 - * @author: cz - * @create: 2023-05-04 15:15 - **/ -@Slf4j -@Component -public class AepPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public AepPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public AepPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析 预处理 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - }else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java deleted file mode 100644 index 6ab1368..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import java.nio.charset.Charset; -import java.util.List; - -@Slf4j -@Component -public class NbPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public NbPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public NbPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析,预处理base编码 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - } else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java index 21c7b07..9f9e279 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java @@ -40,8 +40,6 @@ */ @Autowired private SensorhubProperties sensorhubProperties; - @Autowired - private SensorhubServerChannelInitialHandler sensorhubHandler; public SensorhubServer(SensorhubProperties sensorhubProperties) { this.sensorhubProperties = sensorhubProperties; @@ -76,7 +74,7 @@ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) // 设置EventLoopGroup .channel(NioServerSocketChannel.class) // 指明新的Channel的类型 - .childHandler(sensorhubHandler) // 指定ChannelHandler + .childHandler(new SensorhubServerChannelInitialHandler()) // 指定ChannelHandler .option(ChannelOption.SO_BACKLOG, 1024) // 设置的ServerChannel的一些选项 .childOption(ChannelOption.SO_KEEPALIVE, true); // 当SO_KEEPALIVE=true的时候,服务端可以探测客户端的连接是否还存活着,如果客户端关闭了,那么服务端的连接可以关闭掉,释放资源 // 绑定端口,开始接收进来的连接 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java index 827744f..24a6fef 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java @@ -1,15 +1,13 @@ package com.casic.missiles.netty.handler; -import com.casic.missiles.codec.SensorhubCodec; +import com.casic.missiles.util.ClassUtil; import com.casic.missiles.codec.SensorhubDecoder; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; +import com.casic.missiles.predecodec.AbstractPreprocessing; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import java.util.List; @@ -17,21 +15,18 @@ * @author cz * @date 2023-06-05 */ -@Component public class SensorhubServerChannelInitialHandler extends ChannelInitializer { - @Autowired - private List abstractPreprocessingList; - @Override protected void initChannel(SocketChannel socketChannel) { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); + //添加前置处理器 + ClassUtil classUtil=new ClassUtil(); + List abstractPreprocessingList=classUtil.getSubClassList(AbstractPreprocessing.class); for (AbstractPreprocessing abstractPreprocessing:abstractPreprocessingList) { pipeline.addLast(abstractPreprocessing); } -// pipeline.addLast(new LengthFieldBasedFrameDecoder(1024*100, 0, 2, 0, 2)); - pipeline.addLast("codec", new SensorhubCodec()); pipeline.addLast(new SensorhubDecoder()); pipeline.addLast(new SensorhubServerHandler()); // 心跳续约 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java new file mode 100644 index 0000000..27c2838 --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java @@ -0,0 +1,179 @@ +package com.casic.missiles.parser.safe.impl; + +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.crypto.symmetric.SM4; +import cn.hutool.crypto.symmetric.SymmetricCrypto; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.ByteBufUtil; +import lombok.extern.slf4j.Slf4j; +import org.bouncycastle.crypto.engines.SM4Engine; +import org.bouncycastle.crypto.params.KeyParameter; +import org.bouncycastle.util.encoders.Hex; + +import static cn.hutool.crypto.Mode.ECB; +import static cn.hutool.crypto.Padding.NoPadding; + +/** + * SM4算法工具类 + */ +@Slf4j +public class Sm4 { + + //加解密的字节快大小 + public static final int BlockSize = 16; + + /** + * SM4ECB加密算法,对内 + * + * @param in 待加密字节数组 + * @param keyBytes 密钥 + * @return + */ + private static byte[] ecb_encrypt(byte[] in, byte[] keyBytes) { + + + SM4Engine sm4Engine = new SM4Engine(); + sm4Engine.init(true, new KeyParameter(keyBytes)); + byte[] out = new byte[in.length]; + int times = in.length / BlockSize; + for (int i = 0; i < times; i++) { + sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); + } + return out; + } + + + /** + * SM4ECB加密算法 + * + * @param in 待加密字节数组 + * @param keyBytes 密钥 + * @return + */ + public static String EcbEncrypt(byte[] in, byte[] keyBytes) { + byte[] out = ecb_encrypt(in, keyBytes); + String cipher = Hex.toHexString(out); + return cipher; + } + + + /** + * hutool 解密SM4 + * + * @param encodeContent + * @return + */ + public static String decode(String encodeContent) { + String key = "24ad182507479a5da3ad94d9d78ea203"; + try { + SymmetricCrypto sm4 = new SM4(ECB, NoPadding, key.getBytes()); + String decryptStr = sm4.decryptStr(encodeContent, CharsetUtil.CHARSET_UTF_8); + return decryptStr; + } catch (Exception e) { + e.printStackTrace(); + log.error("SM4解密失败"); + } + return null; + } + + + /** + * SM4的ECB加密算法 + * + * @param content 待加密字符串, hex字符串 + * @param key 密钥 + * @return + */ + public static String EcbEncrypt(String content, String key) { + byte[] in = Hex.decode(content); + byte[] keyBytes = Hex.decode(key); + + String cipher = EcbEncrypt(in, keyBytes); + return cipher; + } + + /** + * SM4的ECB解密算法,对内 + * + * @param in 输入的密文字符串 + * @param keyBytes 密钥 + * @return + */ + private static byte[] ecb_decrypt(byte[] in, byte[] keyBytes) { + SM4Engine sm4Engine = new SM4Engine(); + sm4Engine.init(false, new KeyParameter(keyBytes)); + int inLen = in.length; + byte[] out = new byte[inLen]; + + int times = inLen / BlockSize; + + for (int i = 0; i < times; i++) { + sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); + } + + return out; + } + + /** + * SM4的ECB解密算法,对外 + * + * @param in 输入的密文字节数组 + * @param keyBytes 密钥 + * @return + */ + public static String EcbDecrypt(byte[] in, byte[] keyBytes) { + byte[] out = ecb_decrypt(in, keyBytes); + String plain = Hex.toHexString(out); + return plain; + } + + /** + * SM4的ECB解密算法 + * + * @param cipher 输入的密文字符串 + * @param key 密钥 + * @return + */ + public static String EcbDecrypt(String cipher, String key) { + byte[] in = Hex.decode(cipher); + byte[] keyBytes = Hex.decode(key); + + String plain = EcbDecrypt(in, keyBytes); + return plain; + } + + /** + * 主函数 + * + * @param args + */ + public static void main(String[] args) { + byte[] ciphertext = { + (byte) 0xE9, 0x1C, (byte) 0xCD, 0x53, 0x02, (byte) 0xD7, 0x0C, (byte) 0x9B, (byte) 0x96, 0x30, 0x62, (byte) 0xE8, (byte) 0xFB, (byte) 0xEF, (byte) 0xB9, (byte) 0xFF, + (byte) 0xDE, 0x19, (byte) 0x80, 0x35, 0x3E, (byte) 0x90, 0x4F, 0x2F, 0x12, 0x2B, 0x19, (byte) 0x8B, 0x6C, 0x7A, (byte) 0x85, (byte) 0xA1, + (byte) 0xB1, (byte) 0x86, (byte) 0xA3, 0x7A, 0x46, 0x0C, 0x2F, (byte) 0x94, 0x4F, 0x7F, 0x2C, 0x56, 0x14, (byte) 0xF3, (byte) 0x89, (byte) 0xC3, + 0x3F, 0x20, (byte) 0xF9, (byte) 0x87, (byte) 0xA1, 0x60, (byte) 0xD9, (byte) 0xA8, (byte) 0xFD, (byte) 0xD7, (byte) 0x9B, 0x4B, 0x26, 0x06, 0x30, 0x44}; + ByteBuf in = ByteBufAllocator.DEFAULT.heapBuffer(); + in.writeBytes(ciphertext); + + byte[] keyByte = { + 0x24, (byte) 0xad, 0x18, 0x25, 0x07, 0x47, (byte) 0x9a, 0x5d, (byte) 0xa3, (byte) 0xad, (byte) 0x94, (byte) 0xd9, (byte) 0xd7, (byte) 0x8e, (byte) 0xa2, 0x03 + }; + ByteBuf key = ByteBufAllocator.DEFAULT.heapBuffer(); + key.writeBytes(ciphertext); + String resCipherData = decode(ByteBufUtil.hexDump(in)); + ByteBuf out = ByteBufAllocator.DEFAULT.heapBuffer(); +// out.writeBytes(resCipherData); +// System.out.println(hexBytes); + System.out.println(resCipherData.toString()); +// String resPlainData = EcbDecrypt(resCipherData, key); +// System.out.println("原始明文:" + data); +// System.out.println("解密后明文:" + resPlainData); + } + +} + + + diff --git a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java deleted file mode 100644 index 29eac23..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java +++ /dev/null @@ -1,150 +0,0 @@ -package com.casic.missiles.parser.safe.impl; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.buffer.ByteBufUtil; -import org.bouncycastle.crypto.engines.SM4Engine; -import org.bouncycastle.crypto.params.KeyParameter; -import org.bouncycastle.util.encoders.Hex; - -/** - * SM4算法工具类 - */ -public class Sm4CryptStrategy { - - //加解密的字节快大小 - public static final int BlockSize = 16; - - /** - * SM4ECB加密算法,对内 - * - * @param in 待加密字节数组 - * @param keyBytes 密钥 - * @return - */ - private static byte[] ecb_encrypt(byte[] in, byte[] keyBytes) { - - - SM4Engine sm4Engine = new SM4Engine(); - sm4Engine.init(true, new KeyParameter(keyBytes)); - byte[] out = new byte[in.length]; - int times = in.length / BlockSize; - for (int i = 0; i < times; i++) { - sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); - } - return out; - } - - - /** - * SM4ECB加密算法 - * - * @param in 待加密字节数组 - * @param keyBytes 密钥 - * @return - */ - public static String EcbEncrypt(byte[] in, byte[] keyBytes) { - byte[] out = ecb_encrypt(in, keyBytes); - String cipher = Hex.toHexString(out); - return cipher; - } - - /** - * SM4的ECB加密算法 - * - * @param content 待加密字符串, hex字符串 - * @param key 密钥 - * @return - */ - public static String EcbEncrypt(String content, String key) { - byte[] in = Hex.decode(content); - byte[] keyBytes = Hex.decode(key); - - String cipher = EcbEncrypt(in, keyBytes); - return cipher; - } - - /** - * SM4的ECB解密算法,对内 - * - * @param in 输入的密文字符串 - * @param keyBytes 密钥 - * @return - */ - private static byte[] ecb_decrypt(byte[] in, byte[] keyBytes) { - SM4Engine sm4Engine = new SM4Engine(); - sm4Engine.init(false, new KeyParameter(keyBytes)); - int inLen = in.length; - byte[] out = new byte[inLen]; - - int times = inLen / BlockSize; - - for (int i = 0; i < times; i++) { - sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); - } - - return out; - } - - /** - * SM4的ECB解密算法,对外 - * - * @param in 输入的密文字节数组 - * @param keyBytes 密钥 - * @return - */ - public static String EcbDecrypt(byte[] in, byte[] keyBytes) { - byte[] out = ecb_decrypt(in, keyBytes); - String plain = Hex.toHexString(out); - return plain; - } - - /** - * SM4的ECB解密算法 - * - * @param cipher 输入的密文字符串 - * @param key 密钥 - * @return - */ - public static String EcbDecrypt(String cipher, String key) { - byte[] in = Hex.decode(cipher); - byte[] keyBytes = Hex.decode(key); - - String plain = EcbDecrypt(in, keyBytes); - return plain; - } - - /** - * 主函数 - * - * @param args - */ - public static void main(String[] args) { - byte[] ciphertext = { - (byte) 0xE9, 0x1C, (byte) 0xCD, 0x53, 0x02, (byte) 0xD7, 0x0C, (byte) 0x9B, (byte) 0x96, 0x30, 0x62, (byte)0xE8, (byte)0xFB, (byte)0xEF, (byte) 0xB9, (byte)0xFF, - (byte)0xDE, 0x19, (byte)0x80, 0x35, 0x3E, (byte)0x90, 0x4F, 0x2F, 0x12, 0x2B, 0x19, (byte) 0x8B, 0x6C, 0x7A, (byte) 0x85, (byte) 0xA1, - (byte)0xB1, (byte)0x86, (byte)0xA3, 0x7A, 0x46, 0x0C, 0x2F, (byte) 0x94, 0x4F, 0x7F, 0x2C, 0x56, 0x14, (byte) 0xF3, (byte)0x89, (byte) 0xC3, - 0x3F, 0x20, (byte)0xF9, (byte) 0x87, (byte)0xA1, 0x60, (byte)0xD9, (byte)0xA8, (byte) 0xFD, (byte)0xD7, (byte) 0x9B, 0x4B, 0x26, 0x06, 0x30, 0x44}; - String key = "24ad182507479a5da3ad94d9d78ea203"; - ByteBuf in= ByteBufAllocator.DEFAULT.heapBuffer(); - in.writeBytes(ciphertext); - - byte[] keyByte ={ - 0x24,(byte)0xad,0x18,0x25,0x07,0x47,(byte)0x9a,0x5d,(byte)0xa3,(byte)0xad,(byte)0x94,(byte)0xd9,(byte)0xd7,(byte)0x8e,(byte)0xa2,0x03 - }; - byte[] resCipherData = ecb_encrypt(ciphertext, keyByte); - ByteBuf out= ByteBufAllocator.DEFAULT.heapBuffer(); - out.writeBytes(resCipherData); -// System.out.println(hexBytes); - System.out.println(ciphertext.length);//66 - System.out.println(ByteBufUtil.hexDump(out)); -// String resPlainData = EcbDecrypt(resCipherData, key); -// System.out.println("原始明文:" + data); - System.out.println("解密后明文:" + resCipherData.toString()); -// System.out.println("解密后明文:" + resPlainData); - } - -} - - - diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/sensorhub-core/pom.xml b/sensorhub-core/pom.xml index ae68ea8..2a77c72 100644 --- a/sensorhub-core/pom.xml +++ b/sensorhub-core/pom.xml @@ -58,7 +58,7 @@ org.bouncycastle bcprov-jdk15to18 - 1.64 + 1.71 @@ -75,14 +75,6 @@ 0.0.1-SNAPSHOT - - - org.reflections - reflections - 0.9.10 - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java deleted file mode 100644 index 80c3458..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.casic.missiles.codec; - -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import lombok.extern.slf4j.Slf4j; -import org.reflections.Reflections; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -@Slf4j -public class ClassUtil { - -// public static List get(Class clazz){ -// List abstractPreprocessings = new ArrayList<>(); -// try{ -// String pk = clazz.getPackage().getName(); -// String path = pk.replace('.', '/'); -// //获取该路径下所有类 -// Reflections reflections = new Reflections(path); -// //获取继承了ISuperClass的所有类 -// Set> classSet = reflections.getSubTypesOf(clazz); -// -// for (Class clazz : classSet) { -// // 实例化获取到的类 -// T obj = clazz.newInstance(); -// // TODO 自己的处理逻辑 -// abstractPreprocessings.add(obj); -// } -// }catch (IllegalAccessException iae){ -// -// }catch (InstantiationException iae){ -// -// } -// return abstractPreprocessings; -// } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java deleted file mode 100644 index 3658376..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.casic.missiles.codec.predecodec; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - -import java.util.List; - -public class AbstractPreprocessing extends ByteToMessageDecoder { - - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { - list.add(byteBuf); - } -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java deleted file mode 100644 index ae08584..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; - -import java.nio.charset.Charset; -import java.util.List; - -/** - * @description: 将从接口取到的数据编码 - * @author: cz - * @create: 2023-05-04 15:15 - **/ -@Slf4j -@Component -public class AepPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public AepPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public AepPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析 预处理 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - }else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java deleted file mode 100644 index 6ab1368..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import java.nio.charset.Charset; -import java.util.List; - -@Slf4j -@Component -public class NbPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public NbPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public NbPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析,预处理base编码 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - } else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java index 21c7b07..9f9e279 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java @@ -40,8 +40,6 @@ */ @Autowired private SensorhubProperties sensorhubProperties; - @Autowired - private SensorhubServerChannelInitialHandler sensorhubHandler; public SensorhubServer(SensorhubProperties sensorhubProperties) { this.sensorhubProperties = sensorhubProperties; @@ -76,7 +74,7 @@ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) // 设置EventLoopGroup .channel(NioServerSocketChannel.class) // 指明新的Channel的类型 - .childHandler(sensorhubHandler) // 指定ChannelHandler + .childHandler(new SensorhubServerChannelInitialHandler()) // 指定ChannelHandler .option(ChannelOption.SO_BACKLOG, 1024) // 设置的ServerChannel的一些选项 .childOption(ChannelOption.SO_KEEPALIVE, true); // 当SO_KEEPALIVE=true的时候,服务端可以探测客户端的连接是否还存活着,如果客户端关闭了,那么服务端的连接可以关闭掉,释放资源 // 绑定端口,开始接收进来的连接 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java index 827744f..24a6fef 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java @@ -1,15 +1,13 @@ package com.casic.missiles.netty.handler; -import com.casic.missiles.codec.SensorhubCodec; +import com.casic.missiles.util.ClassUtil; import com.casic.missiles.codec.SensorhubDecoder; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; +import com.casic.missiles.predecodec.AbstractPreprocessing; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import java.util.List; @@ -17,21 +15,18 @@ * @author cz * @date 2023-06-05 */ -@Component public class SensorhubServerChannelInitialHandler extends ChannelInitializer { - @Autowired - private List abstractPreprocessingList; - @Override protected void initChannel(SocketChannel socketChannel) { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); + //添加前置处理器 + ClassUtil classUtil=new ClassUtil(); + List abstractPreprocessingList=classUtil.getSubClassList(AbstractPreprocessing.class); for (AbstractPreprocessing abstractPreprocessing:abstractPreprocessingList) { pipeline.addLast(abstractPreprocessing); } -// pipeline.addLast(new LengthFieldBasedFrameDecoder(1024*100, 0, 2, 0, 2)); - pipeline.addLast("codec", new SensorhubCodec()); pipeline.addLast(new SensorhubDecoder()); pipeline.addLast(new SensorhubServerHandler()); // 心跳续约 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java new file mode 100644 index 0000000..27c2838 --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java @@ -0,0 +1,179 @@ +package com.casic.missiles.parser.safe.impl; + +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.crypto.symmetric.SM4; +import cn.hutool.crypto.symmetric.SymmetricCrypto; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.ByteBufUtil; +import lombok.extern.slf4j.Slf4j; +import org.bouncycastle.crypto.engines.SM4Engine; +import org.bouncycastle.crypto.params.KeyParameter; +import org.bouncycastle.util.encoders.Hex; + +import static cn.hutool.crypto.Mode.ECB; +import static cn.hutool.crypto.Padding.NoPadding; + +/** + * SM4算法工具类 + */ +@Slf4j +public class Sm4 { + + //加解密的字节快大小 + public static final int BlockSize = 16; + + /** + * SM4ECB加密算法,对内 + * + * @param in 待加密字节数组 + * @param keyBytes 密钥 + * @return + */ + private static byte[] ecb_encrypt(byte[] in, byte[] keyBytes) { + + + SM4Engine sm4Engine = new SM4Engine(); + sm4Engine.init(true, new KeyParameter(keyBytes)); + byte[] out = new byte[in.length]; + int times = in.length / BlockSize; + for (int i = 0; i < times; i++) { + sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); + } + return out; + } + + + /** + * SM4ECB加密算法 + * + * @param in 待加密字节数组 + * @param keyBytes 密钥 + * @return + */ + public static String EcbEncrypt(byte[] in, byte[] keyBytes) { + byte[] out = ecb_encrypt(in, keyBytes); + String cipher = Hex.toHexString(out); + return cipher; + } + + + /** + * hutool 解密SM4 + * + * @param encodeContent + * @return + */ + public static String decode(String encodeContent) { + String key = "24ad182507479a5da3ad94d9d78ea203"; + try { + SymmetricCrypto sm4 = new SM4(ECB, NoPadding, key.getBytes()); + String decryptStr = sm4.decryptStr(encodeContent, CharsetUtil.CHARSET_UTF_8); + return decryptStr; + } catch (Exception e) { + e.printStackTrace(); + log.error("SM4解密失败"); + } + return null; + } + + + /** + * SM4的ECB加密算法 + * + * @param content 待加密字符串, hex字符串 + * @param key 密钥 + * @return + */ + public static String EcbEncrypt(String content, String key) { + byte[] in = Hex.decode(content); + byte[] keyBytes = Hex.decode(key); + + String cipher = EcbEncrypt(in, keyBytes); + return cipher; + } + + /** + * SM4的ECB解密算法,对内 + * + * @param in 输入的密文字符串 + * @param keyBytes 密钥 + * @return + */ + private static byte[] ecb_decrypt(byte[] in, byte[] keyBytes) { + SM4Engine sm4Engine = new SM4Engine(); + sm4Engine.init(false, new KeyParameter(keyBytes)); + int inLen = in.length; + byte[] out = new byte[inLen]; + + int times = inLen / BlockSize; + + for (int i = 0; i < times; i++) { + sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); + } + + return out; + } + + /** + * SM4的ECB解密算法,对外 + * + * @param in 输入的密文字节数组 + * @param keyBytes 密钥 + * @return + */ + public static String EcbDecrypt(byte[] in, byte[] keyBytes) { + byte[] out = ecb_decrypt(in, keyBytes); + String plain = Hex.toHexString(out); + return plain; + } + + /** + * SM4的ECB解密算法 + * + * @param cipher 输入的密文字符串 + * @param key 密钥 + * @return + */ + public static String EcbDecrypt(String cipher, String key) { + byte[] in = Hex.decode(cipher); + byte[] keyBytes = Hex.decode(key); + + String plain = EcbDecrypt(in, keyBytes); + return plain; + } + + /** + * 主函数 + * + * @param args + */ + public static void main(String[] args) { + byte[] ciphertext = { + (byte) 0xE9, 0x1C, (byte) 0xCD, 0x53, 0x02, (byte) 0xD7, 0x0C, (byte) 0x9B, (byte) 0x96, 0x30, 0x62, (byte) 0xE8, (byte) 0xFB, (byte) 0xEF, (byte) 0xB9, (byte) 0xFF, + (byte) 0xDE, 0x19, (byte) 0x80, 0x35, 0x3E, (byte) 0x90, 0x4F, 0x2F, 0x12, 0x2B, 0x19, (byte) 0x8B, 0x6C, 0x7A, (byte) 0x85, (byte) 0xA1, + (byte) 0xB1, (byte) 0x86, (byte) 0xA3, 0x7A, 0x46, 0x0C, 0x2F, (byte) 0x94, 0x4F, 0x7F, 0x2C, 0x56, 0x14, (byte) 0xF3, (byte) 0x89, (byte) 0xC3, + 0x3F, 0x20, (byte) 0xF9, (byte) 0x87, (byte) 0xA1, 0x60, (byte) 0xD9, (byte) 0xA8, (byte) 0xFD, (byte) 0xD7, (byte) 0x9B, 0x4B, 0x26, 0x06, 0x30, 0x44}; + ByteBuf in = ByteBufAllocator.DEFAULT.heapBuffer(); + in.writeBytes(ciphertext); + + byte[] keyByte = { + 0x24, (byte) 0xad, 0x18, 0x25, 0x07, 0x47, (byte) 0x9a, 0x5d, (byte) 0xa3, (byte) 0xad, (byte) 0x94, (byte) 0xd9, (byte) 0xd7, (byte) 0x8e, (byte) 0xa2, 0x03 + }; + ByteBuf key = ByteBufAllocator.DEFAULT.heapBuffer(); + key.writeBytes(ciphertext); + String resCipherData = decode(ByteBufUtil.hexDump(in)); + ByteBuf out = ByteBufAllocator.DEFAULT.heapBuffer(); +// out.writeBytes(resCipherData); +// System.out.println(hexBytes); + System.out.println(resCipherData.toString()); +// String resPlainData = EcbDecrypt(resCipherData, key); +// System.out.println("原始明文:" + data); +// System.out.println("解密后明文:" + resPlainData); + } + +} + + + diff --git a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java deleted file mode 100644 index 29eac23..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java +++ /dev/null @@ -1,150 +0,0 @@ -package com.casic.missiles.parser.safe.impl; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.buffer.ByteBufUtil; -import org.bouncycastle.crypto.engines.SM4Engine; -import org.bouncycastle.crypto.params.KeyParameter; -import org.bouncycastle.util.encoders.Hex; - -/** - * SM4算法工具类 - */ -public class Sm4CryptStrategy { - - //加解密的字节快大小 - public static final int BlockSize = 16; - - /** - * SM4ECB加密算法,对内 - * - * @param in 待加密字节数组 - * @param keyBytes 密钥 - * @return - */ - private static byte[] ecb_encrypt(byte[] in, byte[] keyBytes) { - - - SM4Engine sm4Engine = new SM4Engine(); - sm4Engine.init(true, new KeyParameter(keyBytes)); - byte[] out = new byte[in.length]; - int times = in.length / BlockSize; - for (int i = 0; i < times; i++) { - sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); - } - return out; - } - - - /** - * SM4ECB加密算法 - * - * @param in 待加密字节数组 - * @param keyBytes 密钥 - * @return - */ - public static String EcbEncrypt(byte[] in, byte[] keyBytes) { - byte[] out = ecb_encrypt(in, keyBytes); - String cipher = Hex.toHexString(out); - return cipher; - } - - /** - * SM4的ECB加密算法 - * - * @param content 待加密字符串, hex字符串 - * @param key 密钥 - * @return - */ - public static String EcbEncrypt(String content, String key) { - byte[] in = Hex.decode(content); - byte[] keyBytes = Hex.decode(key); - - String cipher = EcbEncrypt(in, keyBytes); - return cipher; - } - - /** - * SM4的ECB解密算法,对内 - * - * @param in 输入的密文字符串 - * @param keyBytes 密钥 - * @return - */ - private static byte[] ecb_decrypt(byte[] in, byte[] keyBytes) { - SM4Engine sm4Engine = new SM4Engine(); - sm4Engine.init(false, new KeyParameter(keyBytes)); - int inLen = in.length; - byte[] out = new byte[inLen]; - - int times = inLen / BlockSize; - - for (int i = 0; i < times; i++) { - sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); - } - - return out; - } - - /** - * SM4的ECB解密算法,对外 - * - * @param in 输入的密文字节数组 - * @param keyBytes 密钥 - * @return - */ - public static String EcbDecrypt(byte[] in, byte[] keyBytes) { - byte[] out = ecb_decrypt(in, keyBytes); - String plain = Hex.toHexString(out); - return plain; - } - - /** - * SM4的ECB解密算法 - * - * @param cipher 输入的密文字符串 - * @param key 密钥 - * @return - */ - public static String EcbDecrypt(String cipher, String key) { - byte[] in = Hex.decode(cipher); - byte[] keyBytes = Hex.decode(key); - - String plain = EcbDecrypt(in, keyBytes); - return plain; - } - - /** - * 主函数 - * - * @param args - */ - public static void main(String[] args) { - byte[] ciphertext = { - (byte) 0xE9, 0x1C, (byte) 0xCD, 0x53, 0x02, (byte) 0xD7, 0x0C, (byte) 0x9B, (byte) 0x96, 0x30, 0x62, (byte)0xE8, (byte)0xFB, (byte)0xEF, (byte) 0xB9, (byte)0xFF, - (byte)0xDE, 0x19, (byte)0x80, 0x35, 0x3E, (byte)0x90, 0x4F, 0x2F, 0x12, 0x2B, 0x19, (byte) 0x8B, 0x6C, 0x7A, (byte) 0x85, (byte) 0xA1, - (byte)0xB1, (byte)0x86, (byte)0xA3, 0x7A, 0x46, 0x0C, 0x2F, (byte) 0x94, 0x4F, 0x7F, 0x2C, 0x56, 0x14, (byte) 0xF3, (byte)0x89, (byte) 0xC3, - 0x3F, 0x20, (byte)0xF9, (byte) 0x87, (byte)0xA1, 0x60, (byte)0xD9, (byte)0xA8, (byte) 0xFD, (byte)0xD7, (byte) 0x9B, 0x4B, 0x26, 0x06, 0x30, 0x44}; - String key = "24ad182507479a5da3ad94d9d78ea203"; - ByteBuf in= ByteBufAllocator.DEFAULT.heapBuffer(); - in.writeBytes(ciphertext); - - byte[] keyByte ={ - 0x24,(byte)0xad,0x18,0x25,0x07,0x47,(byte)0x9a,0x5d,(byte)0xa3,(byte)0xad,(byte)0x94,(byte)0xd9,(byte)0xd7,(byte)0x8e,(byte)0xa2,0x03 - }; - byte[] resCipherData = ecb_encrypt(ciphertext, keyByte); - ByteBuf out= ByteBufAllocator.DEFAULT.heapBuffer(); - out.writeBytes(resCipherData); -// System.out.println(hexBytes); - System.out.println(ciphertext.length);//66 - System.out.println(ByteBufUtil.hexDump(out)); -// String resPlainData = EcbDecrypt(resCipherData, key); -// System.out.println("原始明文:" + data); - System.out.println("解密后明文:" + resCipherData.toString()); -// System.out.println("解密后明文:" + resPlainData); - } - -} - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java new file mode 100644 index 0000000..9fa95de --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java @@ -0,0 +1,15 @@ +package com.casic.missiles.predecodec; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; + +import java.util.List; + +public class AbstractPreprocessing extends ByteToMessageDecoder { + + @Override + protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { + list.add(byteBuf); + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/sensorhub-core/pom.xml b/sensorhub-core/pom.xml index ae68ea8..2a77c72 100644 --- a/sensorhub-core/pom.xml +++ b/sensorhub-core/pom.xml @@ -58,7 +58,7 @@ org.bouncycastle bcprov-jdk15to18 - 1.64 + 1.71 @@ -75,14 +75,6 @@ 0.0.1-SNAPSHOT - - - org.reflections - reflections - 0.9.10 - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java deleted file mode 100644 index 80c3458..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.casic.missiles.codec; - -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import lombok.extern.slf4j.Slf4j; -import org.reflections.Reflections; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -@Slf4j -public class ClassUtil { - -// public static List get(Class clazz){ -// List abstractPreprocessings = new ArrayList<>(); -// try{ -// String pk = clazz.getPackage().getName(); -// String path = pk.replace('.', '/'); -// //获取该路径下所有类 -// Reflections reflections = new Reflections(path); -// //获取继承了ISuperClass的所有类 -// Set> classSet = reflections.getSubTypesOf(clazz); -// -// for (Class clazz : classSet) { -// // 实例化获取到的类 -// T obj = clazz.newInstance(); -// // TODO 自己的处理逻辑 -// abstractPreprocessings.add(obj); -// } -// }catch (IllegalAccessException iae){ -// -// }catch (InstantiationException iae){ -// -// } -// return abstractPreprocessings; -// } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java deleted file mode 100644 index 3658376..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.casic.missiles.codec.predecodec; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - -import java.util.List; - -public class AbstractPreprocessing extends ByteToMessageDecoder { - - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { - list.add(byteBuf); - } -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java deleted file mode 100644 index ae08584..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; - -import java.nio.charset.Charset; -import java.util.List; - -/** - * @description: 将从接口取到的数据编码 - * @author: cz - * @create: 2023-05-04 15:15 - **/ -@Slf4j -@Component -public class AepPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public AepPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public AepPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析 预处理 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - }else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java deleted file mode 100644 index 6ab1368..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import java.nio.charset.Charset; -import java.util.List; - -@Slf4j -@Component -public class NbPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public NbPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public NbPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析,预处理base编码 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - } else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java index 21c7b07..9f9e279 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java @@ -40,8 +40,6 @@ */ @Autowired private SensorhubProperties sensorhubProperties; - @Autowired - private SensorhubServerChannelInitialHandler sensorhubHandler; public SensorhubServer(SensorhubProperties sensorhubProperties) { this.sensorhubProperties = sensorhubProperties; @@ -76,7 +74,7 @@ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) // 设置EventLoopGroup .channel(NioServerSocketChannel.class) // 指明新的Channel的类型 - .childHandler(sensorhubHandler) // 指定ChannelHandler + .childHandler(new SensorhubServerChannelInitialHandler()) // 指定ChannelHandler .option(ChannelOption.SO_BACKLOG, 1024) // 设置的ServerChannel的一些选项 .childOption(ChannelOption.SO_KEEPALIVE, true); // 当SO_KEEPALIVE=true的时候,服务端可以探测客户端的连接是否还存活着,如果客户端关闭了,那么服务端的连接可以关闭掉,释放资源 // 绑定端口,开始接收进来的连接 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java index 827744f..24a6fef 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java @@ -1,15 +1,13 @@ package com.casic.missiles.netty.handler; -import com.casic.missiles.codec.SensorhubCodec; +import com.casic.missiles.util.ClassUtil; import com.casic.missiles.codec.SensorhubDecoder; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; +import com.casic.missiles.predecodec.AbstractPreprocessing; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import java.util.List; @@ -17,21 +15,18 @@ * @author cz * @date 2023-06-05 */ -@Component public class SensorhubServerChannelInitialHandler extends ChannelInitializer { - @Autowired - private List abstractPreprocessingList; - @Override protected void initChannel(SocketChannel socketChannel) { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); + //添加前置处理器 + ClassUtil classUtil=new ClassUtil(); + List abstractPreprocessingList=classUtil.getSubClassList(AbstractPreprocessing.class); for (AbstractPreprocessing abstractPreprocessing:abstractPreprocessingList) { pipeline.addLast(abstractPreprocessing); } -// pipeline.addLast(new LengthFieldBasedFrameDecoder(1024*100, 0, 2, 0, 2)); - pipeline.addLast("codec", new SensorhubCodec()); pipeline.addLast(new SensorhubDecoder()); pipeline.addLast(new SensorhubServerHandler()); // 心跳续约 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java new file mode 100644 index 0000000..27c2838 --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java @@ -0,0 +1,179 @@ +package com.casic.missiles.parser.safe.impl; + +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.crypto.symmetric.SM4; +import cn.hutool.crypto.symmetric.SymmetricCrypto; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.ByteBufUtil; +import lombok.extern.slf4j.Slf4j; +import org.bouncycastle.crypto.engines.SM4Engine; +import org.bouncycastle.crypto.params.KeyParameter; +import org.bouncycastle.util.encoders.Hex; + +import static cn.hutool.crypto.Mode.ECB; +import static cn.hutool.crypto.Padding.NoPadding; + +/** + * SM4算法工具类 + */ +@Slf4j +public class Sm4 { + + //加解密的字节快大小 + public static final int BlockSize = 16; + + /** + * SM4ECB加密算法,对内 + * + * @param in 待加密字节数组 + * @param keyBytes 密钥 + * @return + */ + private static byte[] ecb_encrypt(byte[] in, byte[] keyBytes) { + + + SM4Engine sm4Engine = new SM4Engine(); + sm4Engine.init(true, new KeyParameter(keyBytes)); + byte[] out = new byte[in.length]; + int times = in.length / BlockSize; + for (int i = 0; i < times; i++) { + sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); + } + return out; + } + + + /** + * SM4ECB加密算法 + * + * @param in 待加密字节数组 + * @param keyBytes 密钥 + * @return + */ + public static String EcbEncrypt(byte[] in, byte[] keyBytes) { + byte[] out = ecb_encrypt(in, keyBytes); + String cipher = Hex.toHexString(out); + return cipher; + } + + + /** + * hutool 解密SM4 + * + * @param encodeContent + * @return + */ + public static String decode(String encodeContent) { + String key = "24ad182507479a5da3ad94d9d78ea203"; + try { + SymmetricCrypto sm4 = new SM4(ECB, NoPadding, key.getBytes()); + String decryptStr = sm4.decryptStr(encodeContent, CharsetUtil.CHARSET_UTF_8); + return decryptStr; + } catch (Exception e) { + e.printStackTrace(); + log.error("SM4解密失败"); + } + return null; + } + + + /** + * SM4的ECB加密算法 + * + * @param content 待加密字符串, hex字符串 + * @param key 密钥 + * @return + */ + public static String EcbEncrypt(String content, String key) { + byte[] in = Hex.decode(content); + byte[] keyBytes = Hex.decode(key); + + String cipher = EcbEncrypt(in, keyBytes); + return cipher; + } + + /** + * SM4的ECB解密算法,对内 + * + * @param in 输入的密文字符串 + * @param keyBytes 密钥 + * @return + */ + private static byte[] ecb_decrypt(byte[] in, byte[] keyBytes) { + SM4Engine sm4Engine = new SM4Engine(); + sm4Engine.init(false, new KeyParameter(keyBytes)); + int inLen = in.length; + byte[] out = new byte[inLen]; + + int times = inLen / BlockSize; + + for (int i = 0; i < times; i++) { + sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); + } + + return out; + } + + /** + * SM4的ECB解密算法,对外 + * + * @param in 输入的密文字节数组 + * @param keyBytes 密钥 + * @return + */ + public static String EcbDecrypt(byte[] in, byte[] keyBytes) { + byte[] out = ecb_decrypt(in, keyBytes); + String plain = Hex.toHexString(out); + return plain; + } + + /** + * SM4的ECB解密算法 + * + * @param cipher 输入的密文字符串 + * @param key 密钥 + * @return + */ + public static String EcbDecrypt(String cipher, String key) { + byte[] in = Hex.decode(cipher); + byte[] keyBytes = Hex.decode(key); + + String plain = EcbDecrypt(in, keyBytes); + return plain; + } + + /** + * 主函数 + * + * @param args + */ + public static void main(String[] args) { + byte[] ciphertext = { + (byte) 0xE9, 0x1C, (byte) 0xCD, 0x53, 0x02, (byte) 0xD7, 0x0C, (byte) 0x9B, (byte) 0x96, 0x30, 0x62, (byte) 0xE8, (byte) 0xFB, (byte) 0xEF, (byte) 0xB9, (byte) 0xFF, + (byte) 0xDE, 0x19, (byte) 0x80, 0x35, 0x3E, (byte) 0x90, 0x4F, 0x2F, 0x12, 0x2B, 0x19, (byte) 0x8B, 0x6C, 0x7A, (byte) 0x85, (byte) 0xA1, + (byte) 0xB1, (byte) 0x86, (byte) 0xA3, 0x7A, 0x46, 0x0C, 0x2F, (byte) 0x94, 0x4F, 0x7F, 0x2C, 0x56, 0x14, (byte) 0xF3, (byte) 0x89, (byte) 0xC3, + 0x3F, 0x20, (byte) 0xF9, (byte) 0x87, (byte) 0xA1, 0x60, (byte) 0xD9, (byte) 0xA8, (byte) 0xFD, (byte) 0xD7, (byte) 0x9B, 0x4B, 0x26, 0x06, 0x30, 0x44}; + ByteBuf in = ByteBufAllocator.DEFAULT.heapBuffer(); + in.writeBytes(ciphertext); + + byte[] keyByte = { + 0x24, (byte) 0xad, 0x18, 0x25, 0x07, 0x47, (byte) 0x9a, 0x5d, (byte) 0xa3, (byte) 0xad, (byte) 0x94, (byte) 0xd9, (byte) 0xd7, (byte) 0x8e, (byte) 0xa2, 0x03 + }; + ByteBuf key = ByteBufAllocator.DEFAULT.heapBuffer(); + key.writeBytes(ciphertext); + String resCipherData = decode(ByteBufUtil.hexDump(in)); + ByteBuf out = ByteBufAllocator.DEFAULT.heapBuffer(); +// out.writeBytes(resCipherData); +// System.out.println(hexBytes); + System.out.println(resCipherData.toString()); +// String resPlainData = EcbDecrypt(resCipherData, key); +// System.out.println("原始明文:" + data); +// System.out.println("解密后明文:" + resPlainData); + } + +} + + + diff --git a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java deleted file mode 100644 index 29eac23..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java +++ /dev/null @@ -1,150 +0,0 @@ -package com.casic.missiles.parser.safe.impl; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.buffer.ByteBufUtil; -import org.bouncycastle.crypto.engines.SM4Engine; -import org.bouncycastle.crypto.params.KeyParameter; -import org.bouncycastle.util.encoders.Hex; - -/** - * SM4算法工具类 - */ -public class Sm4CryptStrategy { - - //加解密的字节快大小 - public static final int BlockSize = 16; - - /** - * SM4ECB加密算法,对内 - * - * @param in 待加密字节数组 - * @param keyBytes 密钥 - * @return - */ - private static byte[] ecb_encrypt(byte[] in, byte[] keyBytes) { - - - SM4Engine sm4Engine = new SM4Engine(); - sm4Engine.init(true, new KeyParameter(keyBytes)); - byte[] out = new byte[in.length]; - int times = in.length / BlockSize; - for (int i = 0; i < times; i++) { - sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); - } - return out; - } - - - /** - * SM4ECB加密算法 - * - * @param in 待加密字节数组 - * @param keyBytes 密钥 - * @return - */ - public static String EcbEncrypt(byte[] in, byte[] keyBytes) { - byte[] out = ecb_encrypt(in, keyBytes); - String cipher = Hex.toHexString(out); - return cipher; - } - - /** - * SM4的ECB加密算法 - * - * @param content 待加密字符串, hex字符串 - * @param key 密钥 - * @return - */ - public static String EcbEncrypt(String content, String key) { - byte[] in = Hex.decode(content); - byte[] keyBytes = Hex.decode(key); - - String cipher = EcbEncrypt(in, keyBytes); - return cipher; - } - - /** - * SM4的ECB解密算法,对内 - * - * @param in 输入的密文字符串 - * @param keyBytes 密钥 - * @return - */ - private static byte[] ecb_decrypt(byte[] in, byte[] keyBytes) { - SM4Engine sm4Engine = new SM4Engine(); - sm4Engine.init(false, new KeyParameter(keyBytes)); - int inLen = in.length; - byte[] out = new byte[inLen]; - - int times = inLen / BlockSize; - - for (int i = 0; i < times; i++) { - sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); - } - - return out; - } - - /** - * SM4的ECB解密算法,对外 - * - * @param in 输入的密文字节数组 - * @param keyBytes 密钥 - * @return - */ - public static String EcbDecrypt(byte[] in, byte[] keyBytes) { - byte[] out = ecb_decrypt(in, keyBytes); - String plain = Hex.toHexString(out); - return plain; - } - - /** - * SM4的ECB解密算法 - * - * @param cipher 输入的密文字符串 - * @param key 密钥 - * @return - */ - public static String EcbDecrypt(String cipher, String key) { - byte[] in = Hex.decode(cipher); - byte[] keyBytes = Hex.decode(key); - - String plain = EcbDecrypt(in, keyBytes); - return plain; - } - - /** - * 主函数 - * - * @param args - */ - public static void main(String[] args) { - byte[] ciphertext = { - (byte) 0xE9, 0x1C, (byte) 0xCD, 0x53, 0x02, (byte) 0xD7, 0x0C, (byte) 0x9B, (byte) 0x96, 0x30, 0x62, (byte)0xE8, (byte)0xFB, (byte)0xEF, (byte) 0xB9, (byte)0xFF, - (byte)0xDE, 0x19, (byte)0x80, 0x35, 0x3E, (byte)0x90, 0x4F, 0x2F, 0x12, 0x2B, 0x19, (byte) 0x8B, 0x6C, 0x7A, (byte) 0x85, (byte) 0xA1, - (byte)0xB1, (byte)0x86, (byte)0xA3, 0x7A, 0x46, 0x0C, 0x2F, (byte) 0x94, 0x4F, 0x7F, 0x2C, 0x56, 0x14, (byte) 0xF3, (byte)0x89, (byte) 0xC3, - 0x3F, 0x20, (byte)0xF9, (byte) 0x87, (byte)0xA1, 0x60, (byte)0xD9, (byte)0xA8, (byte) 0xFD, (byte)0xD7, (byte) 0x9B, 0x4B, 0x26, 0x06, 0x30, 0x44}; - String key = "24ad182507479a5da3ad94d9d78ea203"; - ByteBuf in= ByteBufAllocator.DEFAULT.heapBuffer(); - in.writeBytes(ciphertext); - - byte[] keyByte ={ - 0x24,(byte)0xad,0x18,0x25,0x07,0x47,(byte)0x9a,0x5d,(byte)0xa3,(byte)0xad,(byte)0x94,(byte)0xd9,(byte)0xd7,(byte)0x8e,(byte)0xa2,0x03 - }; - byte[] resCipherData = ecb_encrypt(ciphertext, keyByte); - ByteBuf out= ByteBufAllocator.DEFAULT.heapBuffer(); - out.writeBytes(resCipherData); -// System.out.println(hexBytes); - System.out.println(ciphertext.length);//66 - System.out.println(ByteBufUtil.hexDump(out)); -// String resPlainData = EcbDecrypt(resCipherData, key); -// System.out.println("原始明文:" + data); - System.out.println("解密后明文:" + resCipherData.toString()); -// System.out.println("解密后明文:" + resPlainData); - } - -} - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java new file mode 100644 index 0000000..9fa95de --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java @@ -0,0 +1,15 @@ +package com.casic.missiles.predecodec; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; + +import java.util.List; + +public class AbstractPreprocessing extends ByteToMessageDecoder { + + @Override + protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { + list.add(byteBuf); + } +} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/AepPreprocessing.java new file mode 100644 index 0000000..40a5265 --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/AepPreprocessing.java @@ -0,0 +1,53 @@ +package com.casic.missiles.predecodec.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.predecodec.AbstractPreprocessing; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.base64.Base64; +import io.netty.handler.codec.base64.Base64Dialect; +import io.netty.util.internal.ObjectUtil; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; +import java.util.List; + +/** + * @description: 将从接口取到的数据编码 + * @author: cz + * @create: 2023-05-04 15:15 + **/ +@Slf4j +public class AepPreprocessing extends AbstractPreprocessing { + + private final Base64Dialect dialect; + + public AepPreprocessing() { + this(Base64Dialect.STANDARD); + } + + public AepPreprocessing(Base64Dialect dialect) { + this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); + } + + //执行nb平台前的协议解析 预处理 + @Override + protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { + //aep平台处理 + if (msg.toString(Charset.defaultCharset()).contains("aep")) { + String httpContent = msg.toString(Charset.defaultCharset()); + log.info(" String : " + msg.toString(Charset.defaultCharset())); + String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); + String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); + log.info("----------------------------------" + values); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); + out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); + }else { + out.add(msg); + } + } + +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/sensorhub-core/pom.xml b/sensorhub-core/pom.xml index ae68ea8..2a77c72 100644 --- a/sensorhub-core/pom.xml +++ b/sensorhub-core/pom.xml @@ -58,7 +58,7 @@ org.bouncycastle bcprov-jdk15to18 - 1.64 + 1.71 @@ -75,14 +75,6 @@ 0.0.1-SNAPSHOT - - - org.reflections - reflections - 0.9.10 - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java deleted file mode 100644 index 80c3458..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.casic.missiles.codec; - -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import lombok.extern.slf4j.Slf4j; -import org.reflections.Reflections; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -@Slf4j -public class ClassUtil { - -// public static List get(Class clazz){ -// List abstractPreprocessings = new ArrayList<>(); -// try{ -// String pk = clazz.getPackage().getName(); -// String path = pk.replace('.', '/'); -// //获取该路径下所有类 -// Reflections reflections = new Reflections(path); -// //获取继承了ISuperClass的所有类 -// Set> classSet = reflections.getSubTypesOf(clazz); -// -// for (Class clazz : classSet) { -// // 实例化获取到的类 -// T obj = clazz.newInstance(); -// // TODO 自己的处理逻辑 -// abstractPreprocessings.add(obj); -// } -// }catch (IllegalAccessException iae){ -// -// }catch (InstantiationException iae){ -// -// } -// return abstractPreprocessings; -// } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java deleted file mode 100644 index 3658376..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.casic.missiles.codec.predecodec; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - -import java.util.List; - -public class AbstractPreprocessing extends ByteToMessageDecoder { - - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { - list.add(byteBuf); - } -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java deleted file mode 100644 index ae08584..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; - -import java.nio.charset.Charset; -import java.util.List; - -/** - * @description: 将从接口取到的数据编码 - * @author: cz - * @create: 2023-05-04 15:15 - **/ -@Slf4j -@Component -public class AepPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public AepPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public AepPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析 预处理 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - }else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java deleted file mode 100644 index 6ab1368..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import java.nio.charset.Charset; -import java.util.List; - -@Slf4j -@Component -public class NbPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public NbPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public NbPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析,预处理base编码 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - } else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java index 21c7b07..9f9e279 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java @@ -40,8 +40,6 @@ */ @Autowired private SensorhubProperties sensorhubProperties; - @Autowired - private SensorhubServerChannelInitialHandler sensorhubHandler; public SensorhubServer(SensorhubProperties sensorhubProperties) { this.sensorhubProperties = sensorhubProperties; @@ -76,7 +74,7 @@ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) // 设置EventLoopGroup .channel(NioServerSocketChannel.class) // 指明新的Channel的类型 - .childHandler(sensorhubHandler) // 指定ChannelHandler + .childHandler(new SensorhubServerChannelInitialHandler()) // 指定ChannelHandler .option(ChannelOption.SO_BACKLOG, 1024) // 设置的ServerChannel的一些选项 .childOption(ChannelOption.SO_KEEPALIVE, true); // 当SO_KEEPALIVE=true的时候,服务端可以探测客户端的连接是否还存活着,如果客户端关闭了,那么服务端的连接可以关闭掉,释放资源 // 绑定端口,开始接收进来的连接 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java index 827744f..24a6fef 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java @@ -1,15 +1,13 @@ package com.casic.missiles.netty.handler; -import com.casic.missiles.codec.SensorhubCodec; +import com.casic.missiles.util.ClassUtil; import com.casic.missiles.codec.SensorhubDecoder; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; +import com.casic.missiles.predecodec.AbstractPreprocessing; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import java.util.List; @@ -17,21 +15,18 @@ * @author cz * @date 2023-06-05 */ -@Component public class SensorhubServerChannelInitialHandler extends ChannelInitializer { - @Autowired - private List abstractPreprocessingList; - @Override protected void initChannel(SocketChannel socketChannel) { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); + //添加前置处理器 + ClassUtil classUtil=new ClassUtil(); + List abstractPreprocessingList=classUtil.getSubClassList(AbstractPreprocessing.class); for (AbstractPreprocessing abstractPreprocessing:abstractPreprocessingList) { pipeline.addLast(abstractPreprocessing); } -// pipeline.addLast(new LengthFieldBasedFrameDecoder(1024*100, 0, 2, 0, 2)); - pipeline.addLast("codec", new SensorhubCodec()); pipeline.addLast(new SensorhubDecoder()); pipeline.addLast(new SensorhubServerHandler()); // 心跳续约 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java new file mode 100644 index 0000000..27c2838 --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java @@ -0,0 +1,179 @@ +package com.casic.missiles.parser.safe.impl; + +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.crypto.symmetric.SM4; +import cn.hutool.crypto.symmetric.SymmetricCrypto; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.ByteBufUtil; +import lombok.extern.slf4j.Slf4j; +import org.bouncycastle.crypto.engines.SM4Engine; +import org.bouncycastle.crypto.params.KeyParameter; +import org.bouncycastle.util.encoders.Hex; + +import static cn.hutool.crypto.Mode.ECB; +import static cn.hutool.crypto.Padding.NoPadding; + +/** + * SM4算法工具类 + */ +@Slf4j +public class Sm4 { + + //加解密的字节快大小 + public static final int BlockSize = 16; + + /** + * SM4ECB加密算法,对内 + * + * @param in 待加密字节数组 + * @param keyBytes 密钥 + * @return + */ + private static byte[] ecb_encrypt(byte[] in, byte[] keyBytes) { + + + SM4Engine sm4Engine = new SM4Engine(); + sm4Engine.init(true, new KeyParameter(keyBytes)); + byte[] out = new byte[in.length]; + int times = in.length / BlockSize; + for (int i = 0; i < times; i++) { + sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); + } + return out; + } + + + /** + * SM4ECB加密算法 + * + * @param in 待加密字节数组 + * @param keyBytes 密钥 + * @return + */ + public static String EcbEncrypt(byte[] in, byte[] keyBytes) { + byte[] out = ecb_encrypt(in, keyBytes); + String cipher = Hex.toHexString(out); + return cipher; + } + + + /** + * hutool 解密SM4 + * + * @param encodeContent + * @return + */ + public static String decode(String encodeContent) { + String key = "24ad182507479a5da3ad94d9d78ea203"; + try { + SymmetricCrypto sm4 = new SM4(ECB, NoPadding, key.getBytes()); + String decryptStr = sm4.decryptStr(encodeContent, CharsetUtil.CHARSET_UTF_8); + return decryptStr; + } catch (Exception e) { + e.printStackTrace(); + log.error("SM4解密失败"); + } + return null; + } + + + /** + * SM4的ECB加密算法 + * + * @param content 待加密字符串, hex字符串 + * @param key 密钥 + * @return + */ + public static String EcbEncrypt(String content, String key) { + byte[] in = Hex.decode(content); + byte[] keyBytes = Hex.decode(key); + + String cipher = EcbEncrypt(in, keyBytes); + return cipher; + } + + /** + * SM4的ECB解密算法,对内 + * + * @param in 输入的密文字符串 + * @param keyBytes 密钥 + * @return + */ + private static byte[] ecb_decrypt(byte[] in, byte[] keyBytes) { + SM4Engine sm4Engine = new SM4Engine(); + sm4Engine.init(false, new KeyParameter(keyBytes)); + int inLen = in.length; + byte[] out = new byte[inLen]; + + int times = inLen / BlockSize; + + for (int i = 0; i < times; i++) { + sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); + } + + return out; + } + + /** + * SM4的ECB解密算法,对外 + * + * @param in 输入的密文字节数组 + * @param keyBytes 密钥 + * @return + */ + public static String EcbDecrypt(byte[] in, byte[] keyBytes) { + byte[] out = ecb_decrypt(in, keyBytes); + String plain = Hex.toHexString(out); + return plain; + } + + /** + * SM4的ECB解密算法 + * + * @param cipher 输入的密文字符串 + * @param key 密钥 + * @return + */ + public static String EcbDecrypt(String cipher, String key) { + byte[] in = Hex.decode(cipher); + byte[] keyBytes = Hex.decode(key); + + String plain = EcbDecrypt(in, keyBytes); + return plain; + } + + /** + * 主函数 + * + * @param args + */ + public static void main(String[] args) { + byte[] ciphertext = { + (byte) 0xE9, 0x1C, (byte) 0xCD, 0x53, 0x02, (byte) 0xD7, 0x0C, (byte) 0x9B, (byte) 0x96, 0x30, 0x62, (byte) 0xE8, (byte) 0xFB, (byte) 0xEF, (byte) 0xB9, (byte) 0xFF, + (byte) 0xDE, 0x19, (byte) 0x80, 0x35, 0x3E, (byte) 0x90, 0x4F, 0x2F, 0x12, 0x2B, 0x19, (byte) 0x8B, 0x6C, 0x7A, (byte) 0x85, (byte) 0xA1, + (byte) 0xB1, (byte) 0x86, (byte) 0xA3, 0x7A, 0x46, 0x0C, 0x2F, (byte) 0x94, 0x4F, 0x7F, 0x2C, 0x56, 0x14, (byte) 0xF3, (byte) 0x89, (byte) 0xC3, + 0x3F, 0x20, (byte) 0xF9, (byte) 0x87, (byte) 0xA1, 0x60, (byte) 0xD9, (byte) 0xA8, (byte) 0xFD, (byte) 0xD7, (byte) 0x9B, 0x4B, 0x26, 0x06, 0x30, 0x44}; + ByteBuf in = ByteBufAllocator.DEFAULT.heapBuffer(); + in.writeBytes(ciphertext); + + byte[] keyByte = { + 0x24, (byte) 0xad, 0x18, 0x25, 0x07, 0x47, (byte) 0x9a, 0x5d, (byte) 0xa3, (byte) 0xad, (byte) 0x94, (byte) 0xd9, (byte) 0xd7, (byte) 0x8e, (byte) 0xa2, 0x03 + }; + ByteBuf key = ByteBufAllocator.DEFAULT.heapBuffer(); + key.writeBytes(ciphertext); + String resCipherData = decode(ByteBufUtil.hexDump(in)); + ByteBuf out = ByteBufAllocator.DEFAULT.heapBuffer(); +// out.writeBytes(resCipherData); +// System.out.println(hexBytes); + System.out.println(resCipherData.toString()); +// String resPlainData = EcbDecrypt(resCipherData, key); +// System.out.println("原始明文:" + data); +// System.out.println("解密后明文:" + resPlainData); + } + +} + + + diff --git a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java deleted file mode 100644 index 29eac23..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java +++ /dev/null @@ -1,150 +0,0 @@ -package com.casic.missiles.parser.safe.impl; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.buffer.ByteBufUtil; -import org.bouncycastle.crypto.engines.SM4Engine; -import org.bouncycastle.crypto.params.KeyParameter; -import org.bouncycastle.util.encoders.Hex; - -/** - * SM4算法工具类 - */ -public class Sm4CryptStrategy { - - //加解密的字节快大小 - public static final int BlockSize = 16; - - /** - * SM4ECB加密算法,对内 - * - * @param in 待加密字节数组 - * @param keyBytes 密钥 - * @return - */ - private static byte[] ecb_encrypt(byte[] in, byte[] keyBytes) { - - - SM4Engine sm4Engine = new SM4Engine(); - sm4Engine.init(true, new KeyParameter(keyBytes)); - byte[] out = new byte[in.length]; - int times = in.length / BlockSize; - for (int i = 0; i < times; i++) { - sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); - } - return out; - } - - - /** - * SM4ECB加密算法 - * - * @param in 待加密字节数组 - * @param keyBytes 密钥 - * @return - */ - public static String EcbEncrypt(byte[] in, byte[] keyBytes) { - byte[] out = ecb_encrypt(in, keyBytes); - String cipher = Hex.toHexString(out); - return cipher; - } - - /** - * SM4的ECB加密算法 - * - * @param content 待加密字符串, hex字符串 - * @param key 密钥 - * @return - */ - public static String EcbEncrypt(String content, String key) { - byte[] in = Hex.decode(content); - byte[] keyBytes = Hex.decode(key); - - String cipher = EcbEncrypt(in, keyBytes); - return cipher; - } - - /** - * SM4的ECB解密算法,对内 - * - * @param in 输入的密文字符串 - * @param keyBytes 密钥 - * @return - */ - private static byte[] ecb_decrypt(byte[] in, byte[] keyBytes) { - SM4Engine sm4Engine = new SM4Engine(); - sm4Engine.init(false, new KeyParameter(keyBytes)); - int inLen = in.length; - byte[] out = new byte[inLen]; - - int times = inLen / BlockSize; - - for (int i = 0; i < times; i++) { - sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); - } - - return out; - } - - /** - * SM4的ECB解密算法,对外 - * - * @param in 输入的密文字节数组 - * @param keyBytes 密钥 - * @return - */ - public static String EcbDecrypt(byte[] in, byte[] keyBytes) { - byte[] out = ecb_decrypt(in, keyBytes); - String plain = Hex.toHexString(out); - return plain; - } - - /** - * SM4的ECB解密算法 - * - * @param cipher 输入的密文字符串 - * @param key 密钥 - * @return - */ - public static String EcbDecrypt(String cipher, String key) { - byte[] in = Hex.decode(cipher); - byte[] keyBytes = Hex.decode(key); - - String plain = EcbDecrypt(in, keyBytes); - return plain; - } - - /** - * 主函数 - * - * @param args - */ - public static void main(String[] args) { - byte[] ciphertext = { - (byte) 0xE9, 0x1C, (byte) 0xCD, 0x53, 0x02, (byte) 0xD7, 0x0C, (byte) 0x9B, (byte) 0x96, 0x30, 0x62, (byte)0xE8, (byte)0xFB, (byte)0xEF, (byte) 0xB9, (byte)0xFF, - (byte)0xDE, 0x19, (byte)0x80, 0x35, 0x3E, (byte)0x90, 0x4F, 0x2F, 0x12, 0x2B, 0x19, (byte) 0x8B, 0x6C, 0x7A, (byte) 0x85, (byte) 0xA1, - (byte)0xB1, (byte)0x86, (byte)0xA3, 0x7A, 0x46, 0x0C, 0x2F, (byte) 0x94, 0x4F, 0x7F, 0x2C, 0x56, 0x14, (byte) 0xF3, (byte)0x89, (byte) 0xC3, - 0x3F, 0x20, (byte)0xF9, (byte) 0x87, (byte)0xA1, 0x60, (byte)0xD9, (byte)0xA8, (byte) 0xFD, (byte)0xD7, (byte) 0x9B, 0x4B, 0x26, 0x06, 0x30, 0x44}; - String key = "24ad182507479a5da3ad94d9d78ea203"; - ByteBuf in= ByteBufAllocator.DEFAULT.heapBuffer(); - in.writeBytes(ciphertext); - - byte[] keyByte ={ - 0x24,(byte)0xad,0x18,0x25,0x07,0x47,(byte)0x9a,0x5d,(byte)0xa3,(byte)0xad,(byte)0x94,(byte)0xd9,(byte)0xd7,(byte)0x8e,(byte)0xa2,0x03 - }; - byte[] resCipherData = ecb_encrypt(ciphertext, keyByte); - ByteBuf out= ByteBufAllocator.DEFAULT.heapBuffer(); - out.writeBytes(resCipherData); -// System.out.println(hexBytes); - System.out.println(ciphertext.length);//66 - System.out.println(ByteBufUtil.hexDump(out)); -// String resPlainData = EcbDecrypt(resCipherData, key); -// System.out.println("原始明文:" + data); - System.out.println("解密后明文:" + resCipherData.toString()); -// System.out.println("解密后明文:" + resPlainData); - } - -} - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java new file mode 100644 index 0000000..9fa95de --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java @@ -0,0 +1,15 @@ +package com.casic.missiles.predecodec; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; + +import java.util.List; + +public class AbstractPreprocessing extends ByteToMessageDecoder { + + @Override + protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { + list.add(byteBuf); + } +} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/AepPreprocessing.java new file mode 100644 index 0000000..40a5265 --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/AepPreprocessing.java @@ -0,0 +1,53 @@ +package com.casic.missiles.predecodec.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.predecodec.AbstractPreprocessing; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.base64.Base64; +import io.netty.handler.codec.base64.Base64Dialect; +import io.netty.util.internal.ObjectUtil; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; +import java.util.List; + +/** + * @description: 将从接口取到的数据编码 + * @author: cz + * @create: 2023-05-04 15:15 + **/ +@Slf4j +public class AepPreprocessing extends AbstractPreprocessing { + + private final Base64Dialect dialect; + + public AepPreprocessing() { + this(Base64Dialect.STANDARD); + } + + public AepPreprocessing(Base64Dialect dialect) { + this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); + } + + //执行nb平台前的协议解析 预处理 + @Override + protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { + //aep平台处理 + if (msg.toString(Charset.defaultCharset()).contains("aep")) { + String httpContent = msg.toString(Charset.defaultCharset()); + log.info(" String : " + msg.toString(Charset.defaultCharset())); + String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); + String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); + log.info("----------------------------------" + values); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); + out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); + }else { + out.add(msg); + } + } + +} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/NbPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/NbPreprocessing.java new file mode 100644 index 0000000..1b132a7 --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/NbPreprocessing.java @@ -0,0 +1,48 @@ +package com.casic.missiles.predecodec.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.predecodec.AbstractPreprocessing; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.base64.Base64; +import io.netty.handler.codec.base64.Base64Dialect; +import io.netty.util.internal.ObjectUtil; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; +import java.util.List; + +@Slf4j +public class NbPreprocessing extends AbstractPreprocessing { + + private final Base64Dialect dialect; + + public NbPreprocessing() { + this(Base64Dialect.STANDARD); + } + + public NbPreprocessing(Base64Dialect dialect) { + this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); + } + + //执行nb平台前的协议解析,预处理base编码 + @Override + protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { + //aep平台处理 + if (msg.toString(Charset.defaultCharset()).contains("nb")) { + String httpContent = msg.toString(Charset.defaultCharset()); + log.info(" String : " + msg.toString(Charset.defaultCharset())); + String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); + String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); + log.info("----------------------------------" + values); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); + out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); + } else { + out.add(msg); + } + } + +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/sensorhub-core/pom.xml b/sensorhub-core/pom.xml index ae68ea8..2a77c72 100644 --- a/sensorhub-core/pom.xml +++ b/sensorhub-core/pom.xml @@ -58,7 +58,7 @@ org.bouncycastle bcprov-jdk15to18 - 1.64 + 1.71 @@ -75,14 +75,6 @@ 0.0.1-SNAPSHOT - - - org.reflections - reflections - 0.9.10 - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java deleted file mode 100644 index 80c3458..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.casic.missiles.codec; - -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import lombok.extern.slf4j.Slf4j; -import org.reflections.Reflections; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -@Slf4j -public class ClassUtil { - -// public static List get(Class clazz){ -// List abstractPreprocessings = new ArrayList<>(); -// try{ -// String pk = clazz.getPackage().getName(); -// String path = pk.replace('.', '/'); -// //获取该路径下所有类 -// Reflections reflections = new Reflections(path); -// //获取继承了ISuperClass的所有类 -// Set> classSet = reflections.getSubTypesOf(clazz); -// -// for (Class clazz : classSet) { -// // 实例化获取到的类 -// T obj = clazz.newInstance(); -// // TODO 自己的处理逻辑 -// abstractPreprocessings.add(obj); -// } -// }catch (IllegalAccessException iae){ -// -// }catch (InstantiationException iae){ -// -// } -// return abstractPreprocessings; -// } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java deleted file mode 100644 index 3658376..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.casic.missiles.codec.predecodec; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - -import java.util.List; - -public class AbstractPreprocessing extends ByteToMessageDecoder { - - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { - list.add(byteBuf); - } -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java deleted file mode 100644 index ae08584..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; - -import java.nio.charset.Charset; -import java.util.List; - -/** - * @description: 将从接口取到的数据编码 - * @author: cz - * @create: 2023-05-04 15:15 - **/ -@Slf4j -@Component -public class AepPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public AepPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public AepPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析 预处理 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - }else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java deleted file mode 100644 index 6ab1368..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import java.nio.charset.Charset; -import java.util.List; - -@Slf4j -@Component -public class NbPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public NbPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public NbPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析,预处理base编码 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - } else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java index 21c7b07..9f9e279 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java @@ -40,8 +40,6 @@ */ @Autowired private SensorhubProperties sensorhubProperties; - @Autowired - private SensorhubServerChannelInitialHandler sensorhubHandler; public SensorhubServer(SensorhubProperties sensorhubProperties) { this.sensorhubProperties = sensorhubProperties; @@ -76,7 +74,7 @@ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) // 设置EventLoopGroup .channel(NioServerSocketChannel.class) // 指明新的Channel的类型 - .childHandler(sensorhubHandler) // 指定ChannelHandler + .childHandler(new SensorhubServerChannelInitialHandler()) // 指定ChannelHandler .option(ChannelOption.SO_BACKLOG, 1024) // 设置的ServerChannel的一些选项 .childOption(ChannelOption.SO_KEEPALIVE, true); // 当SO_KEEPALIVE=true的时候,服务端可以探测客户端的连接是否还存活着,如果客户端关闭了,那么服务端的连接可以关闭掉,释放资源 // 绑定端口,开始接收进来的连接 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java index 827744f..24a6fef 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java @@ -1,15 +1,13 @@ package com.casic.missiles.netty.handler; -import com.casic.missiles.codec.SensorhubCodec; +import com.casic.missiles.util.ClassUtil; import com.casic.missiles.codec.SensorhubDecoder; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; +import com.casic.missiles.predecodec.AbstractPreprocessing; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import java.util.List; @@ -17,21 +15,18 @@ * @author cz * @date 2023-06-05 */ -@Component public class SensorhubServerChannelInitialHandler extends ChannelInitializer { - @Autowired - private List abstractPreprocessingList; - @Override protected void initChannel(SocketChannel socketChannel) { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); + //添加前置处理器 + ClassUtil classUtil=new ClassUtil(); + List abstractPreprocessingList=classUtil.getSubClassList(AbstractPreprocessing.class); for (AbstractPreprocessing abstractPreprocessing:abstractPreprocessingList) { pipeline.addLast(abstractPreprocessing); } -// pipeline.addLast(new LengthFieldBasedFrameDecoder(1024*100, 0, 2, 0, 2)); - pipeline.addLast("codec", new SensorhubCodec()); pipeline.addLast(new SensorhubDecoder()); pipeline.addLast(new SensorhubServerHandler()); // 心跳续约 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java new file mode 100644 index 0000000..27c2838 --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java @@ -0,0 +1,179 @@ +package com.casic.missiles.parser.safe.impl; + +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.crypto.symmetric.SM4; +import cn.hutool.crypto.symmetric.SymmetricCrypto; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.ByteBufUtil; +import lombok.extern.slf4j.Slf4j; +import org.bouncycastle.crypto.engines.SM4Engine; +import org.bouncycastle.crypto.params.KeyParameter; +import org.bouncycastle.util.encoders.Hex; + +import static cn.hutool.crypto.Mode.ECB; +import static cn.hutool.crypto.Padding.NoPadding; + +/** + * SM4算法工具类 + */ +@Slf4j +public class Sm4 { + + //加解密的字节快大小 + public static final int BlockSize = 16; + + /** + * SM4ECB加密算法,对内 + * + * @param in 待加密字节数组 + * @param keyBytes 密钥 + * @return + */ + private static byte[] ecb_encrypt(byte[] in, byte[] keyBytes) { + + + SM4Engine sm4Engine = new SM4Engine(); + sm4Engine.init(true, new KeyParameter(keyBytes)); + byte[] out = new byte[in.length]; + int times = in.length / BlockSize; + for (int i = 0; i < times; i++) { + sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); + } + return out; + } + + + /** + * SM4ECB加密算法 + * + * @param in 待加密字节数组 + * @param keyBytes 密钥 + * @return + */ + public static String EcbEncrypt(byte[] in, byte[] keyBytes) { + byte[] out = ecb_encrypt(in, keyBytes); + String cipher = Hex.toHexString(out); + return cipher; + } + + + /** + * hutool 解密SM4 + * + * @param encodeContent + * @return + */ + public static String decode(String encodeContent) { + String key = "24ad182507479a5da3ad94d9d78ea203"; + try { + SymmetricCrypto sm4 = new SM4(ECB, NoPadding, key.getBytes()); + String decryptStr = sm4.decryptStr(encodeContent, CharsetUtil.CHARSET_UTF_8); + return decryptStr; + } catch (Exception e) { + e.printStackTrace(); + log.error("SM4解密失败"); + } + return null; + } + + + /** + * SM4的ECB加密算法 + * + * @param content 待加密字符串, hex字符串 + * @param key 密钥 + * @return + */ + public static String EcbEncrypt(String content, String key) { + byte[] in = Hex.decode(content); + byte[] keyBytes = Hex.decode(key); + + String cipher = EcbEncrypt(in, keyBytes); + return cipher; + } + + /** + * SM4的ECB解密算法,对内 + * + * @param in 输入的密文字符串 + * @param keyBytes 密钥 + * @return + */ + private static byte[] ecb_decrypt(byte[] in, byte[] keyBytes) { + SM4Engine sm4Engine = new SM4Engine(); + sm4Engine.init(false, new KeyParameter(keyBytes)); + int inLen = in.length; + byte[] out = new byte[inLen]; + + int times = inLen / BlockSize; + + for (int i = 0; i < times; i++) { + sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); + } + + return out; + } + + /** + * SM4的ECB解密算法,对外 + * + * @param in 输入的密文字节数组 + * @param keyBytes 密钥 + * @return + */ + public static String EcbDecrypt(byte[] in, byte[] keyBytes) { + byte[] out = ecb_decrypt(in, keyBytes); + String plain = Hex.toHexString(out); + return plain; + } + + /** + * SM4的ECB解密算法 + * + * @param cipher 输入的密文字符串 + * @param key 密钥 + * @return + */ + public static String EcbDecrypt(String cipher, String key) { + byte[] in = Hex.decode(cipher); + byte[] keyBytes = Hex.decode(key); + + String plain = EcbDecrypt(in, keyBytes); + return plain; + } + + /** + * 主函数 + * + * @param args + */ + public static void main(String[] args) { + byte[] ciphertext = { + (byte) 0xE9, 0x1C, (byte) 0xCD, 0x53, 0x02, (byte) 0xD7, 0x0C, (byte) 0x9B, (byte) 0x96, 0x30, 0x62, (byte) 0xE8, (byte) 0xFB, (byte) 0xEF, (byte) 0xB9, (byte) 0xFF, + (byte) 0xDE, 0x19, (byte) 0x80, 0x35, 0x3E, (byte) 0x90, 0x4F, 0x2F, 0x12, 0x2B, 0x19, (byte) 0x8B, 0x6C, 0x7A, (byte) 0x85, (byte) 0xA1, + (byte) 0xB1, (byte) 0x86, (byte) 0xA3, 0x7A, 0x46, 0x0C, 0x2F, (byte) 0x94, 0x4F, 0x7F, 0x2C, 0x56, 0x14, (byte) 0xF3, (byte) 0x89, (byte) 0xC3, + 0x3F, 0x20, (byte) 0xF9, (byte) 0x87, (byte) 0xA1, 0x60, (byte) 0xD9, (byte) 0xA8, (byte) 0xFD, (byte) 0xD7, (byte) 0x9B, 0x4B, 0x26, 0x06, 0x30, 0x44}; + ByteBuf in = ByteBufAllocator.DEFAULT.heapBuffer(); + in.writeBytes(ciphertext); + + byte[] keyByte = { + 0x24, (byte) 0xad, 0x18, 0x25, 0x07, 0x47, (byte) 0x9a, 0x5d, (byte) 0xa3, (byte) 0xad, (byte) 0x94, (byte) 0xd9, (byte) 0xd7, (byte) 0x8e, (byte) 0xa2, 0x03 + }; + ByteBuf key = ByteBufAllocator.DEFAULT.heapBuffer(); + key.writeBytes(ciphertext); + String resCipherData = decode(ByteBufUtil.hexDump(in)); + ByteBuf out = ByteBufAllocator.DEFAULT.heapBuffer(); +// out.writeBytes(resCipherData); +// System.out.println(hexBytes); + System.out.println(resCipherData.toString()); +// String resPlainData = EcbDecrypt(resCipherData, key); +// System.out.println("原始明文:" + data); +// System.out.println("解密后明文:" + resPlainData); + } + +} + + + diff --git a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java deleted file mode 100644 index 29eac23..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java +++ /dev/null @@ -1,150 +0,0 @@ -package com.casic.missiles.parser.safe.impl; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.buffer.ByteBufUtil; -import org.bouncycastle.crypto.engines.SM4Engine; -import org.bouncycastle.crypto.params.KeyParameter; -import org.bouncycastle.util.encoders.Hex; - -/** - * SM4算法工具类 - */ -public class Sm4CryptStrategy { - - //加解密的字节快大小 - public static final int BlockSize = 16; - - /** - * SM4ECB加密算法,对内 - * - * @param in 待加密字节数组 - * @param keyBytes 密钥 - * @return - */ - private static byte[] ecb_encrypt(byte[] in, byte[] keyBytes) { - - - SM4Engine sm4Engine = new SM4Engine(); - sm4Engine.init(true, new KeyParameter(keyBytes)); - byte[] out = new byte[in.length]; - int times = in.length / BlockSize; - for (int i = 0; i < times; i++) { - sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); - } - return out; - } - - - /** - * SM4ECB加密算法 - * - * @param in 待加密字节数组 - * @param keyBytes 密钥 - * @return - */ - public static String EcbEncrypt(byte[] in, byte[] keyBytes) { - byte[] out = ecb_encrypt(in, keyBytes); - String cipher = Hex.toHexString(out); - return cipher; - } - - /** - * SM4的ECB加密算法 - * - * @param content 待加密字符串, hex字符串 - * @param key 密钥 - * @return - */ - public static String EcbEncrypt(String content, String key) { - byte[] in = Hex.decode(content); - byte[] keyBytes = Hex.decode(key); - - String cipher = EcbEncrypt(in, keyBytes); - return cipher; - } - - /** - * SM4的ECB解密算法,对内 - * - * @param in 输入的密文字符串 - * @param keyBytes 密钥 - * @return - */ - private static byte[] ecb_decrypt(byte[] in, byte[] keyBytes) { - SM4Engine sm4Engine = new SM4Engine(); - sm4Engine.init(false, new KeyParameter(keyBytes)); - int inLen = in.length; - byte[] out = new byte[inLen]; - - int times = inLen / BlockSize; - - for (int i = 0; i < times; i++) { - sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); - } - - return out; - } - - /** - * SM4的ECB解密算法,对外 - * - * @param in 输入的密文字节数组 - * @param keyBytes 密钥 - * @return - */ - public static String EcbDecrypt(byte[] in, byte[] keyBytes) { - byte[] out = ecb_decrypt(in, keyBytes); - String plain = Hex.toHexString(out); - return plain; - } - - /** - * SM4的ECB解密算法 - * - * @param cipher 输入的密文字符串 - * @param key 密钥 - * @return - */ - public static String EcbDecrypt(String cipher, String key) { - byte[] in = Hex.decode(cipher); - byte[] keyBytes = Hex.decode(key); - - String plain = EcbDecrypt(in, keyBytes); - return plain; - } - - /** - * 主函数 - * - * @param args - */ - public static void main(String[] args) { - byte[] ciphertext = { - (byte) 0xE9, 0x1C, (byte) 0xCD, 0x53, 0x02, (byte) 0xD7, 0x0C, (byte) 0x9B, (byte) 0x96, 0x30, 0x62, (byte)0xE8, (byte)0xFB, (byte)0xEF, (byte) 0xB9, (byte)0xFF, - (byte)0xDE, 0x19, (byte)0x80, 0x35, 0x3E, (byte)0x90, 0x4F, 0x2F, 0x12, 0x2B, 0x19, (byte) 0x8B, 0x6C, 0x7A, (byte) 0x85, (byte) 0xA1, - (byte)0xB1, (byte)0x86, (byte)0xA3, 0x7A, 0x46, 0x0C, 0x2F, (byte) 0x94, 0x4F, 0x7F, 0x2C, 0x56, 0x14, (byte) 0xF3, (byte)0x89, (byte) 0xC3, - 0x3F, 0x20, (byte)0xF9, (byte) 0x87, (byte)0xA1, 0x60, (byte)0xD9, (byte)0xA8, (byte) 0xFD, (byte)0xD7, (byte) 0x9B, 0x4B, 0x26, 0x06, 0x30, 0x44}; - String key = "24ad182507479a5da3ad94d9d78ea203"; - ByteBuf in= ByteBufAllocator.DEFAULT.heapBuffer(); - in.writeBytes(ciphertext); - - byte[] keyByte ={ - 0x24,(byte)0xad,0x18,0x25,0x07,0x47,(byte)0x9a,0x5d,(byte)0xa3,(byte)0xad,(byte)0x94,(byte)0xd9,(byte)0xd7,(byte)0x8e,(byte)0xa2,0x03 - }; - byte[] resCipherData = ecb_encrypt(ciphertext, keyByte); - ByteBuf out= ByteBufAllocator.DEFAULT.heapBuffer(); - out.writeBytes(resCipherData); -// System.out.println(hexBytes); - System.out.println(ciphertext.length);//66 - System.out.println(ByteBufUtil.hexDump(out)); -// String resPlainData = EcbDecrypt(resCipherData, key); -// System.out.println("原始明文:" + data); - System.out.println("解密后明文:" + resCipherData.toString()); -// System.out.println("解密后明文:" + resPlainData); - } - -} - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java new file mode 100644 index 0000000..9fa95de --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java @@ -0,0 +1,15 @@ +package com.casic.missiles.predecodec; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; + +import java.util.List; + +public class AbstractPreprocessing extends ByteToMessageDecoder { + + @Override + protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { + list.add(byteBuf); + } +} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/AepPreprocessing.java new file mode 100644 index 0000000..40a5265 --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/AepPreprocessing.java @@ -0,0 +1,53 @@ +package com.casic.missiles.predecodec.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.predecodec.AbstractPreprocessing; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.base64.Base64; +import io.netty.handler.codec.base64.Base64Dialect; +import io.netty.util.internal.ObjectUtil; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; +import java.util.List; + +/** + * @description: 将从接口取到的数据编码 + * @author: cz + * @create: 2023-05-04 15:15 + **/ +@Slf4j +public class AepPreprocessing extends AbstractPreprocessing { + + private final Base64Dialect dialect; + + public AepPreprocessing() { + this(Base64Dialect.STANDARD); + } + + public AepPreprocessing(Base64Dialect dialect) { + this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); + } + + //执行nb平台前的协议解析 预处理 + @Override + protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { + //aep平台处理 + if (msg.toString(Charset.defaultCharset()).contains("aep")) { + String httpContent = msg.toString(Charset.defaultCharset()); + log.info(" String : " + msg.toString(Charset.defaultCharset())); + String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); + String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); + log.info("----------------------------------" + values); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); + out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); + }else { + out.add(msg); + } + } + +} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/NbPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/NbPreprocessing.java new file mode 100644 index 0000000..1b132a7 --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/NbPreprocessing.java @@ -0,0 +1,48 @@ +package com.casic.missiles.predecodec.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.predecodec.AbstractPreprocessing; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.base64.Base64; +import io.netty.handler.codec.base64.Base64Dialect; +import io.netty.util.internal.ObjectUtil; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; +import java.util.List; + +@Slf4j +public class NbPreprocessing extends AbstractPreprocessing { + + private final Base64Dialect dialect; + + public NbPreprocessing() { + this(Base64Dialect.STANDARD); + } + + public NbPreprocessing(Base64Dialect dialect) { + this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); + } + + //执行nb平台前的协议解析,预处理base编码 + @Override + protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { + //aep平台处理 + if (msg.toString(Charset.defaultCharset()).contains("nb")) { + String httpContent = msg.toString(Charset.defaultCharset()); + log.info(" String : " + msg.toString(Charset.defaultCharset())); + String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); + String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); + log.info("----------------------------------" + values); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); + out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); + } else { + out.add(msg); + } + } + +} diff --git a/sensorhub-support/pom.xml b/sensorhub-support/pom.xml index 8fa2789..9a71296 100644 --- a/sensorhub-support/pom.xml +++ b/sensorhub-support/pom.xml @@ -42,6 +42,13 @@ ${redis.version} + + + org.reflections + reflections + 0.9.10 + + \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e20bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +**/target/** +logs/ +*.iml diff --git a/sensorhub-core/.gitignore b/sensorhub-core/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/sensorhub-core/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/sensorhub-core/pom.xml b/sensorhub-core/pom.xml index ae68ea8..2a77c72 100644 --- a/sensorhub-core/pom.xml +++ b/sensorhub-core/pom.xml @@ -58,7 +58,7 @@ org.bouncycastle bcprov-jdk15to18 - 1.64 + 1.71 @@ -75,14 +75,6 @@ 0.0.1-SNAPSHOT - - - org.reflections - reflections - 0.9.10 - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java deleted file mode 100644 index 80c3458..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/ClassUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.casic.missiles.codec; - -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import lombok.extern.slf4j.Slf4j; -import org.reflections.Reflections; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -@Slf4j -public class ClassUtil { - -// public static List get(Class clazz){ -// List abstractPreprocessings = new ArrayList<>(); -// try{ -// String pk = clazz.getPackage().getName(); -// String path = pk.replace('.', '/'); -// //获取该路径下所有类 -// Reflections reflections = new Reflections(path); -// //获取继承了ISuperClass的所有类 -// Set> classSet = reflections.getSubTypesOf(clazz); -// -// for (Class clazz : classSet) { -// // 实例化获取到的类 -// T obj = clazz.newInstance(); -// // TODO 自己的处理逻辑 -// abstractPreprocessings.add(obj); -// } -// }catch (IllegalAccessException iae){ -// -// }catch (InstantiationException iae){ -// -// } -// return abstractPreprocessings; -// } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java deleted file mode 100644 index 3658376..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/AbstractPreprocessing.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.casic.missiles.codec.predecodec; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - -import java.util.List; - -public class AbstractPreprocessing extends ByteToMessageDecoder { - - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { - list.add(byteBuf); - } -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java deleted file mode 100644 index ae08584..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/AepPreprocessing.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; - -import java.nio.charset.Charset; -import java.util.List; - -/** - * @description: 将从接口取到的数据编码 - * @author: cz - * @create: 2023-05-04 15:15 - **/ -@Slf4j -@Component -public class AepPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public AepPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public AepPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析 预处理 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - }else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java deleted file mode 100644 index 6ab1368..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/codec/predecodec/impl/NbPreprocessing.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.casic.missiles.codec.predecodec.impl; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.base64.Base64; -import io.netty.handler.codec.base64.Base64Dialect; -import io.netty.util.internal.ObjectUtil; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import java.nio.charset.Charset; -import java.util.List; - -@Slf4j -@Component -public class NbPreprocessing extends AbstractPreprocessing { - - private final Base64Dialect dialect; - - public NbPreprocessing() { - this(Base64Dialect.STANDARD); - } - - public NbPreprocessing(Base64Dialect dialect) { - this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); - } - - //执行nb平台前的协议解析,预处理base编码 - @Override - protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { - //aep平台处理 - if (msg.toString(Charset.defaultCharset()).contains("aep")) { - String httpContent = msg.toString(Charset.defaultCharset()); - log.info(" String : " + msg.toString(Charset.defaultCharset())); - String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); - String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); - log.info("----------------------------------" + values); - ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); - bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); - out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); - } else { - out.add(msg); - } - } - -} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java index 21c7b07..9f9e279 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/SensorhubServer.java @@ -40,8 +40,6 @@ */ @Autowired private SensorhubProperties sensorhubProperties; - @Autowired - private SensorhubServerChannelInitialHandler sensorhubHandler; public SensorhubServer(SensorhubProperties sensorhubProperties) { this.sensorhubProperties = sensorhubProperties; @@ -76,7 +74,7 @@ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) // 设置EventLoopGroup .channel(NioServerSocketChannel.class) // 指明新的Channel的类型 - .childHandler(sensorhubHandler) // 指定ChannelHandler + .childHandler(new SensorhubServerChannelInitialHandler()) // 指定ChannelHandler .option(ChannelOption.SO_BACKLOG, 1024) // 设置的ServerChannel的一些选项 .childOption(ChannelOption.SO_KEEPALIVE, true); // 当SO_KEEPALIVE=true的时候,服务端可以探测客户端的连接是否还存活着,如果客户端关闭了,那么服务端的连接可以关闭掉,释放资源 // 绑定端口,开始接收进来的连接 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java index 827744f..24a6fef 100644 --- a/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java +++ b/sensorhub-core/src/main/java/com/casic/missiles/netty/handler/SensorhubServerChannelInitialHandler.java @@ -1,15 +1,13 @@ package com.casic.missiles.netty.handler; -import com.casic.missiles.codec.SensorhubCodec; +import com.casic.missiles.util.ClassUtil; import com.casic.missiles.codec.SensorhubDecoder; -import com.casic.missiles.codec.predecodec.AbstractPreprocessing; +import com.casic.missiles.predecodec.AbstractPreprocessing; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import java.util.List; @@ -17,21 +15,18 @@ * @author cz * @date 2023-06-05 */ -@Component public class SensorhubServerChannelInitialHandler extends ChannelInitializer { - @Autowired - private List abstractPreprocessingList; - @Override protected void initChannel(SocketChannel socketChannel) { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); + //添加前置处理器 + ClassUtil classUtil=new ClassUtil(); + List abstractPreprocessingList=classUtil.getSubClassList(AbstractPreprocessing.class); for (AbstractPreprocessing abstractPreprocessing:abstractPreprocessingList) { pipeline.addLast(abstractPreprocessing); } -// pipeline.addLast(new LengthFieldBasedFrameDecoder(1024*100, 0, 2, 0, 2)); - pipeline.addLast("codec", new SensorhubCodec()); pipeline.addLast(new SensorhubDecoder()); pipeline.addLast(new SensorhubServerHandler()); // 心跳续约 diff --git a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java new file mode 100644 index 0000000..27c2838 --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4.java @@ -0,0 +1,179 @@ +package com.casic.missiles.parser.safe.impl; + +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.crypto.symmetric.SM4; +import cn.hutool.crypto.symmetric.SymmetricCrypto; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.ByteBufUtil; +import lombok.extern.slf4j.Slf4j; +import org.bouncycastle.crypto.engines.SM4Engine; +import org.bouncycastle.crypto.params.KeyParameter; +import org.bouncycastle.util.encoders.Hex; + +import static cn.hutool.crypto.Mode.ECB; +import static cn.hutool.crypto.Padding.NoPadding; + +/** + * SM4算法工具类 + */ +@Slf4j +public class Sm4 { + + //加解密的字节快大小 + public static final int BlockSize = 16; + + /** + * SM4ECB加密算法,对内 + * + * @param in 待加密字节数组 + * @param keyBytes 密钥 + * @return + */ + private static byte[] ecb_encrypt(byte[] in, byte[] keyBytes) { + + + SM4Engine sm4Engine = new SM4Engine(); + sm4Engine.init(true, new KeyParameter(keyBytes)); + byte[] out = new byte[in.length]; + int times = in.length / BlockSize; + for (int i = 0; i < times; i++) { + sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); + } + return out; + } + + + /** + * SM4ECB加密算法 + * + * @param in 待加密字节数组 + * @param keyBytes 密钥 + * @return + */ + public static String EcbEncrypt(byte[] in, byte[] keyBytes) { + byte[] out = ecb_encrypt(in, keyBytes); + String cipher = Hex.toHexString(out); + return cipher; + } + + + /** + * hutool 解密SM4 + * + * @param encodeContent + * @return + */ + public static String decode(String encodeContent) { + String key = "24ad182507479a5da3ad94d9d78ea203"; + try { + SymmetricCrypto sm4 = new SM4(ECB, NoPadding, key.getBytes()); + String decryptStr = sm4.decryptStr(encodeContent, CharsetUtil.CHARSET_UTF_8); + return decryptStr; + } catch (Exception e) { + e.printStackTrace(); + log.error("SM4解密失败"); + } + return null; + } + + + /** + * SM4的ECB加密算法 + * + * @param content 待加密字符串, hex字符串 + * @param key 密钥 + * @return + */ + public static String EcbEncrypt(String content, String key) { + byte[] in = Hex.decode(content); + byte[] keyBytes = Hex.decode(key); + + String cipher = EcbEncrypt(in, keyBytes); + return cipher; + } + + /** + * SM4的ECB解密算法,对内 + * + * @param in 输入的密文字符串 + * @param keyBytes 密钥 + * @return + */ + private static byte[] ecb_decrypt(byte[] in, byte[] keyBytes) { + SM4Engine sm4Engine = new SM4Engine(); + sm4Engine.init(false, new KeyParameter(keyBytes)); + int inLen = in.length; + byte[] out = new byte[inLen]; + + int times = inLen / BlockSize; + + for (int i = 0; i < times; i++) { + sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); + } + + return out; + } + + /** + * SM4的ECB解密算法,对外 + * + * @param in 输入的密文字节数组 + * @param keyBytes 密钥 + * @return + */ + public static String EcbDecrypt(byte[] in, byte[] keyBytes) { + byte[] out = ecb_decrypt(in, keyBytes); + String plain = Hex.toHexString(out); + return plain; + } + + /** + * SM4的ECB解密算法 + * + * @param cipher 输入的密文字符串 + * @param key 密钥 + * @return + */ + public static String EcbDecrypt(String cipher, String key) { + byte[] in = Hex.decode(cipher); + byte[] keyBytes = Hex.decode(key); + + String plain = EcbDecrypt(in, keyBytes); + return plain; + } + + /** + * 主函数 + * + * @param args + */ + public static void main(String[] args) { + byte[] ciphertext = { + (byte) 0xE9, 0x1C, (byte) 0xCD, 0x53, 0x02, (byte) 0xD7, 0x0C, (byte) 0x9B, (byte) 0x96, 0x30, 0x62, (byte) 0xE8, (byte) 0xFB, (byte) 0xEF, (byte) 0xB9, (byte) 0xFF, + (byte) 0xDE, 0x19, (byte) 0x80, 0x35, 0x3E, (byte) 0x90, 0x4F, 0x2F, 0x12, 0x2B, 0x19, (byte) 0x8B, 0x6C, 0x7A, (byte) 0x85, (byte) 0xA1, + (byte) 0xB1, (byte) 0x86, (byte) 0xA3, 0x7A, 0x46, 0x0C, 0x2F, (byte) 0x94, 0x4F, 0x7F, 0x2C, 0x56, 0x14, (byte) 0xF3, (byte) 0x89, (byte) 0xC3, + 0x3F, 0x20, (byte) 0xF9, (byte) 0x87, (byte) 0xA1, 0x60, (byte) 0xD9, (byte) 0xA8, (byte) 0xFD, (byte) 0xD7, (byte) 0x9B, 0x4B, 0x26, 0x06, 0x30, 0x44}; + ByteBuf in = ByteBufAllocator.DEFAULT.heapBuffer(); + in.writeBytes(ciphertext); + + byte[] keyByte = { + 0x24, (byte) 0xad, 0x18, 0x25, 0x07, 0x47, (byte) 0x9a, 0x5d, (byte) 0xa3, (byte) 0xad, (byte) 0x94, (byte) 0xd9, (byte) 0xd7, (byte) 0x8e, (byte) 0xa2, 0x03 + }; + ByteBuf key = ByteBufAllocator.DEFAULT.heapBuffer(); + key.writeBytes(ciphertext); + String resCipherData = decode(ByteBufUtil.hexDump(in)); + ByteBuf out = ByteBufAllocator.DEFAULT.heapBuffer(); +// out.writeBytes(resCipherData); +// System.out.println(hexBytes); + System.out.println(resCipherData.toString()); +// String resPlainData = EcbDecrypt(resCipherData, key); +// System.out.println("原始明文:" + data); +// System.out.println("解密后明文:" + resPlainData); + } + +} + + + diff --git a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java b/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java deleted file mode 100644 index 29eac23..0000000 --- a/sensorhub-core/src/main/java/com/casic/missiles/parser/safe/impl/Sm4CryptStrategy.java +++ /dev/null @@ -1,150 +0,0 @@ -package com.casic.missiles.parser.safe.impl; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.buffer.ByteBufUtil; -import org.bouncycastle.crypto.engines.SM4Engine; -import org.bouncycastle.crypto.params.KeyParameter; -import org.bouncycastle.util.encoders.Hex; - -/** - * SM4算法工具类 - */ -public class Sm4CryptStrategy { - - //加解密的字节快大小 - public static final int BlockSize = 16; - - /** - * SM4ECB加密算法,对内 - * - * @param in 待加密字节数组 - * @param keyBytes 密钥 - * @return - */ - private static byte[] ecb_encrypt(byte[] in, byte[] keyBytes) { - - - SM4Engine sm4Engine = new SM4Engine(); - sm4Engine.init(true, new KeyParameter(keyBytes)); - byte[] out = new byte[in.length]; - int times = in.length / BlockSize; - for (int i = 0; i < times; i++) { - sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); - } - return out; - } - - - /** - * SM4ECB加密算法 - * - * @param in 待加密字节数组 - * @param keyBytes 密钥 - * @return - */ - public static String EcbEncrypt(byte[] in, byte[] keyBytes) { - byte[] out = ecb_encrypt(in, keyBytes); - String cipher = Hex.toHexString(out); - return cipher; - } - - /** - * SM4的ECB加密算法 - * - * @param content 待加密字符串, hex字符串 - * @param key 密钥 - * @return - */ - public static String EcbEncrypt(String content, String key) { - byte[] in = Hex.decode(content); - byte[] keyBytes = Hex.decode(key); - - String cipher = EcbEncrypt(in, keyBytes); - return cipher; - } - - /** - * SM4的ECB解密算法,对内 - * - * @param in 输入的密文字符串 - * @param keyBytes 密钥 - * @return - */ - private static byte[] ecb_decrypt(byte[] in, byte[] keyBytes) { - SM4Engine sm4Engine = new SM4Engine(); - sm4Engine.init(false, new KeyParameter(keyBytes)); - int inLen = in.length; - byte[] out = new byte[inLen]; - - int times = inLen / BlockSize; - - for (int i = 0; i < times; i++) { - sm4Engine.processBlock(in, i * BlockSize, out, i * BlockSize); - } - - return out; - } - - /** - * SM4的ECB解密算法,对外 - * - * @param in 输入的密文字节数组 - * @param keyBytes 密钥 - * @return - */ - public static String EcbDecrypt(byte[] in, byte[] keyBytes) { - byte[] out = ecb_decrypt(in, keyBytes); - String plain = Hex.toHexString(out); - return plain; - } - - /** - * SM4的ECB解密算法 - * - * @param cipher 输入的密文字符串 - * @param key 密钥 - * @return - */ - public static String EcbDecrypt(String cipher, String key) { - byte[] in = Hex.decode(cipher); - byte[] keyBytes = Hex.decode(key); - - String plain = EcbDecrypt(in, keyBytes); - return plain; - } - - /** - * 主函数 - * - * @param args - */ - public static void main(String[] args) { - byte[] ciphertext = { - (byte) 0xE9, 0x1C, (byte) 0xCD, 0x53, 0x02, (byte) 0xD7, 0x0C, (byte) 0x9B, (byte) 0x96, 0x30, 0x62, (byte)0xE8, (byte)0xFB, (byte)0xEF, (byte) 0xB9, (byte)0xFF, - (byte)0xDE, 0x19, (byte)0x80, 0x35, 0x3E, (byte)0x90, 0x4F, 0x2F, 0x12, 0x2B, 0x19, (byte) 0x8B, 0x6C, 0x7A, (byte) 0x85, (byte) 0xA1, - (byte)0xB1, (byte)0x86, (byte)0xA3, 0x7A, 0x46, 0x0C, 0x2F, (byte) 0x94, 0x4F, 0x7F, 0x2C, 0x56, 0x14, (byte) 0xF3, (byte)0x89, (byte) 0xC3, - 0x3F, 0x20, (byte)0xF9, (byte) 0x87, (byte)0xA1, 0x60, (byte)0xD9, (byte)0xA8, (byte) 0xFD, (byte)0xD7, (byte) 0x9B, 0x4B, 0x26, 0x06, 0x30, 0x44}; - String key = "24ad182507479a5da3ad94d9d78ea203"; - ByteBuf in= ByteBufAllocator.DEFAULT.heapBuffer(); - in.writeBytes(ciphertext); - - byte[] keyByte ={ - 0x24,(byte)0xad,0x18,0x25,0x07,0x47,(byte)0x9a,0x5d,(byte)0xa3,(byte)0xad,(byte)0x94,(byte)0xd9,(byte)0xd7,(byte)0x8e,(byte)0xa2,0x03 - }; - byte[] resCipherData = ecb_encrypt(ciphertext, keyByte); - ByteBuf out= ByteBufAllocator.DEFAULT.heapBuffer(); - out.writeBytes(resCipherData); -// System.out.println(hexBytes); - System.out.println(ciphertext.length);//66 - System.out.println(ByteBufUtil.hexDump(out)); -// String resPlainData = EcbDecrypt(resCipherData, key); -// System.out.println("原始明文:" + data); - System.out.println("解密后明文:" + resCipherData.toString()); -// System.out.println("解密后明文:" + resPlainData); - } - -} - - - diff --git a/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java new file mode 100644 index 0000000..9fa95de --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/AbstractPreprocessing.java @@ -0,0 +1,15 @@ +package com.casic.missiles.predecodec; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; + +import java.util.List; + +public class AbstractPreprocessing extends ByteToMessageDecoder { + + @Override + protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception { + list.add(byteBuf); + } +} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/AepPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/AepPreprocessing.java new file mode 100644 index 0000000..40a5265 --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/AepPreprocessing.java @@ -0,0 +1,53 @@ +package com.casic.missiles.predecodec.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.predecodec.AbstractPreprocessing; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.base64.Base64; +import io.netty.handler.codec.base64.Base64Dialect; +import io.netty.util.internal.ObjectUtil; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; +import java.util.List; + +/** + * @description: 将从接口取到的数据编码 + * @author: cz + * @create: 2023-05-04 15:15 + **/ +@Slf4j +public class AepPreprocessing extends AbstractPreprocessing { + + private final Base64Dialect dialect; + + public AepPreprocessing() { + this(Base64Dialect.STANDARD); + } + + public AepPreprocessing(Base64Dialect dialect) { + this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); + } + + //执行nb平台前的协议解析 预处理 + @Override + protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { + //aep平台处理 + if (msg.toString(Charset.defaultCharset()).contains("aep")) { + String httpContent = msg.toString(Charset.defaultCharset()); + log.info(" String : " + msg.toString(Charset.defaultCharset())); + String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); + String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); + log.info("----------------------------------" + values); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); + out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); + }else { + out.add(msg); + } + } + +} diff --git a/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/NbPreprocessing.java b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/NbPreprocessing.java new file mode 100644 index 0000000..1b132a7 --- /dev/null +++ b/sensorhub-core/src/main/java/com/casic/missiles/predecodec/impl/NbPreprocessing.java @@ -0,0 +1,48 @@ +package com.casic.missiles.predecodec.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.predecodec.AbstractPreprocessing; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.base64.Base64; +import io.netty.handler.codec.base64.Base64Dialect; +import io.netty.util.internal.ObjectUtil; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; +import java.util.List; + +@Slf4j +public class NbPreprocessing extends AbstractPreprocessing { + + private final Base64Dialect dialect; + + public NbPreprocessing() { + this(Base64Dialect.STANDARD); + } + + public NbPreprocessing(Base64Dialect dialect) { + this.dialect = ObjectUtil.checkNotNull(dialect, "dialect"); + } + + //执行nb平台前的协议解析,预处理base编码 + @Override + protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List out) throws Exception { + //aep平台处理 + if (msg.toString(Charset.defaultCharset()).contains("nb")) { + String httpContent = msg.toString(Charset.defaultCharset()); + log.info(" String : " + msg.toString(Charset.defaultCharset())); + String majorContent = httpContent.substring(httpContent.indexOf("{"), httpContent.lastIndexOf("}") + 1); + String values = String.valueOf(((JSONObject) ((JSONObject) JSON.parseObject(majorContent).get("payload")).get("serviceData")).get("Value")); + log.info("----------------------------------" + values); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(values.getBytes(Charset.forName("ISO-8859-1"))); + out.add(Base64.decode(bufferContent, bufferContent.readerIndex(), bufferContent.readableBytes(), this.dialect)); + } else { + out.add(msg); + } + } + +} diff --git a/sensorhub-support/pom.xml b/sensorhub-support/pom.xml index 8fa2789..9a71296 100644 --- a/sensorhub-support/pom.xml +++ b/sensorhub-support/pom.xml @@ -42,6 +42,13 @@ ${redis.version} + + + org.reflections + reflections + 0.9.10 + + \ No newline at end of file diff --git a/sensorhub-support/src/main/java/com/casic/missiles/util/ClassUtil.java b/sensorhub-support/src/main/java/com/casic/missiles/util/ClassUtil.java new file mode 100644 index 0000000..2d37fcc --- /dev/null +++ b/sensorhub-support/src/main/java/com/casic/missiles/util/ClassUtil.java @@ -0,0 +1,43 @@ +package com.casic.missiles.util; + +import lombok.extern.slf4j.Slf4j; +import org.reflections.Reflections; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +@Slf4j +public class ClassUtil { + + /** + * 反射获取所在类下左右子类集合 + * + * @param clazz + * @return + */ + public List getSubClassList(Class clazz) { + List subclassList = new ArrayList<>(); + try { + //获取对应的类路径 + String pk = clazz.getPackage().getName(); + String path = pk.replace('.', '/'); + //获取该路径下所有类 + Reflections reflections = new Reflections(path); + //获取继承了ISuperClass的所有类 + Set> classSet = reflections.getSubTypesOf(clazz); + for (Class subClazz : classSet) { + // 实例化获取到的类 + T obj = subClazz.newInstance(); + // 状态 + subclassList.add(obj); + } + } catch (IllegalAccessException iae) { + log.error("Access permission exception,the error is {} ", iae); + } catch (InstantiationException ie) { + log.error("create instance is faild, the error is {}", ie); + } + return subclassList; + } + +}