summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-base/luasrc/tools/webadmin.lua
blob: 8273175de7d1398aefdc47f5651d6676292893d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
-- Copyright 2008 Steven Barth <steven@midlink.org>
-- Copyright 2008-2015 Jo-Philipp Wich <jow@openwrt.org>
-- Licensed to the public under the Apache License 2.0.

module("luci.tools.webadmin", package.seeall)

local util = require "luci.util"
local uci  = require "luci.model.uci"
local ip   = require "luci.ip"

function byte_format(byte)
	local suff = {"B", "KB", "MB", "GB", "TB"}
	for i=1, 5 do
		if byte > 1024 and i < 5 then
			byte = byte / 1024
		else
			return string.format("%.2f %s", byte, suff[i]) 
		end 
	end
end

function date_format(secs)
	local suff = {"min", "h", "d"}
	local mins = 0
	local hour = 0
	local days = 0
	
	secs = math.floor(secs)
	if secs > 60 then
		mins = math.floor(secs / 60)
		secs = secs % 60
	end
	
	if mins > 60 then
		hour = math.floor(mins / 60)
		mins = mins % 60
	end
	
	if hour > 24 then
		days = math.floor(hour / 24)
		hour = hour % 24
	end
	
	if days > 0 then
		return string.format("%.0fd %02.0fh %02.0fmin %02.0fs", days, hour, mins, secs)
	else
		return string.format("%02.0fh %02.0fmin %02.0fs", hour, mins, secs)
	end
end

function cbi_add_networks(field)
	uci.cursor():foreach("network", "interface",
		function (section)
			if section[".name"] ~= "loopback" then
				field:value(section[".name"])
			end
		end
	)
	field.titleref = luci.dispatcher.build_url("admin", "network", "network")
end

function cbi_add_knownips(field)
	local _, n
	for _, n in ipairs(ip.neighbors({ family = 4 })) do
		if n.dest then
			field:value(n.dest:string())
		end
	end
end

function firewall_find_zone(name)
	local find
	
	luci.model.uci.cursor():foreach("firewall", "zone", 
		function (section)
			if section.name == name then
				find = section[".name"]
			end
		end
	)
	
	return find
end

function iface_get_network(iface)
	local link = ip.link(tostring(iface))
	if link.master then
		iface = link.master
	end

	local cur = uci.cursor()
	local dump = util.ubus("network.interface", "dump", { })
	if dump then
		local _, net
		for _, net in ipairs(dump.interface) do
			if net.l3_device == iface or net.device == iface then
				-- cross check with uci to filter out @name style aliases
				local uciname = cur:get("network", net.interface, "ifname")
				if not uciname or uciname:sub(1, 1) ~= "@" then
					return net.interface
				end
			end
		end
	end
end