From 1dbe4b4ec0cfa4d08bff60e244cc38d73be187cb Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 21 Apr 2023 00:17:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/App.kt | 8 +- .../io/legado/app/data/entities/BaseSource.kt | 22 +-- .../app/model/analyzeRule/AnalyzeRule.kt | 22 +-- .../app/model/analyzeRule/AnalyzeUrl.kt | 22 +-- .../main/java/io/legado/app/rhino/Rhino.kt | 29 ++-- .../com/script/rhino/RhinoScriptEngine.kt | 119 ++++++++-------- .../com/script/rhino/RhinoScriptEngine.kt | 134 +++++++++--------- 7 files changed, 175 insertions(+), 181 deletions(-) diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 0a7fcb539..38dd89cc9 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -10,13 +10,18 @@ import android.os.Build import com.github.liuyueyi.quick.transfer.ChineseUtils import com.github.liuyueyi.quick.transfer.constants.TransType import com.jeremyliao.liveeventbus.LiveEventBus +import com.script.rhino.RhinoScriptEngine import io.legado.app.base.AppContextWrapper import io.legado.app.constant.AppConst.channelIdDownload import io.legado.app.constant.AppConst.channelIdReadAloud import io.legado.app.constant.AppConst.channelIdWeb import io.legado.app.constant.PreferKey import io.legado.app.data.appDb -import io.legado.app.help.* +import io.legado.app.help.AppWebDav +import io.legado.app.help.CrashHandler +import io.legado.app.help.DefaultData +import io.legado.app.help.LifecycleHelp +import io.legado.app.help.RuleBigDataHelp import io.legado.app.help.book.BookHelp import io.legado.app.help.config.AppConfig import io.legado.app.help.config.ThemeConfig.applyDayNight @@ -36,6 +41,7 @@ class App : Application() { override fun onCreate() { super.onCreate() + RhinoScriptEngine oldConfig = Configuration(resources.configuration) CrashHandler(this) //预下载Cronet so diff --git a/app/src/main/java/io/legado/app/data/entities/BaseSource.kt b/app/src/main/java/io/legado/app/data/entities/BaseSource.kt index 9f87a2591..1f4c89f3a 100644 --- a/app/src/main/java/io/legado/app/data/entities/BaseSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BaseSource.kt @@ -1,6 +1,8 @@ package io.legado.app.data.entities import cn.hutool.crypto.symmetric.AES +import com.script.SimpleBindings +import com.script.rhino.RhinoScriptEngine import io.legado.app.constant.AppConst import io.legado.app.constant.AppLog import io.legado.app.data.entities.rule.RowUi @@ -10,9 +12,6 @@ import io.legado.app.help.config.AppConfig import io.legado.app.help.http.CookieStore import io.legado.app.model.SharedJsScope import io.legado.app.rhino.Bindings -import io.legado.app.rhino.Rhino -import io.legado.app.rhino.evaluate -import io.legado.app.rhino.putBindings import io.legado.app.utils.* import org.intellij.lang.annotations.Language import org.mozilla.javascript.Scriptable @@ -237,14 +236,15 @@ interface BaseSource : JsExtensions { bindings["baseUrl"] = getKey() bindings["cookie"] = CookieStore bindings["cache"] = CacheManager - return Rhino.use { - val scope = initStandardObjects() - scope.putBindings(bindings) - getShareScope()?.let { - scope.prototype = it - } - evaluate(scope, jsStr) - } +// return Rhino.use { +// val scope = initStandardObjects() +// scope.putBindings(bindings) +// getShareScope()?.let { +// scope.prototype = it +// } +// evaluate(scope, jsStr) +// } + return RhinoScriptEngine.eval(jsStr, SimpleBindings(bindings)) } fun getShareScope(): Scriptable? { diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index f1c75422e..c1530d614 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -2,6 +2,8 @@ package io.legado.app.model.analyzeRule import android.text.TextUtils import androidx.annotation.Keep +import com.script.SimpleBindings +import com.script.rhino.RhinoScriptEngine import io.legado.app.constant.AppPattern.JS_PATTERN import io.legado.app.data.entities.* import io.legado.app.help.CacheManager @@ -9,9 +11,6 @@ import io.legado.app.help.JsExtensions import io.legado.app.help.http.CookieStore import io.legado.app.model.webBook.WebBook import io.legado.app.rhino.Bindings -import io.legado.app.rhino.Rhino -import io.legado.app.rhino.evaluate -import io.legado.app.rhino.putBindings import io.legado.app.utils.* import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withTimeout @@ -695,14 +694,15 @@ class AnalyzeRule( bindings["title"] = chapter?.title bindings["src"] = content bindings["nextChapterUrl"] = nextChapterUrl - return Rhino.use { - val scope = initStandardObjects() - scope.putBindings(bindings) - source?.getShareScope()?.let { - scope.prototype = it - } - evaluate(scope, jsStr) - } +// return Rhino.use { +// val scope = initStandardObjects() +// scope.putBindings(bindings) +// source?.getShareScope()?.let { +// scope.prototype = it +// } +// evaluate(scope, jsStr) +// } + return RhinoScriptEngine.eval(jsStr, SimpleBindings(bindings)) } override fun getSource(): BaseSource? { diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 91042723b..4e2836923 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -5,6 +5,8 @@ import android.util.Base64 import androidx.annotation.Keep import cn.hutool.core.util.HexUtil import com.bumptech.glide.load.model.GlideUrl +import com.script.SimpleBindings +import com.script.rhino.RhinoScriptEngine import io.legado.app.constant.AppConst.UA_NAME import io.legado.app.constant.AppPattern import io.legado.app.constant.AppPattern.JS_PATTERN @@ -19,9 +21,6 @@ import io.legado.app.help.config.AppConfig import io.legado.app.help.glide.GlideHeaders import io.legado.app.help.http.* import io.legado.app.rhino.Bindings -import io.legado.app.rhino.Rhino -import io.legado.app.rhino.evaluate -import io.legado.app.rhino.putBindings import io.legado.app.utils.* import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking @@ -267,14 +266,15 @@ class AnalyzeUrl( bindings["book"] = ruleData as? Book bindings["source"] = source bindings["result"] = result - return Rhino.use { - val scope = initStandardObjects() - scope.putBindings(bindings) - source?.getShareScope()?.let { - scope.prototype = it - } - evaluate(scope, jsStr) - } +// return Rhino.use { +// val scope = initStandardObjects() +// scope.putBindings(bindings) +// source?.getShareScope()?.let { +// scope.prototype = it +// } +// evaluate(scope, jsStr) +// } + return RhinoScriptEngine.eval(jsStr, SimpleBindings(bindings)) } fun put(key: String, value: String): String { diff --git a/app/src/main/java/io/legado/app/rhino/Rhino.kt b/app/src/main/java/io/legado/app/rhino/Rhino.kt index 0a69a5b2c..c5d17184c 100644 --- a/app/src/main/java/io/legado/app/rhino/Rhino.kt +++ b/app/src/main/java/io/legado/app/rhino/Rhino.kt @@ -1,8 +1,6 @@ package io.legado.app.rhino -import com.script.RhinoContextFactory import org.mozilla.javascript.Context -import org.mozilla.javascript.ContextFactory import org.mozilla.javascript.Undefined import org.mozilla.javascript.Wrapper @@ -25,19 +23,18 @@ object Rhino { return if (result1 is Undefined) null else result1 } - init { - ContextFactory.initGlobal(object : RhinoContextFactory() { - - override fun makeContext(): Context { - val cx = super.makeContext() - cx.languageVersion = 200 - cx.optimizationLevel = -1 - cx.setClassShutter(RhinoClassShutter) - //cx.wrapFactory = RhinoWrapFactory - return cx - } - - }) - } +// init { +// ContextFactory.initGlobal(object : RhinoContextFactory() { +// +// override fun makeContext(): Context { +// val cx = super.makeContext() +// cx.languageVersion = 200 +// cx.optimizationLevel = -1 +// cx.setClassShutter(RhinoClassShutter) +// return cx +// } +// +// }) +// } } \ No newline at end of file diff --git a/modules/rhino1.7.3/src/main/java/com/script/rhino/RhinoScriptEngine.kt b/modules/rhino1.7.3/src/main/java/com/script/rhino/RhinoScriptEngine.kt index fdfc55cfd..ae0759c3b 100644 --- a/modules/rhino1.7.3/src/main/java/com/script/rhino/RhinoScriptEngine.kt +++ b/modules/rhino1.7.3/src/main/java/com/script/rhino/RhinoScriptEngine.kt @@ -42,7 +42,7 @@ import java.security.* * @since 1.6 */ @Suppress("MemberVisibilityCanBePrivate") -class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { +object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { var accessContext: AccessControlContext? = null private var topLevel: RhinoTopLevel? = null private val indexedProps: MutableMap @@ -198,6 +198,62 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { } init { + ContextFactory.initGlobal(object : ContextFactory() { + + override fun makeContext(): Context { + val cx = super.makeContext() + cx.languageVersion = 200 + cx.optimizationLevel = -1 + cx.setClassShutter(RhinoClassShutter) + cx.wrapFactory = RhinoWrapFactory + return cx + } + + override fun hasFeature(cx: Context, featureIndex: Int): Boolean { + @Suppress("UNUSED_EXPRESSION") + return when (featureIndex) { + //Context.FEATURE_ENABLE_JAVA_MAP_ACCESS -> true + else -> super.hasFeature(cx, featureIndex) + } + } + + override fun doTopCall( + callable: Callable, + cx: Context, + scope: Scriptable, + thisObj: Scriptable, + args: Array + ): Any? { + var accContext: AccessControlContext? = null + val global = ScriptableObject.getTopLevelScope(scope) + val globalProto = global.prototype + if (globalProto is RhinoTopLevel) { + accContext = globalProto.accessContext + } + return if (accContext != null) AccessController.doPrivileged( + PrivilegedAction { + superDoTopCall(callable, cx, scope, thisObj, args) + }, accContext + ) else superDoTopCall( + callable, + cx, + scope, + thisObj, + args + ) + } + + private fun superDoTopCall( + callable: Callable, + cx: Context, + scope: Scriptable, + thisObj: Scriptable, + args: Array + ): Any? { + return super.doTopCall(callable, cx, scope, thisObj, args) + } + }) + if (System.getSecurityManager() != null) { try { AccessController.checkPermission(AllPermission()) @@ -251,65 +307,4 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { } } - @Suppress("unused") - companion object { - - init { - ContextFactory.initGlobal(object : ContextFactory() { - - override fun makeContext(): Context { - val cx = super.makeContext() - cx.languageVersion = 200 - cx.optimizationLevel = -1 - cx.setClassShutter(RhinoClassShutter) - cx.wrapFactory = RhinoWrapFactory - return cx - } - - override fun hasFeature(cx: Context, featureIndex: Int): Boolean { - @Suppress("UNUSED_EXPRESSION") - return when (featureIndex) { - //Context.FEATURE_ENABLE_JAVA_MAP_ACCESS -> true - else -> super.hasFeature(cx, featureIndex) - } - } - - override fun doTopCall( - callable: Callable, - cx: Context, - scope: Scriptable, - thisObj: Scriptable, - args: Array - ): Any? { - var accContext: AccessControlContext? = null - val global = ScriptableObject.getTopLevelScope(scope) - val globalProto = global.prototype - if (globalProto is RhinoTopLevel) { - accContext = globalProto.accessContext - } - return if (accContext != null) AccessController.doPrivileged( - PrivilegedAction { - superDoTopCall(callable, cx, scope, thisObj, args) - }, accContext - ) else superDoTopCall( - callable, - cx, - scope, - thisObj, - args - ) - } - - private fun superDoTopCall( - callable: Callable, - cx: Context, - scope: Scriptable, - thisObj: Scriptable, - args: Array - ): Any? { - return super.doTopCall(callable, cx, scope, thisObj, args) - } - }) - } - } } \ No newline at end of file diff --git a/modules/rhino1.7.4/src/main/java/com/script/rhino/RhinoScriptEngine.kt b/modules/rhino1.7.4/src/main/java/com/script/rhino/RhinoScriptEngine.kt index 9baecc3f4..a224e72e2 100644 --- a/modules/rhino1.7.4/src/main/java/com/script/rhino/RhinoScriptEngine.kt +++ b/modules/rhino1.7.4/src/main/java/com/script/rhino/RhinoScriptEngine.kt @@ -34,15 +34,15 @@ import java.lang.reflect.Method import java.security.* /** - * Implementation of `ScriptEngine` using the Mozilla Rhino - * interpreter. - * - * @author Mike Grogan - * @author A. Sundararajan - * @since 1.6 - */ -@Suppress("MemberVisibilityCanBePrivate") -class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { + * Implementation of `ScriptEngine` using the Mozilla Rhino + * interpreter. + * + * @author Mike Grogan + * @author A. Sundararajan + * @since 1.6 + */ + @Suppress("MemberVisibilityCanBePrivate") + object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { var accessContext: AccessControlContext? = null private var topLevel: RhinoTopLevel? = null private val indexedProps: MutableMap @@ -198,6 +198,62 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { } init { + + ContextFactory.initGlobal(object : ContextFactory() { + + override fun makeContext(): Context { + val cx = super.makeContext() + cx.languageVersion = 200 + cx.optimizationLevel = -1 + cx.setClassShutter(RhinoClassShutter) + cx.wrapFactory = RhinoWrapFactory + return cx + } + + override fun hasFeature(cx: Context, featureIndex: Int): Boolean { + return when (featureIndex) { + Context.FEATURE_ENABLE_JAVA_MAP_ACCESS -> true + else -> super.hasFeature(cx, featureIndex) + } + } + + override fun doTopCall( + callable: Callable, + cx: Context, + scope: Scriptable, + thisObj: Scriptable, + args: Array + ): Any? { + var accContext: AccessControlContext? = null + val global = ScriptableObject.getTopLevelScope(scope) + val globalProto = global.prototype + if (globalProto is RhinoTopLevel) { + accContext = globalProto.accessContext + } + return if (accContext != null) AccessController.doPrivileged( + PrivilegedAction { + superDoTopCall(callable, cx, scope, thisObj, args) + }, accContext + ) else superDoTopCall( + callable, + cx, + scope, + thisObj, + args + ) + } + + private fun superDoTopCall( + callable: Callable, + cx: Context, + scope: Scriptable, + thisObj: Scriptable, + args: Array + ): Any? { + return super.doTopCall(callable, cx, scope, thisObj, args) + } + }) + if (System.getSecurityManager() != null) { try { AccessController.checkPermission(AllPermission()) @@ -251,64 +307,4 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { } } - @Suppress("unused") - companion object { - - init { - ContextFactory.initGlobal(object : ContextFactory() { - - override fun makeContext(): Context { - val cx = super.makeContext() - cx.languageVersion = 200 - cx.optimizationLevel = -1 - cx.setClassShutter(RhinoClassShutter) - cx.wrapFactory = RhinoWrapFactory - return cx - } - - override fun hasFeature(cx: Context, featureIndex: Int): Boolean { - return when (featureIndex) { - Context.FEATURE_ENABLE_JAVA_MAP_ACCESS -> true - else -> super.hasFeature(cx, featureIndex) - } - } - - override fun doTopCall( - callable: Callable, - cx: Context, - scope: Scriptable, - thisObj: Scriptable, - args: Array - ): Any? { - var accContext: AccessControlContext? = null - val global = ScriptableObject.getTopLevelScope(scope) - val globalProto = global.prototype - if (globalProto is RhinoTopLevel) { - accContext = globalProto.accessContext - } - return if (accContext != null) AccessController.doPrivileged( - PrivilegedAction { - superDoTopCall(callable, cx, scope, thisObj, args) - }, accContext - ) else superDoTopCall( - callable, - cx, - scope, - thisObj, - args - ) - } - - private fun superDoTopCall( - callable: Callable, - cx: Context, - scope: Scriptable, - thisObj: Scriptable, - args: Array - ): Any? { - return super.doTopCall(callable, cx, scope, thisObj, args) - } - }) - } - } } \ No newline at end of file