Skip to content

Commit 6c4fea2

Browse files
authored
Merge pull request activepieces#6401 from amrabuaza/fix/AP-194/get-public-ip-failed-due-to-google-dns-is-blocked
fix(get-public-ip): resolve issue when Google DNS is blocked
2 parents 7914032 + c0e0123 commit 6c4fea2

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

packages/server/api/src/app/flows/flow-version/flow-version-validator-util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ function validateProps(
205205
const propsValidator = TypeCompiler.Compile(propsSchema)
206206
const valid = propsValidator.Check(input)
207207
const cleanInput = !isNil(input) ? Object.fromEntries(
208-
Object.keys(props).map(key => [key, input?.[key]])
208+
Object.keys(props).map(key => [key, input?.[key]]),
209209
) : undefined
210210

211211
return {

packages/server/shared/src/lib/network-utils.ts

+35-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dns from 'node:dns/promises'
2+
import os from 'os'
23
import { ApEnvironment, isNil } from '@activepieces/shared'
34
import { FastifyRequest } from 'fastify'
45

@@ -12,25 +13,52 @@ type IpMetadata = {
1213

1314
let ipMetadata: IpMetadata | undefined
1415

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+
1531
const getPublicIp = async (): Promise<IpMetadata> => {
1632
if (ipMetadata !== undefined) {
1733
return ipMetadata
1834
}
1935

20-
dns.setServers([GOOGLE_DNS])
36+
try {
37+
dns.setServers([GOOGLE_DNS])
2138

22-
const ipList = await dns.resolve(PUBLIC_IP_ADDRESS_QUERY, 'TXT')
39+
const ipList = await dns.resolve(PUBLIC_IP_ADDRESS_QUERY, 'TXT')
2340

24-
ipMetadata = {
25-
ip: ipList[0][0],
26-
}
41+
ipMetadata = {
42+
ip: ipList[0][0],
43+
}
2744

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+
}
2957
}
3058

3159
const getPublicUrl = async (environment: ApEnvironment, frontendUrl: string): Promise<string> => {
3260
let url = frontendUrl
33-
61+
3462
if (extractHostname(url) === 'localhost' && environment === ApEnvironment.PRODUCTION) {
3563
url = `http://${(await networkUtls.getPublicIp()).ip}`
3664
}

0 commit comments

Comments
 (0)