From 494ea4c1bb9ab954e89e369ad08dee81a15a7577 Mon Sep 17 00:00:00 2001 From: Ng Yat Yan Date: Sun, 20 Oct 2024 09:11:15 +0800 Subject: [PATCH] V1.4.1 Unlike normal shell, echo part of a command must be evaluated. --- pom.xml | 7 ++++++- src/main/java/com/example/sshd/Boot.java | 2 +- .../com/example/sshd/config/AppConfig.java | 2 +- .../com/example/sshd/config/SshConfig.java | 6 +++--- .../java/com/example/sshd/core/EchoShell.java | 7 ++++--- .../example/sshd/core/EchoShellFactory.java | 1 - .../com/example/sshd/core/OnetimeCommand.java | 3 ++- .../java/com/example/sshd/test/SplitTest.java | 19 +++++++++++++++++++ 8 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 src/test/java/com/example/sshd/test/SplitTest.java diff --git a/pom.xml b/pom.xml index d8b115c..0a2c180 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.example.sshd echo-sshd-server - 1.4.0 + 1.4.1 ECHO SSH SERVER Learning Apache Mina SSHD library @@ -75,6 +75,11 @@ org.apache.httpcomponents.client5 httpclient5 + + junit + junit + test + diff --git a/src/main/java/com/example/sshd/Boot.java b/src/main/java/com/example/sshd/Boot.java index 08b966b..35947ce 100644 --- a/src/main/java/com/example/sshd/Boot.java +++ b/src/main/java/com/example/sshd/Boot.java @@ -27,5 +27,5 @@ public class Boot { System.setProperty("logging.config", configDirectory + "/log4j2.xml"); } SpringApplication.run(Boot.class, args); - } + } } diff --git a/src/main/java/com/example/sshd/config/AppConfig.java b/src/main/java/com/example/sshd/config/AppConfig.java index 7aca54a..42cf335 100644 --- a/src/main/java/com/example/sshd/config/AppConfig.java +++ b/src/main/java/com/example/sshd/config/AppConfig.java @@ -21,7 +21,7 @@ public class AppConfig { public Map remoteSessionMapping() { return Collections.synchronizedMap(new HashMap<>()); } - + @Bean @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) public Map ipInfoMapping() { diff --git a/src/main/java/com/example/sshd/config/SshConfig.java b/src/main/java/com/example/sshd/config/SshConfig.java index 710291b..ede4d7e 100644 --- a/src/main/java/com/example/sshd/config/SshConfig.java +++ b/src/main/java/com/example/sshd/config/SshConfig.java @@ -41,7 +41,7 @@ public class SshConfig { @Value("${ssh-server.hash-replies.location}") private String hashReplies; - + @Value("${ssh-server.regex-mapping.location}") private String regexMapping; @@ -63,7 +63,7 @@ public class SshConfig { }); sshd.setShellFactory(applicationContext.getBean(EchoShellFactory.class)); sshd.setCommandFactory(command -> applicationContext.getBean(OnetimeCommand.class, command)); - + sshd.start(); sshd.getSessionFactory().addListener(applicationContext.getBean(EchoSessionListener.class)); return sshd; @@ -78,7 +78,7 @@ public class SshConfig { prop.load(stream); return prop; } - + @Bean @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) public Properties regexMapping() throws IOException { diff --git a/src/main/java/com/example/sshd/core/EchoShell.java b/src/main/java/com/example/sshd/core/EchoShell.java index a1e9112..085771b 100644 --- a/src/main/java/com/example/sshd/core/EchoShell.java +++ b/src/main/java/com/example/sshd/core/EchoShell.java @@ -9,6 +9,7 @@ import java.net.InetSocketAddress; import java.util.Arrays; import java.util.Properties; +import org.apache.commons.lang3.StringUtils; import org.apache.sshd.server.Command; import org.apache.sshd.server.Environment; import org.apache.sshd.server.ExitCallback; @@ -73,7 +74,7 @@ public class EchoShell implements Command, Runnable, SessionAware { protected String remoteIpAddress() { String remoteIpAddress = ""; - + if (session.getIoSession().getRemoteAddress() instanceof InetSocketAddress) { InetSocketAddress remoteAddress = (InetSocketAddress) session.getIoSession().getRemoteAddress(); remoteIpAddress = remoteAddress.getAddress().getHostAddress(); @@ -82,7 +83,7 @@ public class EchoShell implements Command, Runnable, SessionAware { } return remoteIpAddress; } - + @Override public void destroy() { thread.interrupt(); @@ -102,7 +103,7 @@ public class EchoShell implements Command, Runnable, SessionAware { int s = r.read(); if (s == 13 || s == 10) { - boolean containsExit = Arrays.asList(command.split(";")).stream().map(cmd -> { + boolean containsExit = Arrays.asList(StringUtils.split(command, ";|&")).stream().map(cmd -> { boolean wantsExit = false; try { wantsExit = replyUtil.replyToCommand(cmd.trim(), out, prompt, session); diff --git a/src/main/java/com/example/sshd/core/EchoShellFactory.java b/src/main/java/com/example/sshd/core/EchoShellFactory.java index 8684bc2..a00cd69 100644 --- a/src/main/java/com/example/sshd/core/EchoShellFactory.java +++ b/src/main/java/com/example/sshd/core/EchoShellFactory.java @@ -17,5 +17,4 @@ public class EchoShellFactory implements Factory { return (Command) applicationContext.getBean("echoShell"); } - } \ No newline at end of file diff --git a/src/main/java/com/example/sshd/core/OnetimeCommand.java b/src/main/java/com/example/sshd/core/OnetimeCommand.java index ae73641..ad22f68 100644 --- a/src/main/java/com/example/sshd/core/OnetimeCommand.java +++ b/src/main/java/com/example/sshd/core/OnetimeCommand.java @@ -2,6 +2,7 @@ package com.example.sshd.core; import java.util.Arrays; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory; @@ -23,7 +24,7 @@ public class OnetimeCommand extends EchoShell { @Override public void run() { try { - Arrays.asList(command.split(";")).stream().forEach(cmd -> { + Arrays.asList(StringUtils.split(command, ";|&")).stream().forEach(cmd -> { try { replyUtil.replyToCommand(cmd.trim(), out, "", session); out.flush(); diff --git a/src/test/java/com/example/sshd/test/SplitTest.java b/src/test/java/com/example/sshd/test/SplitTest.java new file mode 100644 index 0000000..de2ed75 --- /dev/null +++ b/src/test/java/com/example/sshd/test/SplitTest.java @@ -0,0 +1,19 @@ +package com.example.sshd.test; + +import java.util.Arrays; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Assert; +import org.junit.Test; + +public class SplitTest { + + @Test + public void test() { + String command = "echo hello | lscpu && uname ; whoami"; + String[] splited = StringUtils.split(command,";|&"); + System.out.println(Arrays.asList(splited)); + Assert.assertTrue(splited.length == 4); + } + +}