From cb2aea7c66105dc14faab545aab3195d1530961f Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 11 May 2022 12:10:40 +0200 Subject: [PATCH] Use IP address library --- ATTRIBUTION.md | 1 + app/build.gradle | 5 +++++ app/src/main/assets/ATTRIBUTION.md | 1 + .../eu/faircode/email/ConnectionHelper.java | 21 ++++--------------- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 469f86036f..81c72ff1e2 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -43,3 +43,4 @@ FairEmail uses: * [Caladea font](https://fonts.google.com/specimen/Caladea). By Andrés Torresi, Carolina Giovanolli. [Apache License 2.0](https://fonts.google.com/specimen/Caladea#license). * [Apache Commons Compress](https://commons.apache.org/proper/commons-compress/). Copyright © 2002-2021 The Apache Software Foundation. All Rights Reserved. [Apache License 2.0](https://www.apache.org/licenses/). * [LeakCanary](https://github.com/square/leakcanary). Copyright 2015 Square, Inc. [Apache License 2.0](https://github.com/square/leakcanary/blob/main/LICENSE.txt). +* [IPAddress](https://github.com/seancfoley/IPAddress). Copyright 2016-2018 Sean C Foley. [Apache License 2.0](https://github.com/seancfoley/IPAddress/blob/master/LICENSE). diff --git a/app/build.gradle b/app/build.gradle index afa4dfa528..5d1a999486 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -375,6 +375,7 @@ dependencies { def rxjava2_version = "2.2.21" def svg_version = "1.4" def compress_version = "1.21" + def ipaddress_version = "5.3.4" def canary_version = "2.8.1" // https://developer.android.com/jetpack/androidx/releases/startup @@ -579,6 +580,10 @@ dependencies { // https://mvnrepository.com/artifact/org.apache.commons/commons-compress implementation "org.apache.commons:commons-compress:$compress_version" + // https://seancfoley.github.io/IPAddress/ + // https://mvnrepository.com/artifact/com.github.seancfoley/ipaddress + implementation "com.github.seancfoley:ipaddress:$ipaddress_version" + // https://github.com/square/leakcanary // https://square.github.io/leakcanary/getting_started/ // https://mvnrepository.com/artifact/com.squareup.leakcanary/leakcanary-android diff --git a/app/src/main/assets/ATTRIBUTION.md b/app/src/main/assets/ATTRIBUTION.md index 469f86036f..81c72ff1e2 100644 --- a/app/src/main/assets/ATTRIBUTION.md +++ b/app/src/main/assets/ATTRIBUTION.md @@ -43,3 +43,4 @@ FairEmail uses: * [Caladea font](https://fonts.google.com/specimen/Caladea). By Andrés Torresi, Carolina Giovanolli. [Apache License 2.0](https://fonts.google.com/specimen/Caladea#license). * [Apache Commons Compress](https://commons.apache.org/proper/commons-compress/). Copyright © 2002-2021 The Apache Software Foundation. All Rights Reserved. [Apache License 2.0](https://www.apache.org/licenses/). * [LeakCanary](https://github.com/square/leakcanary). Copyright 2015 Square, Inc. [Apache License 2.0](https://github.com/square/leakcanary/blob/main/LICENSE.txt). +* [IPAddress](https://github.com/seancfoley/IPAddress). Copyright 2016-2018 Sean C Foley. [Apache License 2.0](https://github.com/seancfoley/IPAddress/blob/master/LICENSE). diff --git a/app/src/main/java/eu/faircode/email/ConnectionHelper.java b/app/src/main/java/eu/faircode/email/ConnectionHelper.java index 35a5423a5d..016811c07a 100644 --- a/app/src/main/java/eu/faircode/email/ConnectionHelper.java +++ b/app/src/main/java/eu/faircode/email/ConnectionHelper.java @@ -59,6 +59,8 @@ import javax.net.SocketFactory; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; +import inet.ipaddr.IPAddressString; + public class ConnectionHelper { static final List PREF_NETWORK = Collections.unmodifiableList(Arrays.asList( "metered", "roaming", "rlah", "require_validated", "vpn_only" // update network state @@ -558,23 +560,8 @@ public class ConnectionHelper { static boolean inSubnet(final String ip, final String net, final int prefix) { try { - byte[] _ip = InetAddress.getByName(ip).getAddress(); - byte[] _net = InetAddress.getByName(net).getAddress(); - - if (_ip.length != _net.length) - return false; - - int i = 0; - int p = prefix; - while (p >= 8) { - if (_ip[i] != _net[i]) - return false; - ++i; - p -= 8; - } - - int m = (0xFF00 >> p) & 0xFF; - return (_ip[i] & m) == (_net[i] & m); + return new IPAddressString(net + "/" + prefix).getAddress() + .contains(new IPAddressString(ip).getAddress()); } catch (Throwable ex) { Log.w(ex); return false;