Compare commits

...
This repository has been archived on 2022-10-07. You can view files and clone it, but cannot push or open issues or pull requests.

40 Commits

Author SHA1 Message Date
Ivaylo Novakov f367e013f1
all found? 2022-03-14 18:34:13 +01:00
Ivaylo Novakov a235dd6629
set limits 2022-03-14 18:30:44 +01:00
Ivaylo Novakov 3297cc0f88
get limits 2022-03-14 18:27:45 +01:00
Ivaylo Novakov 5eb013cdf8
more 2022-03-14 18:23:56 +01:00
Ivaylo Novakov a03b21107a
simple 2022-03-14 18:20:51 +01:00
Ivaylo Novakov a3a5d6fa88
make sure 2022-03-14 18:03:08 +01:00
Ivaylo Novakov 0e793f3456
accs 2022-03-14 17:55:12 +01:00
Ivaylo Novakov c4dc3222b2
accs 2022-03-14 17:53:02 +01:00
Ivaylo Novakov 45fa704d77
empty abl block 2022-03-14 17:49:56 +01:00
Ivaylo Novakov 902de07aec
. 2022-03-14 17:48:07 +01:00
Ivaylo Novakov 79c205c298
limit rates 2022-03-14 17:44:08 +01:00
Ivaylo Novakov dcf6e85448
proxy buffer 2022-03-14 17:40:34 +01:00
Ivaylo Novakov d99cacebdc
step 2022-03-14 17:37:33 +01:00
Ivaylo Novakov 8a7f41701d
fix 2022-03-14 17:36:02 +01:00
Ivaylo Novakov 4f93028c1a
fix 2022-03-14 17:32:53 +01:00
Ivaylo Novakov 7ff42c4326
Revert "fix"
This reverts commit cfbe65e8d5.
2022-03-14 17:32:37 +01:00
Ivaylo Novakov cfbe65e8d5
fix 2022-03-14 17:32:11 +01:00
Ivaylo Novakov 33a61eb307
comment the new stuff, check again 2022-03-14 17:26:57 +01:00
Ivaylo Novakov b92a515126
restore the rest? 2022-03-14 17:21:15 +01:00
Ivaylo Novakov e9e63a2625
is it the proxy cache itself? 2022-03-14 17:17:35 +01:00
Ivaylo Novakov e1d9037902
proxy cache inline 2022-03-14 16:13:38 +01:00
Ivaylo Novakov c8d5abb397
single set_by_lua_block 2022-03-14 16:10:48 +01:00
Ivaylo Novakov e5adf4017d
Revert "nocache=0?"
This reverts commit 39454e0ded.
2022-03-14 16:07:19 +01:00
Ivaylo Novakov 39454e0ded
nocache=0? 2022-03-14 14:41:44 +01:00
Ivaylo Novakov 08c0d35a1e
nocache=1 2022-03-14 14:39:02 +01:00
Ivaylo Novakov 5eb41d7762
put back the rest of it? 2022-03-14 14:32:48 +01:00
Ivaylo Novakov d2884d3f5e
proxy-cache-download? 2022-03-14 14:29:13 +01:00
Ivaylo Novakov 5c246d8299
one step back 2022-03-14 14:24:35 +01:00
Ivaylo Novakov 65c6e37b38
go back to super simple 2022-03-14 14:21:50 +01:00
Ivaylo Novakov 0678e298e2
disable nginx cache download 2022-03-14 14:17:49 +01:00
Ivaylo Novakov 9b95c671ac
disable nginx cache download 2022-03-14 14:13:31 +01:00
Ivaylo Novakov f5ac3da694
ops 2022-03-14 12:25:49 +01:00
Ivaylo Novakov 197c05fef2
skylinkv1 2022-03-14 12:22:18 +01:00
Ivaylo Novakov c2d9a3c0ba
. 2022-03-14 12:19:25 +01:00
Ivaylo Novakov 789d179475
put everything back and remove only rate limiting 2022-03-14 12:12:54 +01:00
Karol Wypchlo a271f1e90f
foo 2022-03-11 09:17:29 +01:00
Karol Wypchlo 8ee5b56cf1
foo 2022-03-11 09:15:37 +01:00
Karol Wypchlo 0f3df03f43
foo 2022-03-11 09:14:08 +01:00
Karol Wypchlo 993ef93fb0
foo 2022-03-11 09:12:46 +01:00
Karol Wypchlo a0e908fdce
foo 2022-03-11 09:10:12 +01:00
2 changed files with 79 additions and 75 deletions

