-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
170 lines (153 loc) · 3.96 KB
/
index.js
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
const fs = require("fs");
const JSON5 = require("json5");
const { UDPClient } = require("dns2");
const levelup = require("levelup");
const leveldown = require("leveldown");
const CacheableLookup = require("cacheable-lookup");
const lookup = new CacheableLookup();
const config = JSON5.parse(fs.readFileSync("./config.json5"));
lookup.servers = config.nameservers;
let mainPortal = config.siaPortals[0];
let skylinkCache = levelup(leveldown("./record_cache"));
(async () => {
const dns2 = require("dns2");
const fetch = (await import("node-fetch")).default;
const { Packet } = dns2;
setInterval(async () => {
mainPortal = await getAlivePortal();
}, 100000);
const rootResolve = UDPClient({
dns: config.rootNameserver.split(":")[0],
port: parseInt(config.rootNameserver.split(":")[1]),
});
const server = dns2.createServer({
udp: true,
handle: async (request, send, rinfo) => {
const response = Packet.createResponseFromRequest(request);
const [question] = request.questions;
const { name } = question;
let blockchainRecords = await rootResolve(name);
let recordsLink = blockchainRecords.authorities.find(
(record) =>
record &&
record.ns &&
(record.ns.endsWith("._skyname") ||
record.ns.endsWith(".skyname.xyz"))
);
if (!recordsLink || !recordsLink.ns) {
return send(response);
} else {
recordsLink = recordsLink.ns;
}
let records = await fetchSia("sia://" + recordsLink.split(".")[0]);
if (!records || (typeof records != "object" && Array.isArray(records))) {
return send(response);
} else {
records.forEach((record) => {
if (
!record.type ||
!Object.keys(Packet.TYPE).includes(record.type) ||
!record.ttl
) {
return send(response);
}
response.answers.push({
...record,
name: (record.name + "." + name)
.split(".")
.join(" ")
.trim()
.split(" ")
.join("."), //trim dots if no subdomain provided
type: Packet.TYPE[record.type],
class: Packet.CLASS.IN,
});
});
}
send(response);
},
});
server.on("request", (request, response, rinfo) => {});
server.on("close", () => {
console.log("server closed");
});
server.listen({
udp: 53,
});
// eventually
})();
async function getAlivePortal() {
const fetch = (await import("node-fetch")).default;
return new Promise(async (resolve, reject) => {
let found = false;
config.siaPortals.forEach((portal, index) => {
setTimeout(async () => {
if (found) {
return;
} else {
try {
let resource = await (
await fetch(
portal + "AACo6KGldohjcBP39JUxFEWWMwiTTV_wQfrd5z28gUoYxA",
{
headers: {
"User-agent": "Sia-Agent",
},
}
)
).text();
if (resource == "test") {
found = true;
resolve(portal);
} else {
return;
}
} catch (e) {
return;
}
}
}, index * 200);
});
});
}
async function fetchSia(siaLink) {
let cached = await skylinkCache.get(siaLink).catch((e) => {
if (e.notFound) {
return;
}
});
let resolverSkylink;
if (cached) {
return JSON.parse(cached.toString());
}
const fetch = (await import("node-fetch")).default;
let resource;
try {
let fileMeta = await fetch(mainPortal + siaLink.slice("sia://".length), {
headers: { "User-agent": "Sia-Agent" },
});
if (siaLink == "sia://" + fileMeta.headers.get("skynet-skylink")) {
resource = fileMeta;
resolverSkylink = false;
if (
resource.headers.get("content-length") > 20480 ||
resource.headers.get("content-type") !== "application/json"
) {
return null;
} else {
let data = await resource.json();
if (!resolverSkylink) {
skylinkCache.put(siaLink, JSON.stringify(data));
}
return data;
}
} else {
siaLink = "sia://" + fileMeta.headers.get("skynet-skylink");
resource = await fetchSia(siaLink);
resolverSkylink = true;
return resource;
}
} catch (error) {
return null;
}
}