diff --git a/src/clj_ssh/proxy.clj b/src/clj_ssh/proxy.clj new file mode 100644 index 0000000..80352dc --- /dev/null +++ b/src/clj_ssh/proxy.clj @@ -0,0 +1,37 @@ +(ns clj-ssh.proxy + "Provides an SSH proxy" + (:import + [com.jcraft.jsch ProxySOCKS4 ProxySOCKS5])) + +;; based on http://sourceforge.net/apps/mediawiki/jsch/index.php?title=ProxySSH + +;; (defrecord SSHProxy [gateway channel istream ostream] +;; Proxy +;; (close [_] (.disconnect channel)) +;; (connect [_ _ host port timeout] +;; (reset! channel (.openChannel gateway "direct-tcpip")) +;; (doto channel +;; (.setHost host) +;; (.setPort port)) +;; (reset! iStream (.getInputStream @channel)) +;; (reset! oStream (.getOutputStream @channel)) +;; (.connect @channel)) +;; (getInputStream [_] iStream) +;; (getOutputStream [_] oStream) +;; (getSocket [_])) + + +(defn socks5-proxy + "Return an SOCKS5 proxy using the specified host and port." + [host port] + (ProxySOCKS5. host port)) + +(defn socks4-proxy + "Return an SOCKS5 proxy using the specified host and port." + [host port] + (ProxySOCKS4. host port)) + +(defn set-session-proxy + "Set the proxy for the given session." + [session proxy] + (.setProxy session proxy)) diff --git a/test/clj_ssh/proxy_test.clj b/test/clj_ssh/proxy_test.clj new file mode 100644 index 0000000..3ad5d64 --- /dev/null +++ b/test/clj_ssh/proxy_test.clj @@ -0,0 +1,15 @@ +(ns clj-ssh.proxy-test + (:require + [clojure.test :refer :all] + [clj-ssh.proxy :refer :all] + [clj-ssh.ssh :refer [session ssh ssh-agent with-connection]])) + +(deftest ^:proxy proxy-test + ;; requires a SOCS proxy available on localhost port 1080 + (let [agent (ssh-agent {}) + proxy (socks5-proxy "localhost" 1080) + session (session agent "127.0.0.1" {:strict-host-key-checking :no})] + (set-session-proxy session proxy) + (with-connection session + (let [result (ssh session {:in "echo hello"})] + (println (result :out))))))