Created
October 3, 2012 05:58
-
-
Save dylanvee/3825303 to your computer and use it in GitHub Desktop.
JRuby + Apache MINA SSHD
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
# First, download a binary distribution from | |
# http://mina.apache.org/sshd/sshd-070.html | |
# and copy lib/*.jar to your cwd | |
# Load all the jar files | |
require 'java' | |
Dir['./*.jar'].each {|jar| require jar } | |
# Import some Java classes | |
java_import java.util.EnumSet | |
java_import org.apache.sshd.SshServer | |
java_import org.apache.sshd.common.compression.CompressionZlib | |
java_import org.apache.sshd.server.PasswordAuthenticator | |
java_import org.apache.sshd.server.PublickeyAuthenticator | |
java_import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider | |
java_import org.apache.sshd.server.shell.ProcessShellFactory | |
PORT = 5000 | |
class CustomPasswordAuthenticator | |
include PasswordAuthenticator | |
def authenticate(username, password, session) | |
# Your password authentication magic here | |
# Return true for auth success and false for failure | |
true | |
end | |
end | |
class CustomPublickeyAuthenticator | |
include PublickeyAuthenticator | |
def authenticate(username, key, session) | |
# Your public key authentication magic here | |
# Return true for auth success and false for failure | |
true | |
end | |
end | |
# Set up the server with sensible defaults for ciphers, etc. | |
sshd = SshServer.setUpDefaultServer | |
# Set the port | |
sshd.setPort(PORT) | |
# Create a new host key or read an existing one | |
sshd.setKeyPairProvider(SimpleGeneratorHostKeyProvider.new('key.ser')) | |
# Enable zlib compression (optional, omit to reduce CPU usage) | |
sshd.setCompressionFactories([CompressionZlib::Factory.new]) | |
# Install your custom authenticator(s) | |
sshd.setPasswordAuthenticator(CustomPasswordAuthenticator.new) | |
sshd.setPublickeyAuthenticator(CustomPublickeyAuthenticator.new) | |
# Set the shell to run in each new session | |
# The user's name will be available in the $USER environment variable | |
sshd.setShellFactory( | |
ProcessShellFactory.new( | |
['/bin/sh', '-i', '-l'].to_java(:string), | |
EnumSet.of( | |
ProcessShellFactory::TtyOptions::ONlCr, | |
ProcessShellFactory::TtyOptions::ICrNl))) | |
# Start the server | |
sshd.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment