Skip to content

Commit

Permalink
fix: make ping work without tailscale DNS (#22)
Browse files Browse the repository at this point in the history
* fix: prevent php warning when loading status info

* fix: make ping work without tailscale DNS
  • Loading branch information
dkaser authored Nov 5, 2024
1 parent 0fc3e51 commit 0128f9f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function getPeerStatus(): array
$peer = new PeerStatus();

$peer->Name = trim($status->DNSName, ".");
$peer->IP = implode("<br />", $status->TailscaleIPs);
$peer->IP = $status->TailscaleIPs;

$peer->LoginName = $this->status->User->{$status->UserID}->LoginName;
$peer->SharedUser = isset($status->ShareeNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
class PeerStatus
{
public string $Name = "";
public string $IP = "";
public string $LoginName = "";
public bool $SharedUser = false;

/** @var string[] */
public array $IP = array();

public string $Address = "";

public bool $Online = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct()

$dynamix = parse_ini_file('/boot/config/plugins/dynamix/dynamix.cfg', true) ?: array();

$locale = $_SESSION['locale'] ?? ($login_locale ?? $dynamix['display']['locale']);
$locale = $_SESSION['locale'] ?? ($login_locale ?? ($dynamix['display']['locale'] ?? "none"));
$tailscale_locale = (array) json_decode(file_get_contents("/usr/local/emhttp/plugins/tailscale/locales/en_US.json") ?: "{}", true);

if (file_exists("/usr/local/emhttp/plugins/tailscale/locales/{$locale}.json")) {
Expand Down
25 changes: 16 additions & 9 deletions src/usr/local/emhttp/plugins/tailscale/include/data/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
$tailscaleConfig = $tailscaleConfig ?? new Config();
$tr = $tr ?? new Translator();

if ( ! $tailscaleConfig->Enable) {
echo("{}");
return;
}

switch ($_POST['action']) {
case 'get':
if ( ! $tailscaleConfig->Enable) {
echo("{}");
return;
}

$tailscaleInfo = $tailscaleInfo ?? new Info($tr);
$rows = "";

Expand All @@ -26,11 +26,12 @@
$txBytes = $peer->Traffic ? $peer->TxBytes : "";
$rxBytes = $peer->Traffic ? $peer->RxBytes : "";
$pingHost = ($peer->SharedUser || $peer->Active || ! $peer->Online) ? "" : "<input type='button' class='ping' value='Ping' onclick='pingHost(\"{$peer->Name}\")'>";
$ips = implode("<br />", $peer->IP);

$rows .= <<<EOT
<tr>
<td>{$user}</td>
<td>{$peer->IP}</td>
<td>{$ips}</td>
<td>{$peer->LoginName}</td>
<td>{$online}</td>
<td>{$exitNode}</td>
Expand Down Expand Up @@ -70,10 +71,16 @@
echo json_encode($rtn);
break;
case 'ping':
$pingHost = escapeshellarg($_POST['host']);
$tailscaleInfo = $tailscaleInfo ?? new Info($tr);
$out = "Could not find host.";

$out = Utils::run_command("tailscale ping --c 3 {$pingHost}");
echo implode("<br>", $out);
foreach ($tailscaleInfo->getPeerStatus() as $peer) {
if ($peer->Name == $_POST['host']) {
$out = implode("<br>", Utils::run_command("tailscale ping --c 3 {$peer->IP[0]}"));
break;
}
}

echo $out;
break;
}

0 comments on commit 0128f9f

Please sign in to comment.