Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add api #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion files/wifidog-ng.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ config gateway
option checkinterval 30
option client_timeout 5
option temppass_time 30
option apikey ''

config server
option host 'authserver.com'
Expand All @@ -17,4 +18,4 @@ config server
option portal_path 'portal'
option msg_path 'gw_message.php'
option ping_path 'ping'
option auth_path 'auth'
option auth_path 'auth'
61 changes: 55 additions & 6 deletions files/wifidog-ng/auth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ local httpd = require "wifidog-ng.httpd"
local http = require "socket.http"
local util = require "wifidog-ng.util"
local config = require "wifidog-ng.config"
local json = require "luci.json"

local M = {}

Expand Down Expand Up @@ -212,11 +213,6 @@ local function http_callback_ctl(req)
new_term(ip, mac, token)
end
end
elseif op == "kick" then
local mac = params["mac"]
if mac then
deny_user(mac)
end
elseif op == "reload" then
config.reload()
end
Expand All @@ -230,14 +226,67 @@ local function http_callback_ctl(req)
req:send(content)
end

local function http_callback_api(req)
local params = req.params

local content = nil
local apikey = config.get().apikey

if apikey and #apikey > 0 then
local key = params["key"]
if not key then
content = json.encode({code = 10001, msg = "Api key is empty"})
elseif key ~= config.get().apikey then
content = json.encode({code = 10002, msg = "Api key is error"})
end
if content then
local headers = {
["Content-Type"] = "application/json",
["Content-Length"] = #content
}
req:send_head(200, headers)
req:send(content)
return
end
end

local op = params["op"]

if op == "kick" then
local mac = params["mac"]
if mac then
deny_user(mac)
end
content = json.encode({code = 0})
elseif op == "add" then
local mac = params["mac"]
if mac then
os.execute("ipset add wifidog-ng-mac " .. mac)
end
content = json.encode({code = 0})
elseif op == "show" then
content = json.encode({code = 0, terms = M.get_terms()})
else
content = json.encode({code = 10003, msg = "no such operate"})
end

local headers = {
["Content-Type"] = "application/json",
["Content-Length"] = #content
}
req:send_head(200, headers)
req:send(content)
end

function M.init()
local cfg = config.get()

local handlers = {
["404"] = http_callback_404,
["/wifidog/temppass"] = http_callback_temppass,
["/wifidog/auth"] = http_callback_auth,
["/wifidog/ctl"] = http_callback_ctl
["/wifidog/ctl"] = http_callback_ctl,
["/wifidog/api"] = http_callback_api
}

httpd.new(cfg.gw_address, cfg.gw_port, handlers)
Expand Down
1 change: 1 addition & 0 deletions files/wifidog-ng/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function M.parse()
cfg.temppass_time = tonumber(temppass_time)
cfg.gw_address = s.address
cfg.gw_id = s.id
cfg.apikey = s.apikey

local st = util.ubus("network.interface." .. interface, "status")
cfg.gw_ifname = st.device
Expand Down