luci-app-cjdns: luci admin support for cjdns-v20.2 (#394)
[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 local peertable = peerstats_join(peer.addr)
55 peer.ipv6 = peertable['ipv6']
56 peer.version = peertable['version']
57 peer.label = peertable['label']
58 peer.pubkey = peertable['pubkey']
59 uci.cursor():foreach("cjdns", "udp_peer", function(udp_peer)
60 if peer.pubkey == udp_peer.public_key then
61 peer.user = udp_peer.user
62 end
63 end)
64 peers[#peers + 1] = peer
65 end
66
67 if response.more then
68 page = page + 1
69 else
70 page = nil
71 end
72 end
73
74 luci.http.status(200, "OK")
75 luci.http.prepare_content("application/json")
76 luci.http.write_json(peers)
77 end
78
79 function act_ping()
80 require("cjdns/uci")
81 admin = cjdns.uci.makeInterface()
82
83 local response, err = admin:auth({
84 q = "SwitchPinger_ping",
85 path = luci.http.formvalue("label"),
86 timeout = tonumber(luci.http.formvalue("timeout"))
87 })
88
89 if err or response.error then
90 luci.http.status(502, "Bad Gateway")
91 luci.http.prepare_content("application/json")
92 luci.http.write_json({ err = err, response = response })
93 return
94 end
95
96 luci.http.status(200, "OK")
97 luci.http.prepare_content("application/json")
98 luci.http.write_json(response)
99 end
100
101 function peerstats_join(addrLine)
102 local pubkey = addrLine:sub(addrLine:len() - 53)
103 local process = io.popen("/usr/bin/publictoip6 " .. pubkey, "r")
104 local ipv6 = process:read()
105 local label = 'label'
106 process:close()
107 local version = addrLine:match("^(v%w+)%.") or 'v0'
108 local label = addrLine:sub(version:len() + 2, version:len() + 20)
109 return { pubkey=pubkey, ipv6=ipv6, label=label, version=version }
110 end