Skip to content

Commit 6ddf1ea

Browse files
committed
Refactor local port forwarding support
1 parent 1d99321 commit 6ddf1ea

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

src/clj_ssh/ssh.clj

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -392,24 +392,28 @@ keys. All other option key pairs will be passed as SSH config options."
392392
(.toByteArray err-stream)
393393
(.toString err-stream out))]))))
394394

395-
(defmacro with-local-tunnel
396-
"Creates a context in which a local SSH tunnel is established for the session. (Use before the connection is opened.)"
397-
[session local-port remote-port & body]
395+
(defn forward-local-port
396+
"Start local port forwarding"
397+
([session local-port remote-port remote-host]
398+
(.setPortForwardingL session local-port remote-host remote-port))
399+
([session local-port remote-port]
400+
(forward-local-port session local-port remote-port "localhost")))
401+
402+
(defn unforward-local-port
403+
"Remove local port forwarding"
404+
[session local-port]
405+
(.delPortForwardingL session local-port))
406+
407+
(defmacro with-local-port-forward
408+
"Creates a context in which a local SSH tunnel is established for the session.
409+
(Use before the connection is opened.)"
410+
[[session local-port remote-port & [remote-host & _]] & body]
398411
`(try
399-
(.setPortForwardingL ~session ~local-port "localhost" ~remote-port)
412+
(forward-local-port
413+
~session ~local-port ~remote-port ~(or remote-host "localhost"))
400414
~@body
401415
(finally
402-
(.delPortForwardingL ~session "localhost" ~local-port))))
403-
404-
(defn ssh-tunnel
405-
"Run a ssh tunnel."
406-
[session local-port remote-port]
407-
(with-local-tunnel session local-port remote-port
408-
(.setDaemonThread session true)
409-
(with-connection session
410-
(.setServerAliveInterval session 1000)
411-
(while (connected? session)
412-
(Thread/sleep 100)))))
416+
(unforward-local-port ~session ~local-port))))
413417

414418
(defn default-session [host username port password]
415419
(doto (session-impl

test/clj_ssh/ssh_test.clj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,22 @@ list, Alan Dipert and MeikelBrandmeyer."
522522
(with-ssh-agent []
523523
(let [[priv pub] (generate-keypair :rsa 1024 "hello")]
524524
(add-identity *ssh-agent* "name" priv pub (.getBytes "hello")))))
525+
526+
(deftest forward-local-port-test
527+
(testing "minimal test"
528+
(with-ssh-agent [false]
529+
(add-identity (private-key-path))
530+
(let [session (session "localhost" :username (username)
531+
:strict-host-key-checking :no)]
532+
(is (instance? com.jcraft.jsch.Session session))
533+
(is (not (connected? session)))
534+
(connect session)
535+
(is (connected? session))
536+
(forward-local-port session 2222 22)
537+
(unforward-local-port session 2222)
538+
(forward-local-port session 2222 22 "localhost")
539+
(unforward-local-port session 2222)
540+
(with-local-port-forward [session 2222 22]
541+
(is true))
542+
(with-local-port-forward [session 2222 22 "localhost"]
543+
(is true))))))

0 commit comments

Comments
 (0)