Merge pull request #1831 from SkynetLabs/redirect-base64-skylink-to-base32

permanent redirect some-portal.com/[skylink] to [base32-skylink].some-portal.com
This commit is contained in:
Marcin S 2022-03-04 16:28:54 +00:00 committed by GitHub
commit e998882ca8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 9 deletions

View File

@ -7,7 +7,7 @@ limit_conn downloads_by_ip 100; # ddos protection: max 100 downloads at a time
# ensure that skylink that we pass around is base64 encoded (transform base32 encoded ones) # ensure that skylink that we pass around is base64 encoded (transform base32 encoded ones)
# this is important because we want only one format in cache keys and logs # this is important because we want only one format in cache keys and logs
set_by_lua_block $skylink { return require("skynet.skylink").parse(ngx.var.skylink) } set_by_lua_block $skylink { return require("skynet.skylink").base64(ngx.var.skylink) }
# $skylink_v1 and $skylink_v2 variables default to the same value but in case the requested skylink was: # $skylink_v1 and $skylink_v2 variables default to the same value but in case the requested skylink was:
# a) skylink v1 - it would not matter, no additional logic is executed # a) skylink v1 - it would not matter, no additional logic is executed

View File

@ -352,7 +352,13 @@ location ~ "^/(([a-zA-Z0-9-_]{46}|[a-z0-9]{55})(/.*)?)$" {
set $skylink $2; set $skylink $2;
set $path $3; set $path $3;
include /etc/nginx/conf.d/include/location-skylink; rewrite_by_lua_block {
local skynet_skylink = require("skynet.skylink")
local base32_skylink = skynet_skylink.base32(ngx.var.skylink)
local base32_url = ngx.var.scheme .. "://" .. base32_skylink .. "." .. ngx.var.skynet_portal_domain .. ngx.var.path .. ngx.var.is_args .. ngx.var.args
return ngx.redirect(base32_url, ngx.HTTP_MOVED_PERMANENTLY)
}
} }
location ~ "^/file/(([a-zA-Z0-9-_]{46}|[a-z0-9]{55})(/.*)?)$" { location ~ "^/file/(([a-zA-Z0-9-_]{46}|[a-z0-9]{55})(/.*)?)$" {

View File

@ -37,7 +37,7 @@ location / {
ngx.var.skylink = cache_value ngx.var.skylink = cache_value
end end
ngx.var.skylink = require("skynet.skylink").parse(ngx.var.skylink) ngx.var.skylink = require("skynet.skylink").base64(ngx.var.skylink)
ngx.var.skylink_v1 = ngx.var.skylink ngx.var.skylink_v1 = ngx.var.skylink
ngx.var.skylink_v2 = ngx.var.skylink ngx.var.skylink_v2 = ngx.var.skylink
} }

View File

@ -3,10 +3,13 @@ local _M = {}
local basexx = require("basexx") local basexx = require("basexx")
local hasher = require("hasher") local hasher = require("hasher")
-- use lowercase alphabet since our skylinks are part of urls
local base32_alphabet = "0123456789abcdefghijklmnopqrstuv"
-- parse any skylink and return base64 version -- parse any skylink and return base64 version
function _M.parse(skylink) function _M.base64(skylink)
if string.len(skylink) == 55 then if string.len(skylink) == 55 then
local decoded = basexx.from_basexx(string.upper(skylink), "0123456789ABCDEFGHIJKLMNOPQRSTUV", 5) local decoded = basexx.from_basexx(string.lower(skylink), base32_alphabet, 5)
return basexx.to_url64(decoded) return basexx.to_url64(decoded)
end end
@ -14,10 +17,21 @@ function _M.parse(skylink)
return skylink return skylink
end end
-- parse any skylink and return base32 version
function _M.base32(skylink)
if string.len(skylink) == 46 then
local decoded = basexx.from_url64(skylink)
return basexx.to_basexx(decoded, base32_alphabet, 5)
end
return skylink
end
-- hash skylink into 32 bytes hash used in blocklist -- hash skylink into 32 bytes hash used in blocklist
function _M.hash(skylink) function _M.hash(skylink)
-- ensure that the skylink is base64 encoded -- ensure that the skylink is base64 encoded
local base64Skylink = _M.parse(skylink) local base64Skylink = _M.base64(skylink)
-- decode skylink from base64 encoding -- decode skylink from base64 encoding
local rawSkylink = basexx.from_url64(base64Skylink) local rawSkylink = basexx.from_url64(base64Skylink)

View File

@ -1,15 +1,28 @@
local skynet_skylink = require("skynet.skylink") local skynet_skylink = require("skynet.skylink")
describe("parse", function() describe("base64", function()
local base32 = "0404dsjvti046fsua4ktor9grrpe76erq9jot9cvopbhsvsu76r4r30" local base32 = "0404dsjvti046fsua4ktor9grrpe76erq9jot9cvopbhsvsu76r4r30"
local base64 = "AQBG8n_sgEM_nlEp3G0w3vLjmdvSZ46ln8ZXHn-eObZNjA" local base64 = "AQBG8n_sgEM_nlEp3G0w3vLjmdvSZ46ln8ZXHn-eObZNjA"
it("should return unchanged base64 skylink", function() it("should return unchanged base64 skylink", function()
assert.is.same(skynet_skylink.parse(base64), base64) assert.is.same(skynet_skylink.base64(base64), base64)
end) end)
it("should transform base32 skylink into base64", function() it("should transform base32 skylink into base64", function()
assert.is.same(skynet_skylink.parse(base32), base64) assert.is.same(skynet_skylink.base64(base32), base64)
end)
end)
describe("base32", function()
local base32 = "0404dsjvti046fsua4ktor9grrpe76erq9jot9cvopbhsvsu76r4r30"
local base64 = "AQBG8n_sgEM_nlEp3G0w3vLjmdvSZ46ln8ZXHn-eObZNjA"
it("should return unchanged base32 skylink", function()
assert.is.same(skynet_skylink.base32(base32), base32)
end)
it("should transform base64 skylink into base32", function()
assert.is.same(skynet_skylink.base32(base64), base32)
end) end)
end) end)