Repository navigation
fix: update executor and driver server URL state on directConnect #2500
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
Open
hoanle0126
wants to merge
3
commits into
appium:master
Choose a base branch
from
hoanle0126:fix/direct-connect-server-url-state
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
6d010be
fix: update executor and driver server URL state on directConnect
hoanle0126 e4eed84
fix: address review feedback on executor client state and log broadca…
hoanle0126 8ee115c
fix: address review feedback on LogBroadcastEndpoints and DirectConne…
hoanle0126 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
src/main/java/io/appium/java_client/internal/LogBroadcastEndpoints.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * See the NOTICE file distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.appium.java_client.internal; | ||
|
|
||
| import io.appium.java_client.CommandExecutionHelper; | ||
| import io.appium.java_client.ExecutesMethod; | ||
| import io.appium.java_client.remote.AppiumCommandExecutor; | ||
| import io.appium.java_client.remote.AppiumRemoteWebDriver; | ||
| import io.appium.java_client.remote.DirectConnect; | ||
| import io.appium.java_client.remote.SessionId; | ||
| import io.appium.java_client.ws.StringWebSocketClient; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import java.net.URI; | ||
| import java.net.URL; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Shared endpoint resolution and WebSocket startup helpers for device log broadcast listeners. | ||
| */ | ||
| @NullMarked | ||
| public final class LogBroadcastEndpoints { | ||
| private static final Set<String> LOOPBACK_HOSTS = Set.of("localhost", "127.0.0.1", "::1", "0:0:0:0:0:0:0:1"); | ||
| private static final Set<String> WILDCARD_HOSTS = Set.of("0.0.0.0", "::", "0:0:0:0:0:0:0:0"); | ||
|
|
||
| private LogBroadcastEndpoints() { | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the active remote server URL from a driver's command executor. | ||
| * | ||
| * @param driver the driver executing log broadcast commands | ||
| * @return the current remote server URL, or {@code null} if unavailable | ||
| */ | ||
| @Nullable | ||
| public static URL resolveServerUrl(ExecutesMethod driver) { | ||
| return getExecutor(driver).getAddressOfRemoteServer(); | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the host for a log broadcast WebSocket connection from the server URL, | ||
| * mapping missing or wildcard bind hosts ({@code 0.0.0.0}, {@code [::]}) to {@code fallbackHost}. | ||
| * | ||
| * @param serverUrl the active remote server URL, or {@code null} if unknown | ||
| * @param fallbackHost the loopback host to return when {@code serverUrl} has no usable host | ||
| * @return the resolved broadcast host | ||
| */ | ||
| public static String resolveHost(@Nullable URL serverUrl, String fallbackHost) { | ||
| String host = serverUrl != null ? serverUrl.getHost() : ""; | ||
| if (host.isEmpty() || WILDCARD_HOSTS.contains(normalizeHost(host))) { | ||
| return fallbackHost; | ||
| } | ||
| return host; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the port for a log broadcast WebSocket connection from the server URL. | ||
| * | ||
| * @param serverUrl the active remote server URL, or {@code null} if unknown | ||
| * @param fallbackPort the port to return when {@code serverUrl} defines neither an explicit nor default port | ||
| * @return the resolved broadcast port | ||
| */ | ||
| public static int resolvePort(@Nullable URL serverUrl, int fallbackPort) { | ||
| if (serverUrl == null) { | ||
| return fallbackPort; | ||
| } | ||
| if (serverUrl.getPort() > 0) { | ||
| return serverUrl.getPort(); | ||
| } | ||
| return serverUrl.getDefaultPort() > 0 ? serverUrl.getDefaultPort() : fallbackPort; | ||
| } | ||
|
|
||
| /** | ||
| * Starts server-side log streaming and connects the WebSocket client to the resolved broadcast endpoint. | ||
| * | ||
| * @param driver the driver executing {@code mobile: startLogsBroadcast} | ||
| * @param client the target WebSocket client | ||
| * @param host the target WebSocket host | ||
| * @param port the target WebSocket port | ||
| * @param deviceLogType the log endpoint suffix (such as {@code logcat} or {@code syslog}) | ||
| */ | ||
| public static void startBroadcast( | ||
| ExecutesMethod driver, | ||
| StringWebSocketClient client, | ||
| String host, | ||
| int port, | ||
| String deviceLogType | ||
| ) { | ||
| var remoteWebDriver = (AppiumRemoteWebDriver) driver; | ||
| var executor = getExecutor(driver); | ||
| CommandExecutionHelper.executeScript(driver, "mobile: startLogsBroadcast"); | ||
| URI endpoint = buildBroadcastUri( | ||
| executor.getAddressOfRemoteServer(), | ||
| executor.getReplacedHttpServerUrls(), | ||
| host, | ||
| port, | ||
| remoteWebDriver.getSessionId(), | ||
| deviceLogType | ||
| ); | ||
| client.connect(endpoint); | ||
| } | ||
|
|
||
| /** | ||
| * Builds a device log broadcast WebSocket URI. Uses {@code wss} when {@code serverUrl} uses | ||
| * {@code https}, unless the requested {@code host:port} matches a plain-{@code http} server URL | ||
| * previously replaced (e.g. by {@code directConnect}) and does not match the active {@code serverUrl}. | ||
| * | ||
| * @param serverUrl the active remote server URL, or {@code null} if unknown | ||
| * @param replacedHttpServerUrls plain-{@code http} server URLs previously replaced on the executor | ||
| * @param host the target WebSocket host | ||
| * @param port the target WebSocket port | ||
| * @param sessionId the active session identifier | ||
| * @param deviceLogType the log endpoint suffix (such as {@code logcat} or {@code syslog}) | ||
| * @return the WebSocket endpoint URI | ||
| */ | ||
| public static URI buildBroadcastUri( | ||
| @Nullable URL serverUrl, | ||
| List<URL> replacedHttpServerUrls, | ||
| String host, | ||
| int port, | ||
| @Nullable SessionId sessionId, | ||
| String deviceLogType | ||
| ) { | ||
| String scheme = chooseScheme(serverUrl, replacedHttpServerUrls, host, port); | ||
| String endpoint = String.format( | ||
| "%s://%s:%s/ws/session/%s/appium/device/%s", | ||
| scheme, DirectConnect.formatHost(host), port, sessionId, deviceLogType | ||
| ); | ||
| return URI.create(endpoint); | ||
| } | ||
|
|
||
| private static AppiumCommandExecutor getExecutor(ExecutesMethod driver) { | ||
| var remoteWebDriver = (AppiumRemoteWebDriver) driver; | ||
| return (AppiumCommandExecutor) remoteWebDriver.getCommandExecutor(); | ||
| } | ||
|
|
||
| private static String chooseScheme( | ||
| @Nullable URL serverUrl, | ||
| List<URL> replacedHttpServerUrls, | ||
| String host, | ||
| int port | ||
| ) { | ||
| if (serverUrl == null || !"https".equalsIgnoreCase(serverUrl.getProtocol())) { | ||
| return "ws"; | ||
| } | ||
| if (!matchesEndpoint(serverUrl, host, port) | ||
| && replacedHttpServerUrls.stream().anyMatch(url -> matchesEndpoint(url, host, port))) { | ||
| return "ws"; | ||
| } | ||
| return "wss"; | ||
| } | ||
|
|
||
| private static boolean matchesEndpoint(URL candidateUrl, String host, int port) { | ||
| if (resolvePort(candidateUrl, -1) != port) { | ||
| return false; | ||
| } | ||
| String candidateHost = normalizeComparableHost(candidateUrl.getHost()); | ||
| String targetHost = normalizeComparableHost(host); | ||
| if (candidateHost.isEmpty() || targetHost.isEmpty()) { | ||
| return false; | ||
| } | ||
| return candidateHost.equals(targetHost) | ||
| || LOOPBACK_HOSTS.contains(candidateHost) && LOOPBACK_HOSTS.contains(targetHost); | ||
| } | ||
|
|
||
| private static String normalizeComparableHost(String host) { | ||
| String normalized = normalizeHost(host); | ||
| return WILDCARD_HOSTS.contains(normalized) ? "127.0.0.1" : normalized; | ||
| } | ||
|
|
||
| private static String normalizeHost(String host) { | ||
| String trimmed = host.trim().toLowerCase(Locale.ROOT); | ||
| if (trimmed.startsWith("[") && trimmed.endsWith("]") && trimmed.length() > 2) { | ||
| return trimmed.substring(1, trimmed.length() - 1); | ||
| } | ||
| return trimmed; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.