Merge pull request #2046 from SkynetLabs/track-uploader-ip

track uploader ip
This commit is contained in:
Karol Wypchło 2022-04-22 17:38:39 +02:00 committed by GitHub
commit a6ab6b31b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 26 deletions

View File

@ -235,7 +235,12 @@ location /skynet/skyfile {
local skynet_tracker = require("skynet.tracker")
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
if skynet_modules.is_enabled("s") then
@ -315,7 +320,12 @@ location /skynet/tus {
local skynet_tracker = require("skynet.tracker")
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
if skynet_modules.is_enabled("s") then
@ -346,7 +356,12 @@ location /skynet/pin {
local skynet_tracker = require("skynet.tracker")
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
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
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
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)
local res, err = httpc:request_uri("http://10.10.10.70:3000/track/upload/" .. skylink, {
method = "POST",
headers = auth_headers,
headers = headers,
body = "ip=" .. uploader_ip,
})
if err or (res and res.status ~= 204) then
@ -47,11 +56,11 @@ function _M.track_upload_timer(premature, skylink, auth_headers)
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
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
end
end

View File

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