2021-06-04 10:33:37 +00:00
|
|
|
# register the upload in accounts service (cookies should contain jwt)
|
|
|
|
log_by_lua_block {
|
|
|
|
-- this block runs only when accounts are enabled
|
2021-07-26 10:31:12 +00:00
|
|
|
if os.getenv("ACCOUNTS_ENABLED") ~= "true" then return end
|
2021-06-04 10:33:37 +00:00
|
|
|
|
2021-08-27 12:15:22 +00:00
|
|
|
local function track(premature, skylink, jwt)
|
|
|
|
if premature then return end
|
|
|
|
|
|
|
|
local httpc = require("resty.http").new()
|
|
|
|
|
|
|
|
-- 10.10.10.70 points to accounts service (alias not available when using resty-http)
|
|
|
|
local res, err = httpc:request_uri("http://10.10.10.70:3000/track/upload/" .. skylink, {
|
2021-06-04 10:33:37 +00:00
|
|
|
method = "POST",
|
2021-08-27 12:15:22 +00:00
|
|
|
headers = { ["Cookie"] = "skynet-jwt=" .. jwt },
|
|
|
|
})
|
2021-12-09 15:21:49 +00:00
|
|
|
|
2021-08-27 12:15:22 +00:00
|
|
|
if err or (res and res.status ~= ngx.HTTP_NO_CONTENT) then
|
|
|
|
ngx.log(ngx.ERR, "Failed accounts service request /track/upload/" .. skylink .. ": ", err or ("[HTTP " .. res.status .. "] " .. res.body))
|
2021-06-04 10:33:37 +00:00
|
|
|
end
|
|
|
|
end
|
2021-08-27 12:15:22 +00:00
|
|
|
|
2021-12-09 15:21:49 +00:00
|
|
|
local function scan(premature, skylink, jwt)
|
|
|
|
if premature then return end
|
|
|
|
|
|
|
|
local httpc = require("resty.http").new()
|
|
|
|
|
|
|
|
-- 10.10.10.101 points to malware-scanner service (alias not available when using resty-http)
|
|
|
|
local res, err = httpc:request_uri("http://10.10.10.101:3000/scan/" .. skylink, {
|
|
|
|
method = "POST",
|
|
|
|
headers = { ["Cookie"] = "skynet-jwt=" .. jwt },
|
|
|
|
})
|
|
|
|
|
2021-12-14 12:38:40 +00:00
|
|
|
if err or (res and res.status ~= ngx.HTTP_OK) then
|
2021-12-09 15:21:49 +00:00
|
|
|
ngx.log(ngx.ERR, "Failed malware-scanner request /scan/" .. skylink .. ": ", err or ("[HTTP " .. res.status .. "] " .. res.body))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-27 12:15:22 +00:00
|
|
|
if ngx.header["Skynet-Skylink"] and ngx.var.skynet_jwt ~= "" then
|
|
|
|
local ok, err = ngx.timer.at(0, track, ngx.header["Skynet-Skylink"], ngx.var.skynet_jwt)
|
|
|
|
if err then ngx.log(ngx.ERR, "Failed to create timer: ", err) end
|
|
|
|
end
|
2021-12-09 15:21:49 +00:00
|
|
|
|
|
|
|
-- Unlike accounts, malware-scanner wants to be pinged about each skylink,
|
|
|
|
-- not only the ones uploaded by registered accounts.
|
|
|
|
local scan_ok, scan_err = ngx.timer.at(0, scan, ngx.header["Skynet-Skylink"], ngx.var.skynet_jwt)
|
|
|
|
if scan_err then ngx.log(ngx.ERR, "Failed to create timer: ", scan_err) end
|
2021-06-04 10:35:16 +00:00
|
|
|
}
|