Use IP address library

This commit is contained in:
M66B 2022-05-11 12:10:40 +02:00
parent d26ee2d300
commit cb2aea7c66
4 changed files with 11 additions and 17 deletions

View File

@ -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).

View File

@ -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

View File

@ -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).

View File

@ -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<String> 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;