@@ -8,13 +8,13 @@ const ddnsResolver = {
8
8
9
9
/**
10
10
* Resolves the given address to its IP
11
- * @param {String } address
12
- * @param {boolean } forceUpdate: whether to force resolution instead of using the cached value
11
+ * @param {String } domainName domain name of the dynamic DNS record
12
+ * @param {boolean } forceUpdate option to force resolution instead of using the cached value
13
13
*/
14
- resolveAddress : ( address , forceUpdate = false ) => {
15
- if ( ! forceUpdate && ddnsResolver . _cache . has ( address ) ) {
14
+ resolveAddress : ( domainName , forceUpdate = false ) => {
15
+ if ( ! forceUpdate && ddnsResolver . _cache . has ( domainName ) ) {
16
16
// Check if it is still valid
17
- const value = ddnsResolver . _cache . get ( address ) ;
17
+ const value = ddnsResolver . _cache . get ( domainName ) ;
18
18
const ip = value [ 0 ] ;
19
19
const lastUpdated = value [ 1 ] ;
20
20
const nowSeconds = Date . now ( ) ;
@@ -23,39 +23,35 @@ const ddnsResolver = {
23
23
return Promise . resolve ( ip ) ;
24
24
}
25
25
}
26
- ddnsResolver . _cache . delete ( address ) ;
26
+ ddnsResolver . _cache . delete ( domainName ) ;
27
27
// Reach here only if cache value doesn't exist or needs to be updated
28
- let host = address . toLowerCase ( ) ;
28
+ let host = domainName . toLowerCase ( ) ;
29
29
return ddnsResolver . _queryHost ( host )
30
30
. then ( ( resolvedIP ) => {
31
- ddnsResolver . _cache . set ( address , [ resolvedIP , Date . now ( ) ] ) ;
31
+ ddnsResolver . _cache . set ( domainName , [ resolvedIP , Date . now ( ) ] ) ;
32
32
return resolvedIP ;
33
33
} )
34
34
. catch ( ( /*error*/ ) => {
35
35
// return input address in case of failure
36
- return address ;
36
+ logger . error ( `Failed to resolve IP for ${ host } ` ) ;
37
+ return domainName ;
37
38
} ) ;
38
39
} ,
39
40
40
41
41
- /** Private **/
42
- // Properties
43
- /**
44
- * cache mapping host to (ip address, last updated time)
45
- */
42
+ /** Cache mapping host to (ip address, last updated time) */
46
43
_cache : new Map ( ) ,
47
44
48
- // Methods
49
45
/**
50
- *
51
- * @param {String } host
52
- * @returns {Promise }
46
+ * Uses execSafe to query the IP address of the given host
47
+ * @param {String } host host to query
48
+ * @returns {Promise } resolves to the IPV4 address of the host
53
49
*/
54
50
_queryHost : ( host ) => {
55
51
return utils . execSafe ( 'getent' , [ 'ahostsv4' , 'hosts' , host ] )
56
52
. then ( ( result ) => {
57
- if ( result . length < 8 ) {
58
- logger . error ( `IP lookup for ${ host } returned invalid output: ${ result } ` ) ;
53
+ if ( result . length < 8 || ! / ^ ( \d { 1 , 3 } \. ) { 3 } \d { 1 , 3 } $ / . test ( result ) ) {
54
+ logger . error ( `IPV4 lookup for ${ host } returned invalid output: ${ result } ` ) ;
59
55
throw error . ValidationError ( 'Invalid output from getent hosts' ) ;
60
56
}
61
57
const out = result . split ( / \s + / ) ;
0 commit comments