-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzrwoxy.lua
106 lines (78 loc) · 2.93 KB
/
zrwoxy.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
-- This is zrwoxy, a trolling HTTP proxy.
-- Manipulate HTTP traffic and HTML content using Nginx and Lua.
-- HTTP and HTML libraries
-- https://github.com/pintsized/lua-resty-http
-- https://github.com/craigbarnes/lua-gumbo
local http = require("resty.http")
local gumbo = require("gumbo")
-- Logging
--ngx.log(ngx.INFO, "zrwoxy processing HTTP request")
-- Read request body and request URI
ngx.req.read_body()
local body = ngx.req.get_body_data()
local uri = ngx.var.scheme .. "://" .. ngx.var.host .. ngx.var.request_uri
-- *Request* body manipulation would be super evil
-- if body ~= nil then
-- body = body:gsub("1996","2017")
-- end
-- Proxy HTTP request
local httpc = http.new()
local res, err = httpc:request_uri(uri, {
method = ngx.var.request_method,
headers = ngx.req.get_headers(),
body = body,
})
if not res then
ngx.say("Request failed: ", err)
return
end
-- Propagate response status
ngx.status = res.status
-- Propagate response headers
for key, value in pairs(res.headers) do
ngx.header[key] = value
end
-- Response body manipulation, only for HTML
-- TODO: Also handle gzipped responses by deflating them
local is_html = ngx.header["Content-Type"] and string.find(ngx.header["Content-Type"], "text/html", 1, true) ~= nil
local is_gzip = ngx.header["Content-Encoding"] == "gzip"
if is_html and not is_gzip then
-- Logging
ngx.log(ngx.INFO, "zrwoxy manipulating HTTP response")
-- Parse HTML into DOM
local document = assert(gumbo.parse(res.body))
-- Traverse DOM and manipulate text nodes
for node in document.body:walk() do
-- Node.TEXT_NODE == 3
-- See also https://craigbarnes.gitlab.io/lua-gumbo/#node
if node.nodeType == 3 then
-- Verbatim string replacement
node.data = node.data:gsub("1996", "2018")
-- Remove vowels
node.data = node.data:gsub("[AEIOUaeiou]", "")
end
end
-- Inject CSS style as last element into <head></head>
local css = "body { background: red; font-style: italic; font-size: larger; }"
local style = document:createElement("style")
local content = document:createTextNode(css)
style:appendChild(content)
document.head:appendChild(style)
-- Serialize DOM to HTML
local body = document:serialize()
-- When manipulating the response body, we should properly account
-- for the changed Content-Length. Otherwise, clients will stall
-- on receiving the response if the manipulated response payload
-- is shorter than before.
ngx.header["Content-Length"] = string.len(body)
-- If set, remove the "Transfer-Encoding: chunked" header as
-- the response body was manipulated and will get propagated 1:1.
if ngx.header["Transfer-Encoding"] == "chunked" then
ngx.header["Transfer-Encoding"] = nil
end
-- Propagate modified response body
ngx.say(body)
else
-- Propagate verbatim response
ngx.say(res.body)
end