Skip to content

Commit

Permalink
Add custom control server support (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Apr 14, 2023
1 parent 781999f commit 6dd9a6b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 31 deletions.
23 changes: 16 additions & 7 deletions tailscale/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,9 @@ tags:
- tag:example
- tag:homeassistant
log_level: info
login_server: "https://controlplane.tailscale.com"
```
### Option: `tags`

This option allows you to specify specific ACL tags for this Tailscale
instance. They need to start with `tag:`.

More information: <https://tailscale.com/kb/1068/acl-tags/>

### Option: `log_level`

Optionally enable tailscaled debug messages in the add-on's log. Turn it on only
Expand All @@ -90,6 +84,20 @@ more severe level, e.g., `debug` also shows `info` messages. By default,
the `log_level` is set to `info`, which is the recommended setting unless
you are troubleshooting.

### Option: `login_server`

This option lets you specify you to specify a custom control server instead of
the default (`https://controlplane.tailscale.com`). This is useful if you
are running your own Tailscale control server, for example, a self-hosted
[Headscale] instance.

### Option: `tags`

This option allows you to specify specific ACL tags for this Tailscale
instance. They need to start with `tag:`.

More information: <https://tailscale.com/kb/1068/acl-tags/>

## Taildrop

This add-on support [Tailscale's Taildrop][taildrop] feature, which allows
Expand Down Expand Up @@ -164,6 +172,7 @@ SOFTWARE.
[discord]: https://discord.me/hassioaddons
[forum]: https://community.home-assistant.io/?u=frenck
[frenck]: https://github.com/frenck
[headscale]: https://github.com/juanfont/headscale
[issue]: https://github.com/hassio-addons/addon-tailscale/issues
[reddit]: https://reddit.com/r/homeassistant
[releases]: https://github.com/hassio-addons/addon-tailscale/releases
Expand Down
3 changes: 2 additions & 1 deletion tailscale/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ host_network: true
map:
- share:rw
schema:
tags: ["match(^tag:[a-zA-Z0-9]-?[a-zA-Z0-9]+$)?"]
log_level: list(trace|debug|info|notice|warning|error|fatal)?
login_server: url?
tags: ["match(^tag:[a-zA-Z0-9]-?[a-zA-Z0-9]+$)?"]
50 changes: 27 additions & 23 deletions tailscale/rootfs/etc/s6-overlay/s6-rc.d/post-tailscaled/run
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,33 @@
# Runs after the machine has been logged in into the Tailscale network
# ==============================================================================
declare -a addresses=()
declare -a options
declare -a routes=()
declare ipinfo
declare login_server
declare tags

function appendarray() {
local -n array=${1}
readarray -t -O "${#array[@]}" array
}

# Default options
options+=(--accept-routes)
options+=(--advertise-exit-node)
options+=(--hostname "$(bashio::info.hostname)")

# Get configured control server
if bashio::config.has_value "login_server";
then
tags=$(bashio::config "login_server")
options+=(--login_server="${login_server}")
fi

# Get configured tags
tags=$(bashio::config "tags//[] | join(\",\")" "")
options+=(--advertise-tags="${tags}")

# Find interfaces and matching addresses from which we can extract routes to be advertised
for interface in $(bashio::network.interfaces); do
appendarray addresses < <(bashio::network.ipv4_address "${interface}")
Expand Down Expand Up @@ -41,34 +59,20 @@ for address in "${addresses[@]}"; do
routes+=("$(bashio::jq "${ipinfo}" '.NETWORK + "/" + .PREFIX')")
fi
done

# Remove duplicate entries
readarray -t routes < <(printf "%s\n" "${routes[@]}" | sort -u)

# Get configured tags
tags=$(bashio::config "tags//[] | join(\",\")" "")

# Wait for socket to be available
while ! bashio::fs.socket_exists "/var/run/tailscale/tailscaled.sock";
do
sleep 1
done
IFS=","
options+=(--advertise-routes="${routes[*]}")
unset IFS

# Wait for the network to be available and logged in
while true;
do
if /opt/tailscale status --json --peers=false --self=false \
while ! bashio::fs.socket_exists "/var/run/tailscale/tailscaled.sock" || \
! /opt/tailscale status --json --peers=false --self=false \
| jq --exit-status '.BackendState == "Running" or .BackendState == "NeedsLogin"' > /dev/null;
then
IFS=","
/opt/tailscale up \
--hostname "$(bashio::info.hostname)" \
--advertise-exit-node \
--accept-routes \
--advertise-routes="${routes[*]}" \
--advertise-tags="${tags}"

bashio::exit.ok
fi
# Well... wait a bit more
do
sleep 2
done

/opt/tailscale up "${options[@]}"
6 changes: 6 additions & 0 deletions tailscale/translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ configuration:
description: >-
Controls the level of log details the add-on provides.
This only applies to the add-on itself, not Tailscale.
login_server:
name: Login server
description: >-
This option allows you to specify a custom control server for this
Tailscale instance, for example, a self-host Headscale instance.
By default, it uses the control server provided by Tailscale.
tags:
name: Tags
description: >-
Expand Down

2 comments on commit 6dd9a6b

@travisboss
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still incorrect it is login-server not login_server, getting error in 0.11.1 flag provided but not defined: -login_server

@lmagyar
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #184, though it is unreleased. (I'm just another user.)

Please sign in to comment.