Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a Meteor/Cordova App from connecting to 10.0.2.2? (And why does the app connect there?)

I have a Meteor app which runs on a local server for developement (http://10.0.2.10:3000). The ROOT_URL is set correctly so __meteor_runtime_config__.ROOT_URL equals this URL. Of course the app is working perfectly fine in the browser on a client computer within 10.0.2.0/24. The app is also working fine on mobile chrome/firefox on my android cell phone which is also part of 10.0.2.0/24. However when I try to run it as app on this cell phone with meteor run android-device --mobile-server http://10.0.2.10:3000/ something strange happens:

When the app starts for the first time (or the first time after I clear all app data) it works like it should (content from the DB is loaded) for a few short seconds. Then the app reloads and any remote content from the DB isn't loaded anymore. I have added the following function to see where Meteor tries to connect to:

Meteor.startup(function(){
    console.log(__meteor_runtime_config__.ROOT_URL);
})

The first time when remote content is loaded this returns http://10.0.2.10:3000/ like I would expect. The second time when remote content isn't loaded it returns http://10.0.2.2:3000/.

The question now is, why is Meteor/Cordova doing this and how can I stop this behavior? Because obviously I cannot test the app this way. I'm not yet sure if it would work in production when I have a FQDN and HTTPS proxy but that's beyond the point.

I tried to find 10.0.2.2 as nothing in my LAN is running there and I have not specified this IP anywhere and found it in /cordova-build/www/application/index.html which seems to be generated from boilerplate_web.cordova.html (see this link https://searchcode.com/codesearch/view/91819963/). However Meteor offers the possibility to override these generated files with a folder cordova-build-override and so I did removing the whole

if (/Android/i.test(navigator.userAgent)) {
    //[...]
}

block and added a short console.log('removed'). This gets called so I know the override was successful and when I grep through the whole built .apk file 10.0.2.2 isn't found anymore - still the behavior is the same.

Any ideas what's going on and what to do?

like image 916
Jey DWork Avatar asked Sep 29 '22 14:09

Jey DWork


1 Answers

So even when you set your ROOT_URL correctly there are still special variables for mobile versions of it which do not get set and may contain localhost. And there seem to exist more code snippets in the meteor project which replace localhost with 10.0.2.2 aside from the one I mentioned above, when a Cordova client is connecting. So that seems to cause my app to reconnect to 10.0.2.2.

The mobile URL variables I could find are process.env.MOBILE_ROOT_URL and process.env.MOBILE_DDP_URL. So in a Meteor.startup() function I now set those to my real ROOT_URL on the server side. My Android (Cordova) app now still is reconnecting a few seconds after its first start up but it's reconnecting to the same (and real) server URL (thus everything works fine)!

I still don't know why its reconnecting and those mobile variables and their use don't seem to be very well documented (or I missed something) but I can live with the way things work now.

like image 171
Jey DWork Avatar answered Dec 31 '22 22:12

Jey DWork