From 6b5e09620da2da08818e461be0166215652e0e86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20Wypch=C5=82o?= Date: Thu, 8 Jul 2021 14:44:13 +0200 Subject: [PATCH] Fix skylink v2 redirect (#946) * replace skylink in location header * missing ; * add comments for skylink v2 resolver --- docker/nginx/conf.d/client.conf | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/docker/nginx/conf.d/client.conf b/docker/nginx/conf.d/client.conf index 7316f03b..d9eae33f 100644 --- a/docker/nginx/conf.d/client.conf +++ b/docker/nginx/conf.d/client.conf @@ -409,12 +409,16 @@ server { set $skylink $2; set $path $3; - # v2 support + # $skylink_v1 and $skylink_v2 variables default to the same value but in case the requested skylink was: + # a) skylink v1 - it wouldn't matter, no additional logic is executed + # b) skylink v2 - in a lua block below we will resolve the skylink v2 into skylink v1 and update + # $skylink_v1 variable so then the proxy request to skyd can be cached in nginx (proxy_cache_key + # in proxy-cache-downloads includes $skylink_v1 as a part of the cache key) set $skylink_v1 $skylink; set $skylink_v2 $skylink; access_by_lua_block { - -- disable cache if this is skylink v2 + -- detect whether requested skylink is v2 local isBase32v2 = string.len(ngx.var.skylink) == 55 and string.sub(ngx.var.skylink, 0, 2) == "04" local isBase64v2 = string.len(ngx.var.skylink) == 46 and string.sub(ngx.var.skylink, 0, 2) == "AQ" @@ -440,7 +444,11 @@ server { proxy_read_timeout 600; proxy_set_header User-Agent: Sia-Agent; - # proxy this call to siad /skynet/skylink/ endpoint (make sure the ip is correct) + + # in case the requested skylink was v2 and we already resolved it to skylink v1, we're going to pass resolved + # skylink v1 to skyd to save that extra skylink v2 lookup in skyd but in turn, in case skyd returns a redirect + # we need to rewrite the skylink v1 to skylink v2 in the location header with proxy_redirect + proxy_redirect $skylink_v1 $skylink_v2; proxy_pass http://siad/skynet/skylink/$skylink_v1$path$is_args$args; }