node.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 showStatus(d) {
  33. return function (el) {
  34. el.classList.add(d.flags.online ? "online" : "offline")
  35. el.textContent = d.flags.online ? "online" : "offline, " + d.lastseen.fromNow(true)
  36. }
  37. }
  38. function showFirmware(d) {
  39. var release = dictGet(d.nodeinfo, ["software", "firmware", "release"])
  40. var base = dictGet(d.nodeinfo, ["software", "firmware", "base"])
  41. if (release === null || base === null)
  42. return undefined
  43. return release + " / " + base
  44. }
  45. function showUptime(d) {
  46. if (!("uptime" in d.statistics))
  47. return undefined
  48. return moment.duration(d.statistics.uptime, "seconds").humanize()
  49. }
  50. function showFirstseen(d) {
  51. if (!("firstseen" in d))
  52. return undefined
  53. return d.firstseen.fromNow(true)
  54. }
  55. function showClients(d) {
  56. if (!d.flags.online)
  57. return undefined
  58. return function (el) {
  59. el.appendChild(document.createTextNode(d.statistics.clients > 0 ? d.statistics.clients : "keine"))
  60. el.appendChild(document.createElement("br"))
  61. var span = document.createElement("span")
  62. span.classList.add("clients")
  63. span.textContent = " ".repeat(d.statistics.clients)
  64. el.appendChild(span)
  65. }
  66. }
  67. function showIPs(d) {
  68. var ips = dictGet(d.nodeinfo, ["network", "addresses"])
  69. if (ips === null)
  70. return undefined
  71. ips.sort()
  72. return function (el) {
  73. ips.forEach( function (ip, i) {
  74. var link = !ip.startsWith("fe80:")
  75. if (i > 0)
  76. el.appendChild(document.createElement("br"))
  77. if (link) {
  78. var a = document.createElement("a")
  79. a.href = "http://[" + ip + "]/"
  80. a.textContent = ip
  81. el.appendChild(a)
  82. } else
  83. el.appendChild(document.createTextNode(ip))
  84. })
  85. }
  86. }
  87. function showBar(className, v) {
  88. var span = document.createElement("span")
  89. span.classList.add("bar")
  90. span.classList.add(className)
  91. var bar = document.createElement("span")
  92. bar.style.width = (v * 100) + "%"
  93. span.appendChild(bar)
  94. var label = document.createElement("label")
  95. label.textContent = (Math.round(v * 100)) + " %"
  96. span.appendChild(label)
  97. return span
  98. }
  99. function showRAM(d) {
  100. if (!("memory_usage" in d.statistics))
  101. return undefined
  102. return function (el) {
  103. el.appendChild(showBar("memory-usage", d.statistics.memory_usage))
  104. }
  105. }
  106. function showAutoupdate(d) {
  107. var au = dictGet(d.nodeinfo, ["software", "autoupdater"])
  108. if (!au)
  109. return undefined
  110. return au.enabled ? "aktiviert (" + au.branch + ")" : "deaktiviert"
  111. }
  112. function showStatImg(o, nodeId) {
  113. var content, caption
  114. if (o.thumbnail) {
  115. content = document.createElement("img")
  116. content.src = o.thumbnail.replace("{NODE_ID}", nodeId)
  117. }
  118. if (o.caption) {
  119. caption = o.caption.replace("{NODE_ID}", nodeId)
  120. if (!content)
  121. content = document.createTextNode(caption)
  122. }
  123. var p = document.createElement("p")
  124. if (o.href) {
  125. var link = document.createElement("a")
  126. link.target = "_blank"
  127. link.href = o.href.replace("{NODE_ID}", nodeId)
  128. link.appendChild(content)
  129. if (caption && o.thumbnail)
  130. link.title = caption
  131. p.appendChild(link)
  132. } else
  133. p.appendChild(content)
  134. return p
  135. }
  136. return function(config, el, router, d) {
  137. var h2 = document.createElement("h2")
  138. h2.textContent = d.nodeinfo.hostname
  139. el.appendChild(h2)
  140. var attributes = document.createElement("table")
  141. attributes.classList.add("attributes")
  142. attributeEntry(attributes, "Status", showStatus(d))
  143. attributeEntry(attributes, "Koordinaten", showGeoURI(d))
  144. if (config.showContact)
  145. attributeEntry(attributes, "Kontakt", dictGet(d.nodeinfo, ["owner", "contact"]))
  146. attributeEntry(attributes, "Hardware", dictGet(d.nodeinfo, ["hardware", "model"]))
  147. attributeEntry(attributes, "Primäre MAC", dictGet(d.nodeinfo, ["network", "mac"]))
  148. attributeEntry(attributes, "Node ID", dictGet(d.nodeinfo, ["node_id"]))
  149. attributeEntry(attributes, "Firmware", showFirmware(d))
  150. attributeEntry(attributes, "Uptime", showUptime(d))
  151. attributeEntry(attributes, "Teil des Netzes", showFirstseen(d))
  152. attributeEntry(attributes, "Arbeitsspeicher", showRAM(d))
  153. attributeEntry(attributes, "IP Adressen", showIPs(d))
  154. attributeEntry(attributes, "Autom. Updates", showAutoupdate(d))
  155. attributeEntry(attributes, "Clients", showClients(d))
  156. el.appendChild(attributes)
  157. if (config.nodeInfos)
  158. config.nodeInfos.forEach( function (nodeInfo) {
  159. var h4 = document.createElement("h4")
  160. h4.textContent = nodeInfo.name
  161. el.appendChild(h4)
  162. el.appendChild(showStatImg(nodeInfo, d.nodeinfo.node_id))
  163. })
  164. if (d.neighbours.length > 0) {
  165. var h3 = document.createElement("h3")
  166. h3.textContent = "Nachbarknoten (" + d.neighbours.length + ")"
  167. el.appendChild(h3)
  168. var table = document.createElement("table")
  169. var thead = document.createElement("thead")
  170. var tr = document.createElement("tr")
  171. var th1 = document.createElement("th")
  172. th1.textContent = "Knoten"
  173. th1.classList.add("sort-default")
  174. tr.appendChild(th1)
  175. var th2 = document.createElement("th")
  176. th2.textContent = "TQ"
  177. tr.appendChild(th2)
  178. var th3 = document.createElement("th")
  179. th3.textContent = "Entfernung"
  180. tr.appendChild(th3)
  181. thead.appendChild(tr)
  182. table.appendChild(thead)
  183. var tbody = document.createElement("tbody")
  184. d.neighbours.forEach( function (d) {
  185. var tr = document.createElement("tr")
  186. var td1 = document.createElement("td")
  187. var a1 = document.createElement("a")
  188. a1.classList.add("hostname")
  189. a1.textContent = d.node.nodeinfo.hostname
  190. a1.href = "#"
  191. a1.onclick = router.node(d.node)
  192. td1.appendChild(a1)
  193. if (d.link.vpn)
  194. td1.appendChild(document.createTextNode(" (VPN)"))
  195. if (has_location(d.node)) {
  196. var span = document.createElement("span")
  197. span.classList.add("icon")
  198. span.classList.add("ion-location")
  199. td1.appendChild(span)
  200. }
  201. tr.appendChild(td1)
  202. var td2 = document.createElement("td")
  203. var a2 = document.createElement("a")
  204. a2.href = "#"
  205. a2.textContent = showTq(d.link)
  206. a2.onclick = router.link(d.link)
  207. td2.appendChild(a2)
  208. tr.appendChild(td2)
  209. var td3 = document.createElement("td")
  210. var a3 = document.createElement("a")
  211. a3.href = "#"
  212. a3.textContent = showDistance(d.link)
  213. a3.onclick = router.link(d.link)
  214. td3.appendChild(a3)
  215. td3.setAttribute("data-sort", d.link.distance !== undefined ? -d.link.distance : 1)
  216. tr.appendChild(td3)
  217. tbody.appendChild(tr)
  218. })
  219. table.appendChild(tbody)
  220. new Tablesort(table)
  221. el.appendChild(table)
  222. }
  223. }
  224. })