Merge branch 'master' into portal-latest

This commit is contained in:
Matthew Sevey 2022-04-22 11:40:53 -04:00
commit 4ad46215c4
No known key found for this signature in database
GPG Key ID: 9ADDD344F13057F6
3 changed files with 79 additions and 26 deletions

View File

@ -235,7 +235,12 @@ location /skynet/skyfile {
local skynet_tracker = require("skynet.tracker") local skynet_tracker = require("skynet.tracker")
if skynet_modules.is_enabled("a") then if skynet_modules.is_enabled("a") then
skynet_tracker.track_upload(ngx.header["Skynet-Skylink"], ngx.status, skynet_account.get_auth_headers()) skynet_tracker.track_upload(
ngx.header["Skynet-Skylink"],
ngx.status,
skynet_account.get_auth_headers(),
ngx.var.remote_addr
)
end end
if skynet_modules.is_enabled("s") then if skynet_modules.is_enabled("s") then
@ -315,7 +320,12 @@ location /skynet/tus {
local skynet_tracker = require("skynet.tracker") local skynet_tracker = require("skynet.tracker")
if skynet_modules.is_enabled("a") then if skynet_modules.is_enabled("a") then
skynet_tracker.track_upload(ngx.header["Skynet-Skylink"], ngx.status, skynet_account.get_auth_headers()) skynet_tracker.track_upload(
ngx.header["Skynet-Skylink"],
ngx.status,
skynet_account.get_auth_headers(),
ngx.var.remote_addr
)
end end
if skynet_modules.is_enabled("s") then if skynet_modules.is_enabled("s") then
@ -346,7 +356,12 @@ location /skynet/pin {
local skynet_tracker = require("skynet.tracker") local skynet_tracker = require("skynet.tracker")
if skynet_modules.is_enabled("a") then if skynet_modules.is_enabled("a") then
skynet_tracker.track_upload(ngx.header["Skynet-Skylink"], ngx.status, skynet_account.get_auth_headers()) skynet_tracker.track_upload(
ngx.header["Skynet-Skylink"],
ngx.status,
skynet_account.get_auth_headers(),
ngx.var.remote_addr
)
end end
if skynet_modules.is_enabled("s") then if skynet_modules.is_enabled("s") then

View File

@ -30,15 +30,24 @@ function _M.track_download(skylink, status_code, auth_headers, body_bytes_sent)
end end
end end
function _M.track_upload_timer(premature, skylink, auth_headers) function _M.track_upload_timer(premature, skylink, auth_headers, uploader_ip)
if premature then return end if premature then return end
local httpc = require("resty.http").new() local httpc = require("resty.http").new()
-- set correct content type header and include auth headers
local headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
}
for key, value in pairs(auth_headers) do
headers[key] = value
end
-- 10.10.10.70 points to accounts service (alias not available when using resty-http) -- 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, { local res, err = httpc:request_uri("http://10.10.10.70:3000/track/upload/" .. skylink, {
method = "POST", method = "POST",
headers = auth_headers, headers = headers,
body = "ip=" .. uploader_ip,
}) })
if err or (res and res.status ~= 204) then if err or (res and res.status ~= 204) then
@ -47,11 +56,11 @@ function _M.track_upload_timer(premature, skylink, auth_headers)
end end
end end
function _M.track_upload(skylink, status_code, auth_headers) function _M.track_upload(skylink, status_code, auth_headers, uploader_ip)
local status_success = status_code >= 200 and status_code <= 299 local status_success = status_code >= 200 and status_code <= 299
if skylink and status_success then if skylink and status_success then
local ok, err = ngx.timer.at(0, _M.track_upload_timer, skylink, auth_headers) local ok, err = ngx.timer.at(0, _M.track_upload_timer, skylink, auth_headers, uploader_ip)
if not ok then ngx.log(ngx.ERR, "Failed to create timer: ", err) end if not ok then ngx.log(ngx.ERR, "Failed to create timer: ", err) end
end end
end end

View File

@ -5,6 +5,7 @@ local skynet_tracker = require("skynet.tracker")
local valid_skylink = "AQBG8n_sgEM_nlEp3G0w3vLjmdvSZ46ln8ZXHn-eObZNjA" local valid_skylink = "AQBG8n_sgEM_nlEp3G0w3vLjmdvSZ46ln8ZXHn-eObZNjA"
local valid_status_code = 200 local valid_status_code = 200
local valid_auth_headers = { ["Skynet-Api-Key"] = "foo" } local valid_auth_headers = { ["Skynet-Api-Key"] = "foo" }
local valid_ip = "12.34.56.78"
describe("track_download", function() describe("track_download", function()
local valid_body_bytes_sent = 12345 local valid_body_bytes_sent = 12345
@ -200,20 +201,21 @@ describe("track_upload", function()
it("should schedule a timer when conditions are met", function() it("should schedule a timer when conditions are met", function()
ngx.timer.at.invokes(function() return true, nil end) ngx.timer.at.invokes(function() return true, nil end)
skynet_tracker.track_upload(valid_skylink, valid_status_code, valid_auth_headers) skynet_tracker.track_upload(valid_skylink, valid_status_code, valid_auth_headers, valid_ip)
assert.stub(ngx.timer.at).was_called_with( assert.stub(ngx.timer.at).was_called_with(
0, 0,
skynet_tracker.track_upload_timer, skynet_tracker.track_upload_timer,
valid_skylink, valid_skylink,
valid_auth_headers valid_auth_headers,
valid_ip
) )
end) end)
it("should not schedule a timer if skylink is empty", function() it("should not schedule a timer if skylink is empty", function()
ngx.timer.at.invokes(function() return true, nil end) ngx.timer.at.invokes(function() return true, nil end)
skynet_tracker.track_upload(nil, valid_status_code, valid_auth_headers) skynet_tracker.track_upload(nil, valid_status_code, valid_auth_headers, valid_ip)
assert.stub(ngx.timer.at).was_not_called() assert.stub(ngx.timer.at).was_not_called()
end) end)
@ -222,11 +224,11 @@ describe("track_upload", function()
ngx.timer.at.invokes(function() return true, nil end) ngx.timer.at.invokes(function() return true, nil end)
-- couple of example of 4XX and 5XX codes -- couple of example of 4XX and 5XX codes
skynet_tracker.track_upload(valid_skylink, 401, valid_auth_headers) skynet_tracker.track_upload(valid_skylink, 401, valid_auth_headers, valid_ip)
skynet_tracker.track_upload(valid_skylink, 403, valid_auth_headers) skynet_tracker.track_upload(valid_skylink, 403, valid_auth_headers, valid_ip)
skynet_tracker.track_upload(valid_skylink, 490, valid_auth_headers) skynet_tracker.track_upload(valid_skylink, 490, valid_auth_headers, valid_ip)
skynet_tracker.track_upload(valid_skylink, 500, valid_auth_headers) skynet_tracker.track_upload(valid_skylink, 500, valid_auth_headers, valid_ip)
skynet_tracker.track_upload(valid_skylink, 502, valid_auth_headers) skynet_tracker.track_upload(valid_skylink, 502, valid_auth_headers, valid_ip)
assert.stub(ngx.timer.at).was_not_called() assert.stub(ngx.timer.at).was_not_called()
end) end)
@ -234,13 +236,14 @@ describe("track_upload", function()
it("should schedule a timer if auth headers are empty", function() it("should schedule a timer if auth headers are empty", function()
ngx.timer.at.invokes(function() return true, nil end) ngx.timer.at.invokes(function() return true, nil end)
skynet_tracker.track_upload(valid_skylink, valid_status_code, {}) skynet_tracker.track_upload(valid_skylink, valid_status_code, {}, valid_ip)
assert.stub(ngx.timer.at).was_called_with( assert.stub(ngx.timer.at).was_called_with(
0, 0,
skynet_tracker.track_upload_timer, skynet_tracker.track_upload_timer,
valid_skylink, valid_skylink,
{} {},
valid_ip
) )
end) end)
@ -248,13 +251,14 @@ describe("track_upload", function()
stub(ngx, "log") stub(ngx, "log")
ngx.timer.at.invokes(function() return false, "such a failure" end) ngx.timer.at.invokes(function() return false, "such a failure" end)
skynet_tracker.track_upload(valid_skylink, valid_status_code, valid_auth_headers) skynet_tracker.track_upload(valid_skylink, valid_status_code, valid_auth_headers, valid_ip)
assert.stub(ngx.timer.at).was_called_with( assert.stub(ngx.timer.at).was_called_with(
0, 0,
skynet_tracker.track_upload_timer, skynet_tracker.track_upload_timer,
valid_skylink, valid_skylink,
valid_auth_headers valid_auth_headers,
valid_ip
) )
assert.stub(ngx.log).was_called_with(ngx.ERR, "Failed to create timer: ", "such a failure") assert.stub(ngx.log).was_called_with(ngx.ERR, "Failed to create timer: ", "such a failure")
@ -284,7 +288,8 @@ describe("track_upload", function()
skynet_tracker.track_upload_timer( skynet_tracker.track_upload_timer(
true, true,
valid_skylink, valid_skylink,
valid_auth_headers valid_auth_headers,
valid_ip
) )
assert.stub(request_uri).was_not_called() assert.stub(request_uri).was_not_called()
@ -302,11 +307,19 @@ describe("track_upload", function()
skynet_tracker.track_upload_timer( skynet_tracker.track_upload_timer(
false, false,
valid_skylink, valid_skylink,
valid_auth_headers valid_auth_headers,
valid_ip
) )
local uri = "http://10.10.10.70:3000/track/upload/" .. valid_skylink local uri = "http://10.10.10.70:3000/track/upload/" .. valid_skylink
assert.stub(request_uri).was_called_with(httpc, uri, { method = "POST", headers = valid_auth_headers }) assert.stub(request_uri).was_called_with(httpc, uri, {
method = "POST",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
["Skynet-Api-Key"] = "foo",
},
body = "ip=" .. valid_ip
})
assert.stub(ngx.log).was_not_called() assert.stub(ngx.log).was_not_called()
end) end)
@ -321,11 +334,19 @@ describe("track_upload", function()
skynet_tracker.track_upload_timer( skynet_tracker.track_upload_timer(
false, false,
valid_skylink, valid_skylink,
valid_auth_headers valid_auth_headers,
valid_ip
) )
local uri = "http://10.10.10.70:3000/track/upload/" .. valid_skylink local uri = "http://10.10.10.70:3000/track/upload/" .. valid_skylink
assert.stub(request_uri).was_called_with(httpc, uri, { method = "POST", headers = valid_auth_headers }) assert.stub(request_uri).was_called_with(httpc, uri, {
method = "POST",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
["Skynet-Api-Key"] = "foo",
},
body = "ip=" .. valid_ip
})
assert.stub(ngx.log).was_called_with( assert.stub(ngx.log).was_called_with(
ngx.ERR, ngx.ERR,
"Failed accounts service request /track/upload/" .. valid_skylink .. ": ", "Failed accounts service request /track/upload/" .. valid_skylink .. ": ",
@ -344,11 +365,19 @@ describe("track_upload", function()
skynet_tracker.track_upload_timer( skynet_tracker.track_upload_timer(
false, false,
valid_skylink, valid_skylink,
valid_auth_headers valid_auth_headers,
valid_ip
) )
local uri = "http://10.10.10.70:3000/track/upload/" .. valid_skylink local uri = "http://10.10.10.70:3000/track/upload/" .. valid_skylink
assert.stub(request_uri).was_called_with(httpc, uri, { method = "POST", headers = valid_auth_headers }) assert.stub(request_uri).was_called_with(httpc, uri, {
method = "POST",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
["Skynet-Api-Key"] = "foo",
},
body = "ip=" .. valid_ip
})
assert.stub(ngx.log).was_called_with( assert.stub(ngx.log).was_called_with(
ngx.ERR, ngx.ERR,
"Failed accounts service request /track/upload/" .. valid_skylink .. ": ", "Failed accounts service request /track/upload/" .. valid_skylink .. ": ",