V1.0.2 Look up a command reply in a properties file

master
Ng Yat Yan 1 month ago
parent 61e15af8d4
commit e42de3b92a

@ -0,0 +1,7 @@
hello=hello
5BAABD7C4581F90088133C0E945302C2=\t0\t0\t0 -
uname=Linux
9DD46246144D353C914D7572AEDF8EA6=Linux mail.vidconnect.cyou 4.15.0-70-generic #79-Ubuntu SMP Tue Nov 12 10:36:10 UTC 2019 aarch64 aarch64 aarch64 GNU/Linux
whoami=appadmin
4DE8A3B5E72E54D9D2A70BD43CB5EA9E=Model:\t\t0
99EFF9F4022855955653078ECB2B4CE4=1.9G

@ -2,3 +2,5 @@ ssh-server:
port: 1022
private-key:
location: "conf/private.key"
automatic-replies:
location: "conf/replies.properties"

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.sshd</groupId>
<artifactId>echo-sshd-server</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
<name>ECHO SSH SERVER</name>
<description>Learning Apache Mina SSHD library</description>
<parent>
@ -33,6 +33,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId>

@ -0,0 +1,110 @@
package com.example.sshd.config;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.Environment;
import org.apache.sshd.server.ExitCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class OnetimeCommand implements Command {
private static final Logger logger = LoggerFactory.getLogger(OnetimeCommand.class);
@Autowired
Properties repliesProperties;
private InputStream in;
private OutputStream out;
private OutputStream err;
private ExitCallback callback;
private Environment environment;
private String command;
public OnetimeCommand(String cmd) {
command = cmd;
}
public InputStream getIn() {
return in;
}
public OutputStream getOut() {
return out;
}
public OutputStream getErr() {
return err;
}
public Environment getEnvironment() {
return environment;
}
@Override
public void setInputStream(InputStream in) {
this.in = in;
}
@Override
public void setOutputStream(OutputStream out) {
this.out = out;
}
@Override
public void setErrorStream(OutputStream err) {
this.err = err;
}
@Override
public void setExitCallback(ExitCallback callback) {
this.callback = callback;
}
@Override
public void start(Environment env) throws IOException {
environment = env;
String cmdHash = DigestUtils.md5Hex(command).toUpperCase();
logger.info("command = {}, cmdHash = {}", command, cmdHash);
if (StringUtils.equals(command, "exit")) {
logger.info("Exiting command detected: {}", command);
out.write(("\r\nExiting...\r\n").getBytes());
} else if (repliesProperties.containsKey(command)) {
logger.info("Known command detected: {}", command);
String reply = repliesProperties.getProperty(command).replace("\\r", "\r").replace("\\n", "\n")
.replace("\\t", "\t");
out.write(("\r\n" + reply + "\r\n").getBytes());
} else if (repliesProperties.containsKey(cmdHash)) {
logger.info("Known command-hash detected: {}", cmdHash);
String reply = repliesProperties.getProperty(cmdHash).replace("\\r", "\r").replace("\\n", "\n")
.replace("\\t", "\t");
out.write(("\r\n" + reply + "\r\n").getBytes());
} else {
logger.info("Command not found: {}", command);
out.write(("\r\nCommand '" + command + "' not found. Try 'exit'.\r\n").getBytes());
}
out.flush();
callback.onExit(0);
}
@Override
public void destroy() {
}
public ExitCallback getCallback() {
return callback;
}
}

@ -1,8 +1,10 @@
package com.example.sshd.config;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.apache.sshd.SshServer;
@ -11,15 +13,19 @@ import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class SshConfig {
private static final Logger logger = LoggerFactory.getLogger(SshConfig.class);
@Value("${ssh-server.port}")
private int port;
@ -29,6 +35,12 @@ public class SshConfig {
@Value("${ssh-server.root.username:root}")
private String rootUsername;
@Value("${ssh-server.automatic-replies.location}")
private String repliesProperties;
@Autowired
ApplicationContext applicationContext;
@Bean
public SshServer sshd() throws IOException, NoSuchAlgorithmException {
SshServer sshd = SshServer.setUpDefaultServer();
@ -43,7 +55,18 @@ public class SshConfig {
}
});
sshd.setShellFactory(new EchoShellFactory());
sshd.setCommandFactory(command -> applicationContext.getBean(OnetimeCommand.class, command));
sshd.start();
return sshd;
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public Properties repliesProperties() throws IOException {
Properties prop = new Properties();
File configFile = new File(repliesProperties);
FileInputStream stream = new FileInputStream(configFile);
prop.load(stream);
return prop;
}
}

Loading…
Cancel
Save