diff --git a/build.gradle b/build.gradle index 94f3268..9f9a47c 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,11 @@ plugins { } group = "org.bdware.sc" +<<<<<<< HEAD version = "2.0.0" +======= +version = "1.7.2" +>>>>>>> origin/master repositories { mavenCentral() mavenLocal() @@ -61,7 +65,7 @@ jar { // uncomment this when publish, //while develop at local use "false" configurations.runtimeClasspath.filter { -// it.getAbsolutePath().contains("/lib/") + // it.getAbsolutePath().contains("/lib/") false }.collect { it.isDirectory() ? it : zipTree(it) diff --git a/src/main/debugger/org/bdware/sc/debugger/DebugMain.java b/src/main/debugger/org/bdware/sc/debugger/DebugMain.java index a84ca6d..8ce15f1 100644 --- a/src/main/debugger/org/bdware/sc/debugger/DebugMain.java +++ b/src/main/debugger/org/bdware/sc/debugger/DebugMain.java @@ -63,7 +63,8 @@ public class DebugMain { = new ResultCallback() { @Override public void onResult(String str) { - LOGGER.debug(str); + + LOGGER.info("[PrintCB] " + str); } }; ContractProcess.instance.handler.setDBInfo(wrap("", config.dbPath), printCallback); @@ -80,7 +81,7 @@ public class DebugMain { Map resp = HttpUtil.httpGet(url); String data = (String) resp.get("response"); -// LOGGER.info(JsonUtil.toPrettyJson(JsonUtil.parseStringAsJsonObject(data))); + LOGGER.info(JsonUtil.toPrettyJson(JsonUtil.parseStringAsJsonObject(data))); LOGGER.info("start done!"); } diff --git a/src/main/java/org/bdware/sc/ContractProcess.java b/src/main/java/org/bdware/sc/ContractProcess.java index a9875d4..c58cbbf 100644 --- a/src/main/java/org/bdware/sc/ContractProcess.java +++ b/src/main/java/org/bdware/sc/ContractProcess.java @@ -554,7 +554,6 @@ public class ContractProcess { if (fun.isConfidential()) { fun.appendBeforeInvokeHandler(new ConfidentialHandler(fun)); } - ArgSchemaHandler argSchemaHandler = createHandlerIfExist(fun, fun.annotations, ArgSchemaHandler.class); if (argSchemaHandler != null) { fun.appendBeforeInvokeHandler(argSchemaHandler); @@ -566,6 +565,10 @@ public class ContractProcess { if (fun.isExport()) { //if(fun.annotations...) + AccessHandler accessHandler = createHandlerIfExist(fun,fun.annotations,AccessHandler.class); + if (accessHandler != null) { + fun.appendBeforeInvokeHandler(accessHandler); + } fun.appendAfterInvokeHandler(new ObjToJsonHandler()); // fun.appendBeforeInvokeHandler(new ReadMeHandler()); // Mask是用于返回真正结果之后,做一些偏移,以保护数据隐私。 diff --git a/src/main/java/org/bdware/sc/compiler/ap/Access.java b/src/main/java/org/bdware/sc/compiler/ap/Access.java index 20fb18e..51fad39 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/Access.java +++ b/src/main/java/org/bdware/sc/compiler/ap/Access.java @@ -3,13 +3,14 @@ package org.bdware.sc.compiler.ap; import org.bdware.sc.compiler.AnnotationProcessor; import org.bdware.sc.node.AnnotationNode; import org.bdware.sc.node.ContractNode; +import org.bdware.sc.node.FunctionNode; public class Access extends AnnotationProcessor { @Override public void processContract(AnnotationNode anno, ContractNode contractNode) { - contractNode.sigRequired = false; - if (anno != null) { - contractNode.sigRequired = "\"verified\"".equals(anno.getArgs().get(0)); + for (FunctionNode fn : contractNode.getFunctions()) { + fn.addAnnotation(anno); } } + } diff --git a/src/main/java/org/bdware/sc/engine/DesktopEngine.java b/src/main/java/org/bdware/sc/engine/DesktopEngine.java index 7d7fe5d..12e82d9 100644 --- a/src/main/java/org/bdware/sc/engine/DesktopEngine.java +++ b/src/main/java/org/bdware/sc/engine/DesktopEngine.java @@ -608,33 +608,32 @@ public class DesktopEngine extends JSEngine { public Object executeWithoutLock(FunctionNode fun, ContractRequest input, Object injectedArg) throws ScriptException, NoSuchMethodException { // long start = System.currentTimeMillis(); - ArgPacks argPack = new ArgPacks(input, input.getArg(), null); + ArgPacks argPacks = new ArgPacks(input, input.getArg(), null); if(injectedArg != null) { - argPack.arg = injectedArg; + argPacks.arg = injectedArg; } for (AnnotationHook handler : fun.beforeExecutionAnnotations()) { - argPack = handler.handle(this, argPack); + argPacks = handler.handle(this, argPacks); } // actually invoke! - // todo 这里invoke应该如何调用,改了一行代码正确吗?input.getArg() -> argPack.arg - if (argPack.ret == null) { - argPack.ret = + if (argPacks.ret == null) { + argPacks.ret = engine.invokeFunction( input.getAction(), (fun.isHandler() ? JsonUtil.fromJson(input.getArg(), Event.class) - : JSONTool.convertJsonElementToMirror(argPack.arg)), + : JSONTool.convertJsonElementToMirror(argPacks.arg)), input.getRequester(), input.getRequesterDOI()); } + for (AnnotationHook handler : fun.afterExecutionAnnotations()) { //Mask在after裏面 //System.out.println("afterHook"+contract.Mask); - - argPack = handler.handle(this, argPack); + argPacks = handler.handle(this, argPacks); } - return argPack.ret; + return argPacks.ret; } private String extractException(String msg, List stack) { diff --git a/src/main/java/org/bdware/sc/engine/hook/AccessHandler.java b/src/main/java/org/bdware/sc/engine/hook/AccessHandler.java new file mode 100644 index 0000000..85d18f7 --- /dev/null +++ b/src/main/java/org/bdware/sc/engine/hook/AccessHandler.java @@ -0,0 +1,78 @@ +package org.bdware.sc.engine.hook; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.bdware.sc.JSEngine; +import org.bdware.sc.bean.ContractRequest; +import org.bdware.sc.boundry.ScriptReturnException; +import org.bdware.sc.engine.DesktopEngine; +import org.bdware.sc.engine.JSONTool; +import org.bdware.sc.node.AnnotationHook; +import org.bdware.sc.node.AnnotationNode; +import org.bdware.sc.node.ArgPacks; +import org.bdware.sc.node.FunctionNode; +import wrp.jdk.nashorn.internal.objects.Global; +import wrp.jdk.nashorn.internal.runtime.Context; +import wrp.jdk.nashorn.internal.runtime.PropertyMap; +import wrp.jdk.nashorn.internal.scripts.JO; + +@YJSAnnotation(name = "Access") +public class AccessHandler implements AnnotationHook { + public String acFunction; + public boolean requireSign; + + public AccessHandler(AnnotationNode annoNode) { + requireSign = false; + String content = annoNode.getArgs().get(0); + JsonElement je = JsonParser.parseString(content); + if (je.isJsonPrimitive() && je.getAsJsonPrimitive().getAsString().equals("verified")) { + requireSign = true; + } + if (je.isJsonObject()) { + acFunction = je.getAsJsonObject().get("ACFunction").getAsString(); + } + } + + public static AccessHandler fromAnnotationNode(FunctionNode funNode, AnnotationNode annoNode) { + return new AccessHandler(annoNode); + } + + @Override + public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) { + if (requireSign) { + if (!argPacks.request.verifySignature()) { + throw new ScriptReturnException(JsonParser.parseString("{\"code\":400,\"msg\":\"permission denied\"}")); + } + return argPacks; + } + if (acFunction == null) return argPacks; + DesktopEngine de = (DesktopEngine) desktopEngine; + Global oldGlobal = Context.getGlobal(); + Global newGlobal = de.getDesktopGlobal(); + boolean globalChanged = (oldGlobal != newGlobal); + try { + if (globalChanged) { + Context.setGlobal(newGlobal); + } + ContractRequest input = argPacks.request; + JO jo = new JO(PropertyMap.newMap()); + jo.put("requester", input.getRequester(), false); + jo.put("action", input.getAction(), false); + jo.put("arg", JSONTool.convertJsonElementToMirror(input.getArg()), false); + de.engine.invokeFunction(acFunction, jo); + return argPacks; + } catch (ScriptReturnException e) { + throw e; + } catch (Exception e) { + JsonObject jo = new JsonObject(); + jo.addProperty("code", "401"); + jo.addProperty("msg", "access check meets exception! " + e); + throw new ScriptReturnException(jo); + } finally { + if (globalChanged) Context.setGlobal(oldGlobal); + + } + + } +} diff --git a/src/main/java/org/bdware/sc/engine/hook/ArgSchemaHandler.java b/src/main/java/org/bdware/sc/engine/hook/ArgSchemaHandler.java index 6663fc1..019e6df 100644 --- a/src/main/java/org/bdware/sc/engine/hook/ArgSchemaHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/ArgSchemaHandler.java @@ -29,7 +29,6 @@ public class ArgSchemaHandler implements AnnotationHook { } public static ArgSchemaHandler fromAnnotationNode(FunctionNode funNode, AnnotationNode annoNode) { - //a= annoNode; return new ArgSchemaHandler(annoNode); }