Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reconnection logic error (internal and external url) #4294

Closed
Gifford47 opened this issue Mar 25, 2024 · 8 comments · Fixed by #4297
Closed

reconnection logic error (internal and external url) #4294

Gifford47 opened this issue Mar 25, 2024 · 8 comments · Fixed by #4297
Labels
bug Something isn't working webview

Comments

@Gifford47
Copy link

Gifford47 commented Mar 25, 2024

Home Assistant Android app version(s):
2024.1.5-full
Android version(s):
13
Device model(s):
Samsung Galaxy S21
Home Assistant version:
2024.3.3
Last working Home Assistant release (if known):
not known
Description of problem, include YAML if issue is related to notifications:
Related Issues:
#1361
HA roaming

On my phone I can connect to both internal/external URLs, but switching between the two requires me to shutdown the HA app.
When leaving the Home Network Wifi, th HA app gives: "Connection lost. Reconnecting..." until I shutdown the HA app and start it again.

This happens in both directions:

  • coming home after the phone switches to the wifi (ssl handshake fail -> force closing app)
  • leaving the house the phone switches to 4G (Connection lost, retrying ... -> but nothing happens)

The internal is "mydomain.de" and the external "mydomain.de:8123".
The external url is a cloudflare tunnel. On internal use with same url "mydomain.de" there's a dns rewrite rule to the correct HA ip.

If i leave my home wlan, the connection is lost. It appears a pop-up mentioned here but nothing changes with these options. After closing the app it connects to the external URL when i'm connected to GSM.

If i dont close the app and reconnect to my home wlan again, the app connects to homeassistant successfully. So i think the app never tries to connect to the external URL automatically (don`t see any reconnection logic, see attached log).
I also receive a ssl handshake error (didn't close the app) if coming from external url when connecting to my home wifi. Then the app log shows:

03-25 09:16:13.996  4919  5259 D WebviewActivity: External bus {"type":"connection-status","payload":{"event":"disconnected"},"id":5}
03-25 09:16:14.170  4919  5147 I cr_X509Util: Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
03-25 09:16:14.171  4919  5182 E chromium: [ERROR:ssl_client_socket_impl.cc(970)] handshake failed; returned -1, SSL error code 1, net_error -202
03-25 09:16:14.192  4919  4919 E WebviewActivity: onReceivedSslError: primary error: 3 certificate: Issued to: CN=192.168.1.10,O=server,ST=Some-State,C=DE;
03-25 09:16:14.192  4919  4919 E WebviewActivity: Issued by: CN=192.168.1.10,O=server,ST=Some-State,C=DE;
03-25 09:16:14.192  4919  4919 E WebviewActivity:  on URL: wss://mydomain.de/api/websocket

when closing app and reopen it, it connects as normal.

The issue is exists at least for one year for me.

Companion App Logs:

03-25 08:37:34.257 12798 12798 D IntegrationRepository: isAppLocked(): false. (LockEnabled: false, appActive: true, expireMillis: 1711352226396, currentMillis: 1711352254257)
03-25 08:37:34.257 12798 12798 D IntegrationRepository: setAppActive(): true
03-25 08:37:34.258 12798 12798 D InputMethodManager: startInputInner - Id : 0
03-25 08:37:34.258 12798 12798 I InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
03-25 08:37:35.327 12798 12798 D SensorReceiver: Received intent: android.net.wifi.STATE_CHANGE
03-25 08:37:35.369 12798 15472 D WebviewActivity: External bus {"type":"connection-status","payload":{"event":"disconnected"},"id":11}
03-25 08:37:35.600 12798 12798 D SensorReceiver: Received intent: android.net.wifi.WIFI_STATE_CHANGED
03-25 08:37:35.677 12798 12829 D ServerConnectionInfo: localUrl is: true, usesInternalSsid is: false, usesWifi is: false
03-25 08:37:35.690 12798 12865 D ServerConnectionInfo: localUrl is: true, usesInternalSsid is: false, usesWifi is: false
03-25 08:37:35.692 12798 12824 I WM-WorkerWrapper: Worker result SUCCESS for Work [ id=2bcf43f4-0921-49ae-a692-39ba1ac4fc77, tags={ io.homeassistant.companion.android.websocket.WebsocketManager } ]
03-25 08:37:35.697 12798 12864 D ServerConnectionInfo: localUrl is: true, usesInternalSsid is: false, usesWifi is: false
03-25 08:37:35.868 12798 12798 D ForegrndServiceLauncher: Check if service HighAccuracyLocationService is running. Service running = false
03-25 08:37:35.915 12798 12798 D ForegrndServiceLauncher: Check if service HighAccuracyLocationService is running. Service running = false
03-25 08:37:36.030 12798 12868 D ServerConnectionInfo: localUrl is: true, usesInternalSsid is: false, usesWifi is: false
03-25 08:37:36.034 12798 12868 D ServerConnectionInfo: localUrl is: true, usesInternalSsid is: false, usesWifi is: false
03-25 08:37:36.040 12798 12829 D ServerConnectionInfo: localUrl is: true, usesInternalSsid is: false, usesWifi is: false
03-25 08:37:36.044 12798 12829 D ServerConnectionInfo: localUrl is: true, usesInternalSsid is: false, usesWifi is: false
03-25 08:37:36.096 12798 25162 D TrafficStats: tagSocket(131) with statsTag=0xffffffff, statsUid=-1
03-25 08:37:36.097 12798 25552 D TrafficStats: tagSocket(141) with statsTag=0xffffffff, statsUid=-1
03-25 08:37:37.790 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:37.878 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:37.898 12798 12829 D LogcatReader: Read logcat for pid 12798
03-25 08:37:38.133 12798 12829 D LogcatReader: Done reading logcat for pid 12798
03-25 08:37:38.250 12798 12807 D InputTransport: Input channel destroyed: 'ClientS', fd=152
03-25 08:37:38.988 12798 12798 I Choreographer: Skipped 31 frames!  The application may be doing too much work on its main thread.
03-25 08:37:39.625 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:39.826 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:40.478 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:40.934 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:41.237 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:41.696 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:42.595 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:43.338 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:43.756 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:44.389 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:45.074 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:46.112 12798 25162 D TrafficStats: tagSocket(105) with statsTag=0xffffffff, statsUid=-1
03-25 08:37:46.112 12798 25552 D TrafficStats: tagSocket(143) with statsTag=0xffffffff, statsUid=-1
03-25 08:37:46.656 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:47.051 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:47.537 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:50.078 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:50.312 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:50.916 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:51.235 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:52.281 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:52.885 12798 12798 D ForegrndServiceLauncher: Check if service HighAccuracyLocationService is running. Service running = false
03-25 08:37:53.148 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:54.033 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:54.383 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:54.753 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:55.628 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:55.895 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:56.124 12798 25552 D TrafficStats: tagSocket(98) with statsTag=0xffffffff, statsUid=-1
03-25 08:37:56.125 12798 25162 D TrafficStats: tagSocket(138) with statsTag=0xffffffff, statsUid=-1
03-25 08:37:56.660 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:56.948 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:57.710 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:58.028 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:58.777 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:37:59.434 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:37:59.751 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:38:01.043 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:38:01.317 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:38:03.170 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 0
03-25 08:38:03.276 12798 12798 I ViewRootImpl@55c5706[SettingsActivity]: ViewPostIme pointer 1
03-25 08:38:03.282 12798 12829 D LogcatReader: Read logcat for pid 12798

Screenshot or video of problem:
image
image

Additional information:
another log:
log.txt

is it a normal behaviour that "ServerConnectionInfo: localUrl is: true" is always "true"?
related issue #1361

@Gifford47 Gifford47 added the bug Something isn't working label Mar 25, 2024
@Gifford47 Gifford47 changed the title switching between internal and external connection url reconnection logic error (internal and external url) Mar 25, 2024
@dshokouhi
Copy link
Member

is it a normal behaviour that "ServerConnectionInfo: localUrl is: true" is always "true"?

yes that just means you have a local URL defined.

@dshokouhi
Copy link
Member

dshokouhi commented Mar 25, 2024

so your logs do not indicate taht WiFi is being used for data, that would indeed be a reason why the URL switching is not working as we only use the internal URL if we determine the user is on local WiFi.

03-25 08:37:36.040 12798 12829 D ServerConnectionInfo: localUrl is: true, usesInternalSsid is: false, usesWifi is: false

what happens if you bypass this ad use the local IP address instead?

The external url is a cloudflare tunnel. On internal use with same url "mydomain.de" there's a dns rewrite rule to the correct HA ip.

this could also be the cause of the issue

@dshokouhi dshokouhi added the question Further information is requested label Mar 25, 2024
@Gifford47
Copy link
Author

When I bypass the rewrite rule and set the local IP address as internal URL I get I SSL handshake error because I have to use SSL and the cert is signed to the domain.
Basically the app works fine. I "only" have to exit the app. What is about a function that checks if the connection is working with the internal URL and if not it automatically switches to external and vice versa?

@dshokouhi
Copy link
Member

What is about a function that checks if the connection is working with the internal URL and if not it automatically switches to external and vice versa?

we dont get all the states a server can be in so we have the pop-up setup, for your own use case you may want to consider changing the setting "When loading dashboard and unknown if connected to home network WiFi" to "use internal connection URL" under Settings > Companion app > Selecting your server > select home network

this will allow the other URL to be tried regardless if we determine the user is on the correct network.

@Gifford47
Copy link
Author

I've already tried this setting, but the behaviour is the same.

I think my use case fits to everyone who uses cloudflare as external URL. If possible I would change my setup in a way that this issue is solved, but I tried every combination and I don't know any more.

Could you modify the code to this use case or show me in the right direction/to the responsible code? This situation is a little bit annoying.

@dshokouhi
Copy link
Member

I think my use case fits to everyone who uses cloudflare as external URL.

we have gotten a few PRs on cloudflare and have turned those down so we will not be making changes for that specific situation.

#4160 (comment)

your logs also do not demonstrate that the app should be changing the URL used so as of now we do not see an issue

@Gifford47
Copy link
Author

Gifford47 commented Mar 26, 2024

ok, i`ve tried also with the same domain, but without cloudflare (but with ssl on internal and external connection). same issue. it looks like the app don´t come around with the port in the external url.
whether cloudflare or not. doesn't it make sense to test the external connection anyway if the internal one doesn't work?

log.txt

@Gifford47
Copy link
Author

@dshokouhi Thanks for your fast answers and help! awesome community here 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working webview
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants