-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
305 lines (288 loc) · 9.57 KB
/
app.js
File metadata and controls
305 lines (288 loc) · 9.57 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
addEventListener('fetch', event => {
event.respondWith(fetchAndRespond(event.request))
})
function cleanArray(actual) {
var newArray = new Array();
for (var i = 0; i < actual.length; i++) {
if (actual[i]) {
newArray.push(actual[i]);
}
}
return newArray;
}
function returnHeader(title="Turning dig... to JSON") {
html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>dnsjson.com | ${title}</title>
<link href="/static/bootstrap.min.css" rel="stylesheet">
<style type="text/css">body{padding-top:50px;margin-bottom:60px}.starter-template{padding:40px 15px;text-align:center}.btn-primary,.navbar-inverse,.panel-primary>.panel-heading{background-color:#0077da}a.navbar-brand{color:#fff}.footer{position:fixed;bottom:0;width:100%;height:60px;background-color:#f5f5f5}.container .text-muted{margin:20px 0;color:#707070}</style>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/">dnsjson.com</a>
</div>
</div>
</nav>`
return html
}
function returnFooter() {
html = `<footer class="footer">
<div class="container">
<p class="text-muted">© <script type="text/javascript">document.write(new Date().getFullYear());</script> dnsjson.com - This script is written using only <a href="https://cloudflareworkers.com" target="_blank">Cloudflare Workers</a></p>',
</div>
</footer>
<script src="/static/jquery.min.js"></script>
<script src="/static/bootstrap.min.js"></script>
</body>
</html>`
return html
}
/**
* Fetch and log a given request object
* @param {Request} request
*/
async function fetchAndRespond(request) {
const init = {
method: 'GET'
}
const supportedTypes = [
'A',
'AAAA',
'CAA',
'CNAME',
'DS',
'DNSKEY',
'MX',
'NS',
'NSEC',
'NSEC3',
'RRSIG',
'SOA',
'TXT'
]
reqUrl = new URL(request.url).pathname
if (reqUrl == '/') {
if (request.method == 'POST') {
const formData = await request.formData()
if (formData.get('recordType') == 'Type') {
recordType = 'A'
}
else {
recordType = formData.get('recordType')
}
return new Response('', {
status: 301,
headers: {'Location': `/${formData.get('recordName')}/${recordType}`}
})
}
if (request.method == 'GET') {
let data = `<div class="container">
<div class="starter-template">
<h1>Welcome to dnsjson</h1>
<p class="lead">
Need DNS? Need it in an application accessible method?<br>
If you need this, then dnsjson.com is for you!
</p>
<p> </p>
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="panel-title">Lookup a record</h3>
</div>
<div class="panel-body">
<form class="form-inline" method="POST" action="/">
<fieldset>
<div class="form-group">
<input name="recordName" class="form-control" id="recordName" placeholder="example.com">
</div>
<div class="form-group">
<select class="form-control" id="recordType" name="recordType">
<option>Type</option>
<option>${supportedTypes.join('</option><option>')}</option>
</select>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Lookup</button>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
`
output = [
returnHeader('Turning dig... to JSON'),
data,
returnFooter()
]
return new Response(output.join('\r\n'), {
headers: {
'Content-Type': 'text/html',
'Access-Control-Allow-Origin': '*'
}
})
}
}
if (reqUrl == '/static/bootstrap.min.css') {
req = await fetch('https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.6/cosmo/bootstrap.min.css')
req = await(req.body)
return new Response(req, {
headers: {
'Content-Type': 'text/css',
'Access-Control-Allow-Origin': '*'
}
})
}
if (reqUrl == '/static/bootstrap.min.js') {
req = await fetch('https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js')
req = await(req.body)
return new Response(req, {
headers: {
'Content-Type': 'application/javascript',
'Access-Control-Allow-Origin': '*'
}
})
}
if (reqUrl == '/static/jquery.min.js') {
req = await fetch('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js')
req = await(req.body)
return new Response(req, {
headers: {
'Content-Type': 'text/javascript',
'Access-Control-Allow-Origin': '*'
}
})
}
else {
reqUrl = cleanArray(new URL(request.url).pathname.split('/'))
domain = reqUrl[0].toLowerCase();
data = reqUrl[1].toLowerCase().split('.');
if (data.length < 2) {
qtype = reqUrl[1].toUpperCase()
ctype = 'html'
}
else {
qtype = data[0].toUpperCase()
ctype = data[1]
}
if (supportedTypes.includes(qtype)) {
url = 'https://cloudflare-dns.com/dns-query?ct=application/dns-json&name=' + domain + '&type=' + qtype
req = await fetch(url, init)
req = await req.json()
res = {
'success': true,
'results': {
'name': domain,
'type': qtype,
'records': []
}
}
for (var val in req.Answer) {
res['results']['records'].push(req.Answer[val]['data'])
}
if (res['results']['records'].length < 1) {
res['results']['records'].push(`Unable to find any ${qtype} records for ${domain}`)
}
if (request.headers.get('Content-Type') == 'text/plain' || ctype == 'txt') {
headers = {
headers: {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin': '*'
}
}
return new Response(res['results']['records'].join('\r\n'), headers)
}
if (request.headers.get('Content-Type') == 'application/json' || ctype == 'json') {
headers = {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
}
return new Response(JSON.stringify(res), headers)
}
if (request.headers.get('Content-Type') == 'text/html' || ctype == 'html')
data = `<div class="container">
<div class="starter-template">
<div class="well well-lg">
<h1>${res['results']['name']} (${res['results']['type']})</h1>
<p id="counterVal" style="font-size: 42px;">
<pre>
${res['results']['records'].join('\r\n')}
</pre>
</p>
</div>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Need to script it?</h3>
</div>
<div class="panel-body">
<p>
Verify the results using curl:
</p>
<pre>curl https://dnsjson.com/${res['results']['name']}/${res['results']['type']}.json</pre>
<p>
Need the results in plain text?
</p>
<pre>curl https://dnsjson.com/${res['results']['name']}/${res['results']['type']}.txt</pre>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Need to lookup something else?</h3>
</div>
<div class="panel-body">
<form class="form-inline" method="POST" action="/">
<fieldset>
<div class="form-group">
<input type="text" name="recordName" class="form-control" id="recordName"
placeholder="${res['results']['name']}" value="${res['results']['name']}">
</div>
<div class="form-group">
<select class="form-control" id="recordType" name="recordType">
<option>Type</option>
<option>${supportedTypes.join('</option><option>')}</option>
</select>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Lookup</button>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>`
output = [
returnHeader(res['results']['name'] + ' (' + res['results']['type'] + ')'),
data,
returnFooter()
]
return new Response(output.join('\r\n'), {
headers: {
'Content-Type': 'text/html',
'Access-Control-Allow-Origin': '*'
}
})
}
else {
headers = {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
}
res = {
'success': false,
'message': 'Invalid QType provided'
}
return new Response(JSON.stringify(res), headers)
}
}
}