Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 639024c

Browse files
hacdiasdaviddias
authored andcommitted
feat: /api/v0/dns (#1172)
1 parent fadead1 commit 639024c

File tree

16 files changed

+217
-19
lines changed

16 files changed

+217
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ test/test-data/go-ipfs-repo/LOG.old
4040

4141
# while testing npm5
4242
package-lock.json
43+
yarn.lock

package.json

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"./src/core/runtime/config-nodejs.json": "./src/core/runtime/config-browser.json",
1212
"./src/core/runtime/libp2p-nodejs.js": "./src/core/runtime/libp2p-browser.js",
1313
"./src/core/runtime/repo-nodejs.js": "./src/core/runtime/repo-browser.js",
14+
"./src/core/runtime/dns-nodejs.js": "./src/core/runtime/dns-browser.js",
1415
"./test/utils/create-repo-nodejs.js": "./test/utils/create-repo-browser.js",
1516
"stream": "readable-stream"
1617
},
@@ -66,14 +67,14 @@
6667
"dir-compare": "^1.4.0",
6768
"dirty-chai": "^2.0.1",
6869
"eslint-plugin-react": "^7.5.1",
69-
"execa": "^0.8.0",
70+
"execa": "^0.9.0",
7071
"expose-loader": "^0.7.4",
7172
"form-data": "^2.3.1",
7273
"hat": "0.0.3",
73-
"interface-ipfs-core": "~0.36.15",
74+
"interface-ipfs-core": "~0.40.0",
7475
"left-pad": "^1.2.0",
7576
"lodash": "^4.17.4",
76-
"mocha": "^4.0.1",
77+
"mocha": "^4.1.0",
7778
"ncp": "^2.0.0",
7879
"nexpect": "^0.5.0",
7980
"pre-commit": "^1.2.2",
@@ -91,28 +92,28 @@
9192
"boom": "^7.1.1",
9293
"bs58": "^4.0.1",
9394
"byteman": "^1.3.5",
94-
"cids": "^0.5.2",
95+
"cids": "~0.5.2",
9596
"debug": "^3.1.0",
9697
"file-type": "^7.4.0",
9798
"filesize": "^3.5.11",
9899
"fsm-event": "^2.1.0",
99-
"get-folder-size": "^1.0.0",
100+
"get-folder-size": "^1.0.1",
100101
"glob": "^7.1.2",
101-
"hapi": "^16.2.2",
102+
"hapi": "^16.6.2",
102103
"hapi-set-header": "^1.0.2",
103104
"hoek": "^5.0.2",
104-
"ipfs-api": "^17.2.4",
105+
"ipfs-api": "^17.3.0",
105106
"ipfs-bitswap": "~0.18.0",
106107
"ipfs-block": "~0.6.1",
107108
"ipfs-block-service": "~0.13.0",
108109
"ipfs-multipart": "~0.1.0",
109-
"ipfs-repo": "~0.18.4",
110+
"ipfs-repo": "~0.18.5",
110111
"ipfs-unixfs": "~0.1.14",
111-
"ipfs-unixfs-engine": "~0.24.1",
112+
"ipfs-unixfs-engine": "~0.24.2",
112113
"ipld-resolver": "~0.14.1",
113114
"is-ipfs": "^0.3.2",
114115
"is-stream": "^1.1.0",
115-
"joi": "^13.0.2",
116+
"joi": "^13.1.0",
116117
"libp2p": "~0.15.0",
117118
"libp2p-circuit": "~0.1.4",
118119
"libp2p-floodsub": "~0.13.1",
@@ -121,7 +122,7 @@
121122
"libp2p-multiplex": "~0.5.1",
122123
"libp2p-railing": "~0.7.1",
123124
"libp2p-secio": "~0.9.0",
124-
"libp2p-tcp": "~0.11.1",
125+
"libp2p-tcp": "~0.11.2",
125126
"libp2p-webrtc-star": "~0.13.3",
126127
"libp2p-websocket-star": "~0.7.2",
127128
"libp2p-websockets": "~0.10.4",
@@ -132,21 +133,21 @@
132133
"mafmt": "^3.0.2",
133134
"mime-types": "^2.1.17",
134135
"mkdirp": "~0.5.1",
135-
"multiaddr": "^3.0.1",
136-
"multihashes": "~0.4.12",
136+
"multiaddr": "^3.0.2",
137+
"multihashes": "~0.4.13",
137138
"once": "^1.4.0",
138139
"path-exists": "^3.0.0",
139140
"peer-book": "~0.5.2",
140-
"peer-id": "~0.10.3",
141-
"peer-info": "~0.11.3",
141+
"peer-id": "~0.10.4",
142+
"peer-info": "~0.11.4",
142143
"progress": "^2.0.0",
143144
"promisify-es6": "^1.0.3",
144145
"pull-abortable": "^4.1.1",
145146
"pull-defer": "^0.2.2",
146147
"pull-file": "^1.1.0",
147148
"pull-ndjson": "^0.1.1",
148149
"pull-paramap": "^1.2.2",
149-
"pull-pushable": "^2.1.1",
150+
"pull-pushable": "^2.1.2",
150151
"pull-sort": "^1.0.1",
151152
"pull-stream": "^3.6.1",
152153
"pull-stream-to-stream": "^1.3.4",
@@ -159,8 +160,8 @@
159160
"temp": "~0.8.3",
160161
"through2": "^2.0.3",
161162
"update-notifier": "^2.3.0",
162-
"yargs": "^10.0.3",
163-
"yargs-parser": "^8.0.0"
163+
"yargs": "^10.1.1",
164+
"yargs-parser": "^8.1.0"
164165
},
165166
"optionalDependencies": {
166167
"prom-client": "^10.2.2",

src/cli/commands/dns.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
const print = require('../utils').print
3+
4+
module.exports = {
5+
command: 'dns <domain>',
6+
7+
describe: 'Resolve DNS links',
8+
9+
builder: {
10+
format: {
11+
type: 'string'
12+
}
13+
},
14+
15+
handler (argv) {
16+
argv.ipfs.dns(argv['domain'], (err, path) => {
17+
if (err) {
18+
throw err
19+
}
20+
21+
print(path)
22+
})
23+
}
24+
}

src/core/components/dns.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
// dns-nodejs gets replaced by dns-browser when webpacked/browserified
4+
const dns = require('../runtime/dns-nodejs')
5+
const promisify = require('promisify-es6')
6+
7+
module.exports = () => {
8+
return promisify((domain, opts, callback) => {
9+
if (typeof domain !== 'string') {
10+
return callback(new Error('Invalid arguments, domain must be a string'))
11+
}
12+
13+
if (typeof opts === 'function') {
14+
callback = opts
15+
opts = {}
16+
}
17+
18+
dns(domain, opts, callback)
19+
})
20+
}

src/core/components/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ exports.files = require('./files')
2020
exports.bitswap = require('./bitswap')
2121
exports.pubsub = require('./pubsub')
2222
exports.dht = require('./dht')
23+
exports.dns = require('./dns')

src/core/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class IPFS extends EventEmitter {
9494
this.ping = components.ping(this)
9595
this.pubsub = components.pubsub(this)
9696
this.dht = components.dht(this)
97+
this.dns = components.dns(this)
9798

9899
if (this._options.EXPERIMENTAL.pubsub) {
99100
this.log('EXPERIMENTAL pubsub is enabled')

src/core/runtime/dns-browser.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
module.exports = (domain, opts, callback) => {
4+
domain = encodeURIComponent(domain)
5+
let url = `https://ipfs.io/api/v0/dns?arg=${domain}`
6+
7+
for (const prop in opts) {
8+
url += `&${prop}=${opts[prop]}`
9+
}
10+
11+
self.fetch(url, {mode: 'cors'})
12+
.then((response) => {
13+
return response.json()
14+
})
15+
.then((response) => {
16+
if (response.Path) {
17+
return callback(null, response.Path)
18+
} else {
19+
return callback(new Error(response.Message))
20+
}
21+
})
22+
.catch((error) => {
23+
callback(error)
24+
})
25+
}

src/core/runtime/dns-nodejs.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
const dns = require('dns')
4+
5+
module.exports = (domain, opts, callback) => {
6+
dns.resolveTxt(domain, (err, records) => {
7+
if (err) {
8+
return callback(err, null)
9+
}
10+
11+
// TODO: implement recursive option
12+
13+
for (const record of records) {
14+
if (record[0].startsWith('dnslink=')) {
15+
return callback(null, record[0].substr(8, record[0].length - 1))
16+
}
17+
}
18+
19+
callback(new Error('domain does not have a txt dnslink entry'))
20+
})
21+
}

src/http/api/resources/dns.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
3+
const boom = require('boom')
4+
5+
exports = module.exports
6+
7+
exports.get = (request, reply) => {
8+
if (!request.query.arg) {
9+
return reply({
10+
Message: "Argument 'domain' is required",
11+
Code: 0
12+
}).code(400).takeover()
13+
}
14+
15+
request.server.app.ipfs.dns(request.query.arg, (err, path) => {
16+
if (err) {
17+
return reply(boom.badRequest(err))
18+
}
19+
20+
return reply({
21+
Path: path
22+
})
23+
})
24+
}

src/http/api/resources/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ exports.bitswap = require('./bitswap')
1212
exports.file = require('./file')
1313
exports.files = require('./files')
1414
exports.pubsub = require('./pubsub')
15+
exports.dns = require('./dns')

0 commit comments

Comments
 (0)