1
0

cjdns.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. module("luci.controller.cjdns", package.seeall)
  2. cjdns = require "cjdns/init"
  3. dkjson = require "dkjson"
  4. function index()
  5. if not nixio.fs.access("/etc/config/cjdns") then
  6. return
  7. end
  8. entry({"admin", "services", "cjdns"},
  9. cbi("cjdns/overview"), _("cjdns")).dependent = true
  10. entry({"admin", "services", "cjdns", "overview"},
  11. cbi("cjdns/overview"), _("Overview"), 1).leaf = false
  12. entry({"admin", "services", "cjdns", "peering"},
  13. cbi("cjdns/peering"), _("Peers"), 2).leaf = false
  14. entry({"admin", "services", "cjdns", "iptunnel"},
  15. cbi("cjdns/iptunnel"), _("IP Tunnel"), 3).leaf = false
  16. entry({"admin", "services", "cjdns", "settings"},
  17. cbi("cjdns/settings"), _("Settings"), 4).leaf = false
  18. entry({"admin", "services", "cjdns", "cjdrouteconf"},
  19. cbi("cjdns/cjdrouteconf"), _("cjdroute.conf"), 5).leaf = false
  20. entry({"admin", "services", "cjdns", "peers"}, call("act_peers")).leaf = true
  21. entry({"admin", "services", "cjdns", "ping"}, call("act_ping")).leaf = true
  22. end
  23. function act_peers()
  24. require("cjdns/uci")
  25. admin = cjdns.uci.makeInterface()
  26. local page = 0
  27. local peers = {}
  28. while page do
  29. local response, err = admin:auth({
  30. q = "InterfaceController_peerStats",
  31. page = page
  32. })
  33. if err or response.error then
  34. luci.http.status(502, "Bad Gateway")
  35. luci.http.prepare_content("application/json")
  36. luci.http.write_json({ err = err, response = response })
  37. return
  38. end
  39. for i,peer in pairs(response.peers) do
  40. peer.ipv6 = publictoip6(peer.publicKey)
  41. if peer.user == nil then
  42. peer.user = ''
  43. uci.cursor():foreach("cjdns", "udp_peer", function(udp_peer)
  44. if peer.publicKey == udp_peer.public_key then
  45. peer.user = udp_peer.user
  46. end
  47. end)
  48. end
  49. peers[#peers + 1] = peer
  50. end
  51. if response.more then
  52. page = page + 1
  53. else
  54. page = nil
  55. end
  56. end
  57. luci.http.status(200, "OK")
  58. luci.http.prepare_content("application/json")
  59. luci.http.write_json(peers)
  60. end
  61. function act_ping()
  62. require("cjdns/uci")
  63. admin = cjdns.uci.makeInterface()
  64. local response, err = admin:auth({
  65. q = "SwitchPinger_ping",
  66. path = luci.http.formvalue("label"),
  67. timeout = tonumber(luci.http.formvalue("timeout"))
  68. })
  69. if err or response.error then
  70. luci.http.status(502, "Bad Gateway")
  71. luci.http.prepare_content("application/json")
  72. luci.http.write_json({ err = err, response = response })
  73. return
  74. end
  75. luci.http.status(200, "OK")
  76. luci.http.prepare_content("application/json")
  77. luci.http.write_json(response)
  78. end
  79. function publictoip6(publicKey)
  80. local process = io.popen("/usr/bin/publictoip6 " .. publicKey, "r")
  81. local ipv6 = process:read()
  82. process:close()
  83. return ipv6
  84. end