From af298b6ae74af05eefeea37b98926735e067661b Mon Sep 17 00:00:00 2001 From: CaiHQ Date: Fri, 31 Mar 2023 17:10:40 +0800 Subject: [PATCH] prune old doi logic --- build.gradle | 4 +- .../org/bdware/server/action/CMActions.java | 2 +- .../org/bdware/server/action/FileActions.java | 2 +- .../bdware/server/action/MasterWSAction.java | 9 ---- .../doip/ContractRepositoryHandler.java | 3 +- .../server/doip/ContractRepositoryMain.java | 51 +++++++++++++++++++ .../server/doip/DelegateDoipHandler.java | 22 ++++++++ .../client/NodeCenterClientController.java | 1 - 8 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 src/main/java/org/bdware/server/doip/DelegateDoipHandler.java diff --git a/build.gradle b/build.gradle index 0aad91f..4f17db3 100644 --- a/build.gradle +++ b/build.gradle @@ -40,8 +40,8 @@ dependencies { implementation 'io.grpc:grpc-all:1.43.1' implementation 'org.apache.velocity:velocity-engine-core:2.3' implementation 'com.nimbusds:nimbus-jose-jwt:9.10' - implementation 'org.bdware.doip:doip-sdk:1.3.8' - implementation 'org.bdware.doip:doip-audit-tool:1.2.0' + implementation 'org.bdware.doip:doip-sdk:1.3.9' + implementation 'org.bdware.doip:doip-audit-tool:1.2.1' implementation 'org.bdware.doip:bdosclient:0.0.2' implementation fileTree(dir: 'lib', include: '*.jar') testImplementation 'junit:junit:4.13.2' diff --git a/src/main/java/org/bdware/server/action/CMActions.java b/src/main/java/org/bdware/server/action/CMActions.java index 0b62505..b90eaef 100644 --- a/src/main/java/org/bdware/server/action/CMActions.java +++ b/src/main/java/org/bdware/server/action/CMActions.java @@ -1320,7 +1320,7 @@ public class CMActions implements OnHashCallback { String ret; try { ContractClient cc = manager.getClient(args.get("id").getAsString()); - ret = cc.getContractDOI(); + ret = cc.getContractID(); } catch (Exception e) { ByteArrayOutputStream bo = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(bo)); diff --git a/src/main/java/org/bdware/server/action/FileActions.java b/src/main/java/org/bdware/server/action/FileActions.java index ffb4e4f..84e2282 100644 --- a/src/main/java/org/bdware/server/action/FileActions.java +++ b/src/main/java/org/bdware/server/action/FileActions.java @@ -416,7 +416,7 @@ public class FileActions { c.setScript(ypkPath); c.setOwner(owner); c.setDebug(isDebug); - CMActions.manager.startContractAndRedirect(c, System.out, null); // createPS() + CMActions.manager.startContractAndRedirect(c, System.out); // createPS() } } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/org/bdware/server/action/MasterWSAction.java b/src/main/java/org/bdware/server/action/MasterWSAction.java index 23b364b..ece937d 100644 --- a/src/main/java/org/bdware/server/action/MasterWSAction.java +++ b/src/main/java/org/bdware/server/action/MasterWSAction.java @@ -207,15 +207,6 @@ public class MasterWSAction { // NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(requestConnect)); // 向NC发 //waitForConnection(nodeNames); LOGGER.debug(JsonUtil.toPrettyJson(requestConnect)); - ContractManager.threadPool.execute(() -> { - // 多点合约更新repository信息 - if (contract.getDOI() != null && !contract.getDOI().equals("null")) { - LOGGER.info("multipoint contract doi register"); - GRPCPool.instance.reRegister(contract.getDOI()); - } else { - LOGGER.info("multipoint contract don't update register " + (null == contract.getDOI() ? "null" : contract.getDOI())); - } - }); final long curr = System.currentTimeMillis(); String requestID = curr + "_" + (int) (Math.random() * 10000); ResultCollector collector = new ResultCollector(requestID, new ResultCallback() { diff --git a/src/main/java/org/bdware/server/doip/ContractRepositoryHandler.java b/src/main/java/org/bdware/server/doip/ContractRepositoryHandler.java index 7e402f5..60adebf 100644 --- a/src/main/java/org/bdware/server/doip/ContractRepositoryHandler.java +++ b/src/main/java/org/bdware/server/doip/ContractRepositoryHandler.java @@ -7,7 +7,6 @@ import com.google.gson.JsonPrimitive; import io.netty.channel.ChannelHandler; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.bdware.doip.audit.EndpointConfig; import org.bdware.doip.audit.server.DefaultRepoHandlerInjector; import org.bdware.doip.codec.digitalObject.DigitalObject; import org.bdware.doip.codec.digitalObject.DoType; @@ -205,7 +204,7 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements } private DigitalObject contract2DigitalObject(ContractClient contractClient) { - DigitalObject contractDO = new DigitalObject(contractClient.getContractDOI(), DoType.DO); + DigitalObject contractDO = new DigitalObject(contractClient.getContractID(), DoType.DO); contractDO.addAttribute("contractName", contractClient.getContractName()); // contractDO.addAttribute("script",contract.contract.getScriptStr()); contractDO.addAttribute("owner", contractClient.contractMeta.contract.getOwner()); diff --git a/src/main/java/org/bdware/server/doip/ContractRepositoryMain.java b/src/main/java/org/bdware/server/doip/ContractRepositoryMain.java index 3d9c9aa..fbea2e8 100644 --- a/src/main/java/org/bdware/server/doip/ContractRepositoryMain.java +++ b/src/main/java/org/bdware/server/doip/ContractRepositoryMain.java @@ -1,15 +1,25 @@ package org.bdware.server.doip; import com.google.gson.Gson; +import io.netty.channel.ChannelHandlerContext; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bdware.doip.audit.EndpointConfig; import org.bdware.doip.audit.server.AuditDoipServer; import org.bdware.doip.audit.writer.AuditType; +import org.bdware.doip.codec.doipMessage.DoipMessage; +import org.bdware.doip.codec.doipMessage.DoipMessageFactory; +import org.bdware.doip.codec.doipMessage.DoipResponseCode; +import org.bdware.doip.encrypt.SM2Signer; import org.bdware.doip.endpoint.server.DoipListenerConfig; +import org.bdware.doip.endpoint.server.DoipRequestHandler; import org.bdware.doip.endpoint.server.DoipServiceInfo; +import org.bdware.sc.ContractMeta; import org.bdware.server.GlobalConf; +import org.bdware.server.action.CMActions; +import org.zz.gmhelper.SM2KeyPair; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -40,6 +50,47 @@ public class ContractRepositoryMain { config.repoName = GlobalConf.instance.doaConf.repoName; currentServer = new AuditDoipServer(config, info); currentServer.setRepositoryHandler(new ContractRepositoryHandler(info)); + DoipRequestHandler requestCallback = currentServer.getRequestCallback(); + SM2Signer signer = new SM2Signer(SM2KeyPair.fromJson(new Gson().toJson(config))); + DelegateDoipHandler delegateHandler = new DelegateDoipHandler(requestCallback) { + @Override + protected DoipMessage delegateMessage(ChannelHandlerContext context, DoipMessage message) { + DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); + builder.createResponse(DoipResponseCode.DelegateRequired, message); + String id = message.header.parameters.id; + id = id.replaceAll(".*/", ""); + ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(id); + builder.addAttributes("port", meta.contract.getDoipPort()); + DoipMessage ret; + if (message.credential != null && message.credential.getSigner() != null) { + if (signer.verifyMessage(message)) { + ret = builder.create(); + } else { + DoipMessageFactory.DoipMessageBuilder builder2 = new DoipMessageFactory.DoipMessageBuilder(); + DoipMessageFactory.DoipMessageBuilder resp = builder2.createResponse(DoipResponseCode.UnAuth_Client, message); + resp.setBody("verify failed".getBytes(StandardCharsets.UTF_8)); + ret = resp.create(); + } + signer.signMessage(ret); + } else { + ret = builder.create(); + } + return ret; + } + + @Override + public boolean accept(ChannelHandlerContext context, DoipMessage message) { + String id = message.header.parameters.id; + if (id != null) { + id = id.replaceAll(".*/", ""); + ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(id); + if (meta != null) + return true; + } + return false; + } + }; + currentServer.setRequestCallback(delegateHandler); currentServer.start(); } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/org/bdware/server/doip/DelegateDoipHandler.java b/src/main/java/org/bdware/server/doip/DelegateDoipHandler.java new file mode 100644 index 0000000..5da9d76 --- /dev/null +++ b/src/main/java/org/bdware/server/doip/DelegateDoipHandler.java @@ -0,0 +1,22 @@ +package org.bdware.server.doip; + +import io.netty.channel.ChannelHandlerContext; +import org.bdware.doip.codec.doipMessage.DoipMessage; +import org.bdware.doip.endpoint.server.DoipRequestHandler; + +public abstract class DelegateDoipHandler implements DoipRequestHandler { + private final DoipRequestHandler original; + + public DelegateDoipHandler(DoipRequestHandler original) { + this.original = original; + } + + public DoipMessage onRequest(ChannelHandlerContext context, DoipMessage doipMessage) { + if (accept(context, doipMessage)) { + return delegateMessage(context, doipMessage); + } else return original.onRequest(context, doipMessage); + } + + protected abstract DoipMessage delegateMessage(ChannelHandlerContext context, DoipMessage message); + public abstract boolean accept(ChannelHandlerContext context, DoipMessage message); +} diff --git a/src/main/java/org/bdware/server/nodecenter/client/NodeCenterClientController.java b/src/main/java/org/bdware/server/nodecenter/client/NodeCenterClientController.java index 8b605cd..7a8471f 100644 --- a/src/main/java/org/bdware/server/nodecenter/client/NodeCenterClientController.java +++ b/src/main/java/org/bdware/server/nodecenter/client/NodeCenterClientController.java @@ -246,7 +246,6 @@ public class NodeCenterClientController implements NodeCenterConn { resp.addProperty("pubkey", meta.contract.getPublicKey()); resp.addProperty("name", meta.getName()); resp.addProperty("buildTime", meta.contract.getBuildTime()); - resp.addProperty("doi", meta.contract.getDOI()); ZipFile zipFile = new ZipFile(new File(meta.contract.getScriptStr())); ZipEntry zipEntry = zipFile.getEntry("/assets/README.md"); if (null == zipEntry) {