| 1 | --[[ |
|---|
| 2 | LuCI - Lua Configuration Interface |
|---|
| 3 | |
|---|
| 4 | Copyright 2008 Steven Barth <steven@midlink.org> |
|---|
| 5 | Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net> |
|---|
| 6 | |
|---|
| 7 | Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 8 | you may not use this file except in compliance with the License. |
|---|
| 9 | You may obtain a copy of the License at |
|---|
| 10 | |
|---|
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | |
|---|
| 13 | $Id$ |
|---|
| 14 | ]]-- |
|---|
| 15 | |
|---|
| 16 | module("luci.tools.webadmin", package.seeall) |
|---|
| 17 | require("luci.model.uci") |
|---|
| 18 | require("luci.sys") |
|---|
| 19 | require("luci.ip") |
|---|
| 20 | |
|---|
| 21 | function byte_format(byte) |
|---|
| 22 | local suff = {"B", "KB", "MB", "GB", "TB"} |
|---|
| 23 | for i=1, 5 do |
|---|
| 24 | if byte > 1024 and i < 5 then |
|---|
| 25 | byte = byte / 1024 |
|---|
| 26 | else |
|---|
| 27 | return string.format("%.2f %s", byte, suff[i]) |
|---|
| 28 | end |
|---|
| 29 | end |
|---|
| 30 | end |
|---|
| 31 | |
|---|
| 32 | function date_format(secs) |
|---|
| 33 | local suff = {"min", "h", "d"} |
|---|
| 34 | local mins = 0 |
|---|
| 35 | local hour = 0 |
|---|
| 36 | local days = 0 |
|---|
| 37 | if secs > 60 then |
|---|
| 38 | mins = math.floor(secs / 60) |
|---|
| 39 | secs = secs % 60 |
|---|
| 40 | end |
|---|
| 41 | |
|---|
| 42 | if mins > 60 then |
|---|
| 43 | hour = math.floor(mins / 60) |
|---|
| 44 | mins = mins % 60 |
|---|
| 45 | end |
|---|
| 46 | |
|---|
| 47 | if hour > 24 then |
|---|
| 48 | days = math.floor(hour / 24) |
|---|
| 49 | hour = hour % 24 |
|---|
| 50 | end |
|---|
| 51 | |
|---|
| 52 | if days > 0 then |
|---|
| 53 | return string.format("%dd %02dh %02dmin %02ds", days, hour, mins, secs) |
|---|
| 54 | else |
|---|
| 55 | return string.format("%02dh %02dmin %02ds", hour, mins, secs) |
|---|
| 56 | end |
|---|
| 57 | end |
|---|
| 58 | |
|---|
| 59 | function network_get_addresses(net) |
|---|
| 60 | luci.model.uci.load_state("network") |
|---|
| 61 | local addr = {} |
|---|
| 62 | local ipv4 = luci.model.uci.get("network", net, "ipaddr") |
|---|
| 63 | local mav4 = luci.model.uci.get("network", net, "netmask") |
|---|
| 64 | local ipv6 = luci.model.uci.get("network", net, "ip6addr") |
|---|
| 65 | |
|---|
| 66 | if ipv4 and mav4 then |
|---|
| 67 | ipv4 = luci.ip.IPv4(ipv4, mav4) |
|---|
| 68 | |
|---|
| 69 | if ipv4 then |
|---|
| 70 | table.insert(addr, ipv4:string()) |
|---|
| 71 | end |
|---|
| 72 | end |
|---|
| 73 | |
|---|
| 74 | if ipv6 then |
|---|
| 75 | table.insert(addr, ipv6) |
|---|
| 76 | end |
|---|
| 77 | |
|---|
| 78 | luci.model.uci.foreach("network", "alias", |
|---|
| 79 | function (section) |
|---|
| 80 | if section.interface == net then |
|---|
| 81 | if section.ipaddr and section.netmask then |
|---|
| 82 | local ipv4 = luci.ip.IPv4(section.ipaddr, section.netmask) |
|---|
| 83 | |
|---|
| 84 | if ipv4 then |
|---|
| 85 | table.insert(addr, ipv4:string()) |
|---|
| 86 | end |
|---|
| 87 | end |
|---|
| 88 | |
|---|
| 89 | if section.ip6addr then |
|---|
| 90 | table.insert(addr, section.ip6addr) |
|---|
| 91 | end |
|---|
| 92 | end |
|---|
| 93 | end |
|---|
| 94 | ) |
|---|
| 95 | |
|---|
| 96 | return addr |
|---|
| 97 | end |
|---|
| 98 | |
|---|
| 99 | function cbi_add_networks(field) |
|---|
| 100 | luci.model.uci.foreach("network", "interface", |
|---|
| 101 | function (section) |
|---|
| 102 | if section[".name"] ~= "loopback" then |
|---|
| 103 | field:value(section[".name"]) |
|---|
| 104 | end |
|---|
| 105 | end |
|---|
| 106 | ) |
|---|
| 107 | field.titleref = luci.dispatcher.build_url("admin", "network", "network") |
|---|
| 108 | end |
|---|
| 109 | |
|---|
| 110 | function cbi_add_knownips(field) |
|---|
| 111 | for i, dataset in ipairs(luci.sys.net.arptable()) do |
|---|
| 112 | field:value(dataset["IP address"]) |
|---|
| 113 | end |
|---|
| 114 | end |
|---|
| 115 | |
|---|
| 116 | function network_get_zones(net) |
|---|
| 117 | if not luci.model.uci.load_state("firewall") then |
|---|
| 118 | return nil |
|---|
| 119 | end |
|---|
| 120 | |
|---|
| 121 | local zones = {} |
|---|
| 122 | |
|---|
| 123 | luci.model.uci.foreach("firewall", "zone", |
|---|
| 124 | function (section) |
|---|
| 125 | local znet = section.network or section.name |
|---|
| 126 | if luci.util.contains(luci.util.split(znet, " "), net) then |
|---|
| 127 | table.insert(zones, section.name) |
|---|
| 128 | end |
|---|
| 129 | end |
|---|
| 130 | ) |
|---|
| 131 | |
|---|
| 132 | return zones |
|---|
| 133 | end |
|---|
| 134 | |
|---|
| 135 | function firewall_find_zone(name) |
|---|
| 136 | local find |
|---|
| 137 | |
|---|
| 138 | luci.model.uci.foreach("firewall", "zone", |
|---|
| 139 | function (section) |
|---|
| 140 | if section.name == name then |
|---|
| 141 | find = section[".name"] |
|---|
| 142 | end |
|---|
| 143 | end |
|---|
| 144 | ) |
|---|
| 145 | |
|---|
| 146 | return find |
|---|
| 147 | end |
|---|
| 148 | |
|---|
| 149 | function iface_get_network(iface) |
|---|
| 150 | luci.model.uci.load_state("network") |
|---|
| 151 | local net |
|---|
| 152 | |
|---|
| 153 | luci.model.uci.foreach("network", "interface", |
|---|
| 154 | function (section) |
|---|
| 155 | local ifname = luci.model.uci.get( |
|---|
| 156 | "network", section[".name"], "ifname" |
|---|
| 157 | ) |
|---|
| 158 | |
|---|
| 159 | if iface == ifname then |
|---|
| 160 | net = section[".name"] |
|---|
| 161 | end |
|---|
| 162 | end |
|---|
| 163 | ) |
|---|
| 164 | |
|---|
| 165 | return net |
|---|
| 166 | end |
|---|