V1.2.0 Both OnetimeCommand and EchoShell are now SessionAware!

master
Ng Yat Yan 1 month ago
parent f57a7c780d
commit a0143e96f5

@ -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.1.3</version> <version>1.2.0</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>

@ -5,6 +5,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.UUID; import java.util.UUID;
@ -12,6 +14,8 @@ import org.apache.sshd.common.Factory;
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;
import org.apache.sshd.server.SessionAware;
import org.apache.sshd.server.session.ServerSession;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -30,12 +34,15 @@ public class EchoShellFactory implements Factory<Command> {
@Autowired @Autowired
Properties hashReplies; Properties hashReplies;
@Autowired
Map<String, String> ipInfoMapping;
@Override @Override
public Command create() { public Command create() {
return new EchoShell(); return new EchoShell();
} }
public class EchoShell implements Command, Runnable { public class EchoShell implements Command, Runnable, SessionAware {
protected InputStream in; protected InputStream in;
protected OutputStream out; protected OutputStream out;
@ -43,6 +50,7 @@ public class EchoShellFactory implements Factory<Command> {
protected ExitCallback callback; protected ExitCallback callback;
protected Environment environment; protected Environment environment;
protected Thread thread; protected Thread thread;
protected ServerSession session;
@Override @Override
public void setInputStream(InputStream in) { public void setInputStream(InputStream in) {
@ -67,8 +75,16 @@ public class EchoShellFactory implements Factory<Command> {
@Override @Override
public void start(Environment env) throws IOException { public void start(Environment env) throws IOException {
environment = env; environment = env;
logger.info("environment: {}", environment.getEnv());
thread = new Thread(this, UUID.randomUUID().toString()); if (session.getIoSession().getRemoteAddress() instanceof InetSocketAddress) {
InetSocketAddress remoteAddress = (InetSocketAddress) session.getIoSession().getRemoteAddress();
String remoteIpAddress = remoteAddress.getAddress().getHostAddress();
thread = new Thread(this, remoteIpAddress);
} else {
thread = new Thread(this, session.getIoSession().getRemoteAddress().toString());
}
logger.info("environment: {}, thread-name: {}", environment.getEnv(), thread.getName());
thread.start(); thread.start();
} }
@ -90,7 +106,7 @@ public class EchoShellFactory implements Factory<Command> {
while (!Thread.currentThread().isInterrupted()) { while (!Thread.currentThread().isInterrupted()) {
int s = r.read(); int s = r.read();
if (s == 13 || s == 10) { if (s == 13 || s == 10) {
if (!replyUtil.replyToCommand(command, out, prompt)) { if (!replyUtil.replyToCommand(command, out, prompt, session)) {
out.flush(); out.flush();
return; return;
} }
@ -115,5 +131,10 @@ public class EchoShellFactory implements Factory<Command> {
callback.onExit(0); callback.onExit(0);
} }
} }
@Override
public void setSession(ServerSession session) {
this.session = session;
}
} }
} }

@ -7,6 +7,8 @@ import java.io.OutputStream;
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;
import org.apache.sshd.server.SessionAware;
import org.apache.sshd.server.session.ServerSession;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
@ -16,7 +18,7 @@ import com.example.sshd.util.ReplyUtil;
@Component @Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class OnetimeCommand implements Command { public class OnetimeCommand implements Command, SessionAware {
@Autowired @Autowired
ReplyUtil replyUtil; ReplyUtil replyUtil;
@ -27,6 +29,7 @@ public class OnetimeCommand implements Command {
private ExitCallback callback; private ExitCallback callback;
private Environment environment; private Environment environment;
private String command; private String command;
private ServerSession session;
public OnetimeCommand(String cmd) { public OnetimeCommand(String cmd) {
command = cmd; command = cmd;
@ -71,7 +74,7 @@ public class OnetimeCommand implements Command {
@Override @Override
public void start(Environment env) throws IOException { public void start(Environment env) throws IOException {
environment = env; environment = env;
replyUtil.replyToCommand(command, out, ""); replyUtil.replyToCommand(command, out, "", session);
out.flush(); out.flush();
callback.onExit(0); callback.onExit(0);
} }
@ -83,4 +86,9 @@ public class OnetimeCommand implements Command {
public ExitCallback getCallback() { public ExitCallback getCallback() {
return callback; return callback;
} }
@Override
public void setSession(ServerSession session) {
this.session = session;
}
} }

@ -2,12 +2,15 @@ package com.example.sshd.util;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Properties; import java.util.Properties;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.apache.sshd.server.session.ServerSession;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -21,17 +24,30 @@ public class ReplyUtil {
@Autowired @Autowired
Properties hashReplies; Properties hashReplies;
@Autowired @Autowired
Properties regexMapping; Properties regexMapping;
public boolean replyToCommand(String command, OutputStream out, String prompt) throws IOException { @Autowired
Map<String, String> ipInfoMapping;
public boolean replyToCommand(String command, OutputStream out, String prompt, ServerSession session)
throws IOException {
String cmdHash = DigestUtils.md5Hex(command.trim()).toUpperCase(); String cmdHash = DigestUtils.md5Hex(command.trim()).toUpperCase();
if (StringUtils.equals(command.trim(), "exit")) { if (StringUtils.equals(command.trim(), "about")) {
logger.info("[{}] About command detected: {}", cmdHash, command.trim());
if (session.getIoSession().getRemoteAddress() instanceof InetSocketAddress) {
InetSocketAddress remoteAddress = (InetSocketAddress) session.getIoSession().getRemoteAddress();
String remoteIpAddress = remoteAddress.getAddress().getHostAddress();
out.write(String.format("\r\n%s\r\n%s", ipInfoMapping.get(remoteIpAddress), prompt).getBytes());
} else {
out.write(String.format("\r\n%s\r\n%s", session.getIoSession().getRemoteAddress(), prompt).getBytes());
}
} else if (StringUtils.equals(command.trim(), "exit")) {
logger.info("[{}] Exiting command detected: {}", cmdHash, command.trim()); logger.info("[{}] Exiting command detected: {}", cmdHash, command.trim());
out.write(("\r\nExiting...\r\n").getBytes()); out.write(String.format("\r\nExiting...\r\n%s", prompt).getBytes());
return false; return false;
} else if (hashReplies.containsKey(command.trim())) { } else if (hashReplies.containsKey(command.trim())) {
logger.info("[{}] Known command detected: {}", cmdHash, command.trim()); logger.info("[{}] Known command detected: {}", cmdHash, command.trim());

Loading…
Cancel
Save