1
1
import dns from 'node:dns/promises'
2
+ import os from 'os'
2
3
import { ApEnvironment , isNil } from '@activepieces/shared'
3
4
import { FastifyRequest } from 'fastify'
4
5
@@ -12,25 +13,52 @@ type IpMetadata = {
12
13
13
14
let ipMetadata : IpMetadata | undefined
14
15
16
+ const getLocalIp = ( ) : string | null => {
17
+ const networkInterfaces = os . networkInterfaces ( )
18
+ for ( const interfaceName of Object . keys ( networkInterfaces ) ) {
19
+ const networkInterface = networkInterfaces [ interfaceName ]
20
+ if ( networkInterface ) {
21
+ for ( const iface of networkInterface ) {
22
+ if ( iface . family === 'IPv4' && ! iface . internal ) {
23
+ return iface . address
24
+ }
25
+ }
26
+ }
27
+ }
28
+ return null
29
+ }
30
+
15
31
const getPublicIp = async ( ) : Promise < IpMetadata > => {
16
32
if ( ipMetadata !== undefined ) {
17
33
return ipMetadata
18
34
}
19
35
20
- dns . setServers ( [ GOOGLE_DNS ] )
36
+ try {
37
+ dns . setServers ( [ GOOGLE_DNS ] )
21
38
22
- const ipList = await dns . resolve ( PUBLIC_IP_ADDRESS_QUERY , 'TXT' )
39
+ const ipList = await dns . resolve ( PUBLIC_IP_ADDRESS_QUERY , 'TXT' )
23
40
24
- ipMetadata = {
25
- ip : ipList [ 0 ] [ 0 ] ,
26
- }
41
+ ipMetadata = {
42
+ ip : ipList [ 0 ] [ 0 ] ,
43
+ }
27
44
28
- return ipMetadata
45
+ return ipMetadata
46
+ }
47
+ catch ( error ) {
48
+ const localIp = getLocalIp ( )
49
+ if ( localIp ) {
50
+ ipMetadata = {
51
+ ip : localIp ,
52
+ }
53
+ return ipMetadata
54
+ }
55
+ throw error ;
56
+ }
29
57
}
30
58
31
59
const getPublicUrl = async ( environment : ApEnvironment , frontendUrl : string ) : Promise < string > => {
32
60
let url = frontendUrl
33
-
61
+
34
62
if ( extractHostname ( url ) === 'localhost' && environment === ApEnvironment . PRODUCTION ) {
35
63
url = `http://${ ( await networkUtls . getPublicIp ( ) ) . ip } `
36
64
}
0 commit comments