Function: nrepl--ssh-tunnel-connect
nrepl--ssh-tunnel-connect is a byte-compiled function defined in
nrepl-client.el.
Signature
(nrepl--ssh-tunnel-connect HOST PORT)
Documentation
Connect to a remote machine identified by HOST and PORT through an SSH tunnel.
A free local port is forwarded to PORT on the remote host and the connection is made to that local port, so several remote REPLs that happen to share the same remote port don't collide on localhost.
Source Code
;; Defined in ~/.emacs.d/elpa/cider-20260822.1520/nrepl-client.el
(defun nrepl--ssh-tunnel-connect (host port)
"Connect to a remote machine identified by HOST and PORT through an SSH tunnel.
A free local port is forwarded to PORT on the remote host and the connection
is made to that local port, so several remote REPLs that happen to share the
same remote port don't collide on localhost."
(message "[nREPL] Establishing SSH tunneled connection to %s:%s ..." host port)
(let* ((file-name (or (buffer-file-name) nrepl-project-dir))
(remote-dir (cond
;; If current buffer is a TRAMP buffer and its host is
;; the same as HOST, reuse its connection parameters for
;; SSH tunnel.
((nrepl--ssh-file-name-matches-host-p file-name host) file-name)
;; Otherwise, if HOST was provided, use it for connection.
(host (format "/ssh:%s:" host))
;; Use default directory as fallback.
(t default-directory)))
(ssh (or (executable-find "ssh")
(error "[nREPL] Cannot locate 'ssh' executable")))
(local-port (nrepl--available-local-port))
(args (nrepl--ssh-tunnel-args remote-dir local-port port))
(tunnel-buf (nrepl-tunnel-buffer-name
`((:host ,host) (:port ,port))))
(tunnel (apply #'start-process "nrepl-tunnel" tunnel-buf ssh args))
(deadline (time-add (current-time) nrepl-ssh-tunnel-timeout))
(ready nil))
(set-process-filter tunnel #'nrepl--ssh-tunnel-filter)
;; Wait for the forwarded port to start accepting connections. We probe by
;; actually connecting, which is robust across ssh versions and locales --
;; unlike scraping ssh's diagnostic output. The deadline is pushed back
;; whenever ssh emits output, so an interactive password or 2FA prompt
;; doesn't trip the timeout.
(while (and (not ready)
(process-live-p tunnel)
(time-less-p (current-time) deadline))
(if (nrepl--port-open-p "localhost" local-port)
(setq ready t)
(when (accept-process-output tunnel 0.2)
(setq deadline (time-add (current-time) nrepl-ssh-tunnel-timeout)))))
(let ((endpoint (and ready
(nrepl--direct-connect "localhost" local-port 'no-error))))
(cond
(endpoint
(message "[nREPL] SSH tunnel established (localhost:%s -> %s:%s)"
local-port host port)
(thread-first
endpoint
(plist-put :tunnel tunnel)
(plist-put :remote-host host)))
(t
(when (process-live-p tunnel)
(nrepl--kill-process tunnel))
(error "[nREPL] SSH tunnel to %s:%s failed or timed out; check the '%s' buffer"
host port tunnel-buf))))))