local headers=ngx.req.get_headers()
local ip=headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr or "0.0.0.0"
kong.log.notice('Canary request ip1 is ',ip);
local binary_remote_addr = ngx.var.binary_remote_addr
kong.log.notice('Canary request ip2 is ',binary_remote_addr);
get_client_ip();
local client_ip = get_client_ip();
local url = "http://106.54.100.29:10005/user/status?uname=hml"
local res = httpGet.getUrl(url)
if res == nil then
kong.log.notice("请求GrayRule异常:",ngx.WARN,"非200状态,code:", code);
end
if res then
-- 从灰度规则服务判断当当前请求是灰度IP
kong.log.notice("请求GrayRule成功,当前IP灰度状态:",res);
return true, nil;
else
kong.log.notice("请求GrayRule成功,当前IP灰度状态:",res);
return false, nil;
end
local http = require("socket.http")
local ltn12 = require("ltn12")
local httpGet = {}
function http.get(u)
local t = {}
local r, c, h = http.request{
url = u,
sink = ltn12.sink.table(t)}
return r, c, h, table.concat(t)
end
-- 发送http.get请求,返回响应结果
function httpGet:getUrl(url)
r,c,h,body = http.get(url)
if c~= 200 then
kong.log.notice('ErrorCode: ',c);
return
else
kong.log.notice('返回结果: ',body);
return body
end
end
return httpGet;
-- url = "http://www.baidu.com"
-- body = wb_getUrl(url)
-- if body ~= nil then
-- writeFile("baidu.html", body)
-- end