Compare commits
No commits in common. "bbce7f010091d5c789ed85e64ae41678d353ecf1" and "00a40a9aa29f3034114dea0957cb8d22e7bc9264" have entirely different histories.
bbce7f0100
...
00a40a9aa2
7 changed files with 290 additions and 512 deletions
24
README.md
24
README.md
|
|
@ -2,21 +2,19 @@
|
||||||
|
|
||||||
A lightweight widget plugin that shows Tailscale connectivity status on the Dank Bar with quick controls for toggling connection, switching exit nodes, and copying peer addresses.
|
A lightweight widget plugin that shows Tailscale connectivity status on the Dank Bar with quick controls for toggling connection, switching exit nodes, and copying peer addresses.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- **Status icon** in the bar — `vpn_key` when connected, `vpn_key_off` when disconnected
|
- **Status icon** in the bar — `vpn_key` when connected, `vpn_key_off` when disconnected
|
||||||
- **Right-click** to toggle Tailscale on/off
|
- **Right-click** to toggle Tailscale on/off
|
||||||
- **Left-click** to open a popout showing:
|
- **Left-click** to open a popout showing:
|
||||||
- Your current Tailscale IP (when connected)
|
- Your current Tailscale IP
|
||||||
- Active exit node (with clear button)
|
- Active exit node (with clear button)
|
||||||
- Peer list with hostnames and IPs (when connected)
|
- Peer list with hostnames and IPs
|
||||||
- A clear "Not connected" empty state when disconnected (no stale peer list)
|
|
||||||
- **Click-to-copy** any hostname or IP to clipboard
|
- **Click-to-copy** any hostname or IP to clipboard
|
||||||
- **Exit node selection** — click `↗` on any exit-node-capable peer to route through it
|
- **Exit node selection** — click `↗` on any exit-node-capable peer to route through it
|
||||||
- **On-demand status** — polls Tailscale for ground truth on load, explicit actions, and post-mutation verification (defensive poll-act-poll for toggles; no always-on timer)
|
- **On-demand status** — polls Tailscale for ground truth on load, explicit actions, and post-mutation verification (defensive poll-act-poll for toggles; no always-on timer)
|
||||||
- **Single-flight actions** — concurrent status/toggle/exit/copy chains are rejected while an operation is in flight
|
|
||||||
- **Toast notifications** for all errors
|
- **Toast notifications** for all errors
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
@ -44,8 +42,7 @@ A lightweight widget plugin that shows Tailscale connectivity status on the Dank
|
||||||
tailscalectl/
|
tailscalectl/
|
||||||
├── plugin.json
|
├── plugin.json
|
||||||
├── TailscaleWidget.qml
|
├── TailscaleWidget.qml
|
||||||
├── lib.js
|
└── lib.js
|
||||||
└── i18n/
|
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Reload the plugin:
|
3. Reload the plugin:
|
||||||
|
|
@ -73,14 +70,12 @@ A lightweight widget plugin that shows Tailscale connectivity status on the Dank
|
||||||
"id": "tailscalectl",
|
"id": "tailscalectl",
|
||||||
"name": "Tailscale",
|
"name": "Tailscale",
|
||||||
"description": "Tailscale status and controls on the Dank Bar",
|
"description": "Tailscale status and controls on the Dank Bar",
|
||||||
"author": "John Morris",
|
"version": "0.2.0",
|
||||||
|
"author": "John Morris & Vybe (AI Slop... er... Coding Assistant)",
|
||||||
"icon": "vpn_key",
|
"icon": "vpn_key",
|
||||||
"type": "widget",
|
"type": "widget",
|
||||||
"capabilities": ["dankbar-widget"],
|
|
||||||
"component": "./TailscaleWidget.qml",
|
"component": "./TailscaleWidget.qml",
|
||||||
"permissions": ["process"],
|
"permissions": ["settings_read", "settings_write", "process"]
|
||||||
"requires": ["tailscale"],
|
|
||||||
"version": "0.2.1"
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -89,10 +84,7 @@ A lightweight widget plugin that shows Tailscale connectivity status on the Dank
|
||||||
- Uses `Proc` singleton (from `qs.Common`) for all external `tailscale` commands (one-shot stdout capture + auto cleanup).
|
- Uses `Proc` singleton (from `qs.Common`) for all external `tailscale` commands (one-shot stdout capture + auto cleanup).
|
||||||
- Fully I18n-ready via `I18n.tr(...)` (source keys in American English only today; see `tailscalectl/i18n/` for scaffolding).
|
- Fully I18n-ready via `I18n.tr(...)` (source keys in American English only today; see `tailscalectl/i18n/` for scaffolding).
|
||||||
- Follows current `dms-plugin-dev` + DMS 1.4 plugin best practices (capabilities, requires, no raw Process for one-shots, etc.).
|
- Follows current `dms-plugin-dev` + DMS 1.4 plugin best practices (capabilities, requires, no raw Process for one-shots, etc.).
|
||||||
- Toggle uses intentional defensive poll-act-poll; a failed status poll aborts the pending toggle (does not invent `up`/`down`).
|
- Toggle uses intentional defensive poll-act-poll (see code comments).
|
||||||
- When `BackendState` is not `Running`, peer list / exit node / self IP are cleared so the UI never shows a stale connected-looking peer list.
|
|
||||||
- Status row uses `RowLayout` with a real `Layout.fillWidth` spacer (not a no-op on plain `Row`).
|
|
||||||
- Peer `ListView` uses `Flickable.StopAtBounds` (no desktop rubber-band overshoot).
|
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,66 +16,22 @@ PluginComponent {
|
||||||
property string _copyText: ""
|
property string _copyText: ""
|
||||||
property int _copyIndex: 0
|
property int _copyIndex: 0
|
||||||
|
|
||||||
// Transient coordination for defensive poll-act-poll toggle (not long-term cache).
|
|
||||||
// Poll for ground truth → act → poll again for verification.
|
|
||||||
property string _pendingAction: ""
|
|
||||||
|
|
||||||
// Single-flight guard: prevent interleaved status/toggle/exit/copy chains (#15/#30 class).
|
|
||||||
property bool _busy: false
|
|
||||||
|
|
||||||
layerNamespacePlugin: "tailscalectl"
|
layerNamespacePlugin: "tailscalectl"
|
||||||
popoutWidth: 360
|
popoutWidth: 360
|
||||||
popoutHeight: 400
|
popoutHeight: 400
|
||||||
|
|
||||||
|
// Transient coordination for the defensive poll-act-poll toggle (exact behavior preserved).
|
||||||
|
// We poll for on-the-ground truth (so we choose the correct "up"/"down" and don't lie to the user),
|
||||||
|
// act, then poll again for verification. This is *not* long-term cached state.
|
||||||
|
// The _pendingAction is short-lived per user action only.
|
||||||
|
property string _pendingAction: ""
|
||||||
|
|
||||||
Component.onCompleted: {
|
Component.onCompleted: {
|
||||||
|
// Initial status fetch on load. Subsequent fetches are on-demand (popout open, explicit refresh, or post-action verification).
|
||||||
root._runStatusCheck();
|
root._runStatusCheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
function _runStatusCheck() {
|
function _runStatusCheck() {
|
||||||
if (root._busy && root._pendingAction === "") {
|
|
||||||
// A non-toggle status refresh while something is already in flight: skip.
|
|
||||||
// Toggle path sets _pendingAction first and is allowed to chain after actions clear busy carefully.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
root._busy = true;
|
|
||||||
Proc.runCommand("tailscale-status", TailscaleLib.getStatusCommand(), (stdout, code) => {
|
|
||||||
var statusOk = (code === 0);
|
|
||||||
if (statusOk) {
|
|
||||||
const state = TailscaleLib.parseStatusResult(stdout);
|
|
||||||
root.isConnected = state.isConnected;
|
|
||||||
root.tailscaleIP = state.tailscaleIP;
|
|
||||||
root.currentExitNode = state.currentExitNode;
|
|
||||||
root.peers = state.peers;
|
|
||||||
} else {
|
|
||||||
root.isConnected = false;
|
|
||||||
root.tailscaleIP = "";
|
|
||||||
root.currentExitNode = "";
|
|
||||||
root.peers = [];
|
|
||||||
ToastService.showError("tailscalectl", I18n.tr(TailscaleLib.formatError("status")));
|
|
||||||
}
|
|
||||||
|
|
||||||
const cmd = TailscaleLib.commandForPendingAction(root._pendingAction, root.isConnected, statusOk);
|
|
||||||
if (cmd) {
|
|
||||||
// Fresh poll succeeded; act, then verify with another status poll.
|
|
||||||
Proc.runCommand("tailscale-toggle", cmd, (out, c) => {
|
|
||||||
if (c !== 0) {
|
|
||||||
const action = root.isConnected ? "disconnect" : "connect";
|
|
||||||
ToastService.showError("tailscalectl", I18n.tr(TailscaleLib.formatError(action)));
|
|
||||||
}
|
|
||||||
root._pendingAction = "";
|
|
||||||
// Keep busy through verification poll: call internal runner that assumes we own the lock.
|
|
||||||
root._runStatusCheckUnlocked();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// Includes failed status while a toggle was pending: abort rather than invent up/down.
|
|
||||||
root._pendingAction = "";
|
|
||||||
root._busy = false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Used only as the continuation after toggle action; assumes _busy is already true.
|
|
||||||
function _runStatusCheckUnlocked() {
|
|
||||||
Proc.runCommand("tailscale-status", TailscaleLib.getStatusCommand(), (stdout, code) => {
|
Proc.runCommand("tailscale-status", TailscaleLib.getStatusCommand(), (stdout, code) => {
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
const state = TailscaleLib.parseStatusResult(stdout);
|
const state = TailscaleLib.parseStatusResult(stdout);
|
||||||
|
|
@ -90,51 +46,51 @@ PluginComponent {
|
||||||
root.peers = [];
|
root.peers = [];
|
||||||
ToastService.showError("tailscalectl", I18n.tr(TailscaleLib.formatError("status")));
|
ToastService.showError("tailscalectl", I18n.tr(TailscaleLib.formatError("status")));
|
||||||
}
|
}
|
||||||
root._busy = false;
|
|
||||||
|
const cmd = TailscaleLib.commandForPendingAction(root._pendingAction, root.isConnected);
|
||||||
|
if (cmd) {
|
||||||
|
// If toggle action on deck, then we just retrieved on-the-ground truth and can now act to toggle.
|
||||||
|
Proc.runCommand("tailscale-toggle", cmd, (out, c) => {
|
||||||
|
if (c !== 0) {
|
||||||
|
const action = root.isConnected ? "disconnect" : "connect";
|
||||||
|
ToastService.showError("tailscalectl", I18n.tr(TailscaleLib.formatError(action)));
|
||||||
|
}
|
||||||
|
root._pendingAction = "";
|
||||||
|
root._runStatusCheck(); // post-action verification poll (exact behavior)
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
root._pendingAction = "";
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleTailscale() {
|
function toggleTailscale() {
|
||||||
if (root._busy) {
|
root._pendingAction = "toggle";
|
||||||
return;
|
|
||||||
}
|
|
||||||
root._pendingAction = TailscaleLib.PendingAction.TOGGLE;
|
|
||||||
root._runStatusCheck();
|
root._runStatusCheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
function refreshStatus() {
|
function refreshStatus() {
|
||||||
if (root._busy) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
root._runStatusCheck();
|
root._runStatusCheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
function setExitNode(hostname) {
|
function setExitNode(hostname) {
|
||||||
if (root._busy) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const cmd = TailscaleLib.makeExitNodeCommand(hostname);
|
const cmd = TailscaleLib.makeExitNodeCommand(hostname);
|
||||||
if (!cmd) {
|
if (!cmd) {
|
||||||
ToastService.showError("tailscalectl", I18n.tr(TailscaleLib.getStrings().invalidExitNodeHostname));
|
ToastService.showError("tailscalectl", I18n.tr(TailscaleLib.getStrings().invalidExitNodeHostname));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
root._busy = true;
|
|
||||||
Proc.runCommand("tailscale-exit", cmd, (stdout, code) => {
|
Proc.runCommand("tailscale-exit", cmd, (stdout, code) => {
|
||||||
if (code !== 0) {
|
if (code !== 0) {
|
||||||
|
// Note: no stderr detail available from Proc (accepted per plan).
|
||||||
ToastService.showError("tailscalectl", I18n.tr(TailscaleLib.formatError("set")));
|
ToastService.showError("tailscalectl", I18n.tr(TailscaleLib.formatError("set")));
|
||||||
}
|
}
|
||||||
// Verify via unlocked status continuation (busy already held).
|
root._runStatusCheck();
|
||||||
root._runStatusCheckUnlocked();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function copyToClipboard(text) {
|
function copyToClipboard(text) {
|
||||||
if (root._busy) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
root._copyText = text;
|
root._copyText = text;
|
||||||
root._copyIndex = 0;
|
root._copyIndex = 0;
|
||||||
root._busy = true;
|
|
||||||
root._runNextCopy();
|
root._runNextCopy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -142,13 +98,11 @@ PluginComponent {
|
||||||
const cmds = TailscaleLib.getClipboardCommands(root._copyText);
|
const cmds = TailscaleLib.getClipboardCommands(root._copyText);
|
||||||
if (root._copyIndex >= cmds.length) {
|
if (root._copyIndex >= cmds.length) {
|
||||||
ToastService.showError("tailscalectl", I18n.tr(TailscaleLib.formatError("clipboard")));
|
ToastService.showError("tailscalectl", I18n.tr(TailscaleLib.formatError("clipboard")));
|
||||||
root._busy = false;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Proc.runCommand("tailscale-copy-" + root._copyIndex, cmds[root._copyIndex], (stdout, code) => {
|
Proc.runCommand("tailscale-copy-" + root._copyIndex, cmds[root._copyIndex], (stdout, code) => {
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
ToastService.showInfo(I18n.tr(TailscaleLib.getStrings().copied).arg(root._copyText));
|
ToastService.showInfo(I18n.tr(TailscaleLib.getStrings().copied).arg(root._copyText));
|
||||||
root._busy = false;
|
|
||||||
} else {
|
} else {
|
||||||
root._copyIndex += 1;
|
root._copyIndex += 1;
|
||||||
root._runNextCopy();
|
root._runNextCopy();
|
||||||
|
|
@ -165,9 +119,9 @@ PluginComponent {
|
||||||
Item {
|
Item {
|
||||||
id: contentItem
|
id: contentItem
|
||||||
width: parent.width
|
width: parent.width
|
||||||
height: Theme.spacingM + statusRow.implicitHeight + Theme.spacingM + peerArea.height + Theme.spacingM
|
height: Theme.spacingM + statusRow.implicitHeight + Theme.spacingM + peerList.height + Theme.spacingM
|
||||||
|
|
||||||
RowLayout {
|
Row {
|
||||||
id: statusRow
|
id: statusRow
|
||||||
y: Theme.spacingM
|
y: Theme.spacingM
|
||||||
width: parent.width
|
width: parent.width
|
||||||
|
|
@ -180,11 +134,11 @@ PluginComponent {
|
||||||
MouseArea {
|
MouseArea {
|
||||||
cursorShape: Qt.PointingHandCursor
|
cursorShape: Qt.PointingHandCursor
|
||||||
hoverEnabled: true
|
hoverEnabled: true
|
||||||
Layout.alignment: Qt.AlignVCenter
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
width: toggleIcon.implicitWidth
|
width: toggleIcon.implicitWidth
|
||||||
height: toggleIcon.implicitHeight
|
height: toggleIcon.implicitHeight
|
||||||
onClicked: {
|
onClicked: {
|
||||||
root.toggleTailscale();
|
root.toggleTailscale()
|
||||||
}
|
}
|
||||||
|
|
||||||
DankIcon {
|
DankIcon {
|
||||||
|
|
@ -200,30 +154,27 @@ PluginComponent {
|
||||||
text: root.tailscaleIP || "—"
|
text: root.tailscaleIP || "—"
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
color: Theme.primary
|
color: Theme.primary
|
||||||
Layout.alignment: Qt.AlignVCenter
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
}
|
}
|
||||||
|
|
||||||
Item {
|
Item { width: 1; height: 1; Layout.fillWidth: true }
|
||||||
Layout.fillWidth: true
|
|
||||||
height: 1
|
|
||||||
}
|
|
||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
text: I18n.tr(TailscaleLib.getStrings().exitNodePrefix) + (root.currentExitNode || I18n.tr(TailscaleLib.getStrings().none))
|
text: I18n.tr(TailscaleLib.getStrings().exitNodePrefix) + (root.currentExitNode || I18n.tr(TailscaleLib.getStrings().none))
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
color: Theme.surfaceVariantText
|
color: Theme.surfaceVariantText
|
||||||
Layout.alignment: Qt.AlignVCenter
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
}
|
}
|
||||||
|
|
||||||
MouseArea {
|
MouseArea {
|
||||||
visible: root.currentExitNode !== ""
|
visible: TailscaleLib.shouldShowClearExitNode(root.currentExitNode)
|
||||||
cursorShape: Qt.PointingHandCursor
|
cursorShape: Qt.PointingHandCursor
|
||||||
hoverEnabled: true
|
hoverEnabled: true
|
||||||
Layout.alignment: Qt.AlignVCenter
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
width: clearExitNodeText.implicitWidth
|
width: clearExitNodeText.implicitWidth
|
||||||
height: clearExitNodeText.implicitHeight
|
height: clearExitNodeText.implicitHeight
|
||||||
onClicked: {
|
onClicked: {
|
||||||
root.setExitNode("");
|
root.setExitNode("")
|
||||||
}
|
}
|
||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
|
|
@ -235,46 +186,25 @@ PluginComponent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Peer list when connected; empty-state hint when not (#55).
|
|
||||||
Item {
|
|
||||||
id: peerArea
|
|
||||||
y: Theme.spacingM + statusRow.implicitHeight + Theme.spacingM
|
|
||||||
width: parent.width - Theme.spacingM * 2
|
|
||||||
height: root.isConnected
|
|
||||||
? Math.min(Math.max(root.peers.length, 1) * (Theme.fontSizeSmall + Theme.spacingXS), 200)
|
|
||||||
: (Theme.fontSizeSmall + Theme.spacingXS)
|
|
||||||
anchors.left: parent.left
|
|
||||||
anchors.leftMargin: Theme.spacingM
|
|
||||||
|
|
||||||
StyledText {
|
|
||||||
visible: !root.isConnected
|
|
||||||
anchors.fill: parent
|
|
||||||
text: I18n.tr(TailscaleLib.getStrings().notConnectedHint)
|
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
|
||||||
color: Theme.surfaceVariantText
|
|
||||||
horizontalAlignment: Text.AlignHCenter
|
|
||||||
verticalAlignment: Text.AlignVCenter
|
|
||||||
}
|
|
||||||
|
|
||||||
ListView {
|
ListView {
|
||||||
id: peerList
|
id: peerList
|
||||||
visible: root.isConnected
|
y: Theme.spacingM + statusRow.implicitHeight + Theme.spacingM
|
||||||
anchors.fill: parent
|
width: parent.width - Theme.spacingM * 2
|
||||||
|
height: Math.min(root.peers.length * (Theme.fontSizeSmall + Theme.spacingXS), 200)
|
||||||
|
anchors.left: parent.left
|
||||||
|
anchors.leftMargin: Theme.spacingM
|
||||||
model: root.peers
|
model: root.peers
|
||||||
interactive: true
|
interactive: true
|
||||||
// Desktop popout: no rubber-band overshoot (#27).
|
boundsBehavior: Flickable.DragAndOvershootBounds
|
||||||
boundsBehavior: Flickable.StopAtBounds
|
|
||||||
clip: true
|
|
||||||
|
|
||||||
delegate: Item {
|
delegate: Item {
|
||||||
width: peerList.width
|
width: peerList.width
|
||||||
height: Theme.fontSizeSmall + Theme.spacingXS
|
height: Theme.fontSizeSmall + Theme.spacingXS
|
||||||
|
|
||||||
Row {
|
Row {
|
||||||
anchors.left: parent.left
|
anchors.fill: parent
|
||||||
anchors.right: parent.right
|
|
||||||
anchors.verticalCenter: parent.verticalCenter
|
|
||||||
spacing: Theme.spacingS
|
spacing: Theme.spacingS
|
||||||
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
|
|
||||||
MouseArea {
|
MouseArea {
|
||||||
cursorShape: Qt.PointingHandCursor
|
cursorShape: Qt.PointingHandCursor
|
||||||
|
|
@ -283,7 +213,7 @@ PluginComponent {
|
||||||
width: peerHostnameText.implicitWidth
|
width: peerHostnameText.implicitWidth
|
||||||
height: peerHostnameText.implicitHeight
|
height: peerHostnameText.implicitHeight
|
||||||
onClicked: {
|
onClicked: {
|
||||||
root.copyToClipboard(modelData.hostname);
|
root.copyToClipboard(modelData.hostname)
|
||||||
}
|
}
|
||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
|
|
@ -301,7 +231,7 @@ PluginComponent {
|
||||||
width: peerIpText.implicitWidth
|
width: peerIpText.implicitWidth
|
||||||
height: peerIpText.implicitHeight
|
height: peerIpText.implicitHeight
|
||||||
onClicked: {
|
onClicked: {
|
||||||
root.copyToClipboard(modelData.ip);
|
root.copyToClipboard(modelData.ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
|
|
@ -320,15 +250,14 @@ PluginComponent {
|
||||||
width: exitNodeButton.implicitWidth
|
width: exitNodeButton.implicitWidth
|
||||||
height: exitNodeButton.implicitHeight
|
height: exitNodeButton.implicitHeight
|
||||||
onClicked: {
|
onClicked: {
|
||||||
root.setExitNode(modelData.hostname);
|
root.setExitNode(modelData.hostname)
|
||||||
}
|
}
|
||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
id: exitNodeButton
|
id: exitNodeButton
|
||||||
text: "↗"
|
text: "↗"
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
color: (root.currentExitNode === modelData.hostname) ? Theme.primary : Theme.surfaceVariantText
|
color: TailscaleLib.isActiveExitNode(root.currentExitNode, modelData.hostname) ? Theme.primary : Theme.surfaceVariantText
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -342,7 +271,7 @@ PluginComponent {
|
||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
acceptedButtons: Qt.RightButton
|
acceptedButtons: Qt.RightButton
|
||||||
onClicked: {
|
onClicked: {
|
||||||
root.toggleTailscale();
|
root.toggleTailscale()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,25 @@ This plugin is fully instrumented with `I18n.tr(...)` (from `qs.Common`) for all
|
||||||
|
|
||||||
See `getStrings()` and `errorMessage()` in `lib.js` for the canonical list.
|
See `getStrings()` and `errorMessage()` in `lib.js` for the canonical list.
|
||||||
|
|
||||||
Example `en.json` (for documentation / future tools) lives beside this README.
|
Example `en.json` (for documentation / future tools):
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"Tailscale": "Tailscale",
|
||||||
|
"Connected": "Connected",
|
||||||
|
"Disconnected": "Disconnected",
|
||||||
|
"Exit node: ": "Exit node: ",
|
||||||
|
"None": "None",
|
||||||
|
"Copied %1 to clipboard": "Copied %1 to clipboard",
|
||||||
|
"Invalid exit node hostname": "Invalid exit node hostname",
|
||||||
|
"Failed to connect to Tailscale": "Failed to connect to Tailscale",
|
||||||
|
"Failed to disconnect from Tailscale": "Failed to disconnect from Tailscale",
|
||||||
|
"Failed to set exit node": "Failed to set exit node",
|
||||||
|
"Failed to read Tailscale status": "Failed to read Tailscale status",
|
||||||
|
"Error copying to clipboard": "Error copying to clipboard",
|
||||||
|
"Tailscale command failed": "Tailscale command failed"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
|
|
@ -19,4 +37,4 @@ Example `en.json` (for documentation / future tools) lives beside this README.
|
||||||
- Plugin name/description in `plugin.json` and technical IDs ("tailscalectl") remain English.
|
- Plugin name/description in `plugin.json` and technical IDs ("tailscalectl") remain English.
|
||||||
- This follows DMS `dms-plugin-dev` best practice for future-proofing even when only en is shipped.
|
- This follows DMS `dms-plugin-dev` best practice for future-proofing even when only en is shipped.
|
||||||
|
|
||||||
Written by AI agent working for @jtmorris. Model: Grok 4.5.
|
Written by AI agent working for @jtmorris. Model: grok-build-0.1.
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@
|
||||||
"None": "None",
|
"None": "None",
|
||||||
"Copied %1 to clipboard": "Copied %1 to clipboard",
|
"Copied %1 to clipboard": "Copied %1 to clipboard",
|
||||||
"Invalid exit node hostname": "Invalid exit node hostname",
|
"Invalid exit node hostname": "Invalid exit node hostname",
|
||||||
"Not connected": "Not connected",
|
|
||||||
"Failed to connect to Tailscale": "Failed to connect to Tailscale",
|
"Failed to connect to Tailscale": "Failed to connect to Tailscale",
|
||||||
"Failed to disconnect from Tailscale": "Failed to disconnect from Tailscale",
|
"Failed to disconnect from Tailscale": "Failed to disconnect from Tailscale",
|
||||||
"Failed to set exit node": "Failed to set exit node",
|
"Failed to set exit node": "Failed to set exit node",
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
function parsePeers(peerMap) {
|
function parsePeers(peerMap) {
|
||||||
if (!peerMap) {
|
if (!peerMap) { return []; }
|
||||||
return [];
|
|
||||||
}
|
|
||||||
return Object.keys(peerMap).map(function (key) {
|
return Object.keys(peerMap).map(function (key) {
|
||||||
|
if (!Object.prototype.hasOwnProperty.call(peerMap, key)) { return null; }
|
||||||
var p = peerMap[key];
|
var p = peerMap[key];
|
||||||
return {
|
return {
|
||||||
hostname: p.HostName || key,
|
hostname: p.HostName || key,
|
||||||
|
|
@ -10,7 +9,7 @@ function parsePeers(peerMap) {
|
||||||
online: p.Online || false,
|
online: p.Online || false,
|
||||||
exitNode: p.ExitNodeOption || false
|
exitNode: p.ExitNodeOption || false
|
||||||
};
|
};
|
||||||
});
|
}).filter(function (peer) { return peer !== null; });
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeExitNodeCommand(hostname) {
|
function makeExitNodeCommand(hostname) {
|
||||||
|
|
@ -24,9 +23,7 @@ function makeExitNodeCommand(hostname) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function findActiveExitNode(peerMap) {
|
function findActiveExitNode(peerMap) {
|
||||||
if (!peerMap) {
|
if (!peerMap) { return ""; }
|
||||||
return "";
|
|
||||||
}
|
|
||||||
for (const key of Object.keys(peerMap)) {
|
for (const key of Object.keys(peerMap)) {
|
||||||
const p = peerMap[key];
|
const p = peerMap[key];
|
||||||
if (p.ExitNode) {
|
if (p.ExitNode) {
|
||||||
|
|
@ -55,43 +52,32 @@ function getStrings() {
|
||||||
exitNodePrefix: "Exit node: ",
|
exitNodePrefix: "Exit node: ",
|
||||||
none: "None",
|
none: "None",
|
||||||
copied: "Copied %1 to clipboard",
|
copied: "Copied %1 to clipboard",
|
||||||
invalidExitNodeHostname: "Invalid exit node hostname",
|
invalidExitNodeHostname: "Invalid exit node hostname"
|
||||||
notConnectedHint: "Not connected"
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Light UI predicates — keep the view thin.
|
||||||
|
function shouldShowClearExitNode(currentExitNode) {
|
||||||
|
return currentExitNode !== "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function isActiveExitNode(currentExitNode, hostname) {
|
||||||
|
return currentExitNode === hostname;
|
||||||
|
}
|
||||||
|
|
||||||
// Security: validate hostnames coming from tailscale status JSON.
|
// Security: validate hostnames coming from tailscale status JSON.
|
||||||
// Fail closed on obviously malicious input. Allow multi-label MagicDNS names
|
// Fail closed on obviously malicious input.
|
||||||
// up to DNS FQDN length (253).
|
|
||||||
function isValidExitNodeHostname(hostname) {
|
function isValidExitNodeHostname(hostname) {
|
||||||
if (typeof hostname !== "string") {
|
if (typeof hostname !== "string") { return false; }
|
||||||
return false;
|
if (hostname === "") { return true; }
|
||||||
}
|
return /^[a-zA-Z0-9]([a-zA-Z0-9-_.]{0,62}[a-zA-Z0-9])?$/.test(hostname);
|
||||||
if (hostname === "") {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (hostname.length > 253) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Each label: alnum start/end, alnum/hyphen/underscore inside; dots separate labels.
|
|
||||||
return /^(?=.{1,253}$)([a-zA-Z0-9]([a-zA-Z0-9_-]{0,61}[a-zA-Z0-9])?)(\.([a-zA-Z0-9]([a-zA-Z0-9_-]{0,61}[a-zA-Z0-9])?))*$/.test(hostname);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseStatusResult(jsonText) {
|
function parseStatusResult(jsonText) {
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(jsonText);
|
const data = JSON.parse(jsonText);
|
||||||
const isConnected = data.BackendState === "Running";
|
|
||||||
if (!isConnected) {
|
|
||||||
// #55: when not Running, do not surface stale peer list / exit node / IP.
|
|
||||||
return {
|
return {
|
||||||
isConnected: false,
|
isConnected: data.BackendState === "Running",
|
||||||
tailscaleIP: "",
|
|
||||||
currentExitNode: "",
|
|
||||||
peers: []
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
isConnected: true,
|
|
||||||
tailscaleIP: (data.Self && data.Self.TailscaleIPs && data.Self.TailscaleIPs[0]) || "",
|
tailscaleIP: (data.Self && data.Self.TailscaleIPs && data.Self.TailscaleIPs[0]) || "",
|
||||||
currentExitNode: findActiveExitNode(data.Peer || {}),
|
currentExitNode: findActiveExitNode(data.Peer || {}),
|
||||||
peers: parsePeers(data.Peer || {})
|
peers: parsePeers(data.Peer || {})
|
||||||
|
|
@ -123,7 +109,8 @@ function errorMessage(cmd) {
|
||||||
return messages[cmd] || "Tailscale command failed";
|
return messages[cmd] || "Tailscale command failed";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Central error formatting for the widget. detail is optional truncated stderr or extra context.
|
// Central error formatting for the widget. Used by both success and error paths.
|
||||||
|
// detail is optional truncated stderr or extra context.
|
||||||
function formatError(action, detail) {
|
function formatError(action, detail) {
|
||||||
var base = errorMessage(action);
|
var base = errorMessage(action);
|
||||||
if (detail && detail.length > 0) {
|
if (detail && detail.length > 0) {
|
||||||
|
|
@ -137,12 +124,7 @@ const PendingAction = Object.freeze({
|
||||||
TOGGLE: "toggle"
|
TOGGLE: "toggle"
|
||||||
});
|
});
|
||||||
|
|
||||||
// statusOk must be true (successful status poll) before acting on pending toggle.
|
function commandForPendingAction(pending, freshIsConnected) {
|
||||||
// Never invent up/down from a failed poll (would force "up" after clearing isConnected).
|
|
||||||
function commandForPendingAction(pending, freshIsConnected, statusOk) {
|
|
||||||
if (!statusOk) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (pending === PendingAction.TOGGLE) {
|
if (pending === PendingAction.TOGGLE) {
|
||||||
return buildToggleCommand(freshIsConnected);
|
return buildToggleCommand(freshIsConnected);
|
||||||
}
|
}
|
||||||
|
|
@ -150,19 +132,5 @@ function commandForPendingAction(pending, freshIsConnected, statusOk) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof module !== "undefined" && module.exports) {
|
if (typeof module !== "undefined" && module.exports) {
|
||||||
module.exports = {
|
module.exports = { parsePeers, makeExitNodeCommand, findActiveExitNode, errorMessage, formatError, getStatusCommand, isValidExitNodeHostname, getClipboardCommands, buildToggleCommand, parseStatusResult, getStrings, shouldShowClearExitNode, isActiveExitNode, PendingAction, commandForPendingAction };
|
||||||
parsePeers,
|
|
||||||
makeExitNodeCommand,
|
|
||||||
findActiveExitNode,
|
|
||||||
errorMessage,
|
|
||||||
formatError,
|
|
||||||
getStatusCommand,
|
|
||||||
isValidExitNodeHostname,
|
|
||||||
getClipboardCommands,
|
|
||||||
buildToggleCommand,
|
|
||||||
parseStatusResult,
|
|
||||||
getStrings,
|
|
||||||
PendingAction,
|
|
||||||
commandForPendingAction
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,5 +9,5 @@
|
||||||
"component": "./TailscaleWidget.qml",
|
"component": "./TailscaleWidget.qml",
|
||||||
"permissions": ["process"],
|
"permissions": ["process"],
|
||||||
"requires": ["tailscale"],
|
"requires": ["tailscale"],
|
||||||
"version": "0.2.1"
|
"version": "0.2.0"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
416
test/lib.test.js
416
test/lib.test.js
|
|
@ -1,33 +1,18 @@
|
||||||
import { test } from "node:test";
|
import { test } from "node:test";
|
||||||
import assert from "node:assert";
|
import assert from "node:assert";
|
||||||
import lib from "../tailscalectl/lib.js";
|
import lib from "../tailscalectl/lib.js";
|
||||||
|
const { parsePeers, makeExitNodeCommand, findActiveExitNode, errorMessage, formatError, getStatusCommand, isValidExitNodeHostname, getClipboardCommands, buildToggleCommand, parseStatusResult, getStrings, shouldShowClearExitNode, isActiveExitNode, PendingAction, commandForPendingAction } = lib;
|
||||||
const {
|
|
||||||
parsePeers,
|
|
||||||
makeExitNodeCommand,
|
|
||||||
findActiveExitNode,
|
|
||||||
errorMessage,
|
|
||||||
formatError,
|
|
||||||
getStatusCommand,
|
|
||||||
isValidExitNodeHostname,
|
|
||||||
getClipboardCommands,
|
|
||||||
buildToggleCommand,
|
|
||||||
parseStatusResult,
|
|
||||||
getStrings,
|
|
||||||
PendingAction,
|
|
||||||
commandForPendingAction
|
|
||||||
} = lib;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Unit tests for pure functions in lib.js.
|
* Unit tests for the pure functions exported from lib.js.
|
||||||
*
|
*
|
||||||
* TailscaleWidget.qml has no automated test coverage. Proc.runCommand
|
* All functions in lib.js are exercised via Node's built-in test runner.
|
||||||
* coordination, busy-mutex behavior, and widget UI must be verified
|
*
|
||||||
* manually in a running DMS instance.
|
* TailscaleWidget.qml has no automated test coverage. The Proc.runCommand
|
||||||
|
* calls, callback-based coordination (exact poll-act-poll preserved), and all
|
||||||
|
* widget UI behavior must be verified manually in a running DMS instance.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// --- parsePeers ---
|
|
||||||
|
|
||||||
test("parsePeers extracts exitNode from ExitNodeOption", () => {
|
test("parsePeers extracts exitNode from ExitNodeOption", () => {
|
||||||
const peerMap = {
|
const peerMap = {
|
||||||
"peer-1": {
|
"peer-1": {
|
||||||
|
|
@ -46,44 +31,10 @@ test("parsePeers extracts exitNode from ExitNodeOption", () => {
|
||||||
|
|
||||||
const peers = parsePeers(peerMap);
|
const peers = parsePeers(peerMap);
|
||||||
|
|
||||||
assert.strictEqual(peers.length, 2);
|
|
||||||
assert.strictEqual(peers[0].exitNode, true);
|
assert.strictEqual(peers[0].exitNode, true);
|
||||||
assert.strictEqual(peers[1].exitNode, false);
|
assert.strictEqual(peers[1].exitNode, false);
|
||||||
assert.strictEqual(peers[0].hostname, "router");
|
|
||||||
assert.strictEqual(peers[0].ip, "100.64.0.1");
|
|
||||||
assert.strictEqual(peers[0].online, true);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("parsePeers returns empty array for null/undefined peerMap", () => {
|
|
||||||
assert.deepStrictEqual(parsePeers(null), []);
|
|
||||||
assert.deepStrictEqual(parsePeers(undefined), []);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("parsePeers returns empty array for empty peerMap", () => {
|
|
||||||
assert.deepStrictEqual(parsePeers({}), []);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("parsePeers falls back to key when HostName missing", () => {
|
|
||||||
const peers = parsePeers({
|
|
||||||
"node-key-abc": { TailscaleIPs: ["100.64.0.9"], Online: false }
|
|
||||||
});
|
|
||||||
assert.strictEqual(peers[0].hostname, "node-key-abc");
|
|
||||||
assert.strictEqual(peers[0].ip, "100.64.0.9");
|
|
||||||
assert.strictEqual(peers[0].online, false);
|
|
||||||
assert.strictEqual(peers[0].exitNode, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("parsePeers uses empty ip when TailscaleIPs missing or empty", () => {
|
|
||||||
const peers = parsePeers({
|
|
||||||
a: { HostName: "a" },
|
|
||||||
b: { HostName: "b", TailscaleIPs: [] }
|
|
||||||
});
|
|
||||||
assert.strictEqual(peers[0].ip, "");
|
|
||||||
assert.strictEqual(peers[1].ip, "");
|
|
||||||
});
|
|
||||||
|
|
||||||
// --- makeExitNodeCommand / hostname validation ---
|
|
||||||
|
|
||||||
test("makeExitNodeCommand returns tailscale set command for hostname", () => {
|
test("makeExitNodeCommand returns tailscale set command for hostname", () => {
|
||||||
const cmd = makeExitNodeCommand("router");
|
const cmd = makeExitNodeCommand("router");
|
||||||
assert.deepStrictEqual(cmd, ["tailscale", "set", "--exit-node=router"]);
|
assert.deepStrictEqual(cmd, ["tailscale", "set", "--exit-node=router"]);
|
||||||
|
|
@ -94,49 +45,6 @@ test("makeExitNodeCommand with empty string clears exit node", () => {
|
||||||
assert.deepStrictEqual(cmd, ["tailscale", "set", "--exit-node="]);
|
assert.deepStrictEqual(cmd, ["tailscale", "set", "--exit-node="]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("makeExitNodeCommand returns null for invalid hostname", () => {
|
|
||||||
assert.strictEqual(makeExitNodeCommand("; rm"), null);
|
|
||||||
assert.strictEqual(makeExitNodeCommand("$(whoami)"), null);
|
|
||||||
assert.strictEqual(makeExitNodeCommand(null), null);
|
|
||||||
assert.strictEqual(makeExitNodeCommand(42), null);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("makeExitNodeCommand still produces correct argv for valid input", () => {
|
|
||||||
assert.deepStrictEqual(makeExitNodeCommand(""), ["tailscale", "set", "--exit-node="]);
|
|
||||||
assert.deepStrictEqual(makeExitNodeCommand("gluetun-sjc"), ["tailscale", "set", "--exit-node=gluetun-sjc"]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("isValidExitNodeHostname accepts empty string (clear)", () => {
|
|
||||||
assert.strictEqual(isValidExitNodeHostname(""), true);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("isValidExitNodeHostname accepts realistic Tailscale hostnames", () => {
|
|
||||||
["router", "gluetun-sjc", "my-exit-node-01", "peer_with_underscore", "a.b.c"].forEach((h) => {
|
|
||||||
assert.strictEqual(isValidExitNodeHostname(h), true, h);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test("isValidExitNodeHostname accepts MagicDNS-style FQDNs under 253 chars", () => {
|
|
||||||
assert.strictEqual(isValidExitNodeHostname("my-node.tail1234.ts.net"), true);
|
|
||||||
const longButValid = "a".repeat(60) + "." + "b".repeat(60) + "." + "c".repeat(60);
|
|
||||||
assert.ok(longButValid.length < 253);
|
|
||||||
assert.strictEqual(isValidExitNodeHostname(longButValid), true);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("isValidExitNodeHostname rejects injection attempts and garbage", () => {
|
|
||||||
["; rm -rf /", "$(whoami)", "`id`", "foo;bar", "a&b", "x\ny", "evil$(date)", " spacy ", "-leading", "trailing-"].forEach((h) => {
|
|
||||||
assert.strictEqual(isValidExitNodeHostname(h), false, h);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test("isValidExitNodeHostname rejects non-strings and oversized names", () => {
|
|
||||||
assert.strictEqual(isValidExitNodeHostname(undefined), false);
|
|
||||||
assert.strictEqual(isValidExitNodeHostname({}), false);
|
|
||||||
assert.strictEqual(isValidExitNodeHostname("x".repeat(254)), false);
|
|
||||||
});
|
|
||||||
|
|
||||||
// --- findActiveExitNode ---
|
|
||||||
|
|
||||||
test("findActiveExitNode returns hostname of peer with ExitNode=true", () => {
|
test("findActiveExitNode returns hostname of peer with ExitNode=true", () => {
|
||||||
const peerMap = {
|
const peerMap = {
|
||||||
"peer-1": { HostName: "gluetun-sjc", ExitNode: true, ExitNodeOption: true },
|
"peer-1": { HostName: "gluetun-sjc", ExitNode: true, ExitNodeOption: true },
|
||||||
|
|
@ -152,53 +60,31 @@ test("findActiveExitNode returns empty string when no exit node", () => {
|
||||||
assert.strictEqual(findActiveExitNode(peerMap), "");
|
assert.strictEqual(findActiveExitNode(peerMap), "");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("findActiveExitNode returns empty string for null peerMap", () => {
|
test("errorMessage returns user-friendly message for tailscale up failure", () => {
|
||||||
assert.strictEqual(findActiveExitNode(null), "");
|
const msg = errorMessage("up", 1);
|
||||||
|
assert.strictEqual(msg, "Failed to connect to Tailscale");
|
||||||
|
})
|
||||||
|
|
||||||
|
test("errorMessage returns user-friendly message for tailscale down failure", () => {
|
||||||
|
const msg = errorMessage("down", 1);
|
||||||
|
assert.strictEqual(msg, "Failed to disconnect from Tailscale");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("findActiveExitNode falls back to map key when HostName missing", () => {
|
test("errorMessage returns user-friendly message for tailscale set failure", () => {
|
||||||
const peerMap = {
|
const msg = errorMessage("set", 1);
|
||||||
"key-only": { ExitNode: true }
|
assert.strictEqual(msg, "Failed to set exit node");
|
||||||
};
|
|
||||||
assert.strictEqual(findActiveExitNode(peerMap), "key-only");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- errorMessage / formatError ---
|
test("errorMessage returns user-friendly message for tailscale status failure", () => {
|
||||||
|
const msg = errorMessage("status", 1);
|
||||||
test("errorMessage returns user-friendly messages for known actions", () => {
|
assert.strictEqual(msg, "Failed to read Tailscale status");
|
||||||
assert.strictEqual(errorMessage("up"), "Failed to connect to Tailscale");
|
|
||||||
assert.strictEqual(errorMessage("connect"), "Failed to connect to Tailscale");
|
|
||||||
assert.strictEqual(errorMessage("down"), "Failed to disconnect from Tailscale");
|
|
||||||
assert.strictEqual(errorMessage("disconnect"), "Failed to disconnect from Tailscale");
|
|
||||||
assert.strictEqual(errorMessage("set"), "Failed to set exit node");
|
|
||||||
assert.strictEqual(errorMessage("status"), "Failed to read Tailscale status");
|
|
||||||
assert.strictEqual(errorMessage("clipboard"), "Error copying to clipboard");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("errorMessage returns generic message for unknown command", () => {
|
test("errorMessage returns generic message for unknown command", () => {
|
||||||
assert.strictEqual(errorMessage("unknown"), "Tailscale command failed");
|
const msg = errorMessage("unknown", 1);
|
||||||
|
assert.strictEqual(msg, "Tailscale command failed");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("formatError returns base message without detail", () => {
|
|
||||||
assert.strictEqual(formatError("status"), "Failed to read Tailscale status");
|
|
||||||
assert.strictEqual(formatError("set"), "Failed to set exit node");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("formatError appends and truncates detail to 120 chars", () => {
|
|
||||||
const longDetail = "x".repeat(200);
|
|
||||||
const msg = formatError("up", longDetail);
|
|
||||||
assert.ok(msg.includes("Failed to connect to Tailscale"));
|
|
||||||
assert.ok(msg.endsWith("x".repeat(120)));
|
|
||||||
assert.ok(msg.length < 200);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("formatError handles empty or falsy detail gracefully", () => {
|
|
||||||
assert.strictEqual(formatError("down", ""), "Failed to disconnect from Tailscale");
|
|
||||||
assert.strictEqual(formatError("connect", null), "Failed to connect to Tailscale");
|
|
||||||
});
|
|
||||||
|
|
||||||
// --- clipboard ---
|
|
||||||
|
|
||||||
test("getClipboardCommands returns ordered argv arrays with text appended", () => {
|
test("getClipboardCommands returns ordered argv arrays with text appended", () => {
|
||||||
const cmds = getClipboardCommands("1.2.3.4");
|
const cmds = getClipboardCommands("1.2.3.4");
|
||||||
assert.ok(Array.isArray(cmds));
|
assert.ok(Array.isArray(cmds));
|
||||||
|
|
@ -214,172 +100,158 @@ test("getClipboardCommands handles text with special characters safely (direct a
|
||||||
assert.ok(cmds[0].includes("it's a 'test' with \"quotes\" and\nnewlines"));
|
assert.ok(cmds[0].includes("it's a 'test' with \"quotes\" and\nnewlines"));
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- strings ---
|
test("getClipboardCommands is deterministic and open for future tools", () => {
|
||||||
|
const cmds = getClipboardCommands("foo");
|
||||||
|
assert.ok(Array.isArray(cmds[0]));
|
||||||
|
assert.ok(Array.isArray(cmds[1]));
|
||||||
|
});
|
||||||
|
|
||||||
test("getStrings returns canonical UI strings for the widget", () => {
|
test("getStrings returns canonical UI strings for the widget", () => {
|
||||||
const s = getStrings();
|
const s = getStrings();
|
||||||
assert.ok(s.header);
|
assert.ok(s.header)
|
||||||
assert.ok(s.connected);
|
assert.ok(s.connected)
|
||||||
assert.ok(s.disconnected);
|
assert.ok(s.disconnected)
|
||||||
assert.ok(s.exitNodePrefix);
|
assert.ok(s.exitNodePrefix)
|
||||||
assert.ok(s.none);
|
assert.ok(s.copied)
|
||||||
assert.ok(s.copied);
|
})
|
||||||
assert.ok(s.invalidExitNodeHostname);
|
|
||||||
assert.ok(s.notConnectedHint);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("getStrings.copied is the I18n template key (interpolation via .arg at call site)", () => {
|
test("getStrings.copied is the I18n template key (interpolation happens at call site via .arg)", () => {
|
||||||
const s = getStrings();
|
const s = getStrings();
|
||||||
assert.strictEqual(s.copied, "Copied %1 to clipboard");
|
assert.strictEqual(s.copied, "Copied %1 to clipboard");
|
||||||
});
|
})
|
||||||
|
|
||||||
// --- toggle helpers ---
|
test("shouldShowClearExitNode returns true only when there is a current exit node", () => {
|
||||||
|
assert.strictEqual(shouldShowClearExitNode("router"), true)
|
||||||
|
assert.strictEqual(shouldShowClearExitNode(""), false)
|
||||||
|
})
|
||||||
|
|
||||||
test("buildToggleCommand returns down when connected", () => {
|
test("isActiveExitNode correctly identifies the active exit node button", () => {
|
||||||
assert.deepStrictEqual(buildToggleCommand(true), ["tailscale", "down"]);
|
assert.strictEqual(isActiveExitNode("gluetun-sjc", "gluetun-sjc"), true)
|
||||||
});
|
assert.strictEqual(isActiveExitNode("gluetun-sjc", "gluetun-den"), false)
|
||||||
|
assert.strictEqual(isActiveExitNode("", "router"), false)
|
||||||
|
})
|
||||||
|
|
||||||
test("buildToggleCommand returns up when disconnected", () => {
|
// --- buildToggleCommand ---
|
||||||
assert.deepStrictEqual(buildToggleCommand(false), ["tailscale", "up"]);
|
|
||||||
});
|
test("buildToggleCommand returns down command when connected", () => {
|
||||||
|
assert.deepStrictEqual(buildToggleCommand(true), ["tailscale", "down"])
|
||||||
|
})
|
||||||
|
|
||||||
|
test("buildToggleCommand returns up command when disconnected", () => {
|
||||||
|
assert.deepStrictEqual(buildToggleCommand(false), ["tailscale", "up"])
|
||||||
|
})
|
||||||
|
|
||||||
test("buildToggleCommand treats null and undefined as disconnected", () => {
|
test("buildToggleCommand treats null and undefined as disconnected", () => {
|
||||||
assert.deepStrictEqual(buildToggleCommand(null), ["tailscale", "up"]);
|
assert.deepStrictEqual(buildToggleCommand(null), ["tailscale", "up"])
|
||||||
assert.deepStrictEqual(buildToggleCommand(undefined), ["tailscale", "up"]);
|
assert.deepStrictEqual(buildToggleCommand(undefined), ["tailscale", "up"])
|
||||||
});
|
})
|
||||||
|
|
||||||
test("commandForPendingAction returns toggle command when pending is TOGGLE and statusOk", () => {
|
test("commandForPendingAction returns toggle command when pending is TOGGLE and passes through buildToggleCommand logic", () => {
|
||||||
assert.deepStrictEqual(commandForPendingAction(PendingAction.TOGGLE, true, true), ["tailscale", "down"]);
|
assert.deepStrictEqual(commandForPendingAction(PendingAction.TOGGLE, true), ["tailscale", "down"]);
|
||||||
assert.deepStrictEqual(commandForPendingAction(PendingAction.TOGGLE, false, true), ["tailscale", "up"]);
|
assert.deepStrictEqual(commandForPendingAction(PendingAction.TOGGLE, false), ["tailscale", "up"]);
|
||||||
assert.deepStrictEqual(commandForPendingAction(PendingAction.TOGGLE, null, true), ["tailscale", "up"]);
|
assert.deepStrictEqual(commandForPendingAction(PendingAction.TOGGLE, null), ["tailscale", "up"]);
|
||||||
});
|
|
||||||
|
|
||||||
test("commandForPendingAction returns null when status poll failed (do not invent up/down)", () => {
|
|
||||||
assert.strictEqual(commandForPendingAction(PendingAction.TOGGLE, false, false), null);
|
|
||||||
assert.strictEqual(commandForPendingAction(PendingAction.TOGGLE, true, false), null);
|
|
||||||
assert.strictEqual(commandForPendingAction(PendingAction.TOGGLE, false, undefined), null);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("commandForPendingAction returns null for no pending action or unknown pending value", () => {
|
test("commandForPendingAction returns null for no pending action or unknown pending value", () => {
|
||||||
assert.strictEqual(commandForPendingAction(null, true, true), null);
|
assert.strictEqual(commandForPendingAction(null, true), null);
|
||||||
assert.strictEqual(commandForPendingAction(undefined, false, true), null);
|
assert.strictEqual(commandForPendingAction(undefined, false), null);
|
||||||
assert.strictEqual(commandForPendingAction("something-else", true, true), null);
|
assert.strictEqual(commandForPendingAction("something-else", true), null);
|
||||||
assert.strictEqual(commandForPendingAction("", true, true), null);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- parseStatusResult (#55 and ground-truth parsing) ---
|
test("parseStatusResult produces correct state from valid JSON", () => {
|
||||||
|
|
||||||
test("parseStatusResult produces correct state from valid Running JSON", () => {
|
|
||||||
const json = JSON.stringify({
|
const json = JSON.stringify({
|
||||||
BackendState: "Running",
|
BackendState: "Running",
|
||||||
Self: { TailscaleIPs: ["100.64.0.5"] },
|
Self: { TailscaleIPs: ["100.64.0.5"] },
|
||||||
Peer: {
|
Peer: {
|
||||||
"key-1": {
|
"key-1": { HostName: "router", TailscaleIPs: ["100.64.0.1"], Online: true, ExitNode: true, ExitNodeOption: true }
|
||||||
HostName: "router",
|
|
||||||
TailscaleIPs: ["100.64.0.1"],
|
|
||||||
Online: true,
|
|
||||||
ExitNode: true,
|
|
||||||
ExitNodeOption: true
|
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
});
|
const state = parseStatusResult(json)
|
||||||
const state = parseStatusResult(json);
|
assert.strictEqual(state.isConnected, true)
|
||||||
assert.strictEqual(state.isConnected, true);
|
assert.strictEqual(state.tailscaleIP, "100.64.0.5")
|
||||||
assert.strictEqual(state.tailscaleIP, "100.64.0.5");
|
assert.strictEqual(state.currentExitNode, "router")
|
||||||
assert.strictEqual(state.currentExitNode, "router");
|
assert.strictEqual(state.peers.length, 1)
|
||||||
assert.strictEqual(state.peers.length, 1);
|
})
|
||||||
});
|
|
||||||
|
|
||||||
test("parseStatusResult returns safe defaults for invalid JSON", () => {
|
test("parseStatusResult returns safe defaults for invalid JSON", () => {
|
||||||
const state = parseStatusResult("not json at all");
|
const state = parseStatusResult("not json at all")
|
||||||
assert.strictEqual(state.isConnected, false);
|
assert.strictEqual(state.isConnected, false)
|
||||||
assert.strictEqual(state.tailscaleIP, "");
|
assert.strictEqual(state.tailscaleIP, "")
|
||||||
assert.strictEqual(state.currentExitNode, "");
|
assert.strictEqual(state.currentExitNode, "")
|
||||||
assert.deepStrictEqual(state.peers, []);
|
assert.strictEqual(state.peers.length, 0)
|
||||||
});
|
})
|
||||||
|
|
||||||
test("parseStatusResult handles missing Self gracefully when Running", () => {
|
test("parseStatusResult handles missing Self gracefully", () => {
|
||||||
const json = JSON.stringify({ BackendState: "Running", Peer: {} });
|
const json = JSON.stringify({ BackendState: "Running", Peer: {} })
|
||||||
const state = parseStatusResult(json);
|
const state = parseStatusResult(json)
|
||||||
assert.strictEqual(state.isConnected, true);
|
assert.strictEqual(state.isConnected, true)
|
||||||
assert.strictEqual(state.tailscaleIP, "");
|
assert.strictEqual(state.tailscaleIP, "")
|
||||||
});
|
})
|
||||||
|
|
||||||
test("parseStatusResult handles missing and empty Peer gracefully when Running", () => {
|
test("parseStatusResult handles missing and empty Peer gracefully", () => {
|
||||||
const json = JSON.stringify({
|
const json = JSON.stringify({ BackendState: "Running", Self: { TailscaleIPs: ["100.64.0.5"] } })
|
||||||
BackendState: "Running",
|
const state = parseStatusResult(json)
|
||||||
Self: { TailscaleIPs: ["100.64.0.5"] }
|
assert.strictEqual(state.peers.length, 0)
|
||||||
});
|
assert.strictEqual(state.currentExitNode, "")
|
||||||
const state = parseStatusResult(json);
|
})
|
||||||
assert.strictEqual(state.peers.length, 0);
|
|
||||||
assert.strictEqual(state.currentExitNode, "");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("parseStatusResult sets isConnected false for non-Running BackendState", () => {
|
test("parseStatusResult sets isConnected false for non-Running BackendState", () => {
|
||||||
const json = JSON.stringify({
|
const json = JSON.stringify({ BackendState: "NeedsLogin", Self: { TailscaleIPs: ["100.64.0.5"] }, Peer: {} })
|
||||||
BackendState: "NeedsLogin",
|
const state = parseStatusResult(json)
|
||||||
Self: { TailscaleIPs: ["100.64.0.5"] },
|
assert.strictEqual(state.isConnected, false)
|
||||||
Peer: {}
|
})
|
||||||
});
|
|
||||||
const state = parseStatusResult(json);
|
|
||||||
assert.strictEqual(state.isConnected, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
// #55: disconnected must not surface peer list / exit node from leftover JSON
|
// --- formatError (central error + detail formatting) ---
|
||||||
test("parseStatusResult clears peers and exit node when BackendState is not Running (#55)", () => {
|
|
||||||
const json = JSON.stringify({
|
|
||||||
BackendState: "Stopped",
|
|
||||||
Self: { TailscaleIPs: ["100.64.0.5"] },
|
|
||||||
Peer: {
|
|
||||||
"k1": {
|
|
||||||
HostName: "router",
|
|
||||||
TailscaleIPs: ["100.64.0.1"],
|
|
||||||
Online: false,
|
|
||||||
ExitNode: true,
|
|
||||||
ExitNodeOption: true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const state = parseStatusResult(json);
|
|
||||||
assert.strictEqual(state.isConnected, false);
|
|
||||||
assert.deepStrictEqual(state.peers, []);
|
|
||||||
assert.strictEqual(state.currentExitNode, "");
|
|
||||||
// Self IP may still appear in raw JSON; we clear display IP when disconnected
|
|
||||||
// so the popout does not look "half connected".
|
|
||||||
assert.strictEqual(state.tailscaleIP, "");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("parseStatusResult clears peers for NeedsLogin even if Peer map is populated (#55)", () => {
|
test("formatError returns base message without detail", () => {
|
||||||
const json = JSON.stringify({
|
assert.strictEqual(formatError("status"), "Failed to read Tailscale status")
|
||||||
BackendState: "NeedsLogin",
|
assert.strictEqual(formatError("set"), "Failed to set exit node")
|
||||||
Self: { TailscaleIPs: ["100.64.0.5"] },
|
})
|
||||||
Peer: {
|
|
||||||
"k1": { HostName: "ghost", TailscaleIPs: ["100.64.0.2"], Online: false }
|
test("formatError appends and truncates detail", () => {
|
||||||
}
|
const longDetail = "x".repeat(200)
|
||||||
});
|
const msg = formatError("up", longDetail)
|
||||||
const state = parseStatusResult(json);
|
assert.ok(msg.includes("Failed to connect to Tailscale"))
|
||||||
assert.strictEqual(state.isConnected, false);
|
assert.ok(msg.endsWith("x".repeat(120)))
|
||||||
assert.deepStrictEqual(state.peers, []);
|
assert.ok(msg.length < 200)
|
||||||
assert.strictEqual(state.currentExitNode, "");
|
})
|
||||||
assert.strictEqual(state.tailscaleIP, "");
|
|
||||||
});
|
test("formatError handles empty or falsy detail gracefully", () => {
|
||||||
|
assert.strictEqual(formatError("down", ""), "Failed to disconnect from Tailscale")
|
||||||
|
assert.strictEqual(formatError("connect", null), "Failed to connect to Tailscale")
|
||||||
|
})
|
||||||
|
|
||||||
|
// --- isValidExitNodeHostname + makeExitNodeCommand safety ---
|
||||||
|
|
||||||
|
test("isValidExitNodeHostname accepts empty string (clear)", () => {
|
||||||
|
assert.strictEqual(isValidExitNodeHostname(""), true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("isValidExitNodeHostname accepts realistic Tailscale hostnames", () => {
|
||||||
|
["router", "gluetun-sjc", "my-exit-node-01", "peer_with_underscore", "a.b.c"].forEach(h =>
|
||||||
|
assert.strictEqual(isValidExitNodeHostname(h), true, h)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("isValidExitNodeHostname rejects obvious injection attempts", () => {
|
||||||
|
["; rm -rf /", "$(whoami)", "`id`", "foo;bar", "a&b", "x\ny", "evil$(date)"].forEach(h =>
|
||||||
|
assert.strictEqual(isValidExitNodeHostname(h), false, h)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("makeExitNodeCommand returns null for invalid hostname", () => {
|
||||||
|
assert.strictEqual(makeExitNodeCommand("; rm"), null)
|
||||||
|
assert.strictEqual(makeExitNodeCommand("$(whoami)"), null)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("makeExitNodeCommand still produces correct argv for valid input", () => {
|
||||||
|
assert.deepStrictEqual(makeExitNodeCommand(""), ["tailscale", "set", "--exit-node="])
|
||||||
|
assert.deepStrictEqual(makeExitNodeCommand("gluetun-sjc"), ["tailscale", "set", "--exit-node=gluetun-sjc"])
|
||||||
|
})
|
||||||
|
|
||||||
// --- getStatusCommand ---
|
// --- getStatusCommand ---
|
||||||
|
|
||||||
test("getStatusCommand returns the canonical tailscale status --json argv", () => {
|
test("getStatusCommand returns the canonical tailscale status --json argv", () => {
|
||||||
const cmd = getStatusCommand();
|
const cmd = getStatusCommand()
|
||||||
assert.deepStrictEqual(cmd, ["tailscale", "status", "--json"]);
|
assert.deepStrictEqual(cmd, ["tailscale", "status", "--json"])
|
||||||
});
|
})
|
||||||
|
|
||||||
// --- PendingAction constant ---
|
|
||||||
|
|
||||||
test("PendingAction.TOGGLE is the stable string used by the widget", () => {
|
|
||||||
assert.strictEqual(PendingAction.TOGGLE, "toggle");
|
|
||||||
});
|
|
||||||
|
|
||||||
// --- export surface: no over-abstracted UI predicates ---
|
|
||||||
|
|
||||||
test("lib does not export trivial UI predicates shouldShowClearExitNode / isActiveExitNode", () => {
|
|
||||||
assert.strictEqual(lib.shouldShowClearExitNode, undefined);
|
|
||||||
assert.strictEqual(lib.isActiveExitNode, undefined);
|
|
||||||
});
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue