-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2-cloudflare-auth.lua
More file actions
97 lines (80 loc) · 3.53 KB
/
Copy path2-cloudflare-auth.lua
File metadata and controls
97 lines (80 loc) · 3.53 KB
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
-- 2-cloudflare-auth.lua
-- Cloudflare Access Service Token Authentication Patch
-- Injects CF-Access-Client-Id and CF-Access-Client-Secret headers into all HTTP/HTTPS requests
local logger = require("logger")
-- CREDENTIALS
local CF_ID = "<your-client-id-here>"
local CF_SECRET = "<your-client-secret-here>"
logger.info("CF-Auth: Initializing...")
-- We need to hook at the package level BEFORE modules get local references
-- Get the actual module tables from package.loaded
local http_module = package.loaded["socket.http"]
local https_module = package.loaded["ssl.https"]
if not http_module then
logger.warn("CF-Auth: socket.http not loaded yet, loading now...")
http_module = require("socket.http")
end
if not https_module then
logger.warn("CF-Auth: ssl.https not loaded yet, loading now...")
https_module = require("ssl.https")
end
-- Store original functions
local original_http_request = http_module.request
local original_https_request = https_module.request
-- Helper to inject headers into request table
local function inject_cf_headers(request)
if type(request) == "table" then
-- Initialize headers table if it doesn't exist
if not request.headers then
request.headers = {}
end
-- Inject Cloudflare headers (only if not already present)
if not request.headers["CF-Access-Client-Id"] then
request.headers["CF-Access-Client-Id"] = CF_ID
request.headers["CF-Access-Client-Secret"] = CF_SECRET
logger.info("CF-Auth: ✓ Injected headers for URL:", request.url or "unknown")
else
logger.info("CF-Auth: Headers already present for:", request.url or "unknown")
end
end
return request
end
-- Replace the functions in the actual module table
-- This way ALL references (including local ones in other files) will use our hooked version
http_module.request = function(req, body)
logger.info("CF-Auth: >>> http.request intercepted, type=" .. type(req))
if type(req) == "table" then
logger.info("CF-Auth: Request URL:", req.url or "no url")
inject_cf_headers(req)
elseif type(req) == "string" then
logger.info("CF-Auth: String URL request:", req)
end
-- Call original and capture results
local result1, result2, result3, result4 = original_http_request(req, body)
-- Log the response
logger.info("CF-Auth: Response code:", result2 or "nil")
logger.info("CF-Auth: Response status:", result4 or "nil")
if type(result3) == "table" and result3.location then
logger.info("CF-Auth: Redirect location:", result3.location)
end
return result1, result2, result3, result4
end
https_module.request = function(req, body)
logger.info("CF-Auth: >>> https.request intercepted, type=" .. type(req))
if type(req) == "table" then
logger.info("CF-Auth: Request URL:", req.url or "no url")
inject_cf_headers(req)
elseif type(req) == "string" then
logger.info("CF-Auth: String URL request:", req)
end
-- Call original and capture results
local result1, result2, result3, result4 = original_https_request(req, body)
-- Log the response
logger.info("CF-Auth: Response code:", result2 or "nil")
logger.info("CF-Auth: Response status:", result4 or "nil")
if type(result3) == "table" and result3.location then
logger.info("CF-Auth: Redirect location:", result3.location)
end
return result1, result2, result3, result4
end
logger.info("CF-Auth: ✓✓✓ Hooks installed successfully ✓✓✓")