adjust hns endpoint

This commit is contained in:
Karol Wypchlo 2020-09-16 14:21:22 +02:00
parent b7578c00c9
commit 54cda20b32
1 changed files with 15 additions and 20 deletions

View File

@ -9,6 +9,7 @@ limit_conn_status 429;
# as the request address so we need to use real_ip_header module to use ip address from
# X-Forwarded-For header as a real ip address of the request
set_real_ip_from 10.0.0.0/8;
set_real_ip_from 127.0.0.1/32;
set_real_ip_from 172.16.0.0/12;
set_real_ip_from 192.168.0.0/16;
real_ip_header X-Forwarded-For;
@ -127,42 +128,36 @@ server {
}
location /hns {
include /etc/nginx/conf.d/include/cors;
include /etc/nginx/conf.d/include/proxy-buffer;
include /etc/nginx/conf.d/include/proxy-cache-downloads;
limit_conn downloads_by_ip 100; # ddos protection: max 100 downloads at a time
add_header Cache-Control "public, max-age=300"; # allow consumer to cache response just for 5 minutes
set $skylink '';
set $skylink_path '';
set $rest '';
access_by_lua_block {
local json = require('cjson')
local hns_domain_name, request_uri_path = string.match(ngx.var.request_uri, "/hns/([^/]+)(.*)")
local hns_domain_name, request_uri_rest = string.match(ngx.var.request_uri, "/hns/([^/?]+)(.*)")
local hnsres_res = ngx.location.capture("/hnsres/" .. hns_domain_name)
local hnsres_json = json.decode(hnsres_res.body)
local skylink_prefix, skylink, skylink_path = string.match(hnsres_json.skylink, "(sia://)([^/]+)(.*)")
local skylink_prefix, skylink, skylink_rest = string.match(hnsres_json.skylink, "(sia://)([^/?]+)(.*)")
ngx.var.skylink = skylink
if request_uri_path == "" or (request_uri_path == "/" and skylink_path ~= "") then
ngx.var.skylink_path = skylink_path
if request_uri_rest == "" or (request_uri_rest == "/" and skylink_rest ~= "") then
ngx.var.rest = skylink_rest
else
ngx.var.skylink_path = request_uri_path
ngx.var.rest = request_uri_rest
end
}
proxy_read_timeout 600;
proxy_set_header User-Agent: Sia-Agent;
proxy_pass http://siad/skynet/skylink/$skylink$skylink_path;
# overwrite the Cache-Control header to only cache for 60s in case the domain gets updated
more_set_headers 'Cache-Control: public, max-age=60';
proxy_pass http://127.0.0.1/$skylink$rest;
header_filter_by_lua_block {
if ngx.header.Location then
local json = require('cjson')
local hns_domain_name = string.match(ngx.var.request_uri, "/hns/(.+)/?")
local location = string.gsub(ngx.header.Location, ngx.var.skylink, hns_domain_name)
if ngx.header.location then
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
ngx.header.location = location
end
}
}