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