diff --git a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/FunctionInvocationHandler.java b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/FunctionInvocationHandler.java new file mode 100644 index 00000000000..dcac948d73a --- /dev/null +++ b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/FunctionInvocationHandler.java @@ -0,0 +1,53 @@ +package com.taobao.arthas.core.shell.term.impl; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +import com.taobao.arthas.common.ArthasConstants; +import com.taobao.arthas.core.shell.session.Session; + +import io.termd.core.readline.Function; +import io.termd.core.readline.Readline; +import io.termd.core.readline.Readline.Interaction; + +/** + * 拦截指定的 Function 的 apply 函数 + * + * @author hengyunabc 2023-08-24 + * + */ +public class FunctionInvocationHandler implements InvocationHandler { + + private TermImpl termImpl; + + private Function target; + + public FunctionInvocationHandler(TermImpl termImpl, Function target) { + this.termImpl = termImpl; + this.target = target; + } + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + + String name = method.getName(); + + if (name.equals("apply")) { + Session session = termImpl.getSession(); + if (session != null) { + boolean authenticated = session.get(ArthasConstants.SUBJECT_KEY) != null; + if (authenticated) { + return method.invoke(target, args); + } else { + Readline.Interaction interaction = (Interaction) args[0]; + // 必要 + interaction.resume(); + return null; + } + } + } + + return method.invoke(target, args); + } + +} diff --git a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/TermImpl.java b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/TermImpl.java index 1a47e8c6f5a..b103ad71a0d 100644 --- a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/TermImpl.java +++ b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/TermImpl.java @@ -1,10 +1,15 @@ package com.taobao.arthas.core.shell.term.impl; +import java.io.File; +import java.lang.reflect.Proxy; +import java.util.ArrayList; +import java.util.List; + import com.taobao.arthas.core.shell.cli.Completion; +import com.taobao.arthas.core.shell.handlers.Handler; import com.taobao.arthas.core.shell.handlers.term.CloseHandlerWrapper; import com.taobao.arthas.core.shell.handlers.term.DefaultTermStdinHandler; import com.taobao.arthas.core.shell.handlers.term.EventHandler; -import com.taobao.arthas.core.shell.handlers.Handler; import com.taobao.arthas.core.shell.handlers.term.RequestHandler; import com.taobao.arthas.core.shell.handlers.term.SizeHandlerWrapper; import com.taobao.arthas.core.shell.handlers.term.StdinHandlerWrapper; @@ -13,17 +18,15 @@ import com.taobao.arthas.core.shell.term.Term; import com.taobao.arthas.core.util.Constants; import com.taobao.arthas.core.util.FileUtils; + import io.termd.core.function.Consumer; import io.termd.core.readline.Function; import io.termd.core.readline.Keymap; import io.termd.core.readline.Readline; +import io.termd.core.readline.functions.HistorySearchForward; import io.termd.core.tty.TtyConnection; import io.termd.core.util.Helper; -import java.io.File; -import java.util.ArrayList; -import java.util.List; - /** * @author Julien Viet */ @@ -50,6 +53,18 @@ public TermImpl(Keymap keymap, TtyConnection conn) { readline = new Readline(keymap); readline.setHistory(FileUtils.loadCommandHistory(new File(Constants.CMD_HISTORY_FILE))); for (Function function : readlineFunctions) { + /** + * 防止没有鉴权时,查看历史命令 + * + * @see io.termd.core.readline.functions.HistorySearchForward + */ + if (function.name().contains("history")) { + FunctionInvocationHandler funcHandler = new FunctionInvocationHandler(this, function); + function = (Function) Proxy.newProxyInstance(this.getClass().getClassLoader(), + HistorySearchForward.class.getInterfaces(), funcHandler); + + } + readline.addFunction(function); } @@ -64,6 +79,10 @@ public Term setSession(Session session) { return this; } + public Session getSession() { + return session; + } + @Override public void readline(String prompt, Handler lineHandler) { if (conn.getStdinHandler() != echoHandler) {