From c981a1c45167b912aaf96fdce4fc0125c1180292 Mon Sep 17 00:00:00 2001 From: dman776 Date: Tue, 30 Apr 2019 20:29:17 -0500 Subject: [PATCH] feature: api - added current dhcp leases (#394) * feature: api - added current dhcp leases * move dhcp info to secure page * changed filename --- files/etc/httpd.conf | 1 + files/usr/lib/lua/aredn/info.lua | 21 +++++++ files/www/cgi-bin/apiprotected | 96 ++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100755 files/www/cgi-bin/apiprotected diff --git a/files/etc/httpd.conf b/files/etc/httpd.conf index 53ae31ce..c4373cfb 100644 --- a/files/etc/httpd.conf +++ b/files/etc/httpd.conf @@ -6,3 +6,4 @@ /cgi-bin/supporttool:root:$p$root /cgi-bin/scan:root:$p$root /cgi-bin/advancedconfig:root:$p$root +/cgi-bin/apiprotected:root:$p$root diff --git a/files/usr/lib/lua/aredn/info.lua b/files/usr/lib/lua/aredn/info.lua index ead933d3..6c923749 100755 --- a/files/usr/lib/lua/aredn/info.lua +++ b/files/usr/lib/lua/aredn/info.lua @@ -425,6 +425,27 @@ function model.getDefaultGW() return gw end +------------------------------------- +-- Returns Table of current DHCP leases +------------------------------------- +function model.getCurrentDHCPLeases() + local lines={} + local leases={} + local filename="/tmp/dhcp.leases" + if file_exists(filename) then + for line in io.lines(filename) do table.insert(lines,line) end + for n, l in pairs(lines) do + local lease={} + local data=l:splitWhiteSpace() + lease["mac"]=data[2] + lease["ip"]=data[3] + lease["host"]=data[4] + table.insert(leases, lease) + end + end + return leases +end + ------------------------------------- -- Returns Local Hosts ------------------------------------- diff --git a/files/www/cgi-bin/apiprotected b/files/www/cgi-bin/apiprotected new file mode 100755 index 00000000..edaadfa9 --- /dev/null +++ b/files/www/cgi-bin/apiprotected @@ -0,0 +1,96 @@ +#!/usr/bin/lua +--[[ + + Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks + Copyright (C) 2019 Darryl Quinn + See Contributors file for additional contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation version 3 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + Additional Terms: + + Additional use restrictions exist on the AREDN(TM) trademark and logo. + See AREDNLicense.txt for more info. + + Attributions to the AREDN Project must be retained in the source code. + If importing this code into a new or existing project attribution + to the AREDN project must be added to the source code. + + You must not misrepresent the origin of the material contained within. + + Modified versions must be modified to attribute to the original source + and be marked in reasonable ways as differentiate it from the original + version + +--]] + +require("uci") +require("aredn.uci") +require("aredn.utils") +require("aredn.http") +local aredn_olsr = require("aredn.olsr") +local aredn_info = require("aredn.info") +require("nixio") +local json = require("luci.jsonc") +require("iwinfo") + +-- Function extensions +os.capture = capture + +-- ==== MAIN ===== +ctx = uci.cursor() +if not ctx then + error("Failed to get uci cursor") +end + +info={} + +-- get/process query string +local qsset={} +if (arg[1]==nil and arg[2]==nil) then + qs=nixio.getenv("QUERY_STRING") + if qs~="" then + qsset=parseQueryString(qs) + else + -- maybe default to a help page + qsset["apiprotected"]="help" + end +else + qsset[arg[1]]=arg[2] +end + +info['topics']={} + +for topic, comps in pairs(qsset) do + if not setContains(info['topics'],topic) then + info['topics'][topic]={} + end + + if topic=="apiprotected" then + info['topics'][topic]=nil + info['api_help']="AREDN Protected API. This API's primary function is to be used by the Web UI and calls are protected via uhttpd" + elseif page=="dhcp" then + for i,comp in pairs(comps:split(',')) do + if comp=="currentleases" then + info['topics'][topic][comp]=aredn_info.getCurrentDHCPLeases() + end + end + end +end + +-- Output the HTTP header for JSON +-- json_header() +json_header() + +-- Output the info table as json +print(json.stringify(info,true)) \ No newline at end of file