Open andreyorst opened 10 months ago
Hi @andreyorst, thanks for the detailed issue!
I'd favor 1.
because it would imply that CIDER has less state to keep track of - every connection would be independent can can be closed without having to worry about its neighbors.
(oftentimes, choosing port 0
results in a random port being chosen for us)
Would you be interested in opening a PR?
instead of remembering what port this particular service uses every time.
Probably if we gave 'contexts' as described here https://github.com/clojure-emacs/cider/issues/3400 this problem wouldn't exist
Would you be interested in opening a PR?
I can do that but probably a bit later
You can run (nrepl--ssh-tunnel-command (executable-find "ssh") "/ssh:cider-devs@192.168.50.9#12345:" "1234")
to see the ssh command that we produce, namely:
"/usr/bin/ssh -v -N -L 1234:localhost:1234 -l 'cider-devs' '192.168.50.9' -p '12345' "
Apparently, specifying 0:localhost:1234
would work. Inspecting the -v
output would tell us the chosen port.
Maybe we don't even need to keep track of what port was chosen - maybe it would just work, and every REPL process would get its own ssh tunnel with a different local port.
I'd appreciate if you could try it out at some point.
Cheers - V
Hi @andreyorst , did you have the chance to try port 0?
not yet, but hopefully this week. Renovation is a huge timesink :)
@vemv sorry it took so long, the end of the year is a busy time
Unfortunately the -L
switch doesn't allow for using 0:localhost:remote-port
. I've tried the recipe and indeed it fails.
So I tried to use this kind of trick:
(defun nrepl--ssh-tunnel-command (ssh dir port)
"Command string to open SSH tunnel to the host associated with DIR's PORT."
(with-parsed-tramp-file-name dir v
;; this abuses the -v option for ssh to get output when the port
;; forwarding is set up, which is used to synchronise on, so that
;; the port forwarding is up when we try to connect.
(format-spec
"%s -v -N -L %l:localhost:%p %u'%h' %n"
`((?s . ,ssh)
(?l . ,(+ 1024 (random 54511)))
(?p . ,port)
(?h . ,v-host)
(?u . ,(if v-user (format "-l '%s' " v-user) ""))
(?n . ,(if v-port (format "-p '%s' " v-port) ""))))))
It works but is unreliable, as the port is randomly chosen and not checked to be available. Perhaps, we can simply loop until the valid port pops up, this is suggested on the internet, and I couldn't find any other way to do it. Or maybe there's a Elisp function to get a free local port, but I didn't dig deeper into this yet.
I often have to connect to a remote REPL running behind an SSH. CIDER has this custom option
nrepl-use-ssh-fallback-for-remote-hosts
which I relied on for quite a long time until recently.The problem is when I use
cider-connect-clj
, and give it a remote host and port it will open an SSH tunnel to that host and port, which by itself is fine unless you want to connect to the same port from multiple projects, which I often have to do. It is problematic because CIDER creates a port forwarding from the given port to the same port, e.g. from localhost:1234 to user@remote:1234. Thus, when I go into the next project and try to do the same thing, CIDER tries to set up port forwarding from 1234 again, but it is already taken.There are two possible solutions, I think:
M-x cider-connect-clj RET user@host RET 1234 RET
, bind localhost:46573 to user@host:1234 instead of localhost:1234. It is similar to how CIDER currently works when usingcider-jack-in-clj
.As an alternative, I've been using a custom
ssh-tunnel
function I've made for myself:It's also handy to have the ability to fix a specific remote port in a dir local variable, like I do above, so I could connect to a specific port immediately, instead of remembering what port this particular service uses every time.