github / gh-net

A network bridge between a Codespace and a local machine.
https://github.com/github/gh-net
285 stars 21 forks source link

DNS for a localhost on my personal machine #44

Open KyleJamesWalker opened 2 years ago

KyleJamesWalker commented 2 years ago

Is there a way connect to my laptop's machine running a local port?

We use StrongDM to connect to secure system, and the StrongDM node is protected by our VPC. So we need to connect to the VPN and then have StrongDM open a local port. Something like host.docker.internal does when needing to connect to your host machine.

;TLDR Add dns like host.gh-net.internal to connect to ports on your personal machine.

legomushroom commented 1 year ago

hey @KyleJamesWalker 👋 This scenario was not the main focus for the extension, but we definitely want to support this. In fact, unless you have some exotic network setup, it should work already 🤗

Given your local machine's default network interface has a distinct IP address (IP address that is not on any subnets of the network interfaces inside your Codespace), the extension should be able to find route and forward traffic to it. The missing part is the DNS name of course, but that is fix - add a {IP address} host.gh-net.internal record to the /etc/hosts file inside the Codespace 💻 I'll be working on supporting this by default and officially next tho, so would be great if you can give it a shot when it's ready 😊

Try this:

  1. Find out default gateway interface on your local machine. For me it is 192.168.86.31 on my local network. If the IP address is not unique enough compared to the network interface subnets inside a Codespace, you can add an additional IP alias to the local interface.
  2. Start a server on 0.0.0.0 host and a port, for instance 3000.
  3. Start gh-net extension and connect to a Codespace.
  4. Try to make a request from within the Codespace to the server using the default interface IP address. For instance if that is an HTTP server, wget -d 192.168.86.31:3000 would do the trick.

If this works for you, you can also add the aforementioned record to the hosts file inside Codespace to be able to make the request by the host name.

Few questions:

Thanks!

KyleJamesWalker commented 1 year ago

Thanks for the detailed response, for some reason this isn't working here's what I did:

  1. Ran route get default | grep gateway
    • Result: 192.168.10.65
  2. Start a basic http server with: docker run --rm -it -p 0.0.0.0:8000:80 strm/helloworld-http
  3. Test locally: wget -d http://192.168.10.65:8000
    
    DEBUG output created by Wget 1.21.3 on darwin21.3.0.

Reading HSTS entries from /Users/kyle.walker/.wget-hsts URI encoding = ‘UTF-8’ Converted file name 'index.html' (UTF-8) -> 'index.html' (UTF-8) --2022-09-16 11:56:09-- http://192.168.10.65:8000/ Connecting to 192.168.10.65:8000... connected. Created socket 3. Releasing 0x00006000038341e0 (new refcount 0). Deleting unused 0x00006000038341e0.

---request begin--- GET / HTTP/1.1 Host: 192.168.10.65:8000 User-Agent: Wget/1.21.3 Accept: / Accept-Encoding: identity Connection: Keep-Alive

---request end--- HTTP request sent, awaiting response... ---response begin--- HTTP/1.0 200 OK Server: SimpleHTTP/0.6 Python/2.7.9 Date: Fri, 16 Sep 2022 18:56:08 GMT Content-type: text/html Content-Length: 102 Last-Modified: Fri, 16 Sep 2022 18:55:12 GMT

---response end--- 200 OK Registered socket 3 for persistent reuse. Length: 102 [text/html] Saving to: ‘index.html’

index.html 100%[========================================================================================================================================================================>] 102 --.-KB/s in 0s

2022-09-16 11:56:09 (48.6 MB/s) - ‘index.html’ saved [102/102]

5. Start the codespace
6. Start the extension: `sudo gh net start`
7. From within the codespace run: `wget -d http://192.168.10.65:8000`

DEBUG output created by Wget 1.21 on linux-gnu.

Reading HSTS entries from /home/vscode/.wget-hsts URI encoding = ‘UTF-8’ Converted file name 'index.html' (UTF-8) -> 'index.html' (UTF-8) --2022-09-16 19:02:58-- https://192.168.10.65:8000/ Certificates loaded: 129 Connecting to 192.168.10.65:8000... ^C

8. Try running ping: `ping 192.168.10.65`:

PING 192.168.10.65 (192.168.10.65): 56 data bytes ^C--- 192.168.10.65 ping statistics --- 57 packets transmitted, 0 packets received, 100% packet loss

9. Try traceroute: `traceroute 192.186.10.65`

traceroute to 192.186.10.65 (192.186.10.65), 30 hops max, 60 byte packets 1 2 3 ... 28 29 30



I'm on a Mac M1 Max and need TCP.
> * Are you on mac, windows or linux machine?
> * What transport protocols do you need? (e.g. `TCP`/`UDP`/`SCTP` etc)

**Edit**: Additionally I tried all the ip address that came back with `ifconfig | grep "inet "` just to be sure one of the other was working
legomushroom commented 1 year ago

@KyleJamesWalker thanks for trying it out! Sorry I was not clear enough - you want to find out the IP address of the interface that will forward to the default gateway(aka default interface IP), not the default gateway IP itself. I usually do this:

ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}' 

Given you've tried all of them, I assume you've also tried the correct one already. As I said this is not yet "officially" supported yet, so it might not work in all cases. I'm looking into adding such support at the moment.

If you need TCP only, you can go a bit simpler route to unblock yourself meanwhile. The approach based on using reverse port forwarding capabilities of the SSH.

  1. Set the 127.0.0.1 local record in the /etc/hosts file on your local machine.
  2. Use GH CLI to SSH into a Codespace and reverse port-forward:
gh codespace ssh -- -R 3000:local:3000

Of course you can use the host.gh-net.internal or similar instead of the local above.

KyleJamesWalker commented 1 year ago

Fantastic this worked perfectly for me! Thank you so much!!!!!