summaryrefslogtreecommitdiffstats
path: root/web/gui/v2/registry-hello.html
blob: e95f48c47cd32bd1cad713a09549d511e5c2b457 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!doctype html><html><head><title>Netdata Registry</title><meta name="application-name" content="netdata"/><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta charset="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/><meta name="viewport" content="width=device-width,height=device-height,initial-scale=1,minimum-scale=1"/><meta name="apple-mobile-web-app-capable" content="yes"/><meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"/></head><body><script>var getBasename = function() {
        var pathRegex = /\/(v2\/)?registry-hello.html?.*/
        return window.location.origin + window.location.pathname.replace(pathRegex, "")
      }

      function doRegistryAccess(parentHelloData, originUrl) {
        const helloPromise = !parentHelloData
          ? fetch(originUrl + "/api/v1/registry?action=hello", { cache: "no-cache", credentials: "include" })
              .then(function(response) { return response.json() })
          : Promise.resolve(parentHelloData)

        helloPromise.then(function(data) {
          var retries = 3
          var nodes = data.nodes || []

          var doAccess = function(mg, nm, token) {
            fetch(data.registry + '/api/v1/registry?action=access&machine=' + (mg || data.machine_guid) + '&name=' + encodeURIComponent(nm || data.hostname) + '&url=' + encodeURIComponent(originUrl) + '&agent=' + (data.agent ? data.agent.machine_guid : ""), { cache: "no-cache", credentials: "include", headers: { Authorization: 'Bearer ' + (token || localStorage.getItem("registry-netdata-token")) } })
            .then(function(response) { return response.json() })
            .then(function(data) {
              var token = data.person_guid

              if (data.status === "redirect" && retries > 0) {
                retries = retries - 1
                doAccess(data.machine_guid, data.hostname, token)
              }

              if (data.status === "ok") {
                localStorage.setItem("registry-netdata-token", token)

                let node = nodes.pop()

                if (!!node) {
                  setTimeout(function () {
                    doAccess(node.machine_guid, node.hostname, token)
                  }, 1000)
                }
              }
              window.parent.postMessage(data, "*");
            })
          }
          doAccess(data.machine_guid, data.hostname)
        })
      }

      try {
        var searchParams = new URLSearchParams(location.search)
        var url = searchParams.get("url")
        var mg = searchParams.get("agent_machine_guid")
        var tr_i = searchParams.get("transition_id")

        fetch(url + "/api/v1/registry?action=hello", { cache: "no-cache", credentials: "include" })
          .then(function(response) { return response.json() })
          .then(function(helloData) {
            window.parent.postMessage({
              mg: mg,
              tr_i: tr_i,
              hostname: helloData.hostname,
              url: url,
              isSame: mg === (helloData.agent ? helloData.agent.machine_guid : helloData.machine_guid)
            }, "*");
          }).catch(() => {
            window.parent.postMessage({
              mg: mg,
              tr_i: tr_i,
              url: url,
              isSame: false,
              hasError: true
            }, "*");
          })
        doRegistryAccess(parentHelloData, searchParams.get("originUrl"))
      } catch(e) {
        // do nothing
      }

      function isValidHttpUrl(string) {
        var url;

        try {
          return true
        } catch (_) {
          return false;
        }
      }

      window.addEventListener('message', function(event) {
        var isNetdataMessage = Array.isArray(event.data) ? event.data.length === 3 ? event.data[0] === "netdata-registry" : isValidHttpUrl(event.data[0]) : true

        if (!isNetdataMessage) return

        var originUrl = Array.isArray(event.data) ? event.data.length === 3 ? event.data[1] : event.data[0] : event.data
        var parentHelloData = Array.isArray(event.data) ? event.data.length === 3 ? event.data[2] : event.data[1] : null

        doRegistryAccess(parentHelloData, originUrl)
      });</script></body></html>