LiquidProxy Lua Edition
1local fs = require "fs"
2
3local certype = "application/x-x509-ca-cert"
4local mime = {
5 css = "text/css",
6 html = "text/html",
7
8 cer = certype,
9 pem = certype,
10 p12 = "application/x-pkcs12",
11 mobileconfig = "application/x-apple-aspen-config"
12}
13
14local nft = "Not found"
15local nfl = nft:len()
16
17local cache = {}
18local function preload(filename)
19 fs.readFile("static/"..filename, function(err, data)
20 if err then
21 print("Error loading "..filename)
22 print(err)
23 os.exit(1)
24 end
25 cache[filename] = data
26 end)
27end
28
29local topreload = {
30 "index.html", "cydia.css", "style.css"
31}
32for _, v in pairs(topreload) do preload(v) end
33
34local function serve(req, res)
35 req.path = req.path:gsub("http://.-/", "/"):gsub("%.%.", ""):gsub("//", "/")
36 if req.path == "/" then
37 req.path = "/index.html"
38 end
39 local f = cache[req.path:sub(2)]
40 if not f then
41 l:debug(req.path.." cache miss")
42 f = fs.readFileSync("static"..req.path)
43 if not f then
44 f = fs.readFileSync("certs"..req.path)
45 if not f then
46 return {code = 404, {"Content-Length", nfl}}, nft
47 end
48 end
49 end
50
51 return {
52 code = 200,
53 {"Content-Type", mime[req.path:match("%.(.-)$")] or "application/octet-stream"},
54 {"Content-Length", tostring(f:len())}
55 }, f
56end
57
58return {serve, function(req, res)
59end}