C
Coder.com•3y ago
sarco

port-forwarding with many ports

Hello! For my application I need to port forward a great number of ports on my local machine from an up and running workspace. This: coder port-forward my-ws --tcp p1 --tcp p2 --tcp p3 --tcp p4 --tcp p5 --tcp p6 ... doesn't look great. Can you suggest me a smarter way to achieve this ? Thank you all
21 Replies
sharkymark
sharkymark•3y ago
@sarco if you run coder port-forward --help I think there's an example. coder port-forward <workspace> --tcp 8080:8080 --tcp 9000:3000 --udp 5353:53 Is this what you're asking?
sarco
sarco•3y ago
No actually I'd like to avoid to type --tcp 50 times... i need to port forward a great number of ports so the resulting command: coder port-forward workspace --tcp 1111 --tcp 2222 --tcp 3333 --tcp 4444 -tcp 6666 --tcp 7777 etc... doesn't really looks readable.
sharkymark
sharkymark•3y ago
What would be optimal? an array?
sarco
sarco•3y ago
an array, or even better, a file where the ports are listed
sharkymark
sharkymark•3y ago
like one line per port?
sharkymark
sharkymark•3y ago
I opened an issue @sarco feel free to add onto it. Hope I got it basically right. Your feedback above made it kind of clear. https://github.com/coder/coder/issues/3766
GitHub
Forward a range of ports with the port-forward subcommand · Issue...
A community user with multiple users in their deployment reported a feature request to improve the port-forward experience. Right now, you have to enter --tcp or --udp for each port. The request, a...
sarco
sarco•3y ago
why not yes Great! I'll follow the issue. In order to address my specific problem for the moment, I will just cycle over multiple coder port-forward command: for port in ports: coder port-forward my-ws --tcp port:port
maf
maf•3y ago
Another way to achieve this in bash would be to define a ports array, e.g. via ports=(4000 4005 4010) or reading from a file ports=($(<ports.txt)) and then running the port forward command like so:
ports=(4000 4005 4010)
coder port-forward my-ws ${ports[@]/#/--tcp }
ports=(4000 4005 4010)
coder port-forward my-ws ${ports[@]/#/--tcp }
Phorcys
Phorcys•3y ago
what about having --tcp 1111 2222 3333 4444 --udp 5555 6666 but this is clearly good
maf
maf•3y ago
We could take this principle and make coder work the way we want via a bash function (e.g. when places in .bashrc)
coder() {
if [[ $1 == port-forward ]] && [[ -f $3 ]]; then
ports=($(<$3))
command coder port-forward $2 ${ports[@]/#/--tcp }
else
command "$@"
fi
}
coder() {
if [[ $1 == port-forward ]] && [[ -f $3 ]]; then
ports=($(<$3))
command coder port-forward $2 ${ports[@]/#/--tcp }
else
command "$@"
fi
}
Now we can type coder port-forward my-ws ./ports.txt. Obviously this is very rudimentary, and we may want to specify udp instead of tcp, but the above could be extended to support such use-cases. I think we could be able to do something like --tcp 1111,2222,3333, but I don't think our CLI library supports the space-separated values for a single flag.
Phorcys
Phorcys•3y ago
that's fine by me too
sarco
sarco•3y ago
As soon as I may pass the array of ports to the --tcp options sounds good to me
maf
maf•3y ago
What syntax would you propose? Like I mentioned to @Phorcys suggestion, we can't do --tcp 1111 2222, so --tcp "${ports[@]}" probably won't happen. In theory --tcp "${ports[*]} could work if we allowed space-separated ports in the single string, but I don't like how this syntax easily would lead to incorrect use (e.g. via [@]).
Phorcys
Phorcys•3y ago
honestly comma-separated is good
sarco
sarco•3y ago
I agree
maf
maf•3y ago
Great, posted the suggestion on the GH ticket.
dedboi
dedboi•3y ago
for this one i'm doing smth different -> port forward the ssh session to expose a socks proxy and use it on my browser to reach the exposed ports @sarco ssh -D 65300 coder.$workspace -> then use socks5://localhost:65300 as a proxy on your browser or anywhere else 😉
Phorcys
Phorcys•3y ago
ew
maf
maf•3y ago
Beauty is in the eye of the beholder 😉
Phorcys
Phorcys•3y ago
i mean hey it works
sarco
sarco•3y ago
Sorry for my late reply, I did not check discord. Thank you @dedboi but I was looking for something more straightforward. However I am happy my request triggered the devs to implement this feature
Want results from more Discord servers?
Add your server