From 20f6831eb0aa2edfe359c83ba6a4d363703a2847 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Fri, 22 Apr 2022 15:00:05 +0200 Subject: [PATCH 1/6] track file uploader ip --- docker/nginx/libs/skynet/tracker.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docker/nginx/libs/skynet/tracker.lua b/docker/nginx/libs/skynet/tracker.lua index 56f9dcc4..769de45c 100644 --- a/docker/nginx/libs/skynet/tracker.lua +++ b/docker/nginx/libs/skynet/tracker.lua @@ -1,5 +1,6 @@ local _M = {} +local cjson = require("cjson") local utils = require("utils") function _M.track_download_timer(premature, skylink, status, auth_headers, body_bytes_sent) @@ -30,7 +31,7 @@ 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() @@ -39,6 +40,7 @@ function _M.track_upload_timer(premature, skylink, auth_headers) local res, err = httpc:request_uri("http://10.10.10.70:3000/track/upload/" .. skylink, { method = "POST", headers = auth_headers, + body = cjson.encode({ ip = uploader_ip }) }) if err or (res and res.status ~= 204) then @@ -49,9 +51,10 @@ end function _M.track_upload(skylink, status_code, auth_headers) local status_success = status_code >= 200 and status_code <= 299 + local uploader_ip = ngx.var.remote_addr 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 From e8babb625e6fc9e5a452aeb03314040ec144f838 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Fri, 22 Apr 2022 15:08:01 +0200 Subject: [PATCH 2/6] try formvalue --- docker/nginx/libs/skynet/tracker.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docker/nginx/libs/skynet/tracker.lua b/docker/nginx/libs/skynet/tracker.lua index 769de45c..b26cc5e5 100644 --- a/docker/nginx/libs/skynet/tracker.lua +++ b/docker/nginx/libs/skynet/tracker.lua @@ -39,8 +39,10 @@ function _M.track_upload_timer(premature, skylink, auth_headers, uploader_ip) -- 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, - body = cjson.encode({ ip = uploader_ip }) + body = "ip=" .. uploader_ip, + headers = { + ["Content-Type"] = "application/x-www-form-urlencoded" + } }) if err or (res and res.status ~= 204) then From 5301384609a02523e50210f6cab990180aca49a8 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Fri, 22 Apr 2022 15:16:07 +0200 Subject: [PATCH 3/6] do not include form header --- docker/nginx/libs/skynet/tracker.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docker/nginx/libs/skynet/tracker.lua b/docker/nginx/libs/skynet/tracker.lua index b26cc5e5..43d8319b 100644 --- a/docker/nginx/libs/skynet/tracker.lua +++ b/docker/nginx/libs/skynet/tracker.lua @@ -39,10 +39,8 @@ function _M.track_upload_timer(premature, skylink, auth_headers, uploader_ip) -- 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, body = "ip=" .. uploader_ip, - headers = { - ["Content-Type"] = "application/x-www-form-urlencoded" - } }) if err or (res and res.status ~= 204) then From 39f9b4d19640c24c7dba8bfc1b6e3ef55904217f Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Fri, 22 Apr 2022 15:19:07 +0200 Subject: [PATCH 4/6] fix headers --- docker/nginx/libs/skynet/tracker.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/docker/nginx/libs/skynet/tracker.lua b/docker/nginx/libs/skynet/tracker.lua index 43d8319b..8b3bd0f5 100644 --- a/docker/nginx/libs/skynet/tracker.lua +++ b/docker/nginx/libs/skynet/tracker.lua @@ -1,6 +1,5 @@ local _M = {} -local cjson = require("cjson") local utils = require("utils") function _M.track_download_timer(premature, skylink, status, auth_headers, body_bytes_sent) @@ -36,10 +35,18 @@ function _M.track_upload_timer(premature, skylink, auth_headers, uploader_ip) 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 ipairs(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, }) From f88dcf3eee5af9a2207c1d8cf4d3f166c6e9915e Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Fri, 22 Apr 2022 15:34:28 +0200 Subject: [PATCH 5/6] include unit tests for uploader ip --- docker/nginx/conf.d/server/server.api | 21 ++++++- docker/nginx/libs/skynet/tracker.lua | 5 +- docker/nginx/libs/skynet/tracker.spec.lua | 67 ++++++++++++++++------- 3 files changed, 68 insertions(+), 25 deletions(-) diff --git a/docker/nginx/conf.d/server/server.api b/docker/nginx/conf.d/server/server.api index 0243ab90..48e7a638 100644 --- a/docker/nginx/conf.d/server/server.api +++ b/docker/nginx/conf.d/server/server.api @@ -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 diff --git a/docker/nginx/libs/skynet/tracker.lua b/docker/nginx/libs/skynet/tracker.lua index 8b3bd0f5..2a89bb4b 100644 --- a/docker/nginx/libs/skynet/tracker.lua +++ b/docker/nginx/libs/skynet/tracker.lua @@ -41,7 +41,7 @@ function _M.track_upload_timer(premature, skylink, auth_headers, uploader_ip) } for key, value in ipairs(auth_headers) do headers[key] = value - end + 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, { @@ -56,9 +56,8 @@ function _M.track_upload_timer(premature, skylink, auth_headers, uploader_ip) 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 uploader_ip = ngx.var.remote_addr if skylink and status_success then local ok, err = ngx.timer.at(0, _M.track_upload_timer, skylink, auth_headers, uploader_ip) diff --git a/docker/nginx/libs/skynet/tracker.spec.lua b/docker/nginx/libs/skynet/tracker.spec.lua index d6c59c4a..98d587d8 100644 --- a/docker/nginx/libs/skynet/tracker.spec.lua +++ b/docker/nginx/libs/skynet/tracker.spec.lua @@ -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 .. ": ", From 077295806471cafb4bdf44800d5b3223f584c532 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Fri, 22 Apr 2022 17:06:17 +0200 Subject: [PATCH 6/6] use pairs instead of ipairs --- docker/nginx/libs/skynet/tracker.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/nginx/libs/skynet/tracker.lua b/docker/nginx/libs/skynet/tracker.lua index 2a89bb4b..37413215 100644 --- a/docker/nginx/libs/skynet/tracker.lua +++ b/docker/nginx/libs/skynet/tracker.lua @@ -39,7 +39,7 @@ function _M.track_upload_timer(premature, skylink, auth_headers, uploader_ip) local headers = { ["Content-Type"] = "application/x-www-form-urlencoded", } - for key, value in ipairs(auth_headers) do + for key, value in pairs(auth_headers) do headers[key] = value end