View File

@ -25,85 +25,85 @@ set $skynet_proof '';
set $limit_rate 0; set $limit_rate 0;
access_by_lua_block { access_by_lua_block {
-- the block below only makes sense if we are using nginx cache local accounts = require("skynet.account")
if not ngx.var.skyd_disk_cache_enabled then if accounts.accounts_enabled() then
local httpc = require("resty.http").new()
-- 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"
if isBase32v2 or isBase64v2 then
-- 10.10.10.10 points to sia service (alias not available when using resty-http)
local res, err = httpc:request_uri("http://10.10.10.10:9980/skynet/resolve/" .. ngx.var.skylink_v2, {
headers = { ["User-Agent"] = "Sia-Agent" }
})
-- print error and exit with 500 or exit with response if status is not 200
if err or (res and res.status ~= ngx.HTTP_OK) then
ngx.status = (err and ngx.HTTP_INTERNAL_SERVER_ERROR) or res.status
ngx.header["content-type"] = "text/plain"
ngx.say(err or res.body)
return ngx.exit(ngx.status)
end
local json = require('cjson')
local resolve = json.decode(res.body)
ngx.var.skylink_v1 = resolve.skylink
ngx.var.skynet_proof = res.headers["Skynet-Proof"]
end
-- check if skylink v1 is present on blocklist (compare hashes)
if require("skynet.blocklist").is_blocked(ngx.var.skylink_v1) then
return require("skynet.blocklist").exit_illegal()
end
-- if skylink is found on nocache list then set internal nocache variable
-- to tell nginx that it should not try and cache this file (too large)
if ngx.shared.nocache:get(ngx.var.skylink_v1) then
ngx.var.nocache = "1"
end
end
if require("skynet.account").accounts_enabled() then
-- check if portal is in authenticated only mode -- check if portal is in authenticated only mode
if require("skynet.account").is_access_unauthorized() then if accounts.is_access_unauthorized() then
return require("skynet.account").exit_access_unauthorized() return accounts.exit_access_unauthorized()
end end
-- check if portal is in subscription only mode -- check if portal is in subscription only mode
if require("skynet.account").is_access_forbidden() then if accounts.is_access_forbidden() then
return require("skynet.account").exit_access_forbidden() return accounts.exit_access_forbidden()
end end
-- get account limits of currently authenticated user -- get account limits of currently authenticated user
local limits = require("skynet.account").get_account_limits() local limits = accounts.get_account_limits()
-- apply download speed limit -- apply download speed limit
ngx.var.limit_rate = limits.download -- ngx.var.limit_rate = limits.download
end end
-- -- the block below only makes sense if we are using nginx cache
-- if not ngx.var.skyd_disk_cache_enabled then
-- local httpc = require("resty.http").new()
--
-- -- 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"
--
-- if isBase32v2 or isBase64v2 then
-- -- 10.10.10.10 points to sia service (alias not available when using resty-http)
-- local res, err = httpc:request_uri("http://10.10.10.10:9980/skynet/resolve/" .. ngx.var.skylink_v2, {
-- headers = { ["User-Agent"] = "Sia-Agent" }
-- })
--
-- -- print error and exit with 500 or exit with response if status is not 200
-- if err or (res and res.status ~= ngx.HTTP_OK) then
-- ngx.status = (err and ngx.HTTP_INTERNAL_SERVER_ERROR) or res.status
-- ngx.header["content-type"] = "text/plain"
-- ngx.say(err or res.body)
-- return ngx.exit(ngx.status)
-- end
--
-- local json = require('cjson')
-- local resolve = json.decode(res.body)
-- ngx.var.skylink_v1 = resolve.skylink
-- ngx.var.skynet_proof = res.headers["Skynet-Proof"]
-- end
--
-- -- check if skylink v1 is present on blocklist (compare hashes)
-- if require("skynet.blocklist").is_blocked(ngx.var.skylink_v1) then
-- return require("skynet.blocklist").exit_illegal()
-- end
--
-- -- if skylink is found on nocache list then set internal nocache variable
-- -- to tell nginx that it should not try and cache this file (too large)
-- if ngx.shared.nocache:get(ngx.var.skylink_v1) then
-- ngx.var.nocache = "1"
-- end
-- end
} }
header_filter_by_lua_block { header_filter_by_lua_block {
ngx.header["Skynet-Portal-Api"] = ngx.var.scheme .. "://" .. ngx.var.skynet_portal_domain ngx.header["Skynet-Portal-Api"] = ngx.var.scheme .. "://" .. ngx.var.skynet_portal_domain
ngx.header["Skynet-Server-Api"] = ngx.var.scheme .. "://" .. ngx.var.skynet_server_domain ngx.header["Skynet-Server-Api"] = ngx.var.scheme .. "://" .. ngx.var.skynet_server_domain
-- the block below only makes sense if we are using nginx cache -- -- the block below only makes sense if we are using nginx cache
if not ngx.var.skyd_disk_cache_enabled then -- if not ngx.var.skyd_disk_cache_enabled then
-- not empty skynet_proof means this is a skylink v2 request -- -- not empty skynet_proof means this is a skylink v2 request
-- so we should replace the Skynet-Proof header with the one -- -- so we should replace the Skynet-Proof header with the one
-- we got from /skynet/resolve/ endpoint, otherwise we would -- -- we got from /skynet/resolve/ endpoint, otherwise we would
-- be serving cached empty v1 skylink Skynet-Proof header -- -- be serving cached empty v1 skylink Skynet-Proof header
if ngx.var.skynet_proof and ngx.var.skynet_proof ~= "" then -- if ngx.var.skynet_proof and ngx.var.skynet_proof ~= "" then
ngx.header["Skynet-Proof"] = ngx.var.skynet_proof -- ngx.header["Skynet-Proof"] = ngx.var.skynet_proof
end -- end
--
-- add skylink to nocache list if it exceeds 1GB (1e+9 bytes) threshold -- -- add skylink to nocache list if it exceeds 1GB (1e+9 bytes) threshold
-- (content length can be nil for already cached files - we can ignore them) -- -- (content length can be nil for already cached files - we can ignore them)
if ngx.header["Content-Length"] and tonumber(ngx.header["Content-Length"]) > 1e+9 then -- if ngx.header["Content-Length"] and tonumber(ngx.header["Content-Length"]) > 1e+9 then
ngx.shared.nocache:set(ngx.var.skylink_v1, ngx.header["Content-Length"]) -- ngx.shared.nocache:set(ngx.var.skylink_v1, ngx.header["Content-Length"])
end -- end
end -- end
} }
limit_rate_after 512k; limit_rate_after 512k;

