@@ -2,57 +2,37 @@ include /etc/nginx/conf.d/include/init-optional-variables;
2
2
3
3
location / {
4
4
set $skylink "";
5
- set $path $uri ;
5
+ set $path "" ;
6
6
7
7
rewrite_by_lua_block {
8
+ local basexx = require("basexx")
8
9
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
+ )
11
16
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)
48
19
49
20
ngx.var.skylink = resolved.skylink
21
+ ngx.var.path = resolved.path
22
+
50
23
if resolved.sponsor then
51
24
ngx.req.set_header("Skynet-Api-Key", resolved.sponsor)
52
25
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)
53
35
end
54
-
55
- ngx.var.skylink = require("skynet.skylink").parse(ngx.var.skylink)
56
36
}
57
37
58
38
include /etc/nginx/conf.d/include/location-skylink;
0 commit comments