support v2 caching

This commit is contained in:
Karol Wypchlo 2021-06-01 12:17:08 +02:00
parent 45ce3de7c2
commit 60cb812148
1 changed files with 29 additions and 1 deletions

View File

@ -409,6 +409,13 @@ server {
proxy_pass http://siad;
}
location /skynet/resolve {
include /etc/nginx/conf.d/include/cors;
proxy_set_header User-Agent: Sia-Agent;
proxy_pass http://siad;
}
location ~ "^/(([a-zA-Z0-9-_]{46}|[a-z0-9]{55})(/.*)?)$" {
include /etc/nginx/conf.d/include/cors;
include /etc/nginx/conf.d/include/proxy-buffer;
@ -427,13 +434,24 @@ server {
set $skylink $2;
set $path $3;
# v2 support
set $skylink_v1 $skylink;
access_by_lua_block {
-- disable cache if this is skylink 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"
if isBase32v2 or isBase64v2 then
ngx.var.nocache = 1 # nginx expects 0/1 for boolean
local res = ngx.location.capture("/skynet/resolve/" .. ngx.var.skylink)
if res.status == ngx.HTTP_OK then
local json = require('cjson')
local resolve = json.decode(res.body)
ngx.var.skylink_v1 = resolve.skylink
else
ngx.say(res.body)
ngx.exit(res.status)
end
end
-- this block runs only when accounts are enabled
@ -447,6 +465,16 @@ server {
end
}
# in case siad returns location header and we have skylink v2, we need to replace the skylink v1 with v2
header_filter_by_lua_block {
if ngx.var.skylink != ngx.var.skylink_v1 then
if ngx.header.location then
ngx.header.location = ngx.header.location:gsub("^" .. ngx.var.skylink_v1, ngx.var.skylink)
end
ngx.header["Skynet-Skylink"] = ngx.var.skylink
end
}
# register the download in accounts service (cookies should contain jwt)
log_by_lua_block {
-- this block runs only when accounts are enabled