系统架构
测试站点
vray@openresty:/usr/local/openresty/nginx/conf$ cat nginx.conf
......
include stream/*.conf;
......
vray@openresty:/usr/local/openresty/nginx/conf$ cat stream/nstream.conf
stream {
server {
listen 2256;
preread_by_lua_file "conf/lua/ip_ports.lua";
proxy_pass 10.37.2.151:2256;
}
}
Lua处理数据
- 获取接口数据
local http_endpoint = "http://10.37.2.114:8000/api/get-users"
local auth_token = "cGxheU5hbWUiOiLogpbli"
-- 执行 curl 命令获取接口数据
local command = string.format('curl -X GET -H "Authorization: Bearer %s" %s', auth_token, http_endpoint)
local handle = io.popen(command)
local result = handle:read("*a")
handle:close()
-- 解析接口返回的 JSON 数据
local json = require("cjson")
local data = json.decode(result)
- 获取客户端IP和端口
-- 获取客户端 IP 地址和当前访问的端口
local client_ip = ngx.var.remote_addr
local port = ngx.var.server_port
- 判断信息是否匹配
local function in_array(array, value)
for _, v in ipairs(array) do
if v == value then
return true
end
end
return false
end
local matched = false
for _, item in ipairs(data) do
local ips = {}
local ports = {}
-- 从接口返回数据循环取出客户端ip,插入ips = {}; "([^,]+)"表示匹配除逗号以外的任意连续字符(非逗号字符)
for ip in string.gmatch(item.affiliation, "([^,]+)") do
table.insert(ips, ip)
end
-- 从接口返回数据循环取出客户端请求端口,插入ports = {}; "([^,]+)"表示匹配除逗号以外的任意连续字符(非逗号字符)
for p in string.gmatch(item.yammer, "([^,]+)") do
table.insert(ports, p)
end
-- 判断客户端ip和端口是否都匹配
if in_array(ips, client_ip) and in_array(ports, port) then
matched = true
break
end
end
if not matched then
ngx.exit(403)
end
测试
01/12/2023 09:50.25 /home/mobaxterm curl --connect-timeout 5 -vvv telnet://10.37.2.20:2256
* Trying 10.37.2.20:2256...
* Connected to 10.37.2.20 (10.37.2.20) port 2256 (#0)
* Closing connection 0
# 端口不通
- 修改user表的affiliation和yammer任一字段
01/12/2023 09:50.27 /home/mobaxterm curl --connect-timeout 5 -vvv telnet://10.37.2.20:2256
* Trying 10.37.2.20:2256...
* Connected to 10.37.2.20 (10.37.2.20) port 2256 (#0)
SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.7
# 端口连接成功
缺陷
到这里,系统架构图中除了蓝色的部分,其他功能已经实现了,不足之处还是需要改数据库,下一节,开始权限管理系统。
GZH
- 欢迎关注同名GZH"小红帽rh",获取更多最新分享。