2026-05-18 18:27:41 +00:00
|
|
|
|
function parsePeers(peerMap) {
|
|
|
|
|
|
if (!peerMap) return []
|
|
|
|
|
|
return Object.keys(peerMap).map(function (key) {
|
2026-05-21 10:11:03 +00:00
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(peerMap, key)) return null
|
2026-05-18 18:27:41 +00:00
|
|
|
|
var p = peerMap[key]
|
|
|
|
|
|
return {
|
|
|
|
|
|
hostname: p.HostName || key,
|
|
|
|
|
|
ip: (p.TailscaleIPs && p.TailscaleIPs.length) ? p.TailscaleIPs[0] : "",
|
|
|
|
|
|
online: p.Online || false,
|
|
|
|
|
|
exitNode: p.ExitNodeOption || false
|
|
|
|
|
|
}
|
2026-05-21 10:11:03 +00:00
|
|
|
|
}).filter(function (peer) { return peer !== null })
|
2026-05-18 18:27:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function makeExitNodeCommand(hostname) {
|
2026-05-22 20:46:37 +00:00
|
|
|
|
if (!isValidExitNodeHostname(hostname)) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2026-05-21 10:11:03 +00:00
|
|
|
|
if (hostname === "") {
|
2026-05-22 20:46:37 +00:00
|
|
|
|
return ["tailscale", "set", "--exit-node="];
|
2026-05-21 10:11:03 +00:00
|
|
|
|
}
|
2026-05-22 20:46:37 +00:00
|
|
|
|
return ["tailscale", "set", "--exit-node=" + hostname];
|
2026-05-18 18:27:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-18 22:10:37 +00:00
|
|
|
|
function findActiveExitNode(peerMap) {
|
|
|
|
|
|
if (!peerMap) return ""
|
|
|
|
|
|
for (const key in peerMap) {
|
2026-05-21 10:11:03 +00:00
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(peerMap, key)) continue
|
2026-05-18 22:10:37 +00:00
|
|
|
|
const p = peerMap[key]
|
|
|
|
|
|
if (p.ExitNode) {
|
|
|
|
|
|
return p.HostName || key
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-22 21:35:07 +00:00
|
|
|
|
const clipboardTools = [
|
|
|
|
|
|
{ argv: ["dms", "cl", "copy"] },
|
|
|
|
|
|
{ argv: ["wl-copy"] }
|
|
|
|
|
|
];
|
2026-05-20 10:08:59 +00:00
|
|
|
|
|
2026-05-22 21:35:07 +00:00
|
|
|
|
function getClipboardCommands(text) {
|
|
|
|
|
|
return clipboardTools.map(function (tool) {
|
|
|
|
|
|
return tool.argv.concat([text]);
|
|
|
|
|
|
});
|
2026-05-20 10:11:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-22 06:33:08 +00:00
|
|
|
|
function getStrings() {
|
|
|
|
|
|
return {
|
|
|
|
|
|
header: "Tailscale",
|
|
|
|
|
|
connected: "Connected",
|
|
|
|
|
|
disconnected: "Disconnected",
|
|
|
|
|
|
exitNodePrefix: "Exit node: ",
|
|
|
|
|
|
none: "None",
|
|
|
|
|
|
copied: function (text) { return "Copied " + text + " to clipboard" },
|
|
|
|
|
|
clearExitNode: "×",
|
2026-05-22 20:46:37 +00:00
|
|
|
|
setExitNode: "↗",
|
|
|
|
|
|
invalidExitNodeHostname: "Invalid exit node hostname"
|
2026-05-22 06:33:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-22 21:47:41 +00:00
|
|
|
|
// Light UI predicates — keep the view thin.
|
|
|
|
|
|
function shouldShowClearExitNode(currentExitNode) {
|
2026-05-22 06:33:33 +00:00
|
|
|
|
return currentExitNode !== ""
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function isActiveExitNode(currentExitNode, hostname) {
|
|
|
|
|
|
return currentExitNode === hostname
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-22 21:35:07 +00:00
|
|
|
|
// Security: validate hostnames coming from tailscale status JSON.
|
|
|
|
|
|
// Fail closed on obviously malicious input.
|
2026-05-22 20:46:37 +00:00
|
|
|
|
function isValidExitNodeHostname(hostname) {
|
|
|
|
|
|
if (typeof hostname !== "string") return false;
|
|
|
|
|
|
if (hostname === "") return true; // clear command
|
|
|
|
|
|
// Permissive enough for real Tailscale hostnames while rejecting
|
|
|
|
|
|
// classic shell metacharacters and control characters.
|
|
|
|
|
|
return /^[a-zA-Z0-9]([a-zA-Z0-9-_.]{0,62}[a-zA-Z0-9])?$/.test(hostname);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-20 19:29:02 +00:00
|
|
|
|
function parseStatusResult(jsonText) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const data = JSON.parse(jsonText)
|
|
|
|
|
|
return {
|
|
|
|
|
|
isConnected: data.BackendState === "Running",
|
|
|
|
|
|
tailscaleIP: (data.Self && data.Self.TailscaleIPs && data.Self.TailscaleIPs[0]) || "",
|
|
|
|
|
|
currentExitNode: findActiveExitNode(data.Peer || {}),
|
|
|
|
|
|
peers: parsePeers(data.Peer || {})
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
return { isConnected: false, tailscaleIP: "", currentExitNode: "", peers: [] }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-20 19:21:17 +00:00
|
|
|
|
function buildToggleCommand(isConnected) {
|
|
|
|
|
|
return isConnected ? ["tailscale", "down"] : ["tailscale", "up"]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-22 10:19:39 +00:00
|
|
|
|
// Single source of truth for the status command used for on-demand and post-action verification.
|
|
|
|
|
|
function getStatusCommand() {
|
|
|
|
|
|
return ["tailscale", "status", "--json"]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-18 20:53:32 +00:00
|
|
|
|
function errorMessage(cmd) {
|
|
|
|
|
|
var messages = {
|
|
|
|
|
|
"up": "Failed to connect to Tailscale",
|
|
|
|
|
|
"connect": "Failed to connect to Tailscale",
|
|
|
|
|
|
"down": "Failed to disconnect from Tailscale",
|
|
|
|
|
|
"disconnect": "Failed to disconnect from Tailscale",
|
|
|
|
|
|
"set": "Failed to set exit node",
|
2026-05-22 10:20:27 +00:00
|
|
|
|
"status": "Failed to read Tailscale status",
|
|
|
|
|
|
"clipboard": "Error copying to clipboard"
|
2026-05-22 10:19:25 +00:00
|
|
|
|
};
|
|
|
|
|
|
return messages[cmd] || "Tailscale command failed";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
|
var base = errorMessage(action);
|
|
|
|
|
|
if (detail && detail.length > 0) {
|
|
|
|
|
|
var truncated = detail.length > 120 ? detail.slice(0, 120) : detail;
|
|
|
|
|
|
return base + " — " + truncated;
|
2026-05-18 20:53:32 +00:00
|
|
|
|
}
|
2026-05-22 10:19:25 +00:00
|
|
|
|
return base;
|
2026-05-18 20:53:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-23 04:41:40 +00:00
|
|
|
|
const PendingAction = Object.freeze({
|
|
|
|
|
|
TOGGLE: "toggle"
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function commandForPendingAction(pending, freshIsConnected) {
|
|
|
|
|
|
if (pending === PendingAction.TOGGLE) {
|
|
|
|
|
|
return buildToggleCommand(freshIsConnected);
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-18 18:39:39 +00:00
|
|
|
|
if (typeof module !== "undefined" && module.exports) {
|
2026-05-23 04:41:40 +00:00
|
|
|
|
module.exports = { parsePeers, makeExitNodeCommand, findActiveExitNode, errorMessage, formatError, getStatusCommand, isValidExitNodeHostname, getClipboardCommands, buildToggleCommand, parseStatusResult, getStrings, shouldShowClearExitNode, isActiveExitNode, PendingAction, commandForPendingAction };
|
2026-05-18 18:39:39 +00:00
|
|
|
|
}
|