how_to_use _Lua scripts for_action by deepseek

heson4 12 views 6 slides Mar 26, 2025
Slide 1
Slide 1 of 6
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6

About This Presentation

It's a description for the lua script for automation


Slide Content

使用 Lua scripts 判斷 ("Status": "display-2fa-qrcode", "QRCode": ("secret": response
code =200 後才允許繼續存取 https://example.com/abc.cgi ,請幫忙產生 Lua scripts
总结来说,用户需要的 Lua脚本大致流程如下:
1. 发送HTTP GET请求到指定的 URL(可能是另一个服务端点)。
2. 检查响应状态码是否为 200。
3. 解析响应体的 JSON。
4. 检查是否存在 Status字段且值为 display-2fa-qrcode,以及QRCode.secret是否
存在。
5. 如果满足,允许继续访问(例如,代理请求到 abc.cgi,或者返回成功);否
则,拒绝访问(例如返回 403)。
在OpenResty环境中,可能的实现方式:
使用access_by_lua阶段,在客户端请求到达时,先执行该检查,再决定是否允
许继续。
例如:
location /abc.cgi {
access_by_lua_block {
local http = require "resty.http"
local cjson = require "cjson.safe" -- 使用safe以避免异常
-- 发送请求到检查状态的 URL
local httpc = http.new()
local res, err = httpc:request_uri("https://example.com/status-endpoint", {
ssl_verify = false, -- 如果需要忽略 SSL验证
method = "GET"
})

if not res then
ngx.log(ngx.ERR, "请求状态端点失败 : ", err)
return ngx.exit(500)
end
-- 检查状态码是否为 200
if res.status ~= 200 then
ngx.exit(403)
end
-- 解析JSON
local data, err = cjson.decode(res.body)
if not data then
ngx.log(ngx.ERR, "JSON解析失败 : ", err)
return ngx.exit(500)
end
-- 检查需要的字段
if data.Status ~= "display-2fa-qrcode" or not data.QRCode or not data.QRCode.secret
then
ngx.exit(403)
end
-- 如果所有条件满足,允许继续处理请求到 abc.cgi
-- 这里不需要额外动作,因为 access阶段通过后会继续执行 proxy_pass等
}
proxy_pass https://example.com/abc.cgi;
}

但这里可能需要根据具体配置调整,比如是否需要内部重定向,或者直接代理
转发。但在这个例子中,当访问 /abc.cgi时,会先执行 access_by_lua_block中的
代码,发送请求到 status-endpoint,检查条件,如果满足,则继续执行
proxy_pass到目标URL;否则返回 403。
不过,用户的问题中提到的 URL是https://example.com/abc.cgi,可能这个脚本
是在另一个服务中,用来控制是否允许请求该 URL。或者,可能需要将逻辑放
在请求abc.cgi之前的一个步骤,比如在访问某个路径时进行验证,然后重定向
到abc.cgi。
另外,需要考虑是否要保留原始请求的参数、方法等,或者是否需要将请求转
发到abc.cgi,或者仅仅是允许继续处理。如果使用 ngx.exec或ngx.redirect,可
能需要调整。
可能还需要处理多次重定向的问题,或者会话问题。例如,用户是否已经完成
2FA验证,可能需要会话标识,但用户的问题中没有提到这点,可能只需要在
每次请求时都进行验证。
总结,用户需要的是一个 Lua脚本,在 OpenResty环境下,当访问某个 URL时,
先检查另一个 URL的响应是否符合特定条件,符合则允许继续,否则拒绝。因
此,生成的脚本可能类似上面的示例,使用 lua-resty-http库发送请求,检查状
态码和JSON内容,然后决定是否允许访问。
以下是用於 OpenResty 的 Lua 腳本實現,會在通過 2FA 二維碼驗證條件後允許
存取指定 URL:
lua
复制
local http = require "resty.http"
local cjson = require "cjson.safe"
-- 發送請求到 2FA 驗證端點
local function verify_2fa_condition()
local httpc = http.new()

-- 替換成實際的 2FA 檢查 URL
local res, err = httpc:request_uri("https://your-auth-server/2fa-check", {
method = "GET",
ssl_verify = false, -- 根據需要啟用 SSL 驗證
headers = {
["Content-Type"] = "application/json",
-- 添加需要的 headers (如 Authorization)
}
})
if not res then
ngx.log(ngx.ERR, "2FA 驗證請求失敗 : ", err)
return false
end
-- 檢查 HTTP 狀態碼
if res.status ~= 200 then
ngx.log(ngx.WARN, "非 200 狀態碼: ", res.status)
return false
end
-- 解析 JSON 響應
local data = cjson.decode(res.body)

if not data then
ngx.log(ngx.ERR, "JSON 解析失敗 ")
return false
end
-- 驗證響應內容
return data.Status == "display-2fa-qrcode"
and data.QRCode
and data.QRCode.secret ~= nil
end
-- 主邏輯
if verify_2fa_condition() then
-- 條件滿足時繼續存取目標 URL
ngx.exec("/proxy-abc-cgi") -- 需要在 nginx 配置對應的 location

-- 或者直接代理請求
-- return ngx.redirect("https://example.com/abc.cgi")
else
ngx.status = ngx.HTTP_FORBIDDEN
ngx.say("2FA 驗證未通過 ")
ngx.exit(ngx.HTTP_FORBIDDEN)
end
需搭配以下 nginx 配置使用:

nginx
复制
location /proxy-abc-cgi {
internal;
proxy_pass https://example.com/abc.cgi;
proxy_set_header Host example.com;
proxy_ssl_verify off; # 根據需要調整 SSL 驗證
}
location /your-secure-endpoint {
access_by_lua_block {
-- 在此處插
入上述
Lua 代碼
}
}
主要功能
說明:
1.使用 lua-resty-http 發送驗證請求
2.嚴格
檢查
HTTP 狀態碼需

200
3.使用 cjson.safe 解析 JSON 避免
異常
4.多
層次驗證響應 數據結構
5.條件滿足時通過內部 location 轉
發請求
6.驗證失敗時返回 403 狀態碼