@@ -7,13 +7,14 @@ import { params } from "./params";
77export async function createLocalConfigFile ( device : string ) : Promise < string > {
88 try {
99 const remoteFilePath = getRemoteConfigFilePath ( device , "conf" ) ;
10- const localIp = await getLocalIp ( ) ;
10+ const localIp = await getLocalIpWithRetries ( 30 ) ;
1111 const localEndpoint = `${ localIp } :${ params . SERVER_PORT } ` ;
1212
1313 const remoteConfigFile = fs . readFileSync ( remoteFilePath , "utf8" ) ;
1414 const localConfigFile = setLocalEndpoint ( remoteConfigFile , localEndpoint ) ;
1515
16- if ( localConfigFile === remoteConfigFile ) throw Error ( "Error generating localConfigFile" ) ;
16+ if ( localConfigFile === remoteConfigFile )
17+ throw Error ( "Error generating localConfigFile" ) ;
1718 return localConfigFile ;
1819 } catch ( e ) {
1920 e . message = `Error creating localConfigFile: ${ e . message } ` ;
@@ -23,22 +24,55 @@ export async function createLocalConfigFile(device: string): Promise<string> {
2324
2425// Utils
2526
27+ async function getLocalIpWithRetries ( retries : number ) : Promise < string > {
28+ // An integer n >= 1
29+ retries = Math . max ( Math . floor ( retries ) , 1 ) ;
30+
31+ for ( let i = 0 ; i < retries - 1 ; i ++ ) {
32+ try {
33+ return await getLocalIp ( ) ;
34+ } catch ( e ) {
35+ console . log ( `Error getting local IP: ${ e . message } ` ) ;
36+ console . log ( `Retrying... (${ i + 1 } /${ retries } )` ) ;
37+ }
38+ }
39+
40+ return await getLocalIp ( ) ;
41+ }
42+
2643async function getLocalIp ( ) : Promise < string > {
27- try {
28- const localIp = await got ( params . DAPPNODE_API_URL_GET_INTERNAL_IP ) . text ( ) ;
29- if ( ! localIp ) throw Error ( "localIp is empty" ) ;
30- if ( ! ipRegex ( { exact : true } ) . test ( localIp ) ) throw Error ( "Invalida localIp" ) ;
31- return localIp ;
32- } catch ( e ) {
33- e . message = `Error fetching localIp: ${ e . message } ` ;
34- throw e ;
44+ const dappmanagerHostnames = params . DAPPMANAGER_HOSTNAMES ;
45+ const getLocalIpUrls = dappmanagerHostnames . map (
46+ ( hostname ) => `http://${ hostname } ${ params . GET_INTERNAL_IP_ENDPOINT } `
47+ ) ;
48+
49+ let errorMessages : string [ ] = [ ] ;
50+
51+ for ( const url of getLocalIpUrls ) {
52+ try {
53+ const localIp = await got ( url ) . text ( ) ;
54+ if ( ! localIp ) throw Error ( "Local IP is empty" ) ;
55+ if ( ! ipRegex ( { exact : true } ) . test ( localIp ) )
56+ throw Error ( "Invalid local IP" ) ;
57+ return localIp ;
58+ } catch ( e ) {
59+ errorMessages . push (
60+ `Local IP could not be fetched from ${ url } : ${ e . message } `
61+ ) ;
62+ }
3563 }
64+ throw Error ( errorMessages . join ( "\n" ) ) ;
3665}
3766
38- export function setLocalEndpoint ( configFile : string , localEndpoint : string ) : string {
67+ export function setLocalEndpoint (
68+ configFile : string ,
69+ localEndpoint : string
70+ ) : string {
3971 return configFile
4072 . split ( "\n" )
41- . map ( ( row ) => ( row . startsWith ( "Endpoint =" ) ? `Endpoint = ${ localEndpoint } ` : row ) )
73+ . map ( ( row ) =>
74+ row . startsWith ( "Endpoint =" ) ? `Endpoint = ${ localEndpoint } ` : row
75+ )
4276 . join ( "\n" ) ;
4377}
4478
0 commit comments