diff options
| author | Will Norris <will@tailscale.com> | 2023-08-08 12:59:02 -0700 |
|---|---|---|
| committer | Will Norris <will@tailscale.com> | 2023-08-10 10:38:49 -0700 |
| commit | 5e7666084379b27ea3389851f5366c81384b6d62 (patch) | |
| tree | 7c8952bdaf9d61ef130a4cdc76b713087b50c776 /webui/src/web.ts | |
| parent | 94253129239547f7235b7c4f7ccf0bbd4f25c01c (diff) | |
| download | tailscale-tsweb/client-ui.tar.xz tailscale-tsweb/client-ui.zip | |
stashtsweb/client-ui
Diffstat (limited to 'webui/src/web.ts')
| -rw-r--r-- | webui/src/web.ts | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/webui/src/web.ts b/webui/src/web.ts new file mode 100644 index 000000000..97f1908aa --- /dev/null +++ b/webui/src/web.ts @@ -0,0 +1,90 @@ +export function run() { + +const advertiseExitNode = {{ .AdvertiseExitNode }}; +const isUnraid = {{ .IsUnraid }}; +const unraidCsrfToken = "{{ .UnraidToken }}"; +let fetchingUrl = false; +var data = { +AdvertiseRoutes: "{{ .AdvertiseRoutes }}", +AdvertiseExitNode: advertiseExitNode, +Reauthenticate: false, +ForceLogout: false +}; + +function postData(e) { +e.preventDefault(); + +if (fetchingUrl) { + return; +} + +fetchingUrl = true; +const urlParams = new URLSearchParams(window.location.search); +const token = urlParams.get("SynoToken"); +const nextParams = new URLSearchParams({ up: true }); +if (token) { + nextParams.set("SynoToken", token) +} +const nextUrl = new URL(window.location); +nextUrl.search = nextParams.toString() + +let body = JSON.stringify(data); +let contentType = "application/json"; + +if (isUnraid) { + const params = new URLSearchParams(); + params.append("csrf_token", unraidCsrfToken); + params.append("ts_data", JSON.stringify(data)); + + body = params.toString(); + contentType = "application/x-www-form-urlencoded;charset=UTF-8"; +} + +const url = nextUrl.toString(); +fetch(url, { + method: "POST", + headers: { + "Accept": "application/json", + "Content-Type": contentType, + }, + body: body +}).then(res => res.json()).then(res => { + fetchingUrl = false; + const err = res["error"]; + if (err) { + throw new Error(err); + } + const url = res["url"]; + if (url) { + if(isUnraid) { + window.open(url, "_blank"); + } else { + document.location.href = url; + } + } else { + location.reload(); + } +}).catch(err => { + alert("Failed operation: " + err.message); +}); +} + +document.querySelectorAll(".js-loginButton").forEach(function (el){ +el.addEventListener("click", function(e) { + data.Reauthenticate = true; + postData(e); +}); +}) +document.querySelectorAll(".js-logoutButton").forEach(function(el) { +el.addEventListener("click", function (e) { + data.ForceLogout = true; + postData(e); +}); +}) +document.querySelectorAll(".js-advertiseExitNode").forEach(function (el) { +el.addEventListener("click", function(e) { + data.AdvertiseExitNode = !advertiseExitNode; + postData(e); +}); +}) +}
\ No newline at end of file |
