diff options
| author | Lena <lena@omega> | 2026-07-01 00:00:00 +0000 |
|---|---|---|
| committer | Lena <lena@omega> | 2026-07-01 00:00:00 +0000 |
| commit | 30ee8be82e44d1a8250d1f6e27e71f7d5f24aec5 (patch) | |
| tree | ef45988151ad217ecf2fcc81875b43e334e3aa85 /app/src/main/java/invalid/lena/scrcpy/Devices.java | |
| parent | c03f0464c1f834c2f4d8642cbfad322033d368b7 (diff) | |
| download | scrcpy-android-30ee8be82e44d1a8250d1f6e27e71f7d5f24aec5.tar.gz | |
app: enter the device address as one host:port field
The form asked for a host, a pairing port and a connection port as three
separate fields. Nothing on screen says which port is which, and the two
are only distinguishable if the reader already knows how adb pair works
on Android 11+.
Take the connection endpoint the way the target prints it, as a single
"ip:port" string copied off the Wireless debugging screen, and leave the
pairing dialog with just its port and code. Devices.parseAddress() does
the split and rejects anything malformed rather than guessing: an
unbracketed IPv6 literal would otherwise be split at the wrong colon.
Reported on the F-Droid submission (fdroiddata!41394).
Diffstat (limited to 'app/src/main/java/invalid/lena/scrcpy/Devices.java')
| -rw-r--r-- | app/src/main/java/invalid/lena/scrcpy/Devices.java | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/app/src/main/java/invalid/lena/scrcpy/Devices.java b/app/src/main/java/invalid/lena/scrcpy/Devices.java index c3e5141..0ba61a3 100644 --- a/app/src/main/java/invalid/lena/scrcpy/Devices.java +++ b/app/src/main/java/invalid/lena/scrcpy/Devices.java @@ -30,9 +30,13 @@ public final class Devices { this.port = port; } + // Bracket IPv6 literals so the port stays unambiguous. Round-trips + // through parseAddress(). @Override public String toString() { - return host + ":" + port; + return host.indexOf(':') < 0 + ? host + ":" + port + : "[" + host + "]:" + port; } @Override @@ -49,6 +53,44 @@ public final class Devices { private Devices() {} + // Parse "host:port" exactly as the target's Wireless debugging screen + // prints it. IPv6 literals must be bracketed ("[fe80::1]:5555") because + // the address itself contains colons; an unbracketed one is rejected + // rather than silently split at the wrong colon. Returns null on + // anything malformed - the only caller is a text field. + public static Device parseAddress(String s) { + if (s == null) return null; + s = s.trim(); + String host, port; + if (s.startsWith("[")) { + int end = s.indexOf(']'); + if (end < 0 || !s.startsWith("]:", end)) return null; + host = s.substring(1, end); + port = s.substring(end + 2); + } else { + int colon = s.indexOf(':'); + if (colon < 0 || colon != s.lastIndexOf(':')) return null; + host = s.substring(0, colon); + port = s.substring(colon + 1); + } + int p = parsePort(port); + if (host.isEmpty() || p < 0) return null; + return new Device(host, p); + } + + // Digits only, 1-65535. Returns -1 if it is not a usable port. + // Integer.parseInt() alone would accept "+5555" and " 5555". + public static int parsePort(String s) { + if (s == null) return -1; + s = s.trim(); + if (s.isEmpty() || s.length() > 5) return -1; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) < '0' || s.charAt(i) > '9') return -1; + } + int p = Integer.parseInt(s); + return p >= 1 && p <= 65535 ? p : -1; + } + // Pure-java parse: returns whatever rows are well-formed; logs and // skips anything malformed instead of nuking the list. public static List<Device> parse(String json) { |