1 module("luci.controller.cjdns", package.seeall)
3 cjdns = require "cjdns/init"
4 dkjson = require "dkjson"
7 if not nixio.fs.access("/etc/config/cjdns") then
11 entry({"admin", "services", "cjdns"},
12 cbi("cjdns/overview"), _("cjdns")).dependent = true
14 entry({"admin", "services", "cjdns", "overview"},
15 cbi("cjdns/overview"), _("Overview"), 1).leaf = false
17 entry({"admin", "services", "cjdns", "peering"},
18 cbi("cjdns/peering"), _("Peers"), 2).leaf = false
20 entry({"admin", "services", "cjdns", "iptunnel"},
21 cbi("cjdns/iptunnel"), _("IP Tunnel"), 3).leaf = false
23 entry({"admin", "services", "cjdns", "settings"},
24 cbi("cjdns/settings"), _("Settings"), 4).leaf = false
26 entry({"admin", "services", "cjdns", "cjdrouteconf"},
27 cbi("cjdns/cjdrouteconf"), _("cjdroute.conf"), 5).leaf = false
29 entry({"admin", "services", "cjdns", "peers"}, call("act_peers")).leaf = true
30 entry({"admin", "services", "cjdns", "ping"}, call("act_ping")).leaf = true
35 admin = cjdns.uci.makeInterface()
41 local response, err = admin:auth({
42 q = "InterfaceController_peerStats",
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 })
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
64 peers[#peers + 1] = peer
74 luci.http.status(200, "OK")
75 luci.http.prepare_content("application/json")
76 luci.http.write_json(peers)
81 admin = cjdns.uci.makeInterface()
83 local response, err = admin:auth({
84 q = "SwitchPinger_ping",
85 path = luci.http.formvalue("label"),
86 timeout = tonumber(luci.http.formvalue("timeout"))
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 })
96 luci.http.status(200, "OK")
97 luci.http.prepare_content("application/json")
98 luci.http.write_json(response)
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'
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 }