From c8cec0839ed30d972efb193fc06c062c564351c1 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 30 Mar 2016 10:46:19 +0200 Subject: [PATCH] Only add routes when needed Refs #395 --- .../eu/faircode/netguard/ServiceSinkhole.java | 71 ++++++++++--------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/ServiceSinkhole.java b/app/src/main/java/eu/faircode/netguard/ServiceSinkhole.java index ed245b74..3c910b3a 100644 --- a/app/src/main/java/eu/faircode/netguard/ServiceSinkhole.java +++ b/app/src/main/java/eu/faircode/netguard/ServiceSinkhole.java @@ -969,47 +969,54 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS builder.addDnsServer(dns); } - // Exclude IP ranges - List listExclude = new ArrayList<>(); - listExclude.add(new IPUtil.CIDR("127.0.0.0", 8)); // localhost - if (tethering) { - // USB Tethering 192.168.42.x - // Wi-Fi Tethering 192.168.43.x - listExclude.add(new IPUtil.CIDR("192.168.42.0", 23)); - } + // Routing Configuration config = getResources().getConfiguration(); - if (config.mcc == 310 && config.mnc == 260) { - // T-Mobile Wi-Fi calling - listExclude.add(new IPUtil.CIDR("66.94.2.0", 24)); - listExclude.add(new IPUtil.CIDR("66.94.6.0", 23)); - listExclude.add(new IPUtil.CIDR("66.94.8.0", 22)); - listExclude.add(new IPUtil.CIDR("208.54.0.0", 16)); - } - listExclude.add(new IPUtil.CIDR("224.0.0.0", 3)); // broadcast + if (tethering || (config.mcc == 310 && config.mnc == 260)) { + // Exclude IP ranges + List listExclude = new ArrayList<>(); + listExclude.add(new IPUtil.CIDR("127.0.0.0", 8)); // localhost - Collections.sort(listExclude); + if (tethering) { + // USB Tethering 192.168.42.x + // Wi-Fi Tethering 192.168.43.x + listExclude.add(new IPUtil.CIDR("192.168.42.0", 23)); + } - try { - InetAddress start = InetAddress.getByName("0.0.0.0"); - for (IPUtil.CIDR exclude : listExclude) { - Log.i(TAG, "Exclude " + exclude.getStart().getHostAddress() + "..." + exclude.getEnd().getHostAddress()); - for (IPUtil.CIDR include : IPUtil.toCIDR(start, IPUtil.minus1(exclude.getStart()))) + if (config.mcc == 310 && config.mnc == 260) { + // T-Mobile Wi-Fi calling + listExclude.add(new IPUtil.CIDR("66.94.2.0", 24)); + listExclude.add(new IPUtil.CIDR("66.94.6.0", 23)); + listExclude.add(new IPUtil.CIDR("66.94.8.0", 22)); + listExclude.add(new IPUtil.CIDR("208.54.0.0", 16)); + } + + listExclude.add(new IPUtil.CIDR("224.0.0.0", 3)); // broadcast + + Collections.sort(listExclude); + + try { + InetAddress start = InetAddress.getByName("0.0.0.0"); + for (IPUtil.CIDR exclude : listExclude) { + Log.i(TAG, "Exclude " + exclude.getStart().getHostAddress() + "..." + exclude.getEnd().getHostAddress()); + for (IPUtil.CIDR include : IPUtil.toCIDR(start, IPUtil.minus1(exclude.getStart()))) + try { + builder.addRoute(include.address, include.prefix); + } catch (Throwable ex) { + Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); + } + start = IPUtil.plus1(exclude.getEnd()); + } + for (IPUtil.CIDR include : IPUtil.toCIDR("224.0.0.0", "255.255.255.255")) try { builder.addRoute(include.address, include.prefix); } catch (Throwable ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); } - start = IPUtil.plus1(exclude.getEnd()); + } catch (UnknownHostException ex) { + Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); } - for (IPUtil.CIDR include : IPUtil.toCIDR("224.0.0.0", "255.255.255.255")) - try { - builder.addRoute(include.address, include.prefix); - } catch (Throwable ex) { - Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); - } - } catch (UnknownHostException ex) { - Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); - } + } else + builder.addRoute("0.0.0.0", 0); builder.addRoute("0:0:0:0:0:0:0:0", 0);