From 59347ba9093fd5272b5f5dc34cd2503a0b411c24 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Fri, 4 Mar 2022 13:36:31 +0100 Subject: [PATCH 1/4] permanent redirect some-portal.com/hns/[domain] to [domain].hns.some-portal.com --- docker/nginx/conf.d/server/server.api | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/docker/nginx/conf.d/server/server.api b/docker/nginx/conf.d/server/server.api index 11a3ee75..113a3c41 100644 --- a/docker/nginx/conf.d/server/server.api +++ b/docker/nginx/conf.d/server/server.api @@ -137,15 +137,13 @@ location /abuse/report { location /hns { include /etc/nginx/conf.d/include/cors; - # match the request_uri and extract the hns domain and anything that is passed in the uri after it - # example: /hns/something/foo/bar matches: - # > hns_domain: something - # > path: /foo/bar/ - set_by_lua_block $hns_domain { return string.match(ngx.var.uri, "/hns/([^/?]+)") } - set_by_lua_block $path { return string.match(ngx.var.uri, "/hns/[^/?]+(.*)") } + rewrite_by_lua_block { + local hns_domain = string.match(ngx.var.uri, "/hns/([^/?]+)") + local path = string.match(ngx.var.uri, "/hns/[^/?]+(.*)") + local hns_subdomain_url = ngx.var.scheme .. "://" .. hns_domain .. ".hns." .. ngx.var.skynet_portal_domain .. path .. ngx.var.is_args .. ngx.var.args - proxy_set_header Host $host; - include /etc/nginx/conf.d/include/location-hns; + return ngx.redirect(hns_subdomain_url, ngx.HTTP_MOVED_PERMANENTLY) + } } location /hnsres { From d53965b09531b9c127b9395241728c62ddf460b8 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Fri, 4 Mar 2022 18:20:25 +0100 Subject: [PATCH 2/4] fix no args case --- docker/nginx/conf.d/server/server.api | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docker/nginx/conf.d/server/server.api b/docker/nginx/conf.d/server/server.api index ad0e97cd..b924685d 100644 --- a/docker/nginx/conf.d/server/server.api +++ b/docker/nginx/conf.d/server/server.api @@ -140,7 +140,8 @@ location /hns { rewrite_by_lua_block { local hns_domain = string.match(ngx.var.uri, "/hns/([^/?]+)") local path = string.match(ngx.var.uri, "/hns/[^/?]+(.*)") - local hns_subdomain_url = ngx.var.scheme .. "://" .. hns_domain .. ".hns." .. ngx.var.skynet_portal_domain .. path .. ngx.var.is_args .. ngx.var.args + local args = ngx.var.args and ngx.var.is_args .. ngx.var.args or "" + local hns_subdomain_url = ngx.var.scheme .. "://" .. hns_domain .. ".hns." .. ngx.var.skynet_portal_domain .. path .. args return ngx.redirect(hns_subdomain_url, ngx.HTTP_MOVED_PERMANENTLY) } @@ -353,7 +354,8 @@ location ~ "^/(([a-zA-Z0-9-_]{46}|[a-z0-9]{55})(/.*)?)$" { 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 + local args = ngx.var.args and ngx.var.is_args .. ngx.var.args or "" + local base32_url = ngx.var.scheme .. "://" .. base32_skylink .. "." .. ngx.var.skynet_portal_domain .. ngx.var.path .. args return ngx.redirect(base32_url, ngx.HTTP_MOVED_PERMANENTLY) } From b3f1d0503172d54a51f522a91a4136b9983d87d2 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Fri, 4 Mar 2022 18:36:07 +0100 Subject: [PATCH 3/4] properly handle case where ngx.var.args is nil --- docker/nginx/conf.d/server/server.api | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docker/nginx/conf.d/server/server.api b/docker/nginx/conf.d/server/server.api index d6f7a251..55bb0fe3 100644 --- a/docker/nginx/conf.d/server/server.api +++ b/docker/nginx/conf.d/server/server.api @@ -355,7 +355,8 @@ location ~ "^/(([a-zA-Z0-9-_]{46}|[a-z0-9]{55})(/.*)?)$" { 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 + local args = ngx.var.args and ngx.var.is_args .. ngx.var.args or "" + local base32_url = ngx.var.scheme .. "://" .. base32_skylink .. "." .. ngx.var.skynet_portal_domain .. ngx.var.path .. args return ngx.redirect(base32_url, ngx.HTTP_MOVED_PERMANENTLY) } From dcba6a095097aa776fc26499b58c75fb9ac6ecfe Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Fri, 4 Mar 2022 18:48:36 +0100 Subject: [PATCH 4/4] fix hns domains using redirected skylink endpoint --- docker/nginx/conf.d/include/location-hns | 10 +++++++--- docker/nginx/conf.d/server/server.api | 1 - 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/docker/nginx/conf.d/include/location-hns b/docker/nginx/conf.d/include/location-hns index 62ff9729..fc1708e1 100644 --- a/docker/nginx/conf.d/include/location-hns +++ b/docker/nginx/conf.d/include/location-hns @@ -3,7 +3,8 @@ include /etc/nginx/conf.d/include/proxy-pass-internal; include /etc/nginx/conf.d/include/portal-access-check; # variable definititions - we need to define a variable to be able to access it in lua by ngx.var.something -set $skylink ''; # placeholder for the raw 46 bit skylink +set $skylink ''; # placeholder for the base64 skylink +set $skylink_base32 ''; # placeholder for the base32 skylink # resolve handshake domain by requesting to /hnsres endpoint and assign correct values to $skylink and $rest rewrite_by_lua_block { @@ -74,10 +75,13 @@ rewrite_by_lua_block { if ngx.var.path == "/" and skylink_rest ~= nil and skylink_rest ~= "" and skylink_rest ~= "/" then ngx.var.path = skylink_rest end + + -- assign base32 skylink to be used in proxy_pass + ngx.var.skylink_base32 = require("skynet.skylink").base32(ngx.var.skylink) } -# we proxy to another nginx location rather than directly to siad because we do not want to deal with caching here -proxy_pass https://127.0.0.1/$skylink$path$is_args$args; +proxy_set_header Host $skylink_base32.$skynet_portal_domain; +proxy_pass $scheme://$server_addr$path$is_args$args; # in case siad returns location header, we need to replace the skylink with the domain name header_filter_by_lua_block { diff --git a/docker/nginx/conf.d/server/server.api b/docker/nginx/conf.d/server/server.api index 55bb0fe3..c24ecead 100644 --- a/docker/nginx/conf.d/server/server.api +++ b/docker/nginx/conf.d/server/server.api @@ -144,7 +144,6 @@ location /hns { set_by_lua_block $hns_domain { return string.match(ngx.var.uri, "/hns/([^/?]+)") } set_by_lua_block $path { return string.match(ngx.var.uri, "/hns/[^/?]+(.*)") } - proxy_set_header Host $host; include /etc/nginx/conf.d/include/location-hns; }