Skip to content

Commit 6dc5e7b

Browse files
authored
feat: watch for serve config that conflicts with WebGUI (#37)
1 parent 105381b commit 6dc5e7b

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

src/usr/local/emhttp/plugins/tailscale/include/Tailscale/LocalAPI.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ public function getTkaStatus(): \stdClass
6060
return (object) json_decode($this->tailscaleLocalAPI('v0/tka/status'));
6161
}
6262

63+
public function getServeConfig(): \stdClass
64+
{
65+
return (object) json_decode($this->tailscaleLocalAPI('v0/serve-config'));
66+
}
67+
68+
public function resetServeConfig(): void
69+
{
70+
$this->tailscaleLocalAPI("v0/serve-config", APIMethods::POST, new \stdClass());
71+
}
72+
6373
public function postLoginInteractive(): void
6474
{
6575
$this->tailscaleLocalAPI('v0/login-interactive', APIMethods::POST);

src/usr/local/emhttp/plugins/tailscale/include/Tailscale/System.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ public static function checkWebgui(Config $config, string $tailscale_ipv4): void
5454
$connection = @fsockopen($tailscale_ipv4, $ident_config['PORT']);
5555

5656
if (is_resource($connection)) {
57-
if ((intval(date("i")) % 10 == 0)) {
58-
Utils::logmsg("WebGUI listening on {$tailscale_ipv4}:{$ident_config['PORT']}");
59-
}
57+
Utils::logmsg("WebGUI listening on {$tailscale_ipv4}:{$ident_config['PORT']}", false, true);
6058
} else {
6159
Utils::logmsg("WebGUI not listening on {$tailscale_ipv4}:{$ident_config['PORT']}, terminating and restarting");
6260
Utils::run_command("/etc/rc.d/rc.nginx term");
@@ -66,6 +64,39 @@ public static function checkWebgui(Config $config, string $tailscale_ipv4): void
6664
}
6765
}
6866

67+
public static function checkServeConfig(): void
68+
{
69+
$ident_config = parse_ini_file("/boot/config/ident.cfg") ?: array();
70+
71+
$httpPort = isset($ident_config['PORT']) && is_scalar($ident_config['PORT'])
72+
? intval($ident_config['PORT']) : 80;
73+
$httpsPort = -1;
74+
75+
if (($ident_config['USE_SSL'] ?? "no") != "no") {
76+
$httpsPort = isset($ident_config['PORTSSL']) && is_scalar($ident_config['PORTSSL'])
77+
? intval($ident_config['PORTSSL']) : 443;
78+
}
79+
80+
$localAPI = new LocalAPI();
81+
$serveConfig = $localAPI->getServeConfig();
82+
83+
$tcpConfig = $serveConfig->TCP ?? array();
84+
85+
foreach ($tcpConfig as $key => $val) {
86+
$configPort = intval($key);
87+
88+
if ($configPort == $httpPort || $configPort == $httpsPort) {
89+
Utils::logmsg("Serve TCP Port {$configPort} conflicts with WebGUI, removing");
90+
$localAPI->resetServeConfig();
91+
Utils::run_command(self::RESTART_COMMAND);
92+
93+
return;
94+
} else {
95+
Utils::logmsg("Checked for WebGUI conflict with serve TCP Port {$configPort}", false, true);
96+
}
97+
}
98+
}
99+
69100
public static function restartSystemServices(Config $config): void
70101
{
71102
if ($config->IncludeInterface) {

src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Utils.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,13 @@ public static function run_command(string $command, bool $alwaysShow = false, bo
194194
return $output;
195195
}
196196

197-
public static function logmsg(string $message, bool $debug = false): void
197+
public static function logmsg(string $message, bool $debug = false, bool $rateLimit = false): void
198198
{
199+
if ($rateLimit && (intval(date("i")) % 10 != 0)) {
200+
// Only log rate limited messages every 10 minutes
201+
return;
202+
}
203+
199204
if ($debug) {
200205
if (defined("TAILSCALE_TRUNK")) {
201206
$message = "DEBUG: " . $message;

src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Watcher.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function run(): void
5252
}
5353

5454
Utils::run_task('Tailscale\System::checkWebgui', array($this->config, $tailscale_ipv4));
55+
Utils::run_task('Tailscale\System::checkServeConfig');
5556
Utils::run_task('Tailscale\System::fixLocalSubnetRoutes');
5657

5758
// Watch for changes to the DNS name (e.g., if someone changes the tailnet name or the Tailscale name of the server via the admin console)

0 commit comments

Comments
 (0)