Fix broken hns redirect (#415)

This commit is contained in:
Karol Wypchło 2020-09-21 18:29:47 +02:00 committed by GitHub
parent 75397bce2a
commit 98cd70faea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 4 deletions

View File

@ -130,24 +130,42 @@ server {
location /hns { location /hns {
include /etc/nginx/conf.d/include/proxy-buffer; include /etc/nginx/conf.d/include/proxy-buffer;
# 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 raw 46 bit skylink
set $rest ''; # placeholder for the rest of the url that gets appended to skylink (path and args) set $rest ''; # placeholder for the rest of the url that gets appended to skylink (path and args)
# resolve handshake domain by requesting to /hnsres endpoint and assign correct values to $skylink and $rest # resolve handshake domain by requesting to /hnsres endpoint and assign correct values to $skylink and $rest
access_by_lua_block { access_by_lua_block {
local json = require('cjson') local json = require('cjson')
-- match the request_uri and extract the hns domain and anything that is passed in the uri after it
-- example: /hns/something/foo/bar?baz=1 matches:
-- > hns_domain_name: something
-- > request_uri_rest: /foo/bar/?baz=1
local hns_domain_name, request_uri_rest = string.match(ngx.var.request_uri, "/hns/([^/?]+)(.*)") local hns_domain_name, request_uri_rest = string.match(ngx.var.request_uri, "/hns/([^/?]+)(.*)")
-- make a get request to /hnsres endpoint with the domain name from request_uri
local hnsres_res = ngx.location.capture("/hnsres/" .. hns_domain_name) local hnsres_res = ngx.location.capture("/hnsres/" .. hns_domain_name)
-- we want to fail with a generic 404 when /hnsres returns anything but 200 OK with a skylink
if hnsres_res.status ~= ngx.HTTP_OK then if hnsres_res.status ~= ngx.HTTP_OK then
ngx.exit(ngx.HTTP_NOT_FOUND) ngx.exit(ngx.HTTP_NOT_FOUND)
end end
-- since /hnsres endpoint response is a json, we need to decode it before we access it
-- example response: '{"skylink":"sia://XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg"}'
local hnsres_json = json.decode(hnsres_res.body) local hnsres_json = json.decode(hnsres_res.body)
local skylink_prefix, skylink, skylink_rest = string.match(hnsres_json.skylink, "(sia://)([^/?]+)(.*)")
-- try to match the skylink with sia:// prefix
local skylink, skylink_rest = string.match(hnsres_json.skylink, "sia://([^/?]+)(.*)")
-- in case the skylink did not match, assume that there is no sia:// prefix and try to match again
if skylink == nil then
skylink, skylink_rest = string.match(hnsres_json.skylink, "/?([^/?]+)(.*)")
end
ngx.var.skylink = skylink ngx.var.skylink = skylink
if request_uri_rest == "" or (request_uri_rest == "/" and skylink_rest ~= "") then if request_uri_rest == "/" and skylink_rest ~= "" and skylink_rest ~= "/" then
ngx.var.rest = skylink_rest ngx.var.rest = skylink_rest
else else
ngx.var.rest = request_uri_rest ngx.var.rest = request_uri_rest
@ -163,10 +181,15 @@ server {
# in case siad returns location header, we need to replace the skylink with the domain name # in case siad returns location header, we need to replace the skylink with the domain name
header_filter_by_lua_block { header_filter_by_lua_block {
if ngx.header.location then if ngx.header.location then
-- match hns domain from the request_uri
local hns_domain_name = string.match(ngx.var.request_uri, "/hns/([^/?]+)") local hns_domain_name = string.match(ngx.var.request_uri, "/hns/([^/?]+)")
local location = string.gsub(ngx.header.location, ngx.var.skylink, hns_domain_name)
ngx.header.location = location -- match location redirect part after the skylink
local location_rest = string.match(ngx.header.location, "[^/?]+(.*)");
-- because siad will set the location header to ie. XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg/index.html
-- we need to replace the skylink with the domain_name so we are not redirected to skylink
ngx.header.location = hns_domain_name .. location_rest
end end
} }
} }