-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
disable viewing history commands without auth #2620
- Loading branch information
1 parent
693fe71
commit db0b3bb
Showing
2 changed files
with
77 additions
and
5 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
core/src/main/java/com/taobao/arthas/core/shell/term/impl/FunctionInvocationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <a href="mailto:[email protected]">Julien Viet</a> | ||
*/ | ||
|
@@ -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<String> lineHandler) { | ||
if (conn.getStdinHandler() != echoHandler) { | ||
|