(ns ssh-repl.client | |
(:require [clojure.java.io :as io])) | |
(defn connect | |
([user host repl-port {:as options | |
:keys [ssh-port repl-host key password] | |
:or {ssh-port 22 | |
repl-host "localhost"}}] | |
(let [client (doto (org.apache.sshd.client.SshClient/setUpDefaultClient) .start) | |
session (-> client (.connect user host ssh-port) (doto .await) .getSession)] | |
(if key | |
(.addPublicKeyIdentity session | |
(org.apache.sshd.common.util.security.SecurityUtils/loadKeyPairIdentity | |
"shhhh" (io/input-stream key) | |
(reify org.apache.sshd.common.config.keys.FilePasswordProvider | |
(getPassword [_ _] password)))) | |
(.addPasswordIdentity session password)) | |
(-> session .auth .verify) | |
{:ssh-session session | |
:create-pair | |
(fn [] | |
(let [channel (.createDirectTcpipChannel session | |
(org.apache.sshd.common.util.net.SshdSocketAddress. "ssh-repl-client" 0) | |
(org.apache.sshd.common.util.net.SshdSocketAddress. repl-host repl-port))] | |
(-> channel .open .verify) | |
{:in (-> channel .getInvertedIn (io/writer :encoding "UTF-8")) | |
:out (-> channel .getInvertedOut (io/reader :encoding "UTF-8"))}))}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment