|
| 1 | +// Copyright 2015-present runtime.js project authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +Object.assign(exports, { |
| 17 | + NODATA: 'ENODATA', |
| 18 | + BADFAMILY: 'EBADFAMILY', |
| 19 | + FORMERR: 'EFORMERR', |
| 20 | + SERVFAIL: 'ESERVFAIL', |
| 21 | + NOTFOUND: 'ENOTFOUND', |
| 22 | + NOTIMP: 'ENOTIMP', |
| 23 | + REFUSED: 'EREFUSED', |
| 24 | + BADQUERY: 'EBADQUERY', |
| 25 | + BADNAME: 'EBADNAME', |
| 26 | + BADRESP: 'EBADRESP', |
| 27 | + CONNREFUSED: 'ECONNREFUSED', |
| 28 | + TIMEOUT: 'ETIMEOUT', |
| 29 | + EOF: 'EEOF', |
| 30 | + FILE: 'EFILE', |
| 31 | + NOMEM: 'ENOMEM', |
| 32 | + DESTRUCTION: 'EDESTRUCTION', |
| 33 | + BADSTR: 'EBADSTR', |
| 34 | + BADFLAGS: 'EBADFLAGS', |
| 35 | + NONAME: 'ENONAME', |
| 36 | + BADHINTS: 'EBADHINTS', |
| 37 | + NOTINITIALIZED: 'ENOTINITIALIZED', |
| 38 | + LOADIPHLPAPI: 'ELOADIPHLPAPI', |
| 39 | + ADDRGETNETWORKPARAMS: 'EADDRGETNETWORKPARAMS', |
| 40 | + CANCELLED: 'ECANCELLED' |
| 41 | +}); |
| 42 | + |
| 43 | +let servers = [ |
| 44 | + '8.8.8.8' |
| 45 | +]; |
| 46 | + |
| 47 | +function lookup(hostname, opts, cb) { |
| 48 | + if (opts.family && opts.family === 6) return throwIPv6Err(cb); |
| 49 | + opts.query = opts.query || 'A'; |
| 50 | + if (hostname === 'localhost' && opts.query === 'A') { |
| 51 | + if (!opts.all) { |
| 52 | + if (cb) cb(null, '127.0.0.1', 4); |
| 53 | + } else { |
| 54 | + if (opts.addrOnly) { |
| 55 | + if (cb) cb(null, ['127.0.0.1']); |
| 56 | + } else { |
| 57 | + if (cb) cb(null, [{address: '127.0.0.1', family: 4}]); |
| 58 | + } |
| 59 | + } |
| 60 | + return; |
| 61 | + } |
| 62 | + runtime.dns.resolve(hostname, { |
| 63 | + query: opts.query |
| 64 | + }, function(err, data) { |
| 65 | + if (err) { |
| 66 | + if (cb) cb(err, null, null); |
| 67 | + return; |
| 68 | + } |
| 69 | + var res; |
| 70 | + var ret = []; |
| 71 | + for (var i = 0; i < data.results.length; i++) { |
| 72 | + res = data.results[i]; |
| 73 | + if (!opts.all && i === 0) { |
| 74 | + var addr = res.address.join('.'); |
| 75 | + if (cb) cb(null, addr, 4); |
| 76 | + break; |
| 77 | + } else { |
| 78 | + switch (res.record) { |
| 79 | + case 'A': |
| 80 | + if (opts.addrOnly) { |
| 81 | + ret.push(res.address.join('.')); |
| 82 | + } else { |
| 83 | + ret.push({ |
| 84 | + address: res.address.join('.'), |
| 85 | + family: 4 |
| 86 | + }); |
| 87 | + } |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + if (ret.length === 0) { |
| 93 | + if (cb) cb(new SystemError('dns query failed', exports.NODATA, 'runtime.dns.resolve'), null); |
| 94 | + return; |
| 95 | + } |
| 96 | + if (cb) cb(null, ret); |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +function throwIPv6Err(cb) { |
| 101 | + var err = new SystemError('runtime doesn\'t support IPv6', exports.BADFAMILY); |
| 102 | + if (cb) return cb(err); |
| 103 | + throw err; |
| 104 | +} |
| 105 | + |
| 106 | +exports.getServers = function() { |
| 107 | + return servers; |
| 108 | +} |
| 109 | + |
| 110 | +exports.lookup = function(hostname, opts, cb) { |
| 111 | + if (typeof opts === 'function') { |
| 112 | + cb = opts; |
| 113 | + opts = null; |
| 114 | + } |
| 115 | + if (typeof opts === 'undefined' || opts === null) opts = {}; |
| 116 | + if (typeof opts === 'number' || opts instanceof Number) { |
| 117 | + opts = { |
| 118 | + family: opts |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + return lookup(hostname, opts, cb); |
| 123 | +} |
| 124 | + |
| 125 | +exports.resolve4 = function(hostname, cb) { |
| 126 | + return lookup(hostname, { |
| 127 | + all: true, |
| 128 | + addrOnly: true |
| 129 | + }, cb); |
| 130 | +} |
| 131 | + |
| 132 | +exports.resolve6 = function(hostname, cb) { |
| 133 | + throwIPv6Err(cb); |
| 134 | +} |
| 135 | + |
| 136 | +exports.resolve = function(hostname, rrtype, cb) { |
| 137 | + if (typeof rrtype === 'function') { |
| 138 | + cb = rrtype; |
| 139 | + rrtype = null; |
| 140 | + } |
| 141 | + if (typeof rrtype === 'undefined' || rrtype === null) rrtype = 'A'; |
| 142 | + switch (rrtype) { |
| 143 | + case 'A': |
| 144 | + return exports.resolve4(hostname, cb); |
| 145 | + break; |
| 146 | + case 'AAAA': |
| 147 | + return exports.resolve6(hostname, cb); |
| 148 | + break; |
| 149 | + } |
| 150 | +} |
0 commit comments