forked from PowerDNS/pdns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dnsbackend.rb
143 lines (124 loc) · 3.56 KB
/
dnsbackend.rb
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require 'json'
require 'thread'
class DNSBackendHandler < WEBrick::HTTPServlet::AbstractServlet
def initialize(server, dnsbackend)
@dnsbackend = dnsbackend
@semaphore = Mutex.new
unless defined? @@f
@@f = File.open("/tmp/remotebackend.txt.#{$$}","a")
@@f.sync
end
@dnsbackend.do_initialize({})
end
def parse_arrays(params)
newparams = {}
params.each do |key,val|
if key=~/^(.*)\[(.*)\]\[(.*)\]/
newparams[$1] = {} unless newparams.has_key? $1
newparams[$1][$2] = {} unless newparams[$1].has_key? $2
newparams[$1][$2][$3] = val
params.delete key
elsif key=~/^(.*)\[(.*)\]/
if $2 == ""
newparams[$1] = [] unless newparams.has_key? $1
newparams[$1] << val
else
newparams[$1] = {} unless newparams.has_key? $1
newparams[$1][$2] = val
end
params.delete key
end
end
params.merge newparams
end
def parse_url(url)
url = url.split('/')
method = url.shift.downcase
# do some determining based on method names
args = case method
when "lookup"
{
"qname" => url.shift,
"qtype" => url.shift,
}
when "list"
{
"id" => url.shift,
"zonename" => url.shift
}
when "getbeforeandafternamesabsolute", "getbeforeandafternames"
{
"id" => url.shift.to_i,
"qname" => url.shift
}
when "getdomainmetadata", "setdomainmetadata", "getdomainkeys"
{
"name" => url.shift,
"kind" => url.shift
}
when "removedomainkey", "activatedomainkey", "deactivatedomainkey"
{
"id" => url.shift,
"name" => url.shift
}
when "adddomainkey", "gettsigkey", "getdomaininfo"
{
"name" => url.shift
}
else
{
}
end
[method, args]
end
def do_GET(req,res)
req.continue
tmp = req.path[/dns\/(.*)/,1]
return 400, "Bad request" if (tmp.nil?)
method, args = parse_url(tmp.force_encoding("UTF-8"))
method = "do_#{method}"
# get more arguments
req.each do |k,v|
attr = k[/x-remotebackend-(.*)/i,1]
if attr
args[attr.downcase] = v.force_encoding("UTF-8")
end
end
args = args.merge req.query
if method == "do_adddomainkey"
args["key"] = {
"flags" => args.delete("flags").to_i,
"active" => args.delete("active").to_i,
"content" => args.delete("content")
}
end
args = parse_arrays args
@@f.puts "#{Time.now.to_f} [http]: #{({:method=>method,:parameters=>args}).to_json}"
@semaphore.synchronize do
if @dnsbackend.respond_to?(method.to_sym)
result, log = @dnsbackend.send(method.to_sym, args)
body = {:result => result, :log => log}
res.status = 200
res["Content-Type"] = "application/javascript; charset=utf-8"
res.body = body.to_json
else
res.status = 404
res["Content-Type"] = "application/javascript; charset=utf-8"
res.body = ({:result => false, :log => ["Method not found"]}).to_json
end
@@f.puts "#{Time.now.to_f} [http]: #{res.body}"
end
end
def do_DELETE(req,res)
do_GET(req,res)
end
def do_POST(req,res)
do_GET(req,res)
end
def do_PATCH(req,res)
do_GET(req,res)
end
def do_PUT(req,res)
do_GET(req,res)
end
end