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
Use random local ports when doing automatic port forwarding or reuse existing tunnels #3561
Comments
Hi @andreyorst, thanks for the detailed issue! I'd favor (oftentimes, choosing port Would you be interested in opening a PR? |
Probably if we gave 'contexts' as described here #3400 this problem wouldn't exist |
I can do that but probably a bit later |
You can run
Apparently, specifying 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 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.
The text was updated successfully, but these errors were encountered: