Created
April 25, 2015 23:40
-
-
Save osvein/b9304449198a028f4c8b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.InputStream; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import org.apache.sshd.SshServer; | |
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.PasswordAuthenticator; | |
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider; | |
import org.apache.sshd.server.session.ServerSession; | |
public final class Hermegaas implements Command, Factory<Command>, Runnable, PasswordAuthenticator { | |
private final SshServer sshd = SshServer.setUpDefaultServer(); | |
private OutputStream out; | |
private InputStream in; | |
private OutputStream err; | |
private ExitCallback exit; | |
private boolean loop; | |
public static void main(String[] args) { | |
new Hermegaas(); | |
} | |
public Hermegaas() { | |
sshd.setPort(8000); | |
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser")); | |
sshd.setShellFactory(this); | |
sshd.setPasswordAuthenticator(this); | |
} | |
@Override | |
public void run() { | |
while (loop) { | |
try { | |
int c = this.in.read(); | |
if (c == -1) continue; | |
if (c == 'z') this.exit.onExit(0); | |
this.out.write(c); | |
} | |
catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
@Override | |
public boolean authenticate(String username, String password, ServerSession session) { | |
return username.equals("root") && password.equals("toor"); | |
} | |
@Override | |
public Command create() { | |
return this; | |
} | |
@Override | |
public void start(Environment env) { | |
this.loop = true; | |
new Thread(this).start(); | |
} | |
@Override | |
public void destroy() { | |
this.loop = false; | |
} | |
@Override | |
public void setOutputStream(OutputStream out) { | |
this.out = out; | |
} | |
@Override | |
public void setInputStream(InputStream in) { | |
this.in = in; | |
} | |
@Override | |
public void setErrorStream(OutputStream err) { | |
this.err = err; | |
} | |
@Override | |
public void setExitCallback(ExitCallback callback) { | |
this.exit = callback; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment