-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_router.lua
58 lines (48 loc) · 1.4 KB
/
service_router.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
local resolver = require "resty.dns.resolver"
function abort(reason, code)
ngx.status = code
ngx.say(reason)
return code
end
function log_error(msg)
ngx.log(ngx.ERR, msg, "\n")
end
function log_info(msg)
ngx.log(ngx.INFO, msg, "\n")
end
if string.find(ngx.var.service, ":") then
ngx.var.target = ngx.var.service
return
end
local nameserver = {ngx.var.ns_ip, ngx.var.ns_port}
local dns, err = resolver:new{
nameservers = {nameserver}, retrans = 3
}
if not dns then
log_error("failed to instantiate the resolver: " .. err)
return abort("DNS error", 500)
end
log_info("Querying " .. ngx.var.service)
local records, err = dns:query(ngx.var.service, {qtype = dns.TYPE_SRV})
if not records then
log_error("failed to query the DNS server: " .. err)
return abort("Internal routing error", 500)
end
if records.errcode then
-- error code meanings available in http://bit.ly/1ppRk24
if records.errcode == 3 then
return abort("Not found", 404)
else
log_error("DNS error #" .. records.errcode .. ": " .. records.errstr)
return abort("DNS error", 500)
end
end
if records[1].port then
-- resolve the target to an IP
local target_ip = dns:query(records[1].target)[1].address
-- pass the target ip to avoid resolver errors
ngx.var.target = target_ip .. ":" .. records[1].port
else
log_error("DNS answer didn't include a port")
return abort("Unknown destination port", 500)
end