diff --git a/nginx/conf.d/server/server.dnslink b/nginx/conf.d/server/server.dnslink index 90603da..f274429 100644 --- a/nginx/conf.d/server/server.dnslink +++ b/nginx/conf.d/server/server.dnslink @@ -2,57 +2,37 @@ include /etc/nginx/conf.d/include/init-optional-variables; location / { set $skylink ""; - set $path $uri; + set $path ""; rewrite_by_lua_block { + local basexx = require("basexx") local cjson = require("cjson") - local cache = ngx.shared.dnslink - local cache_value = cache:get(ngx.var.host) + local httpc = require("resty.http").new() + + -- 10.10.10.55 points to dnslink-api service (alias not available when using resty-http) + local res, err = httpc:request_uri( + "http://10.10.10.55:3100/dnslink/" .. ngx.var.host .. "/" .. basexx.to_base64(ngx.var.uri) + ) - if cache_value == nil then - local httpc = require("resty.http").new() - - -- 10.10.10.55 points to dnslink-api service (alias not available when using resty-http) - local res, err = httpc:request_uri("http://10.10.10.55:3100/dnslink/" .. ngx.var.host) - - if err or (res and res.status ~= ngx.HTTP_OK) then - -- check whether we can fallback to regular skylink request - local match_skylink = ngx.re.match(ngx.var.uri, "^/([a-zA-Z0-9-_]{46}|[a-z0-9]{55})(/.*)?") - - if match_skylink then - ngx.var.skylink = match_skylink[1] - ngx.var.path = match_skylink[2] or "/" - else - ngx.status = (err and ngx.HTTP_INTERNAL_SERVER_ERROR) or res.status - -- do not send response on unauthorized error (status code 401) - -- otherwise we will not be able to display custom error page - if ngx.status ~= ngx.HTTP_UNAUTHORIZED then - ngx.header["content-type"] = "text/plain" - ngx.say(err or res.body) - end - ngx.exit(ngx.status) - end - else - local resolved = cjson.decode(res.body) - - ngx.var.skylink = resolved.skylink - if resolved.sponsor then - ngx.req.set_header("Skynet-Api-Key", resolved.sponsor) - end - - local cache_ttl = 300 -- 5 minutes cache expire time - cache:set(ngx.var.host, res.body, cache_ttl) - end - else - local resolved = cjson.decode(cache_value) + if res and res.status == ngx.HTTP_OK then + local resolved = cjson.decode(res.body) ngx.var.skylink = resolved.skylink + ngx.var.path = resolved.path + if resolved.sponsor then ngx.req.set_header("Skynet-Api-Key", resolved.sponsor) end + else + ngx.status = (err and ngx.HTTP_INTERNAL_SERVER_ERROR) or res.status + -- do not send response on unauthorized error (status code 401) + -- otherwise we will not be able to display custom error page + if ngx.status ~= ngx.HTTP_UNAUTHORIZED then + ngx.header["content-type"] = "text/plain" + ngx.say(err or res.body) + end + ngx.exit(ngx.status) end - - ngx.var.skylink = require("skynet.skylink").parse(ngx.var.skylink) } include /etc/nginx/conf.d/include/location-skylink;