@@ -6,6 +6,13 @@ const Packet = require('../packet');
66const protocols = {
77 'http:' : http . get ,
88 'https:' : https . get ,
9+ /**
10+ * HTTP/2 GET
11+ *
12+ * @param {string | URL } url
13+ * @param {http.RequestOptions } options
14+ * @param {(res: http.IncomingMessage) => void } done
15+ */
916 'h2:' : ( url , options , done ) => {
1017 const urlObj = new URL ( url ) ;
1118 const client = http2 . connect ( url . replace ( 'h2:' , 'https:' ) ) ;
@@ -34,19 +41,21 @@ const protocols = {
3441} ;
3542
3643const makeRequest = ( url , query ) => new Promise ( ( resolve , reject ) => {
37- const index = url . indexOf ( '://' ) ;
38- if ( index === - 1 ) url = `https://${ url } ` ;
44+ if ( ! url . includes ( '/' ) ) { // pure IP or pure domain like "1.0.0.1" or "dns.google"
45+ url += '/dns-query' ;
46+ }
47+ if ( ! url . includes ( '://' ) ) url = `https://${ url } ` ;
3948 const u = new URL ( url ) ;
49+
4050 // The DNS query is included in a single variable named “dns” in the
4151 // query component of the request URI. The value of the “dns” variable
4252 // is the content of the DNS request message, encoded with base64url
4353 // [RFC4648](https://datatracker.ietf.org/doc/html/rfc8484#section-4.1).
44- const searchParams = u . searchParams ;
45- searchParams . set ( 'dns' , query ) ;
46- u . search = searchParams . toString ( ) ;
54+ u . searchParams . set ( 'dns' , query ) ;
55+
4756 const get = protocols [ u . protocol ] ;
4857 if ( ! get ) throw new Error ( `Unsupported protocol: ${ u . protocol } , must be specified (http://, https:// or h2://)` ) ;
49- const req = get ( u . toString ( ) , { headers : { accept : 'application/dns-message' } } , resolve ) ;
58+ const req = get ( u , { headers : { accept : 'application/dns-message' } } , resolve ) ;
5059 if ( req ) req . on ( 'error' , reject ) ;
5160} ) ;
5261
0 commit comments