From 7fb7b596c1355d1afbdc868a9233594f7be98662 Mon Sep 17 00:00:00 2001
From: Giovanni d'Amelio <gio@damelio.net>
Date: Tue, 25 Apr 2023 12:36:57 -0700
Subject: [PATCH] Allow configuring the tsnet state directory. Automatically
 configure it to be /data in the Dockerfile

---
 Dockerfile                                | 6 ++++++
 examples/tailscale-custom-domain-dns.toml | 5 +++++
 server/server.go                          | 3 +++
 3 files changed, 14 insertions(+)

diff --git a/Dockerfile b/Dockerfile
index e138c62..dc142d1 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -9,4 +9,10 @@ ADD https://github.com/giodamelio/tailscale-custom-domain-dns/releases/download/
 
 RUN tar xzvf ${RELEASE_FILE} && rm ${RELEASE_FILE}
 
+# Create empty config file so the cli doesn't complain
+RUN mkdir /root/.config/ && touch /root/.config/tailscale-custom-domain-dns.toml
+
+# Save the state directory in /data so it can be added to a volume easily
+ENV TSDNS_TAILSCALE_STATEDIRECTORY=/data
+
 ENTRYPOINT [ "/srv/tailscale-custom-domain-dns" ]
diff --git a/examples/tailscale-custom-domain-dns.toml b/examples/tailscale-custom-domain-dns.toml
index 8064f30..3a3e377 100644
--- a/examples/tailscale-custom-domain-dns.toml
+++ b/examples/tailscale-custom-domain-dns.toml
@@ -38,6 +38,11 @@ oauth-client-secret = ""
 # The hostname that the server will connect to your tailnet with.
 hostname = "tailscale-custom-domain-dns"
 
+# The directory that the Tailscale state is stored in
+# If it is not set, tsnet will choose a path in the users config directory
+# Example: state-dir = "/data"
+state-dir = ""
+
 [dns-server]
 # The port that the DNS server will run on.
 # The port is opened directly on your tailnet using the tsnet library, so you 
diff --git a/server/server.go b/server/server.go
index ea2c1bd..18a8b60 100644
--- a/server/server.go
+++ b/server/server.go
@@ -26,6 +26,9 @@ func Start() {
 	tsServer := new(tsnet.Server)
 	tsServer.Hostname = viper.GetString("tailscale.hostname")
 	tsServer.AuthKey = viper.GetString("tailscale.auth-key")
+	if viper.IsSet("tailscale.state-directory") {
+		tsServer.Dir = viper.GetString("tailscale.state-directory")
+	}
 	tsServer.Logf = func(format string, args ...any) {
 		log.
 			Trace().