Skip to content

Commit

Permalink
feat: improve handling of Tailscale login
Browse files Browse the repository at this point in the history
  • Loading branch information
dkaser committed Aug 1, 2024
1 parent 62bf40d commit 5fa62e6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

// Wait for two minutes, then check to see if nginx is listening on the Unraid address
// If it isn't, this probably means that nginx didn't reload properly and needs to be forced.
if (isset($tailscale_ipv4)) {
// Make certain that the WebGUI is listening on the Tailscale interface
if ($tailscale_config["INCLUDE_INTERFACE"] == 1) {
$ident_config = parse_ini_file("/boot/config/ident.cfg");

$connection = @fsockopen($tailscale_ipv4, $ident_config['PORT']);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
<?php
if ($tailscale_config["INCLUDE_INTERFACE"] == 1) {
logmsg("Restarting Unraid services");
exec($restart_command);

logmsg("Restarting Unraid services");
exec($restart_command);

// Wait for two minutes, then check to see if nginx is listening on the Unraid address
// If it isn't, this probably means that nginx didn't reload properly and needs to be forced.
if (isset($tailscale_ipv4)) {
sleep(12);

$ident_config = parse_ini_file("/boot/config/ident.cfg");

$connection = @fsockopen($tailscale_ipv4, $ident_config['PORT']);

if (is_resource($connection)) {
logmsg("WebGUI listening on {$tailscale_ipv4}:{$ident_config['PORT']}");
} else {
logmsg("WebGUI not listening on {$tailscale_ipv4}:{$ident_config['PORT']}, terminating and restarting");
exec("/etc/init.d/rc.nginx term");
sleep(5);
exec("/etc/init.d/rc.nginx start");
}
// Wait to allow services to restart before continuing
sleep(15);
}
40 changes: 22 additions & 18 deletions src/usr/local/emhttp/plugins/tailscale/tailscale-watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,51 @@
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
require_once "{$docroot}/plugins/tailscale/include/common.php";

$timer = 15;
$saved_addresses = array();
$timer = 15;
$need_ip = true;

logmsg("Starting tailscale-watcher");

while (true) {
sleep($timer);

$interfaces = net_get_interfaces();
$new_addresses = array();
unset($tailscale_ipv4);

$interfaces = net_get_interfaces();

if (isset($interfaces["tailscale1"]["unicast"])) {
foreach ($interfaces["tailscale1"]["unicast"] as $interface) {
if (isset($interface["address"])) {
$new_addresses[] = $interface["address"];
$timer = 60;
if ($interface["family"] == 2) {
$tailscale_ipv4 = $interface["address"];
$timer = 60;
}
}
}
}

if (sort($new_addresses) != $saved_addresses) {
logmsg("Interface has changed, applying configuration");
$saved_addresses = $new_addresses;
if (isset($tailscale_ipv4)) {
if ($need_ip) {
logmsg("Tailscale IP detected, applying configuration");
$need_ip = false;

foreach (glob("{$docroot}/plugins/tailscale/include/tailscale-watcher/*.php") as $file) {
try {
require $file;
} catch (Exception $e) {
logmsg("Caught exception in {$file} : " . $e->getMessage());
}
}
}

foreach (glob("{$docroot}/plugins/tailscale/include/tailscale-watcher/*.php") as $file) {
foreach (glob("{$docroot}/plugins/tailscale/include/tailscale-watcher/always/*.php") as $file) {
try {
require $file;
} catch (Exception $e) {
logmsg("Caught exception in {$file} : " . $e->getMessage());
}
}
}

foreach (glob("{$docroot}/plugins/tailscale/include/tailscale-watcher/always/*.php") as $file) {
try {
require $file;
} catch (Exception $e) {
logmsg("Caught exception in {$file} : " . $e->getMessage());
}
} else {
logmsg("Waiting for Tailscale IP");
}
}

0 comments on commit 5fa62e6

Please sign in to comment.