View File

@ -1,7 +1,8 @@
proxy_cache skynet; # cache name #proxy_cache skynet; # cache name
proxy_cache_key $skylink_v1$path$arg_format$arg_attachment$arg_start$arg_end$http_range; # unique cache key #proxy_cache_key $skylink_v1$path$arg_format$arg_attachment$arg_start$arg_end$http_range; # unique cache key
proxy_cache_min_uses 3; # cache after 3 uses #proxy_cache_key $skylink$path$arg_format$arg_attachment$arg_start$arg_end$http_range; # unique cache key
proxy_cache_valid 200 206 307 308 48h; # keep 200, 206, 307 and 308 responses valid for up to 2 days #proxy_cache_min_uses 3; # cache after 3 uses
#proxy_cache_valid 200 206 307 308 48h; # keep 200, 206, 307 and 308 responses valid for up to 2 days
add_header X-Proxy-Cache $upstream_cache_status; # add response header to indicate cache hits and misses add_header X-Proxy-Cache $upstream_cache_status; # add response header to indicate cache hits and misses
# map skyd env variable value to "1" for true and "0" for false (expected by proxy_no_cache) # map skyd env variable value to "1" for true and "0" for false (expected by proxy_no_cache)
@ -9,6 +10,9 @@ set_by_lua_block $skyd_disk_cache_enabled {
return os.getenv("SKYD_DISK_CACHE_ENABLED") == "true" and "1" or "0" return os.getenv("SKYD_DISK_CACHE_ENABLED") == "true" and "1" or "0"
} }
# I want to execute the above block but I also want to have it be a 1. # TODO: Remove this.
set $skyd_disk_cache_enabled "1";
# bypass - this will bypass cache hit on request (status BYPASS) # bypass - this will bypass cache hit on request (status BYPASS)
# but still stores file in cache if cache conditions are met # but still stores file in cache if cache conditions are met
proxy_cache_bypass $cookie_nocache $arg_nocache $skyd_disk_cache_enabled; proxy_cache_bypass $cookie_nocache $arg_nocache $skyd_disk_cache_enabled;