support skydb in hns
This commit is contained in:
parent
b446159f48
commit
11186f3fd6
|
@ -151,6 +151,7 @@ server {
|
|||
-- example response: '{"skylink":"sia://XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg"}'
|
||||
local hnsres_json = json.decode(hnsres_res.body)
|
||||
|
||||
if hnsres_json.skylink then
|
||||
-- try to match the skylink with sia:// prefix
|
||||
local skylink, skylink_rest = string.match(hnsres_json.skylink, "sia://([^/?]+)(.*)")
|
||||
|
||||
|
@ -158,6 +159,32 @@ server {
|
|||
if skylink == nil then
|
||||
skylink, skylink_rest = string.match(hnsres_json.skylink, "/?([^/?]+)(.*)")
|
||||
end
|
||||
end
|
||||
|
||||
if hnsres_json.registry then
|
||||
local publickey = hnsres_json.registry.publickey
|
||||
local datakey = hnsres_json.registry.datakey
|
||||
|
||||
-- make a get request to /skynet/registry endpoint with the credentials from text record
|
||||
local registry_res = ngx.location.capture("/skynet/registry?publickey=" .. publickey .. "&datakey=" .. datakey)
|
||||
|
||||
ngx.header.content_type = 'text/plain'
|
||||
ngx.print(registry_res.body)
|
||||
|
||||
-- we want to fail with a generic 404 when /skynet/registry returns anything but 200 OK
|
||||
if registry_res.status ~= ngx.HTTP_OK then
|
||||
ngx.exit(ngx.HTTP_NOT_FOUND)
|
||||
end
|
||||
|
||||
-- since /skynet/registry endpoint response is a json, we need to decode it before we access it
|
||||
local registry_json = json.decode(registry_res.body)
|
||||
-- response will contain hex encoded, we need to decode it
|
||||
local data = (registry_json.data:gsub('..', function (cc)
|
||||
return string.char(tonumber(cc, 16))
|
||||
end))
|
||||
|
||||
skylink = data
|
||||
end
|
||||
|
||||
ngx.var.skylink = skylink
|
||||
if request_uri_rest == "/" and skylink_rest ~= "" and skylink_rest ~= "/" then
|
||||
|
|
|
@ -6,7 +6,7 @@ const { NodeClient } = require("hs-client");
|
|||
const host = process.env.HOSTNAME || "0.0.0.0";
|
||||
const port = Number(process.env.PORT) || 3100;
|
||||
|
||||
const hsdNetworkType = process.env.HSD_NETWORK || "regtest";
|
||||
const hsdNetworkType = process.env.HSD_NETWORK || "main";
|
||||
const hsdHost = process.env.HSD_HOST || "localhost";
|
||||
const hsdPort = Number(process.env.HSD_PORT) || 12037;
|
||||
const hsdApiKey = process.env.HSD_API_KEY || "foo";
|
||||
|
@ -18,10 +18,11 @@ const clientOptions = {
|
|||
apiKey: hsdApiKey,
|
||||
};
|
||||
const client = new NodeClient(clientOptions);
|
||||
const cache = new NodeCache({ stdTTL: 300 }); // cache for 5 minutes
|
||||
const cache = new NodeCache({ stdTTL: 1 }); // cache for 5 minutes
|
||||
|
||||
// Match both `sia://HASH` and `HASH` links.
|
||||
const startsWithSkylinkRegExp = /^(sia:\/\/)?[a-zA-Z0-9_-]{46}/;
|
||||
const registryEntryRegExp = /^skydb:\/\/(?<publickey>[a-zA-Z0-9%]+)\/(?<datakey>[a-zA-Z0-9%]+)$/;
|
||||
|
||||
const getDomainRecords = async (name) => {
|
||||
if (cache.has(name)) return cache.get(name);
|
||||
|
@ -36,18 +37,36 @@ const getDomainRecords = async (name) => {
|
|||
return records;
|
||||
};
|
||||
|
||||
const findSkylinkRecord = (records) => {
|
||||
const findSkynetCompatibleRecord = (records) => {
|
||||
// Find the last one, so people can update their domains in a non-destructive
|
||||
// way by simply adding a new link. This will also allow keeping links to
|
||||
// older versions for backwards compatibility.
|
||||
return records
|
||||
?.slice()
|
||||
.reverse()
|
||||
.find(({ txt }) => txt?.some((entry) => isValidSkylink(entry)));
|
||||
.find(({ txt }) => txt?.some((entry) => isValidSkylink(entry) || isValidRegistryEntry(entry)));
|
||||
};
|
||||
|
||||
const getSkylinkFromRecord = (record) => {
|
||||
return record?.txt?.find((entry) => isValidSkylink(entry));
|
||||
const createResponseFromCompatibleRecord = (record) => {
|
||||
const skylink = record?.txt?.find((entry) => isValidSkylink(entry));
|
||||
|
||||
if (skylink) return { skylink };
|
||||
|
||||
const entry = record?.txt?.find((entry) => isValidRegistryEntry(entry));
|
||||
|
||||
if (entry) {
|
||||
const match = entry.match(registryEntryRegExp);
|
||||
|
||||
if (match)
|
||||
return {
|
||||
registry: {
|
||||
publickey: decodeURIComponent(match.groups.publickey),
|
||||
datakey: decodeURIComponent(match.groups.datakey),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error(`No skylink in record: ${JSON.stringify(record)}`);
|
||||
};
|
||||
|
||||
const resolveDomainHandler = async (req, res) => {
|
||||
|
@ -56,11 +75,10 @@ const resolveDomainHandler = async (req, res) => {
|
|||
const records = await getDomainRecords(domain);
|
||||
if (!records) return res.status(404).send(`No records found for ${domain}`);
|
||||
|
||||
const record = findSkylinkRecord(records);
|
||||
if (!record) throw new Error(`No skylink found in dns records of ${domain}`);
|
||||
const record = findSkynetCompatibleRecord(records);
|
||||
if (!record) throw new Error(`No skynet compatible records found in dns records of ${domain}`);
|
||||
|
||||
const skylink = getSkylinkFromRecord(record);
|
||||
return res.json({ skylink });
|
||||
return res.json(createResponseFromCompatibleRecord(record));
|
||||
} catch (error) {
|
||||
res.status(500).send(`Handshake error: ${error.message}`);
|
||||
}
|
||||
|
@ -74,6 +92,10 @@ function isValidSkylink(link) {
|
|||
return Boolean(link.match(startsWithSkylinkRegExp));
|
||||
}
|
||||
|
||||
function isValidRegistryEntry(value) {
|
||||
return Boolean(value && value.match(registryEntryRegExp));
|
||||
}
|
||||
|
||||
const server = express();
|
||||
|
||||
server.get("/hnsres/:name", resolveDomainHandler);
|
||||
|
|
Reference in New Issue