summaryrefslogtreecommitdiffhomepage
path: root/cmd/tsconnect/src/notifier.js
blob: 71317f01ee80d1f4fa3f7416af3909102337e55e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

import {
  showLoginURL,
  hideLoginURL,
  showLogoutButton,
  hideLogoutButton,
} from "./login"
import { showSSHPeers, hideSSHPeers } from "./ssh"

/**
 * @fileoverview Notification callback functions (bridged from ipn.Notify)
 */

/** Mirrors values from ipn/backend.go */
const State = {
  NoState: 0,
  InUseOtherUser: 1,
  NeedsLogin: 2,
  NeedsMachineAuth: 3,
  Stopped: 4,
  Starting: 5,
  Running: 6,
}

export function notifyState(ipn, state) {
  let stateLabel
  switch (state) {
    case State.NoState:
      stateLabel = "Initializing…"
      break
    case State.InUseOtherUser:
      stateLabel = "In-use by another user"
      break
    case State.NeedsLogin:
      stateLabel = "Needs Login"
      hideLogoutButton()
      hideSSHPeers()
      ipn.login()
      break
    case State.NeedsMachineAuth:
      stateLabel = "Needs authorization"
      break
    case State.Stopped:
      stateLabel = "Stopped"
      hideLogoutButton()
      hideSSHPeers()
      break
    case State.Starting:
      stateLabel = "Starting…"
      break
    case State.Running:
      stateLabel = "Running"
      hideLoginURL()
      showLogoutButton(ipn)
      break
  }
  const stateNode = document.getElementById("state")
  stateNode.textContent = stateLabel ?? ""
}

export function notifyNetMap(ipn, netMapStr) {
  const netMap = JSON.parse(netMapStr)
  if (DEBUG) {
    console.log("Received net map: " + JSON.stringify(netMap, null, 2))
  }

  showSSHPeers(netMap.peers, ipn)
}

export function notifyBrowseToURL(ipn, url) {
  showLoginURL(url)
}