node.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. define(["moment", "numeral", "tablesort", "tablesort.numeric"],
  2. function (moment, numeral, Tablesort) {
  3. function showGeoURI(d) {
  4. function showLatitude(d) {
  5. var suffix = Math.sign(d) > -1 ? "' N" : "' S"
  6. d = Math.abs(d)
  7. var a = Math.floor(d)
  8. var min = (d * 60) % 60
  9. a = (a < 10 ? "0" : "") + a
  10. return a + "° " + numeral(min).format("0.000") + suffix
  11. }
  12. function showLongitude(d) {
  13. var suffix = Math.sign(d) > -1 ? "' E" : "' W"
  14. d = Math.abs(d)
  15. var a = Math.floor(d)
  16. var min = (d * 60) % 60
  17. a = (a < 100 ? "0" + (a < 10 ? "0" : "") : "") + a
  18. return a + "° " + numeral(min).format("0.000") + suffix
  19. }
  20. if (!has_location(d))
  21. return undefined
  22. return function (el) {
  23. var latitude = d.nodeinfo.location.latitude
  24. var longitude = d.nodeinfo.location.longitude
  25. var a = document.createElement("a")
  26. a.textContent = showLatitude(latitude) + " " +
  27. showLongitude(longitude)
  28. a.href = "geo:" + latitude + "," + longitude
  29. el.appendChild(a)
  30. }
  31. }
  32. function showConnectedVpnPeers(d) {
  33. var peers = ""
  34. if (d.statistics.vpn_peers) {
  35. var vpnGroups = d.statistics.vpn_peers.groups
  36. for ( var group in vpnGroups ) {
  37. var vpnPeers = vpnGroups[group].peers
  38. for ( var gw in vpnPeers ) {
  39. if ( vpnPeers[gw] !== null ) {
  40. if (peers !== "")
  41. peers += ", "
  42. var time = moment.duration(vpnPeers[gw].established, "seconds").humanize()
  43. peers += gw + " (" + time + ")"
  44. }
  45. }
  46. }
  47. }
  48. if (peers !== "")
  49. return peers
  50. else
  51. return "Keine"
  52. }
  53. function showStatus(d) {
  54. return function (el) {
  55. el.classList.add(d.flags.online ? "online" : "offline")
  56. el.textContent = d.flags.online ? "online" : "offline, " + d.lastseen.fromNow(true)
  57. }
  58. }
  59. function showFirmware(d) {
  60. var release = dictGet(d.nodeinfo, ["software", "firmware", "release"])
  61. var base = dictGet(d.nodeinfo, ["software", "firmware", "base"])
  62. if (release === null || base === null)
  63. return undefined
  64. return release + " / " + base
  65. }
  66. function showUptime(d) {
  67. if (!("uptime" in d.statistics))
  68. return undefined
  69. return moment.duration(d.statistics.uptime, "seconds").humanize()
  70. }
  71. function showFirstseen(d) {
  72. if (!("firstseen" in d))
  73. return undefined
  74. return d.firstseen.fromNow(true)
  75. }
  76. function showClients(d) {
  77. if (!d.flags.online)
  78. return undefined
  79. return function (el) {
  80. el.appendChild(document.createTextNode(d.statistics.clients > 0 ? d.statistics.clients : "keine"))
  81. el.appendChild(document.createElement("br"))
  82. var span = document.createElement("span")
  83. span.classList.add("clients")
  84. span.textContent = " ".repeat(d.statistics.clients)
  85. el.appendChild(span)
  86. }
  87. }
  88. function showIPs(d) {
  89. var ips = dictGet(d.nodeinfo, ["network", "addresses"])
  90. if (ips === null)
  91. return undefined
  92. ips.sort()
  93. return function (el) {
  94. ips.forEach( function (ip, i) {
  95. var link = !ip.startsWith("fe80:")
  96. if (i > 0)
  97. el.appendChild(document.createElement("br"))
  98. if (link) {
  99. var a = document.createElement("a")
  100. a.href = "http://[" + ip + "]/"
  101. a.textContent = ip
  102. el.appendChild(a)
  103. } else
  104. el.appendChild(document.createTextNode(ip))
  105. })
  106. }
  107. }
  108. function showBar(className, v) {
  109. var span = document.createElement("span")
  110. span.classList.add("bar")
  111. span.classList.add(className)
  112. var bar = document.createElement("span")
  113. bar.style.width = (v * 100) + "%"
  114. span.appendChild(bar)
  115. var label = document.createElement("label")
  116. label.textContent = (Math.round(v * 100)) + " %"
  117. span.appendChild(label)
  118. return span
  119. }
  120. function showRAM(d) {
  121. if (!("memory_usage" in d.statistics))
  122. return undefined
  123. return function (el) {
  124. el.appendChild(showBar("memory-usage", d.statistics.memory_usage))
  125. }
  126. }
  127. function showAutoupdate(d) {
  128. var au = dictGet(d.nodeinfo, ["software", "autoupdater"])
  129. if (!au)
  130. return undefined
  131. return au.enabled ? "aktiviert (" + au.branch + ")" : "deaktiviert"
  132. }
  133. function showStatImg(o, nodeId) {
  134. var content, caption
  135. if (o.thumbnail) {
  136. content = document.createElement("img")
  137. content.src = o.thumbnail.replace("{NODE_ID}", nodeId)
  138. }
  139. if (o.caption) {
  140. caption = o.caption.replace("{NODE_ID}", nodeId)
  141. if (!content)
  142. content = document.createTextNode(caption)
  143. }
  144. var p = document.createElement("p")
  145. if (o.href) {
  146. var link = document.createElement("a")
  147. link.target = "_blank"
  148. link.href = o.href.replace("{NODE_ID}", nodeId)
  149. link.appendChild(content)
  150. if (caption && o.thumbnail)
  151. link.title = caption
  152. p.appendChild(link)
  153. } else
  154. p.appendChild(content)
  155. return p
  156. }
  157. function showSite(d) {
  158. var siteName = dictGet(d.nodeinfo, ["system", "site_name"])
  159. var siteCode = dictGet(d.nodeinfo, ["system", "site_code"])
  160. return siteName ? siteName : siteCode
  161. }
  162. return function(config, el, router, d) {
  163. var h2 = document.createElement("h2")
  164. h2.textContent = d.nodeinfo.hostname
  165. el.appendChild(h2)
  166. var attributes = document.createElement("table")
  167. attributes.classList.add("attributes")
  168. attributeEntry(attributes, "Status", showStatus(d))
  169. attributeEntry(attributes, "Koordinaten", showGeoURI(d))
  170. if (config.showContact)
  171. attributeEntry(attributes, "Kontakt", dictGet(d.nodeinfo, ["owner", "contact"]))
  172. attributeEntry(attributes, "Hardware", dictGet(d.nodeinfo, ["hardware", "model"]))
  173. attributeEntry(attributes, "Primäre MAC", dictGet(d.nodeinfo, ["network", "mac"]))
  174. attributeEntry(attributes, "Node ID", dictGet(d.nodeinfo, ["node_id"]))
  175. attributeEntry(attributes, "Region", showSite(d))
  176. attributeEntry(attributes, "Firmware", showFirmware(d))
  177. attributeEntry(attributes, "Uptime", showUptime(d))
  178. attributeEntry(attributes, "Teil des Netzes", showFirstseen(d))
  179. attributeEntry(attributes, "VPN-Verbindung", showConnectedVpnPeers(d))
  180. attributeEntry(attributes, "Arbeitsspeicher", showRAM(d))
  181. attributeEntry(attributes, "IP Adressen", showIPs(d))
  182. attributeEntry(attributes, "Autom. Updates", showAutoupdate(d))
  183. attributeEntry(attributes, "Clients", showClients(d))
  184. el.appendChild(attributes)
  185. if (config.nodeInfos)
  186. config.nodeInfos.forEach( function (nodeInfo) {
  187. var h4 = document.createElement("h4")
  188. h4.textContent = nodeInfo.name
  189. el.appendChild(h4)
  190. el.appendChild(showStatImg(nodeInfo, d.nodeinfo.node_id))
  191. })
  192. if (d.neighbours.length > 0) {
  193. var h3 = document.createElement("h3")
  194. h3.textContent = "Nachbarknoten (" + d.neighbours.length + ")"
  195. el.appendChild(h3)
  196. var table = document.createElement("table")
  197. var thead = document.createElement("thead")
  198. var tr = document.createElement("tr")
  199. var th1 = document.createElement("th")
  200. th1.textContent = "Knoten"
  201. th1.classList.add("sort-default")
  202. tr.appendChild(th1)
  203. var th2 = document.createElement("th")
  204. th2.textContent = "TQ"
  205. tr.appendChild(th2)
  206. var th3 = document.createElement("th")
  207. th3.textContent = "Entfernung"
  208. tr.appendChild(th3)
  209. thead.appendChild(tr)
  210. table.appendChild(thead)
  211. var tbody = document.createElement("tbody")
  212. d.neighbours.forEach( function (d) {
  213. var tr = document.createElement("tr")
  214. var td1 = document.createElement("td")
  215. var a1 = document.createElement("a")
  216. a1.classList.add("hostname")
  217. a1.textContent = d.node.nodeinfo.hostname
  218. a1.href = "#"
  219. a1.onclick = router.node(d.node)
  220. td1.appendChild(a1)
  221. if (d.link.vpn)
  222. td1.appendChild(document.createTextNode(" (VPN)"))
  223. if (d.link.backbone)
  224. td1.appendChild(document.createTextNode(" (Wireless Backbone)"))
  225. if (has_location(d.node)) {
  226. var span = document.createElement("span")
  227. span.classList.add("icon")
  228. span.classList.add("ion-location")
  229. td1.appendChild(span)
  230. }
  231. tr.appendChild(td1)
  232. var td2 = document.createElement("td")
  233. var a2 = document.createElement("a")
  234. a2.href = "#"
  235. a2.textContent = showTq(d.link)
  236. a2.onclick = router.link(d.link)
  237. td2.appendChild(a2)
  238. tr.appendChild(td2)
  239. var td3 = document.createElement("td")
  240. var a3 = document.createElement("a")
  241. a3.href = "#"
  242. a3.textContent = showDistance(d.link)
  243. a3.onclick = router.link(d.link)
  244. td3.appendChild(a3)
  245. td3.setAttribute("data-sort", d.link.distance !== undefined ? -d.link.distance : 1)
  246. tr.appendChild(td3)
  247. tbody.appendChild(tr)
  248. })
  249. table.appendChild(tbody)
  250. new Tablesort(table)
  251. el.appendChild(table)
  252. }
  253. }
  254. })