caddy-tailscale icon indicating copy to clipboard operation
caddy-tailscale copied to clipboard

Set Tailscale hostname through environment variable.

Open kdevan opened this issue 2 years ago • 4 comments
trafficstars

This is a very simple PR that just allows changing the Tailscale hostname through an environment variable. You may want to pass on this if there's plans to allow for multiple hosts per TS node as described here.

My use case is using this with Fly.io which requires matching up the Fly app name with the Tailscale hostname. And since I do require multiple nodes, one for each secure Fly app, I need to be able to match those with multiple Tailscale hostnames. I also would like to be able to do multiple environments like stage and production.

For the logic to get the environment variable or use a fallback, I followed the way that it's done here with TS_AUTHKEY. The fallback is the hardcoded value that's here now. The environment variable name TS_HOSTNAME matches the one that the official Tailscale docker container uses. Then I just updated README.md to reflect this change.

kdevan avatar Aug 11 '23 00:08 kdevan

rather than add more magic env variables, I'd rather actually use caddy's configuration support. You can add configuration options for transports like anything else (see for example the http transport). Additionally, caddy has support for using environment variables in your config using {$VARNAME} syntax. So you could easily imagine using environment variables to configure the tailscale hostname with a caddyfile something like:

   reverse_proxy http://myhost.tailnet.ts.net:80 {
      transport tailscale {
        hostname {$TS_HOSTNAME}
      }
    }

I thought we had an example of parsing a caddyfile in caddy-tailscale, but I guess not. There are caddy docs, as well as plenty of examples in caddy itself. If that's more than you're up for trying to do that's fine... I certainly don't mind adding support for this. It'd be good to get it in place anyway, since that would be the preferred method of configuring other parts of the module anyway.

willnorris avatar Aug 11 '23 03:08 willnorris

This is great, I would not have thought to do it this way. I'm curious to learn more Golang as well as Caddy modules, so... perfect :) I'll give this a shot. Thank you for the feedback.

kdevan avatar Aug 11 '23 09:08 kdevan

@kdevan maybe have a look at #22. There you can see the syntax to use the transport config.

ChibangLW avatar Oct 16 '23 05:10 ChibangLW

Any updates on this issue ?

kernelb00t avatar Jan 14 '24 13:01 kernelb00t