Skip to content
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

Automatically Start/Stop CF Tunnels & Automatically Use them Based On DNS Entries #7

Merged
merged 1 commit into from
Nov 24, 2023

Conversation

varun7654
Copy link
Contributor

@varun7654 varun7654 commented Nov 24, 2023

With this commit tunnels are now automatically started/stopped when needed.

Instead of having to connect to a localhost address this mod will now create the tunnel with the hostname we're connecting to and automatically route the connection though the tunnel it just created it.

It can also automatically start using a tunnel based on DNS entries, or (as a fallback) the user can define hostnames in the forced_tunnels.json file. For this the user also no longer needs to define anything other than the hostname, as the mod will automatically do everything else.

DNS Spec:
We look for a TXT record on the same subdomain as the server we are connecting to.
If there is a TXT record with the value cloudflared-use-tunnel or cloudflared-route=<route> we know that we should use a CF tunnel to connect to the server.
If the server has the TXT record cloudflared-route=<route>, it will use the route as the route for the tunnel.
If the server has the TXT record cloudflared-use-tunnel, it will use the server address as the route for the tunnel.
If the server has neither of the TXT records, it will not use a tunnel. (unless it is in the forced tunnels list)

Note: Only one of the above records are needed.


To do this quite a few internal changes were made.

We're now mixing into the Connection & Disconnection methods to start/stop the CF Tunnels.

The method that downloads cloudflare now returns a future that holds the CloudflareProgram If we want to start/stop a tunnel before we've finished downloading cloudflared we wait for the future to become ready.

We're no longer loading a list of Accesses from access.json. Instead we're loading a list of hostnames from forced_tunnels.json which looks like:

["example.domain.net", "example2.domain.net"]

Access no longer holds a reference to CloudflaredProgram, so the command method now takes a reference to CloudflaredProgram.

The startAccess method now creates takes in a Access object which defines the tunnel it should create. We no longer have a list of accesses (since there is no reason to have multiple tunnels running at the same time)

Known Issues:

  • In some cases, if the client fails to connect to the server the CF Tunnel won't be shut down. The client appears to call the disconnect code whenever we connect to the server so the tunnel will be closed when the user next tries to join a server of closes their client.

With this commit tunnels are now automatically started/stopped when needed.

Instead of having to connect to a localhost address this mod will now create the tunnel with the hostname we're connecting to and automatically route the connection though the tunnel it just created it.

It can also automatically start using a tunnel based on DNS entries, or (as a fallback) the user can define hostnames in the `forced_tunnels.json` file. For this the user also no longer needs to define anything other than the hostname, as the mod will automatically do everything else.

DNS Spec:
We look for a TXT record on the same subdomain as the server we are connecting to.
If there is a TXT record with the value "cloudflared-use-tunnel" or "cloudflared-route=<route>" we know that we should use a CF tunnel to connect to the server.
If the server has the TXT record "cloudflared-route=<route>", it will use the route as the route for the tunnel.
If the server has the TXT record "cloudflared-use-tunnel", it will use the server address as the route for the tunnel.
If the server has neither of the TXT records, it will not use a tunnel. (unless it is in the forced tunnels list)

Note: Only one of the above records are needed.
@HttpRafa HttpRafa merged commit 19bb788 into HttpRafa:fabric/1.20.x Nov 24, 2023
@HttpRafa
Copy link
Owner

HttpRafa commented Nov 27, 2023

@varun7654 Would it be possible for you to update the README file so that users who find this mod via Modrinth also know what they need to set or what features you have added.

@varun7654
Copy link
Contributor Author

Yeah I can do that, but give me a few days since I'll be traveling

@HttpRafa
Copy link
Owner

No problem

HttpRafa added a commit that referenced this pull request Nov 28, 2023
- Automatically Start/Stop CF Tunnels & Automatically Use them Based On DNS Entries by varun7654 (#7)
- Update README & Don't wait for cloudflared to finish downloading to
  disconnect from a server by varun7654 (#8)
- MacOS Support by kanya-approve (#5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants