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 SSH tunnel.

Source Code

;; Defined in ~/.emacs.d/elpa/cider-20260414.1619/nrepl-client.el
(defun nrepl--ssh-tunnel-connect (host port)
  "Connect to a remote machine identified by HOST and PORT through SSH tunnel."
  (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")))
         (cmd (nrepl--ssh-tunnel-command ssh remote-dir port))
         (tunnel-buf (nrepl-tunnel-buffer-name
                      `((:host ,host) (:port ,port))))
         (tunnel (start-process-shell-command "nrepl-tunnel" tunnel-buf cmd)))
    (process-put tunnel :waiting-for-port t)
    (set-process-filter tunnel (nrepl--ssh-tunnel-filter port))
    (while (and (process-live-p tunnel)
                (process-get tunnel :waiting-for-port))
      (accept-process-output nil 0.005))
    (if (not (process-live-p tunnel))
        (error "[nREPL] SSH port forwarding failed.  Check the '%s' buffer" tunnel-buf)
      (message "[nREPL] SSH port forwarding established to localhost:%s" port)
      (let ((endpoint (nrepl--direct-connect "localhost" port)))
        (thread-first
          endpoint
          (plist-put :tunnel tunnel)
          (plist-put :remote-host host))))))