mirror of
https://github.com/Ysurac/openmptcprouter-feeds.git
synced 2025-02-13 11:01:50 +00:00
Add dhcp_lease_common from old OpenWrt release
This commit is contained in:
parent
94ff0f0692
commit
ee49175152
1 changed files with 106 additions and 1 deletions
|
@ -5,6 +5,7 @@ local json = require("luci.json")
|
|||
local fs = require("nixio.fs")
|
||||
local net = require "luci.model.network".init()
|
||||
local ucic = luci.model.uci.cursor()
|
||||
local ipc = require "luci.ip"
|
||||
module("luci.controller.openmptcprouter", package.seeall)
|
||||
|
||||
function index()
|
||||
|
@ -737,6 +738,110 @@ function get_device(interface)
|
|||
return dump['l3_device']
|
||||
end
|
||||
|
||||
-- This function come from modules/luci-bbase/luasrc/tools/status.lua from old OpenWrt
|
||||
-- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
|
||||
-- Licensed to the public under the Apache License 2.0.
|
||||
local function dhcp_leases_common(family)
|
||||
local rv = { }
|
||||
local nfs = require "nixio.fs"
|
||||
local sys = require "luci.sys"
|
||||
local leasefile = "/tmp/dhcp.leases"
|
||||
|
||||
ucic:foreach("dhcp", "dnsmasq",
|
||||
function(s)
|
||||
if s.leasefile and nfs.access(s.leasefile) then
|
||||
leasefile = s.leasefile
|
||||
return false
|
||||
end
|
||||
end)
|
||||
|
||||
local fd = io.open(leasefile, "r")
|
||||
if fd then
|
||||
while true do
|
||||
local ln = fd:read("*l")
|
||||
if not ln then
|
||||
break
|
||||
else
|
||||
local ts, mac, ip, name, duid = ln:match("^(%d+) (%S+) (%S+) (%S+) (%S+)")
|
||||
local expire = tonumber(ts) or 0
|
||||
if ts and mac and ip and name and duid then
|
||||
if family == 4 and not ip:match(":") then
|
||||
rv[#rv+1] = {
|
||||
expires = (expire ~= 0) and os.difftime(expire, os.time()),
|
||||
macaddr = ipc.checkmac(mac) or "00:00:00:00:00:00",
|
||||
ipaddr = ip,
|
||||
hostname = (name ~= "*") and name
|
||||
}
|
||||
elseif family == 6 and ip:match(":") then
|
||||
rv[#rv+1] = {
|
||||
expires = (expire ~= 0) and os.difftime(expire, os.time()),
|
||||
ip6addr = ip,
|
||||
duid = (duid ~= "*") and duid,
|
||||
hostname = (name ~= "*") and name
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
fd:close()
|
||||
end
|
||||
|
||||
local lease6file = "/tmp/hosts/odhcpd"
|
||||
ucic:foreach("dhcp", "odhcpd",
|
||||
function(t)
|
||||
if t.leasefile and nfs.access(t.leasefile) then
|
||||
lease6file = t.leasefile
|
||||
return false
|
||||
end
|
||||
end)
|
||||
local fd = io.open(lease6file, "r")
|
||||
if fd then
|
||||
while true do
|
||||
local ln = fd:read("*l")
|
||||
if not ln then
|
||||
break
|
||||
else
|
||||
local iface, duid, iaid, name, ts, id, length, ip = ln:match("^# (%S+) (%S+) (%S+) (%S+) (-?%d+) (%S+) (%S+) (.*)")
|
||||
local expire = tonumber(ts) or 0
|
||||
if ip and iaid ~= "ipv4" and family == 6 then
|
||||
rv[#rv+1] = {
|
||||
expires = (expire >= 0) and os.difftime(expire, os.time()),
|
||||
duid = duid,
|
||||
ip6addr = ip,
|
||||
hostname = (name ~= "-") and name
|
||||
}
|
||||
elseif ip and iaid == "ipv4" and family == 4 then
|
||||
rv[#rv+1] = {
|
||||
expires = (expire >= 0) and os.difftime(expire, os.time()),
|
||||
macaddr = sys.net.duid_to_mac(duid) or "00:00:00:00:00:00",
|
||||
ipaddr = ip,
|
||||
hostname = (name ~= "-") and name
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
fd:close()
|
||||
end
|
||||
|
||||
if family == 6 then
|
||||
local _, lease
|
||||
local hosts = sys.net.host_hints()
|
||||
for _, lease in ipairs(rv) do
|
||||
local mac = sys.net.duid_to_mac(lease.duid)
|
||||
local host = mac and hosts[mac]
|
||||
if host then
|
||||
if not lease.name then
|
||||
lease.host_hint = host.name or host.ipv4 or host.ipv6
|
||||
elseif host.name and lease.hostname ~= host.name then
|
||||
lease.host_hint = host.name
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return rv
|
||||
end
|
||||
|
||||
function interfaces_status()
|
||||
local ut = require "luci.util"
|
||||
local mArray = ut.ubus("openmptcprouter", "status", {}) or {_=0}
|
||||
|
@ -744,7 +849,7 @@ function interfaces_status()
|
|||
if mArray ~= nil and mArray.openmptcprouter ~= nil then
|
||||
mArray.openmptcprouter["remote_addr"] = luci.http.getenv("REMOTE_ADDR") or ""
|
||||
mArray.openmptcprouter["remote_from_lease"] = false
|
||||
local leases=tools.dhcp_leases()
|
||||
local leases=dhcp_leases_common(4)
|
||||
for _, value in pairs(leases) do
|
||||
if value["ipaddr"] == mArray.openmptcprouter["remote_addr"] then
|
||||
mArray.openmptcprouter["remote_from_lease"] = true
|
||||
|
|
Loading…
Reference in a new issue