-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscanner.lua
More file actions
217 lines (195 loc) · 6.02 KB
/
scanner.lua
File metadata and controls
217 lines (195 loc) · 6.02 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
-- scanner.lua
local currentRes = GetCurrentResourceName()
local exp = exports[currentRes]
local readDir, isDir = exp.readDir, exp.isDir
local getFileMTime = exp.getFileMTime
local LoadFile = LoadResourceFile
local SaveFile = SaveResourceFile
local numRes = GetNumResources
local getResByIdx = GetResourceByFindIndex
local getResState = GetResourceState
local getResPath = GetResourcePath
local gameTimer = GetGameTimer
local osDate = os.date
local jsonEncode = json and json.encode
-- ensure global scanner exists
scanner = scanner or {
enabled = true,
results = utils.results,
stats = utils.stats
}
function scanner.reload()
utils.reloadConfig()
utils.reloadCache()
scanner.results, scanner.stats = {}, {}
utils.results = scanner.results
utils.stats = scanner.stats
end
function scanner.scanResource(res)
local t0 = gameTimer()
local base = getResPath(res)
local stack, found, scanned = {""}, {}, {}
local cnt = 0
utils.log('info', "scanning %s", res)
while #stack > 0 do
local rel = table.remove(stack)
local dir = base .. (rel=="" and "" or "/"..rel)
local ok, items = pcall(readDir, dir)
if ok and items then
for _, f in ipairs(items) do
utils.yield()
local path = (rel=="" and f or rel.."/"..f)
scanned[#scanned+1] = path
if utils.shouldSkipFile(path) then goto cont end
local full = base.."/"..path
if isDir(full) then
if not (utils.config.skipDirs or {})[f:lower()] then
stack[#stack+1] = path
end
elseif utils.fileExt(f)
and (not utils.config.incremental
or getFileMTime(full) > (utils.cache[res] or {})[path])
then
cnt = cnt + 1
local ok2, content = pcall(LoadFile, res, path)
if ok2 and content then
for _, sig in ipairs(utils.config.signatures or {}) do
local m = sig:match('^/.+/$')
and content:match(sig:sub(2,-2))
or content:find(sig,1,true)
if m then
found[#found+1] = path
break
end
end
end
utils.cache[res] = utils.cache[res] or {}
utils.cache[res][path] = getFileMTime(full)
end
::cont::
end
end
end
local dt = gameTimer() - t0
scanner.stats[res] = { files = cnt, matches = #found, time = dt }
scanner.results[res] = found
utils.log('info', "%s → %d arquivos, %d hits em %dms", res, cnt, #found, dt)
if utils.config.showScanned then
utils.log('info', "Arquivos lidos em %s:", res)
for _, p in ipairs(scanned) do
utils.log('info', " - %s", p)
end
end
end
function scanner.generateReport()
local md = {
"# CipherScanner Report\n\n",
"Generated at: " .. osDate("!%Y-%m-%d %H:%M:%S UTC") .. "\n\n",
"| Resource | Files | Matches | Time (ms) |\n",
"|----------|-------|---------|-----------|\n"
}
for r,s in pairs(scanner.stats) do
md[#md+1] = string.format("| %s | %d | %d | %d |\n", r, s.files, s.matches, s.time)
end
SaveFile(currentRes, "report.md", table.concat(md), -1)
utils.log('info', "report.md gerado")
end
function scanner.diff(res)
local hist = utils.loadHistory()
if #hist < 2 then return {}, {} end
local prev = hist[#hist-1].results[res] or {}
local curr = hist[#hist].results[res] or {}
local sp, sc = {}, {}
for _,p in ipairs(prev) do sp[p] = true end
for _,c in ipairs(curr) do sc[c] = true end
local add, rem = {}, {}
for _,c in ipairs(curr) do if not sp[c] then table.insert(add,c) end end
for _,p in ipairs(prev) do if not sc[p] then table.insert(rem,p) end end
return add, rem
end
function scanner.sendWebhook()
utils.reloadConfig()
local w = utils.config.webhook
if not (w and w.enabled and w.url and w.url ~= "") then
print("[cipher][WARN] Webhook disabled or URL missing")
return
end
if not next(scanner.stats) then
print("[cipher][INFO] No stats to send via webhook")
return
end
-- total hits and non-zero list
local totalHits, list = 0, {}
for res,s in pairs(scanner.stats) do
totalHits = totalHits + s.matches
if s.matches > 0 then
table.insert(list, string.format("**%s**: %d hits", res, s.matches))
end
end
local embed = {
title = "**CipherScanner Report**",
color = 3447003,
footer = { text = "Generated at " .. osDate("%Y-%m-%d %H:%M:%S") }
}
if totalHits == 0 then
local countRes = 0
for _ in pairs(scanner.stats) do countRes = countRes + 1 end
embed.description = string.format(
"✅ Nenhuma correspondência em %d recursos varridos.", countRes
)
else
embed.description = table.concat(list, "\n")
end
local payload = {
username = "CipherScanner",
embeds = { embed }
}
local body = jsonEncode(payload)
PerformHttpRequest(
w.url,
function(code, responseBody)
if responseBody then
end
end,
"POST",
body,
{ ["Content-Type"] = "application/json" }
)
end
function scanner.scanAll(target)
scanner.reload()
utils.log('info', "Starting %s scan", target or "full")
local list = {}
if target then
if not (utils.config.skipResources or {})[target] then
list = { target }
end
else
for i=0, numRes()-1 do
utils.yield()
local nm = getResByIdx(i)
if nm and getResState(nm)=='started'
and nm~=currentRes
and not (utils.config.skipResources or {})[nm]
then
table.insert(list, nm)
end
end
end
local total, done = #list, 0
for _, res in ipairs(list) do
CreateThread(function()
scanner.scanResource(res)
done = done + 1
TriggerClientEvent('cipher:resourceScanned', -1, res, scanner.stats[res])
if done == total then
utils.persistCache()
utils.persistResults()
scanner.generateReport()
scanner.sendWebhook()
utils.recordHistory(scanner.results)
TriggerClientEvent('cipher:scanComplete', -1, scanner.stats)
end
end)
end
end