Skip to content

Commit 23a383c

Browse files
authored
Merge pull request #1988 from streamr-dev/front-2053-improve-node-reachability-check
Improve the reachability check
2 parents dda52f4 + 3de4e04 commit 23a383c

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

src/shared/stores/operatorReachability.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,24 @@ const useOperatorReachabilityStore = create<{
6767
draft.pending = true
6868
})
6969

70-
let reachable = false
71-
72-
let ws: WebSocket | undefined
73-
74-
try {
75-
reachable = await new Promise<boolean>((resolve) => {
76-
/**
77-
* Replace the following with a real connectivity
78-
* checking logic.
79-
*/
80-
resolve(!!url)
81-
})
82-
} finally {
83-
ws?.close()
84-
85-
ws = undefined
70+
/**
71+
* StreamrClient opens many WebSocket connections, often hitting
72+
* browser limits.
73+
*
74+
* Normally, we'd attempt a real WSS connection, but since browsers
75+
* handle limits differently, it's hard to tell whether a failure
76+
* is due to node issues or browser constraints.
77+
*
78+
* @todo Let’s revisit this once we have a more reliable method for
79+
* confirming node reachability.
80+
*/
8681

87-
updateProbe(url, (draft) => {
88-
Object.assign(draft, {
89-
pending: false,
90-
reachable,
91-
})
82+
updateProbe(url, (draft) => {
83+
Object.assign(draft, {
84+
pending: false,
85+
reachable: tls && !!port && !!host && !isIPAddress(host),
9286
})
93-
}
87+
})
9488
},
9589
}
9690
})
@@ -152,3 +146,13 @@ export function useIsNodeIdReachable(nodeId: string) {
152146

153147
return pending ? 'pending' : reachable
154148
}
149+
150+
const IPv4RegExp =
151+
/^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)){3}$/
152+
153+
const IPv6RegExp =
154+
/^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4}:){1,7}:|:([0-9a-fA-F]{1,4}:){1,7}|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4})$/
155+
156+
function isIPAddress(value: string): boolean {
157+
return IPv4RegExp.test(value) || IPv6RegExp.test(value)
158+
}

0 commit comments

Comments
 (0)