This repository was archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcascproxy.lua
141 lines (135 loc) · 3.91 KB
/
cascproxy.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
local args = (function()
local parser = require('argparse')()
parser:option('-b --build', 'build(s) to serve'):count('*')
parser:option('-c --cache', 'cache directory', 'cache')
parser:option('-p --port', 'web server port', '8080'):convert(tonumber)
parser:option('-t --product', 'product(s) to serve'):count('*'):choices({
'wow',
'wowt',
'wow_classic',
'wow_classic_beta',
'wow_classic_era',
'wow_classic_era_ptr',
'wow_classic_ptr',
})
return parser:parse()
end)()
local log = print
-- Tune GC very tightly. CASCs are memory hogs.
collectgarbage('setpause', 100)
collectgarbage('setstepmul', 500)
require('lfs').mkdir(args.cache)
local bcascs, pcascs = (function()
local casc = require('casc')
local bcascs = {}
local pcascs = {}
local root = 'http://us.patch.battle.net:1119/'
do
local _, cdn, ckey = casc.cdnbuild(root .. 'wow', 'us')
for _, build in ipairs(args.build) do
log('loading', build)
local handle, err = casc.open({
bkey = build,
cache = args.cache,
cacheFiles = true,
cdn = cdn,
ckey = ckey,
locale = casc.locale.US,
log = log,
zerofillEncryptedChunks = true,
})
if not handle then
print('unable to open ' .. build .. ': ' .. err)
os.exit()
end
bcascs[build] = handle
end
end
for _, product in ipairs(args.product) do
local bkey, cdn, ckey, version = casc.cdnbuild(root .. product, 'us')
if not bkey then
print('unable to open ' .. product .. ': cannot get version')
os.exit()
end
log('loading', product, version)
local handle, err = casc.open({
bkey = bkey,
cache = args.cache,
cacheFiles = true,
cdn = cdn,
ckey = ckey,
locale = casc.locale.US,
log = log,
zerofillEncryptedChunks = true,
})
if not handle then
print('unable to open ' .. product .. ': ' .. err)
os.exit()
end
pcascs[product] = handle
end
return bcascs, pcascs
end)()
local pathparser = (function()
local lpeg = require('lpeg')
local C, P, R = lpeg.C, lpeg.P, lpeg.R
local ty = C(P('product') + P('build'))
local fdid = P('/fdid/') * C(R('09') ^ 1) / tonumber
local name = P('/name/') * C(R('az', 'AZ', '09', '__', '--', '//', '..') ^ 1)
return P('/') * ty * P('/') * C(R('az', 'AZ', '09', '__') ^ 1) * (fdid + name) * P(-1)
end)()
local mkres = require('http.headers').new
local listener = assert(require('http.server').listen({
host = 'localhost',
onerror = function(_, ctx, op, err)
local msg = op .. ' on ' .. tostring(ctx) .. ' failed'
if err then
msg = msg .. ': ' .. tostring(err)
end
assert(io.stderr:write(msg, '\n'))
end,
onstream = function(_, stream)
local req = assert(stream:get_headers())
local res = mkres()
local path = req:get(':path')
if path == '/' then
local data = { 'cascproxy', '' }
for k in pairs(bcascs) do
table.insert(data, k)
end
for k in pairs(pcascs) do
table.insert(data, k)
end
table.insert(data, '')
res:append(':status', '200')
assert(stream:write_headers(res, false))
assert(stream:write_chunk(table.concat(data, '\n'), true))
log('200', '/')
return
end
local ty, name, fid = pathparser:match(path)
local casc = (ty == 'build' and bcascs or pcascs)[name]
if not casc or not fid then
res:append(':status', '400')
assert(stream:write_headers(res, true))
log('400', path)
return
end
local data = casc:readFile(fid)
if not data then
res:append(':status', '404')
assert(stream:write_headers(res, true))
log('404', path)
return
end
res:append(':status', '200')
assert(stream:write_headers(res, false))
assert(stream:write_chunk(data, true))
log('200', path)
end,
port = args.port,
}))
log('HTTP server ready')
pcall(function()
listener:loop()
end)