V1.4.1 Unlike normal shell, echo part of a command must be evaluated.

master
Ng Yat Yan 1 month ago
parent 7f99e9e2f6
commit 494ea4c1bb

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.example.sshd</groupId> <groupId>com.example.sshd</groupId>
<artifactId>echo-sshd-server</artifactId> <artifactId>echo-sshd-server</artifactId>
<version>1.4.0</version> <version>1.4.1</version>
<name>ECHO SSH SERVER</name> <name>ECHO SSH SERVER</name>
<description>Learning Apache Mina SSHD library</description> <description>Learning Apache Mina SSHD library</description>
<parent> <parent>
@ -75,6 +75,11 @@
<groupId>org.apache.httpcomponents.client5</groupId> <groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId> <artifactId>httpclient5</artifactId>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>

@ -27,5 +27,5 @@ public class Boot {
System.setProperty("logging.config", configDirectory + "/log4j2.xml"); System.setProperty("logging.config", configDirectory + "/log4j2.xml");
} }
SpringApplication.run(Boot.class, args); SpringApplication.run(Boot.class, args);
} }
} }

@ -21,7 +21,7 @@ public class AppConfig {
public Map<String, Session> remoteSessionMapping() { public Map<String, Session> remoteSessionMapping() {
return Collections.synchronizedMap(new HashMap<>()); return Collections.synchronizedMap(new HashMap<>());
} }
@Bean @Bean
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public Map<String, String> ipInfoMapping() { public Map<String, String> ipInfoMapping() {

@ -41,7 +41,7 @@ public class SshConfig {
@Value("${ssh-server.hash-replies.location}") @Value("${ssh-server.hash-replies.location}")
private String hashReplies; private String hashReplies;
@Value("${ssh-server.regex-mapping.location}") @Value("${ssh-server.regex-mapping.location}")
private String regexMapping; private String regexMapping;
@ -63,7 +63,7 @@ public class SshConfig {
}); });
sshd.setShellFactory(applicationContext.getBean(EchoShellFactory.class)); sshd.setShellFactory(applicationContext.getBean(EchoShellFactory.class));
sshd.setCommandFactory(command -> applicationContext.getBean(OnetimeCommand.class, command)); sshd.setCommandFactory(command -> applicationContext.getBean(OnetimeCommand.class, command));
sshd.start(); sshd.start();
sshd.getSessionFactory().addListener(applicationContext.getBean(EchoSessionListener.class)); sshd.getSessionFactory().addListener(applicationContext.getBean(EchoSessionListener.class));
return sshd; return sshd;
@ -78,7 +78,7 @@ public class SshConfig {
prop.load(stream); prop.load(stream);
return prop; return prop;
} }
@Bean @Bean
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public Properties regexMapping() throws IOException { public Properties regexMapping() throws IOException {

@ -9,6 +9,7 @@ import java.net.InetSocketAddress;
import java.util.Arrays; import java.util.Arrays;
import java.util.Properties; import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.apache.sshd.server.Command; import org.apache.sshd.server.Command;
import org.apache.sshd.server.Environment; import org.apache.sshd.server.Environment;
import org.apache.sshd.server.ExitCallback; import org.apache.sshd.server.ExitCallback;
@ -73,7 +74,7 @@ public class EchoShell implements Command, Runnable, SessionAware {
protected String remoteIpAddress() { protected String remoteIpAddress() {
String remoteIpAddress = ""; String remoteIpAddress = "";
if (session.getIoSession().getRemoteAddress() instanceof InetSocketAddress) { if (session.getIoSession().getRemoteAddress() instanceof InetSocketAddress) {
InetSocketAddress remoteAddress = (InetSocketAddress) session.getIoSession().getRemoteAddress(); InetSocketAddress remoteAddress = (InetSocketAddress) session.getIoSession().getRemoteAddress();
remoteIpAddress = remoteAddress.getAddress().getHostAddress(); remoteIpAddress = remoteAddress.getAddress().getHostAddress();
@ -82,7 +83,7 @@ public class EchoShell implements Command, Runnable, SessionAware {
} }
return remoteIpAddress; return remoteIpAddress;
} }
@Override @Override
public void destroy() { public void destroy() {
thread.interrupt(); thread.interrupt();
@ -102,7 +103,7 @@ public class EchoShell implements Command, Runnable, SessionAware {
int s = r.read(); int s = r.read();
if (s == 13 || s == 10) { 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; boolean wantsExit = false;
try { try {
wantsExit = replyUtil.replyToCommand(cmd.trim(), out, prompt, session); wantsExit = replyUtil.replyToCommand(cmd.trim(), out, prompt, session);

@ -17,5 +17,4 @@ public class EchoShellFactory implements Factory<Command> {
return (Command) applicationContext.getBean("echoShell"); return (Command) applicationContext.getBean("echoShell");
} }
} }

@ -2,6 +2,7 @@ package com.example.sshd.core;
import java.util.Arrays; import java.util.Arrays;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
@ -23,7 +24,7 @@ public class OnetimeCommand extends EchoShell {
@Override @Override
public void run() { public void run() {
try { try {
Arrays.asList(command.split(";")).stream().forEach(cmd -> { Arrays.asList(StringUtils.split(command, ";|&")).stream().forEach(cmd -> {
try { try {
replyUtil.replyToCommand(cmd.trim(), out, "", session); replyUtil.replyToCommand(cmd.trim(), out, "", session);
out.flush(); out.flush();

@ -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);
}
}
Loading…
Cancel
Save