Files: 69705109becfed7b3713e4f3da60f26ac286bed6 / index.js
1932 bytesRaw
1 | var Dnsd = require("dnsd"); |
2 | var Client = require("ssb-client"); |
3 | var Pull = require("pull-stream"); |
4 | var Paramap = require("pull-paramap"); |
5 | |
6 | var Config; |
7 | try { |
8 | Config = require("./config.js"); |
9 | } catch (err) { |
10 | console.error("\nCouldn't find './config.js'. Using defaults"); |
11 | Config = require("./config.dist.js"); |
12 | } |
13 | |
14 | var log = { |
15 | req: function (req) { |
16 | var q = req.question[0]; |
17 | console.log({ |
18 | name: q.name, |
19 | type: q.type, |
20 | class: q.class, |
21 | }); |
22 | }, |
23 | }; |
24 | |
25 | var answer = function (sbot, req, res) { |
26 | console.log(); |
27 | log.req(req); |
28 | |
29 | var q = req.question; |
30 | |
31 | if (!q.length) { |
32 | console.log("invalid question"); |
33 | res.end(); |
34 | } |
35 | |
36 | var name = q[0].name; |
37 | var type = q[0].type; |
38 | |
39 | Pull(sbot.messagesByType({ |
40 | type: 'ssb-dns', |
41 | }), |
42 | Paramap(function getAvatar(msg, cb) { |
43 | cb(null, msg); |
44 | }), |
45 | Pull.drain(function printMessage(msg) { |
46 | var record = msg.value.content.record; |
47 | |
48 | if (typeof(record) !== 'object') { return; } |
49 | |
50 | if (name && type && |
51 | record.name === name && |
52 | record.type === type && |
53 | record.value) { |
54 | console.log("%s (%s) => %s", name, record.type, record.value); |
55 | |
56 | // returns the first matching record found |
57 | // TODO support multiple returned values |
58 | res.answer.push({ |
59 | name: record.name, |
60 | type: record.type, |
61 | data: record.value, |
62 | ttl: 500, |
63 | }); |
64 | res.end() |
65 | } |
66 | })); |
67 | }; |
68 | |
69 | var createServer = function (sbot, port, host, cb) { |
70 | Dnsd.createServer(function(req, res) { |
71 | answer(sbot, req, res); |
72 | }).listen(port, host, cb); |
73 | }; |
74 | |
75 | Client(function (err, sbot) { |
76 | if (err) { |
77 | console.error(err); |
78 | return void process.exit(1); |
79 | } |
80 | createServer(sbot, Config.port, Config.host, Config.ready); |
81 | }); |
82 | |
83 |
Built with git-ssb-web