birdofpreyru / react-native-static-server

Embedded HTTP server for React Native
https://dr.pogodin.studio/docs/react-native-static-server
Other
135 stars 21 forks source link

Fail to open Server with WiFi without Internet and cellular enabled (android) #103

Open RobinSchuler opened 4 months ago

RobinSchuler commented 4 months ago

Somewhat related to https://github.com/birdofpreyru/react-native-static-server/issues/21

How to reproduce:

I´m currently investigating if I can get a hold of the ip + port to use in that network but wanted to get in touch with you anyway.

If I turn off cellular, still use the wifi ( without internet connection ) and do not specify port + host static server will retrieve the correct ip + port, open the server and other devices within the network can access the server.

Would you be able to handle the case cellular + no internet WiFi on your side ?

Greetings

birdofpreyru commented 4 months ago

Hi @RobinSchuler , yes, it is the same stuff as #21.

The problem is clear — if you set nonLocal flag, the library on Android relies on this simple function to auto-select IP address to bind to: https://github.com/birdofpreyru/react-native-static-server/blob/038d9fddb39045282cd209c20a60f67c665a765c/android/src/main/java/com/drpogodin/reactnativestaticserver/ReactNativeStaticServerModule.kt#L42-L64

you see, it just cycles through all network interfaces, until the first one which is associated with a valid IPv4 address, and tries to use that address for the server. Apparently it does not result in the correct local IP in the situation you describe.

On my end, I don't know exactly how to correctly select the network interface among available ones; and I still have no motivation to spend my time on figuring it out and implementing it correctly. So, if you want to spend you time on making it work better — PRs are welcome!

Yeah, on the high level my thoughts regarding it were that there might be different needs to select the exact interface (like select local WIFI address, or select the mobile, or whatever makes sense). So I thought to deprecate nonLocal flag altogether, and instead allow to assign into hostname (apart of valid IP addresses) some special string constants which would be passed into that IP selection function and tell what exactly should be selected.

RobinSchuler commented 4 months ago

Thanks for the fast reply @birdofpreyru,

My first thought was also something like a "preferred Network" prop (or alternative a exclude network), where the code snippet you posted would be altered to look for a match with the given name. If "preferred Network" is not given or the name not found it would return the first match (like currently)

RobinSchuler commented 4 months ago

@birdofpreyru FYI, I patched it on my side (patch-package) with a filter prop. The filter checks if the interface name (NetworkInterface class) contains the filter criteria. (Interfaces my mobile device had were "wlan0", sth for roaming (cant remember the exact name) and something else (dummy I think)