commit
a4aec058a1
@ -0,0 +1,4 @@
|
||||
/target/
|
||||
/.classpath
|
||||
/.project
|
||||
/.settings
|
@ -0,0 +1 @@
|
||||
/private.key
|
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="DEBUG">
|
||||
<Appenders>
|
||||
<Console name="LogToConsole" target="SYSTEM_OUT">
|
||||
<PatternLayout
|
||||
pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %p [%t] %c : %m%n" />
|
||||
</Console>
|
||||
<RollingFile name="LogToFile"
|
||||
filePattern="logs/server.%d{yyyy-MM-dd}.log"
|
||||
immediateFlush="true">
|
||||
<PatternLayout
|
||||
pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %p [%t] %c : %m%n" />
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
|
||||
</Policies>
|
||||
</RollingFile>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="LogToConsole" />
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
@ -0,0 +1,4 @@
|
||||
ssh-server:
|
||||
port: 1022
|
||||
private-key:
|
||||
location: "conf/private.key"
|
@ -0,0 +1,99 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.example.sshd</groupId>
|
||||
<artifactId>echo-sshd-server</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<name>ECHO SSH SERVER</name>
|
||||
<description>Learning Apache Mina SSHD library</description>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.3.2</version>
|
||||
</parent>
|
||||
<properties>
|
||||
<mina.version>2.0.25</mina.version>
|
||||
<sshd.version>0.14.0</sshd.version>
|
||||
<bouncycastle.version>1.78.1</bouncycastle.version>
|
||||
<commons-io.version>2.13.0</commons-io.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-log4j2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.mina</groupId>
|
||||
<artifactId>mina-core</artifactId>
|
||||
<version>${mina.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.sshd</groupId>
|
||||
<artifactId>sshd-core</artifactId>
|
||||
<version>${sshd.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcprov-jdk18on</artifactId>
|
||||
<version>${bouncycastle.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcpkix-jdk18on</artifactId>
|
||||
<version>${bouncycastle.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>17</source>
|
||||
<target>17</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<mainClass>com.example.sshd.Boot</mainClass>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>build-info</id>
|
||||
<goals>
|
||||
<goal>build-info</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,31 @@
|
||||
package com.example.sshd;
|
||||
|
||||
import org.apache.sshd.SshServer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Boot {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Boot.class);
|
||||
|
||||
@Autowired
|
||||
protected SshServer sshd;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String configDirectory = "conf";
|
||||
if (args.length > 0) {
|
||||
configDirectory = args[0];
|
||||
}
|
||||
logger.info("config directory: {}", configDirectory);
|
||||
|
||||
if (new java.io.File(configDirectory).exists() && new java.io.File(configDirectory).isDirectory()) {
|
||||
System.setProperty("spring.config.location", configDirectory + "/springboot.yml");
|
||||
System.setProperty("logging.config", configDirectory + "/log4j2.xml");
|
||||
}
|
||||
SpringApplication.run(Boot.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
package com.example.sshd.config;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class EchoShellFactory implements Factory<Command> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(EchoShellFactory.class);
|
||||
|
||||
@Override
|
||||
public Command create() {
|
||||
return new EchoShell();
|
||||
}
|
||||
|
||||
public static class EchoShell implements Command, Runnable {
|
||||
|
||||
private InputStream in;
|
||||
private OutputStream out;
|
||||
private OutputStream err;
|
||||
private ExitCallback callback;
|
||||
private Environment environment;
|
||||
private Thread thread;
|
||||
|
||||
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;
|
||||
thread = new Thread(this, UUID.randomUUID().toString());
|
||||
thread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
thread.interrupt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
out.write("$ ".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 (StringUtils.isBlank(command)) {
|
||||
logger.info("Blank command detected: {}", command);
|
||||
out.write(("\r\n$ ").getBytes());
|
||||
} else if (StringUtils.equals(command, "exit")) {
|
||||
logger.info("Exiting command detected: {}", command);
|
||||
return;
|
||||
} else {
|
||||
logger.info("Command not found: {}", command);
|
||||
out.write(("\r\nCommand '" + command + "' not found. Try 'exit'.\r\n$ ").getBytes());
|
||||
}
|
||||
command = "";
|
||||
} else {
|
||||
out.write(s);
|
||||
command += (char) s;
|
||||
}
|
||||
out.flush();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("run error!", e);
|
||||
} finally {
|
||||
callback.onExit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.example.sshd.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.sshd.SshServer;
|
||||
import org.apache.sshd.server.PasswordAuthenticator;
|
||||
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
|
||||
import org.apache.sshd.server.session.ServerSession;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class SshConfig {
|
||||
|
||||
@Value("${ssh-server.port}")
|
||||
private int port;
|
||||
|
||||
@Value("${ssh-server.private-key.location}")
|
||||
private String pkLocation;
|
||||
|
||||
@Value("${ssh-server.root.username:root}")
|
||||
private String rootUsername;
|
||||
|
||||
@Bean
|
||||
public SshServer sshd() throws IOException, NoSuchAlgorithmException {
|
||||
SshServer sshd = SshServer.setUpDefaultServer();
|
||||
sshd.setPort(port);
|
||||
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File(pkLocation).getPath(), "RSA", 2048));
|
||||
|
||||
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
|
||||
@Override
|
||||
public boolean authenticate(final String username, final String password, final ServerSession session) {
|
||||
return StringUtils.equals(username, rootUsername);
|
||||
}
|
||||
});
|
||||
sshd.setShellFactory(new EchoShellFactory());
|
||||
sshd.start();
|
||||
return sshd;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue