summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/parent/ext-dns.js
blob: 6a6cf5c89420bde02d089dc5d633bed74b02d841 (plain)
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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const dnssFlags = {
  allow_name_collisions: Ci.nsIDNSService.RESOLVE_ALLOW_NAME_COLLISION,
  bypass_cache: Ci.nsIDNSService.RESOLVE_BYPASS_CACHE,
  canonical_name: Ci.nsIDNSService.RESOLVE_CANONICAL_NAME,
  disable_ipv4: Ci.nsIDNSService.RESOLVE_DISABLE_IPV4,
  disable_ipv6: Ci.nsIDNSService.RESOLVE_DISABLE_IPV6,
  disable_trr: Ci.nsIDNSService.RESOLVE_DISABLE_TRR,
  offline: Ci.nsIDNSService.RESOLVE_OFFLINE,
  priority_low: Ci.nsIDNSService.RESOLVE_PRIORITY_LOW,
  priority_medium: Ci.nsIDNSService.RESOLVE_PRIORITY_MEDIUM,
  speculate: Ci.nsIDNSService.RESOLVE_SPECULATE,
};

function getErrorString(nsresult) {
  let e = new Components.Exception("", nsresult);
  return e.name;
}

this.dns = class extends ExtensionAPI {
  getAPI() {
    return {
      dns: {
        resolve: function (hostname, flags) {
          let dnsFlags = flags.reduce(
            (mask, flag) => mask | dnssFlags[flag],
            0
          );

          return new Promise((resolve, reject) => {
            let request;
            let response = {
              addresses: [],
            };
            let listener = {
              onLookupComplete: function (inRequest, inRecord, inStatus) {
                if (inRequest === request) {
                  if (!Components.isSuccessCode(inStatus)) {
                    return reject({ message: getErrorString(inStatus) });
                  }
                  inRecord.QueryInterface(Ci.nsIDNSAddrRecord);
                  if (dnsFlags & Ci.nsIDNSService.RESOLVE_CANONICAL_NAME) {
                    try {
                      response.canonicalName = inRecord.canonicalName;
                    } catch (e) {
                      // no canonicalName
                    }
                  }
                  response.isTRR = inRecord.IsTRR();
                  while (inRecord.hasMore()) {
                    let addr = inRecord.getNextAddrAsString();
                    // Sometimes there are duplicate records with the same ip.
                    if (!response.addresses.includes(addr)) {
                      response.addresses.push(addr);
                    }
                  }
                  return resolve(response);
                }
              },
            };
            try {
              request = Services.dns.asyncResolve(
                hostname,
                Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
                dnsFlags,
                null, // AdditionalInfo
                listener,
                null,
                {} /* defaultOriginAttributes */
              );
            } catch (e) {
              // handle exceptions such as offline mode.
              return reject({ message: e.name });
            }
          });
        },
      },
    };
  }
};