parent
82f6bdbae4
commit
83a865b71d
@ -0,0 +1,145 @@
|
||||
package com.example.sshd.core;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Arrays;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.sshd.server.Command;
|
||||
import org.apache.sshd.server.Environment;
|
||||
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.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;
|
||||
|
||||
import com.example.sshd.util.ReplyUtil;
|
||||
|
||||
@Component
|
||||
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||
public class EchoShell implements Command, Runnable, SessionAware {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(EchoShell.class);
|
||||
|
||||
@Autowired
|
||||
ReplyUtil replyUtil;
|
||||
|
||||
@Autowired
|
||||
Properties hashReplies;
|
||||
|
||||
protected InputStream in;
|
||||
protected OutputStream out;
|
||||
protected OutputStream err;
|
||||
protected ExitCallback callback;
|
||||
protected Environment environment;
|
||||
protected Thread thread;
|
||||
protected ServerSession session;
|
||||
|
||||
@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;
|
||||
thread = new Thread(this, remoteIpAddress());
|
||||
logger.info("environment: {}, thread-name: {}", environment.getEnv(), thread.getName());
|
||||
thread.start();
|
||||
}
|
||||
|
||||
protected String remoteIpAddress() {
|
||||
String remoteIpAddress = "";
|
||||
|
||||
if (session.getIoSession().getRemoteAddress() instanceof InetSocketAddress) {
|
||||
InetSocketAddress remoteAddress = (InetSocketAddress) session.getIoSession().getRemoteAddress();
|
||||
remoteIpAddress = remoteAddress.getAddress().getHostAddress();
|
||||
} else {
|
||||
remoteIpAddress = session.getIoSession().getRemoteAddress().toString();
|
||||
}
|
||||
return remoteIpAddress;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
thread.interrupt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String prompt = hashReplies.getProperty("prompt", "$ ");
|
||||
try {
|
||||
out.write(prompt.getBytes());
|
||||
out.flush();
|
||||
|
||||
BufferedReader r = new BufferedReader(new InputStreamReader(in));
|
||||
String command = "";
|
||||
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
int s = r.read();
|
||||
if (s == 13 || s == 10) {
|
||||
|
||||
boolean containsExit = Arrays.asList(command.split(";")).stream().map(cmd -> {
|
||||
boolean wantsExit = false;
|
||||
try {
|
||||
wantsExit = replyUtil.replyToCommand(cmd.trim(), out, prompt, session);
|
||||
out.flush();
|
||||
} catch (Exception e) {
|
||||
logger.error("run error!", e);
|
||||
}
|
||||
return wantsExit;
|
||||
}).reduce((a, b) -> a || b).get();
|
||||
|
||||
if (containsExit) {
|
||||
break;
|
||||
}
|
||||
command = "";
|
||||
} else {
|
||||
logger.trace("input character: {}", s);
|
||||
if (s == 127) {
|
||||
if (command.length() > 0) {
|
||||
command = command.substring(0, command.length() - 1);
|
||||
out.write(s);
|
||||
}
|
||||
} else if (s >= 32 && s < 127) {
|
||||
command += (char) s;
|
||||
out.write(s);
|
||||
}
|
||||
}
|
||||
out.flush();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("run error!", e);
|
||||
} finally {
|
||||
callback.onExit(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSession(ServerSession session) {
|
||||
this.session = session;
|
||||
}
|
||||
}
|
@ -1,139 +1,21 @@
|
||||
package com.example.sshd.core;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.sshd.common.Factory;
|
||||
import org.apache.sshd.server.Command;
|
||||
import org.apache.sshd.server.Environment;
|
||||
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.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.example.sshd.util.ReplyUtil;
|
||||
|
||||
@Component
|
||||
public class EchoShellFactory implements Factory<Command> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(EchoShellFactory.class);
|
||||
|
||||
@Autowired
|
||||
ReplyUtil replyUtil;
|
||||
|
||||
@Autowired
|
||||
Properties hashReplies;
|
||||
|
||||
@Autowired
|
||||
Map<String, String> ipInfoMapping;
|
||||
ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public Command create() {
|
||||
return new EchoShell();
|
||||
}
|
||||
|
||||
public class EchoShell implements Command, Runnable, SessionAware {
|
||||
|
||||
protected InputStream in;
|
||||
protected OutputStream out;
|
||||
protected OutputStream err;
|
||||
protected ExitCallback callback;
|
||||
protected Environment environment;
|
||||
protected Thread thread;
|
||||
protected ServerSession session;
|
||||
|
||||
@Override
|
||||
public void setInputStream(InputStream in) {
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOutputStream(OutputStream out) {
|
||||
this.out = out;
|
||||
return (Command) applicationContext.getBean("echoShell");
|
||||
}
|
||||
|
||||
@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;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
thread.interrupt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String prompt = hashReplies.getProperty("prompt", "$ ");
|
||||
try {
|
||||
out.write(prompt.getBytes());
|
||||
out.flush();
|
||||
|
||||
BufferedReader r = new BufferedReader(new InputStreamReader(in));
|
||||
String command = "";
|
||||
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
int s = r.read();
|
||||
if (s == 13 || s == 10) {
|
||||
if (!replyUtil.replyToCommand(command, out, prompt, session)) {
|
||||
out.flush();
|
||||
return;
|
||||
}
|
||||
command = "";
|
||||
} else {
|
||||
logger.trace("input character: {}", s);
|
||||
if (s == 127) {
|
||||
if (command.length() > 0) {
|
||||
command = command.substring(0, command.length() - 1);
|
||||
out.write(s);
|
||||
}
|
||||
} else if (s >= 32 && s < 127) {
|
||||
command += (char) s;
|
||||
out.write(s);
|
||||
}
|
||||
}
|
||||
out.flush();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("run error!", e);
|
||||
} finally {
|
||||
callback.onExit(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSession(ServerSession session) {
|
||||
this.session = session;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,94 +1,40 @@
|
||||
package com.example.sshd.core;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.sshd.server.Command;
|
||||
import org.apache.sshd.server.Environment;
|
||||
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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.example.sshd.util.ReplyUtil;
|
||||
|
||||
@Component
|
||||
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||
public class OnetimeCommand implements Command, SessionAware {
|
||||
public class OnetimeCommand extends EchoShell {
|
||||
|
||||
@Autowired
|
||||
ReplyUtil replyUtil;
|
||||
private static final Logger logger = LoggerFactory.getLogger(OnetimeCommand.class);
|
||||
|
||||
private InputStream in;
|
||||
private OutputStream out;
|
||||
private OutputStream err;
|
||||
private ExitCallback callback;
|
||||
private Environment environment;
|
||||
private String command;
|
||||
private ServerSession session;
|
||||
|
||||
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;
|
||||
replyUtil.replyToCommand(command, out, "", session);
|
||||
public void run() {
|
||||
try {
|
||||
Arrays.asList(command.split(";")).stream().forEach(cmd -> {
|
||||
try {
|
||||
replyUtil.replyToCommand(cmd.trim(), out, "", session);
|
||||
out.flush();
|
||||
callback.onExit(0);
|
||||
} catch (Exception e) {
|
||||
logger.error("run error!", e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
public ExitCallback getCallback() {
|
||||
return callback;
|
||||
});
|
||||
} catch (Exception e) {
|
||||
logger.error("run error!", e);
|
||||
} finally {
|
||||
callback.onExit(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSession(ServerSession session) {
|
||||
this.session = session;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue