-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
444 lines (410 loc) · 16.7 KB
/
app.py
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import base64
import copy
import crayons
import json
import re
import time
import traceback
import dns.message
import dns.name
import flask
import redis
import requests
from rq import Queue
from flask import Flask, make_response, request
from markupsafe import escape
from logsnag import LogSnag
from prewarm import resolve_ipns, prewarm
import config
logsnag = LogSnag(token=config.logsnag_token, project=config.logsnag_project)
app = Flask(__name__)
q = Queue("dweb", connection=redis.Redis())
def log_successful_resolve(channel, name, result):
if channel == "furl" and not name.endswith(".fc"):
name = name + ".fc"
logsnag.track(
channel=channel,
event=f"Successful Resolve",
description=f"{channel}/{name} -> {result}",
user_id=name,
notify=True,
tags={
"channel": channel,
"result": result
}
)
def make_rr(simple, rdata):
csimple = copy.copy(simple)
rdata_text = rdata.to_text()
if rdata_text.startswith('"') and rdata_text.endswith('"'):
rdata_text = rdata_text[1:-1]
csimple["data"] = rdata_text
return csimple
def flatten_rrset(rrs):
simple = {
"name": str(rrs.name),
"type": rrs.rdtype,
}
if len(rrs) > 0:
simple["TTL"] = rrs.ttl
return [make_rr(simple, rdata) for rdata in rrs]
else:
return [simple]
def to_doh_simple(message):
simple = {"Status": int(message.rcode())}
for f in dns.flags.Flag:
if f != dns.flags.Flag.AA and f != dns.flags.Flag.QR:
# DoH JSON doesn't need AA and omits it. DoH JSON is only
# used in replies so the QR flag is implied.
simple[f.name] = (message.flags & f) != 0
for i, s in enumerate(message.sections):
k = dns.message.MessageSection.to_text(i).title()
simple[k] = []
for rrs in s:
simple[k].extend(flatten_rrset(rrs))
# we don't encode the ecs_client_subnet field
response = flask.Response(json.dumps(simple, indent=2) + "\n")
response.headers.set("Content-Type", "application/dns-json")
response.headers.set("Cloudflare-CDN-Cache-Control", f"public, max-age=${config.dns_cache_ttl}")
return response
def make_empty_message(name="", t=dns.rdatatype.TXT):
name = dns.name.from_text(name)
return dns.message.make_response(dns.message.make_query(name, t))
def output(message, ct="application/dns-json"):
if ct == "application/dns-message":
response = make_response(message.to_wire())
response.headers.set("Content-Type", "application/dns-message")
response.headers.set("Cloudflare-CDN-Cache-Control", f"public, max-age=${config.dns_cache_ttl}")
return response
elif ct == "application/x-javascript":
return to_doh_simple(message)
elif ct == "application/dns-json":
return to_doh_simple(message)
else:
return to_doh_simple(message)
def cache_result(cache_key, result, ttl=30):
rs = redis.Redis(host="localhost", port=6379, db=0)
rs.set(cache_key, result)
rs.expire(cache_key, ttl)
def sol_resolve(name):
print(crayons.cyan("Resolving Solana Name: " + name), flush=True)
cache_key = "sol:" + name
rs = redis.Redis(host="localhost", port=6379, db=0)
cached = rs.get(cache_key)
if cached is not None:
result = cached.decode("utf-8")
log_successful_resolve("sol", name, result)
q.enqueue(prewarm, result)
print(crayons.green("Cache Hit: " + name + " -> " + result), flush=True)
return result
sns_sdk = "https://sns-sdk-proxy.bonfida.workers.dev"
# try: /record-v2/{name}/IPNS
query = sns_sdk + "/record-v2/" + name + "/IPNS"
r = requests.get(query)
if r.status_code == 200:
o = r.json()
if "result" in o and o["result"] is not None and "deserialized" in o["result"]:
ipns = o["result"]["deserialized"]
if ipns.startswith("k51") or ipns.startswith("k2"):
# return "dnslink=" + handle_ipns(ipns)
result = "dnslink=/ipns/" + ipns
log_successful_resolve("sol", name, result)
q.enqueue(prewarm, result)
cache_result(cache_key, result)
return result
if ipns.startswith("ipns://"):
ipns = str(ipns[len("ipns://") :])
# return "dnslink=" + handle_ipns(ipns)
result = "dnslink=/ipns/" + ipns
log_successful_resolve("sol", name, result)
q.enqueue(prewarm, result)
cache_result(cache_key, result)
return result
# try: /record-v2/{name}/IPFS
query = sns_sdk + "/record-v2/" + name + "/IPFS"
r = requests.get(query)
if r.status_code == 200:
o = r.json()
if "result" in o and o["result"] is not None and "deserialized" in o["result"]:
ipfs = o["result"]["deserialized"]
if ipfs.startswith("Qm") or ipfs.startswith("baf"):
result = "dnslink=/ipfs/" + ipfs
log_successful_resolve("sol", name, result)
q.enqueue(prewarm, result)
cache_result(cache_key, result)
return result
if ipfs.startswith("ipfs://"):
ipfs = str(ipfs[len("ipfs://") :])
result = "dnslink=/ipfs/" + ipfs
log_successful_resolve("sol", name, result)
q.enqueue(prewarm, result)
cache_result(cache_key, result)
return result
# try: /domain-data/{name}
query = sns_sdk + "/domain-data/" + name
r = requests.get(query)
if r.status_code == 200:
o = r.json()
if "result" in o and o["result"] is not None:
result = o["result"]
try:
decoded = base64.b64decode(result)
# Discard non-UTF-8 parts and use the usable part of the string
decoded_str = decoded.decode('utf-8', errors='ignore')
# Use regex to find ipns=
ipns = re.compile(r"ipns=(k51[a-zA-Z0-9]{59})").search(decoded_str)
if ipns is not None:
print("Found IPNS: " + ipns.group(1), flush=True)
result = "dnslink=/ipns/" + ipns.group(1)
log_successful_resolve("sol", name, result)
q.enqueue(prewarm, result)
cache_result(cache_key, result)
return result
except UnicodeDecodeError as e:
print(f"UnicodeDecodeError: {e}", flush=True)
return None
def fc_resolve(name):
print(crayons.cyan("Resolving Farcaster Name: " + name), flush=True)
# first: try bio with warpcast API
api = "https://client.warpcast.com/v2/user-by-username?username="
name = name.strip().lower()
if name.endswith(".fc"):
name = name[0:-3]
cache_key = "furl:" + name
rs = redis.Redis(host="localhost", port=6379, db=0)
cached = rs.get(cache_key)
if cached is not None:
result = cached.decode("utf-8")
log_successful_resolve("furl", name, result)
q.enqueue(prewarm, result)
print(crayons.green("Cache Hit: " + name + " -> " + result), flush=True)
return result
api = api + name
r = requests.get(api)
if r.status_code == 200:
o = r.json()
if "result" in o and "user" in o["result"] and "profile" in o["result"]["user"] and "bio" in o["result"]["user"]["profile"]:
bio = o["result"]["user"]["profile"]["bio"]["text"]
# find IPNS in bio
ipns = re.compile(r"(k51[a-zA-Z0-9]{59})").search(bio)
if ipns is not None:
result = "dnslink=/ipns/" + ipns.group(1)
log_successful_resolve("furl", name, result)
q.enqueue(prewarm, result)
cache_result(cache_key, result)
return result
# find CIDv0 in bio
cidv0 = re.compile(r"(Qm[a-zA-Z0-9]{44})").search(bio)
if cidv0 is not None:
result = "dnslink=/ipfs/" + cidv0.group(1)
log_successful_resolve("furl", name, result)
q.enqueue(prewarm, result)
cache_result(cache_key, result)
return result
# find CIDv1 in bio
cidv1 = re.compile(r"(baf[a-zA-Z0-9]{56})").search(bio)
if cidv1 is not None:
result = "dnslink=/ipfs/" + cidv1.group(1)
log_successful_resolve("furl", name, result)
q.enqueue(prewarm, result)
cache_result(cache_key, result)
return result
# second: try USER_DATA_TYPE=5 with Hub
hub = config.farcaster_hub
api = hub + "/v1/userNameProofByName?name=" + name
r = requests.get(api)
if r.status_code == 200:
o = r.json()
if "fid" in o:
fid = o["fid"]
api = hub + "/v1/userDataByFid?fid=" + str(fid) + "&user_data_type=5"
r = requests.get(api)
if r.status_code == 200:
o = r.json()
if "data" in o and "userDataBody" in o["data"] and "value" in o["data"]["userDataBody"]:
value = o["data"]["userDataBody"]["value"]
# find IPNS in value
ipns = re.compile(r"(k51[a-zA-Z0-9]{59})").search(value)
if ipns is not None:
result = "dnslink=/ipns/" + ipns.group(1)
log_successful_resolve("furl", name, result)
q.enqueue(prewarm, result)
cache_result(cache_key, result)
return result
# find CIDv0 in value
cidv0 = re.compile(r"(Qm[a-zA-Z0-9]{44})").search(value)
if cidv0 is not None:
result = "dnslink=/ipfs/" + cidv0.group(1)
log_successful_resolve("furl", name, result)
q.enqueue(prewarm, result)
cache_result(cache_key, result)
return result
# find CIDv1 in value
cidv1 = re.compile(r"(baf[a-zA-Z0-9]{56})").search(value)
if cidv1 is not None:
result = "dnslink=/ipfs/" + cidv1.group(1)
log_successful_resolve("furl", name, result)
q.enqueue(prewarm, result)
cache_result(cache_key, result)
return result
return None
def dotbit_resolve(name):
indexer = "https://indexer-v1.did.id/v1/account/records"
payload = {"account": name}
r = requests.post(indexer, json=payload)
if r.status_code == 200:
o = r.json()
print(r.text, flush=True)
if "data" in o and o["data"] is not None and "records" in o["data"]:
records = o["data"]["records"]
for record in records:
if record["key"] == "dweb.ipns":
return "dnslink=" + handle_ipns(record["value"])
if record["key"] == "dweb.ipfs":
return "dnslink=/ipfs/" + record["value"]
return None
def ens_resolve(name):
resolver = "https://api.planetable.xyz/ens/resolve/"
r = requests.get(resolver + name)
if r.status_code == 200:
o = r.json()
if (
"contentHash" in o
and o["contentHash"] is not None
and o["contentHash"] != ""
):
content_hash = o["contentHash"]
if content_hash.startswith("ipfs://"):
return "dnslink=/ipfs/" + content_hash[len("ipfs://") :]
if content_hash.startswith("ipns://"):
ipns = str(content_hash[len("ipns://") :])
result = handle_ipns(ipns)
return "dnslink=" + result
return None
@app.route("/dns-query", methods=["GET", "POST"])
def dns_query():
if request.data:
try:
question = dns.message.from_wire(request.data)
if question.question:
q = question.question[0]
print(str(q), flush=True)
if q.rdtype == 16:
name = str(q.name)
if name.endswith(".bit."):
name = name[0:-1]
if name.endswith(".sol."):
name = name[0:-1]
if name.endswith(".fc."):
name = name[0:-1]
if name.endswith(".eth."):
name = name[0:-1]
if name.startswith("_dnslink."):
name = name[len("_dnslink.") :]
result = None
if name.endswith(".bit"):
result = dotbit_resolve(name)
if name.endswith(".sol"):
result = sol_resolve(name)
if name.endswith(".fc"):
result = fc_resolve(name)
if name.endswith(".eth"):
result = ens_resolve(name)
if result is not None:
print("Resolved: " + name + " -> " + result, flush=True)
response = dns.message.make_response(question)
response.answer.append(
dns.rrset.from_text(
q.name,
config.dns_cache_ttl,
dns.rdataclass.IN,
dns.rdatatype.TXT,
result,
)
)
return output(response, "application/dns-message")
return output(make_empty_message())
except Exception as e:
print("Error: " + str(e), flush=True)
print(traceback.format_exc(), flush=True)
return output(make_empty_message())
# get type
t = request.values.get("type")
if not t:
# set default type to TXT
t = dns.rdatatype.TXT
try:
t = dns.rdatatype.from_text(dns.rdatatype.to_text(int(t)))
except:
t = dns.rdatatype.TXT
if t not in [dns.rdatatype.TXT, dns.rdatatype.A, dns.rdatatype.AAAA]:
t = dns.rdatatype.TXT
# get ct (content-type)
ct = request.values.get("ct")
ct_accept = request.headers.get("Accept")
if not ct and not ct_accept:
# set default ct to json
ct = "application/x-javascript"
elif not ct and ct_accept:
ct = ct_accept
else:
if ct not in [
"application/dns-message",
"application/x-javascript",
"application/dns-json",
]:
ct = "application/x-javascript"
# name is required
name = request.values.get("name")
if not name:
print("Please provide a name", flush=True)
return output(make_empty_message(t=t), ct)
name = name.lower()
if name.endswith(".bit."):
name = name[0:-1]
if name.endswith(".sol."):
name = name[0:-1]
if name.endswith(".fc."):
name = name[0:-1]
if t != dns.rdatatype.TXT:
print("Type is not TXT: " + str(t), flush=True)
return output(make_empty_message(name, t=t), ct)
result = None
if name.endswith(".bit"):
result = dotbit_resolve(name)
if name.endswith(".sol"):
result = sol_resolve(name)
if name.endswith(".fc"):
result = fc_resolve(name)
if name.endswith(".eth"):
result = ens_resolve(name)
if result is not None:
print("Resolved: " + name + " -> " + result, flush=True)
response = dns.message.make_response(dns.message.make_query(name, t))
if ct == "application/dns-message":
response.answer.append(
dns.rrset.from_text(
name + ".", config.dns_cache_ttl, dns.rdataclass.IN, dns.rdatatype.TXT, result
)
)
else:
response.answer.append(
dns.rrset.from_text(
name, config.dns_cache_ttl, dns.rdataclass.IN, dns.rdatatype.TXT, result
)
)
return output(response, ct)
print("Unsupported: name=" + str(name) + " / t=" + str(t), flush=True)
return output(make_empty_message(name, t=t), ct)
def handle_ipns(ipns: str) -> str:
r = redis.Redis(host="localhost", port=6379, db=0)
r_key = "ipns:" + ipns + ":results"
latest = r.zrevrange(r_key, 0, 0)
if latest is not None and len(latest) > 0:
q.enqueue(revalidate_ipns, ipns)
return latest[0].decode("utf-8")
else:
return resolve_ipns(ipns)
def revalidate_ipns(ipns: str):
value = resolve_ipns(ipns)
print("Revalidated: " + ipns + " -> " + value, flush=True)