Skip to content

Add support for connectiong via SOCKS proxy #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/clj_ssh/proxy.clj
Original file line number Diff line number Diff line change
@@ -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))
15 changes: 15 additions & 0 deletions test/clj_ssh/proxy_test.clj
Original file line number Diff line number Diff line change
@@ -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))))))