Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 051df52

Browse files
authored
Merge pull request #34 from SkynetLabs/simplify-dnslink-nginx-logic
simplify dnslink logic
2 parents 872550d + 1ff8a62 commit 051df52

File tree

1 file changed

+21
-41
lines changed

1 file changed

+21
-41
lines changed

nginx/conf.d/server/server.dnslink

+21-41
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,37 @@ include /etc/nginx/conf.d/include/init-optional-variables;
22

33
location / {
44
set $skylink "";
5-
set $path $uri;
5+
set $path "";
66

77
rewrite_by_lua_block {
8+
local basexx = require("basexx")
89
local cjson = require("cjson")
9-
local cache = ngx.shared.dnslink
10-
local cache_value = cache:get(ngx.var.host)
10+
local httpc = require("resty.http").new()
11+
12+
-- 10.10.10.55 points to dnslink-api service (alias not available when using resty-http)
13+
local res, err = httpc:request_uri(
14+
"http://10.10.10.55:3100/dnslink/" .. ngx.var.host .. "/" .. basexx.to_base64(ngx.var.uri)
15+
)
1116

12-
if cache_value == nil then
13-
local httpc = require("resty.http").new()
14-
15-
-- 10.10.10.55 points to dnslink-api service (alias not available when using resty-http)
16-
local res, err = httpc:request_uri("http://10.10.10.55:3100/dnslink/" .. ngx.var.host)
17-
18-
if err or (res and res.status ~= ngx.HTTP_OK) then
19-
-- check whether we can fallback to regular skylink request
20-
local match_skylink = ngx.re.match(ngx.var.uri, "^/([a-zA-Z0-9-_]{46}|[a-z0-9]{55})(/.*)?")
21-
22-
if match_skylink then
23-
ngx.var.skylink = match_skylink[1]
24-
ngx.var.path = match_skylink[2] or "/"
25-
else
26-
ngx.status = (err and ngx.HTTP_INTERNAL_SERVER_ERROR) or res.status
27-
-- do not send response on unauthorized error (status code 401)
28-
-- otherwise we will not be able to display custom error page
29-
if ngx.status ~= ngx.HTTP_UNAUTHORIZED then
30-
ngx.header["content-type"] = "text/plain"
31-
ngx.say(err or res.body)
32-
end
33-
ngx.exit(ngx.status)
34-
end
35-
else
36-
local resolved = cjson.decode(res.body)
37-
38-
ngx.var.skylink = resolved.skylink
39-
if resolved.sponsor then
40-
ngx.req.set_header("Skynet-Api-Key", resolved.sponsor)
41-
end
42-
43-
local cache_ttl = 300 -- 5 minutes cache expire time
44-
cache:set(ngx.var.host, res.body, cache_ttl)
45-
end
46-
else
47-
local resolved = cjson.decode(cache_value)
17+
if res and res.status == ngx.HTTP_OK then
18+
local resolved = cjson.decode(res.body)
4819

4920
ngx.var.skylink = resolved.skylink
21+
ngx.var.path = resolved.path
22+
5023
if resolved.sponsor then
5124
ngx.req.set_header("Skynet-Api-Key", resolved.sponsor)
5225
end
26+
else
27+
ngx.status = (err and ngx.HTTP_INTERNAL_SERVER_ERROR) or res.status
28+
-- do not send response on unauthorized error (status code 401)
29+
-- otherwise we will not be able to display custom error page
30+
if ngx.status ~= ngx.HTTP_UNAUTHORIZED then
31+
ngx.header["content-type"] = "text/plain"
32+
ngx.say(err or res.body)
33+
end
34+
ngx.exit(ngx.status)
5335
end
54-
55-
ngx.var.skylink = require("skynet.skylink").parse(ngx.var.skylink)
5636
}
5737

5838
include /etc/nginx/conf.d/include/location-skylink;

0 commit comments

Comments
 (0)