From a743b625ff5cacf62d1283c6c09276be5f96402e Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Thu, 10 Mar 2022 09:55:24 +0100 Subject: [PATCH] Revert "Merge pull request #1831 from SkynetLabs/redirect-base64-skylink-to-base32" This reverts commit e998882ca89b04ed25682e538892c4f1217d0109, reversing changes made to 85dd22a72eb77633c1018819fe95c67127291075. --- docker/nginx/conf.d/include/location-skylink | 2 +- docker/nginx/conf.d/server/server.api | 8 +------- docker/nginx/conf.d/server/server.dnslink | 2 +- docker/nginx/libs/skynet/skylink.lua | 20 +++----------------- docker/nginx/libs/skynet/skylink.spec.lua | 19 +++---------------- 5 files changed, 9 insertions(+), 42 deletions(-) diff --git a/docker/nginx/conf.d/include/location-skylink b/docker/nginx/conf.d/include/location-skylink index c613fe29..da4727c7 100644 --- a/docker/nginx/conf.d/include/location-skylink +++ b/docker/nginx/conf.d/include/location-skylink @@ -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) # this is important because we want only one format in cache keys and logs -set_by_lua_block $skylink { return require("skynet.skylink").base64(ngx.var.skylink) } +set_by_lua_block $skylink { return require("skynet.skylink").parse(ngx.var.skylink) } # $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 diff --git a/docker/nginx/conf.d/server/server.api b/docker/nginx/conf.d/server/server.api index 6a2b146b..ce93c669 100644 --- a/docker/nginx/conf.d/server/server.api +++ b/docker/nginx/conf.d/server/server.api @@ -338,13 +338,7 @@ location ~ "^/(([a-zA-Z0-9-_]{46}|[a-z0-9]{55})(/.*)?)$" { set $skylink $2; set $path $3; - 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) - } + include /etc/nginx/conf.d/include/location-skylink; } location ~ "^/file/(([a-zA-Z0-9-_]{46}|[a-z0-9]{55})(/.*)?)$" { diff --git a/docker/nginx/conf.d/server/server.dnslink b/docker/nginx/conf.d/server/server.dnslink index 22ce75a3..32e454cc 100644 --- a/docker/nginx/conf.d/server/server.dnslink +++ b/docker/nginx/conf.d/server/server.dnslink @@ -37,7 +37,7 @@ location / { ngx.var.skylink = cache_value end - ngx.var.skylink = require("skynet.skylink").base64(ngx.var.skylink) + ngx.var.skylink = require("skynet.skylink").parse(ngx.var.skylink) ngx.var.skylink_v1 = ngx.var.skylink ngx.var.skylink_v2 = ngx.var.skylink } diff --git a/docker/nginx/libs/skynet/skylink.lua b/docker/nginx/libs/skynet/skylink.lua index c6372a41..adcf0b70 100644 --- a/docker/nginx/libs/skynet/skylink.lua +++ b/docker/nginx/libs/skynet/skylink.lua @@ -3,13 +3,10 @@ local _M = {} local basexx = require("basexx") 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 -function _M.base64(skylink) +function _M.parse(skylink) if string.len(skylink) == 55 then - local decoded = basexx.from_basexx(string.lower(skylink), base32_alphabet, 5) + local decoded = basexx.from_basexx(string.upper(skylink), "0123456789ABCDEFGHIJKLMNOPQRSTUV", 5) return basexx.to_url64(decoded) end @@ -17,21 +14,10 @@ function _M.base64(skylink) return skylink 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 function _M.hash(skylink) -- ensure that the skylink is base64 encoded - local base64Skylink = _M.base64(skylink) + local base64Skylink = _M.parse(skylink) -- decode skylink from base64 encoding local rawSkylink = basexx.from_url64(base64Skylink) diff --git a/docker/nginx/libs/skynet/skylink.spec.lua b/docker/nginx/libs/skynet/skylink.spec.lua index 9949e534..0502a833 100644 --- a/docker/nginx/libs/skynet/skylink.spec.lua +++ b/docker/nginx/libs/skynet/skylink.spec.lua @@ -1,28 +1,15 @@ local skynet_skylink = require("skynet.skylink") -describe("base64", function() +describe("parse", function() local base32 = "0404dsjvti046fsua4ktor9grrpe76erq9jot9cvopbhsvsu76r4r30" local base64 = "AQBG8n_sgEM_nlEp3G0w3vLjmdvSZ46ln8ZXHn-eObZNjA" it("should return unchanged base64 skylink", function() - assert.is.same(skynet_skylink.base64(base64), base64) + assert.is.same(skynet_skylink.parse(base64), base64) end) it("should transform base32 skylink into base64", function() - 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) + assert.is.same(skynet_skylink.parse(base32), base64) end) end)