From 8b57b53788e51423243e26af82f6e8eb46de52ce Mon Sep 17 00:00:00 2001 From: Mygod Date: Sat, 29 Jul 2017 14:36:51 +0800 Subject: [PATCH] Misc refinements --- .../com/github/shadowsocks/utils/Dns.java | 19 ++------ .../com/github/shadowsocks/acl/Acl.scala | 47 ++++--------------- 2 files changed, 13 insertions(+), 53 deletions(-) diff --git a/mobile/src/main/java/com/github/shadowsocks/utils/Dns.java b/mobile/src/main/java/com/github/shadowsocks/utils/Dns.java index d203f978..921c652d 100644 --- a/mobile/src/main/java/com/github/shadowsocks/utils/Dns.java +++ b/mobile/src/main/java/com/github/shadowsocks/utils/Dns.java @@ -16,24 +16,18 @@ package com.github.shadowsocks.utils; -import android.annotation.SuppressLint; -import android.app.PendingIntent; -import android.content.Context; -import android.content.Intent; import android.annotation.TargetApi; -import android.util.Log; +import android.content.Context; import android.net.ConnectivityManager; import android.net.LinkProperties; import android.os.Build; import java.lang.reflect.Method; -import java.lang.reflect.InvocationTargetException; +import java.net.InetAddress; import java.util.ArrayList; import java.util.Collection; -import java.net.InetAddress; public class Dns { - public static String getDnsResolver(Context context) throws Exception { Collection dnsResolvers = getDnsResolvers(context); if (dnsResolvers.isEmpty()) { @@ -46,25 +40,22 @@ public class Dns { return dnsResolver; } - @TargetApi(Build.VERSION_CODES.LOLLIPOP) private static Collection getDnsResolvers(Context context) throws Exception { ArrayList addresses = new ArrayList(); ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); Class LinkPropertiesClass = Class.forName("android.net.LinkProperties"); - Method getActiveLinkPropertiesMethod = ConnectivityManager.class.getMethod("getActiveLinkProperties", new Class []{}); + Method getActiveLinkPropertiesMethod = ConnectivityManager.class.getMethod("getActiveLinkProperties"); Object linkProperties = getActiveLinkPropertiesMethod.invoke(connectivityManager); if (linkProperties != null) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - Method getDnsesMethod = LinkPropertiesClass.getMethod("getDnses", new Class []{}); + Method getDnsesMethod = LinkPropertiesClass.getMethod("getDnses"); Collection dnses = (Collection)getDnsesMethod.invoke(linkProperties); for (Object dns : dnses) { addresses.add((InetAddress)dns); } } else { - for (InetAddress dns : ((LinkProperties)linkProperties).getDnsServers()) { - addresses.add(dns); - } + addresses.addAll(((LinkProperties) linkProperties).getDnsServers()); } } diff --git a/mobile/src/main/scala/com/github/shadowsocks/acl/Acl.scala b/mobile/src/main/scala/com/github/shadowsocks/acl/Acl.scala index dc953f6c..26d0fe24 100644 --- a/mobile/src/main/scala/com/github/shadowsocks/acl/Acl.scala +++ b/mobile/src/main/scala/com/github/shadowsocks/acl/Acl.scala @@ -26,26 +26,6 @@ class Acl { @DatabaseField var bypass: Boolean = _ - def getBypassHostnamesString: String = bypassHostnames.mkString("\n") - def getProxyHostnamesString: String = proxyHostnames.mkString("\n") - def getSubnetsString: String = subnets.mkString("\n") - def setBypassHostnamesString(value: String) { - bypassHostnames.clear() - bypassHostnames ++= value.split("\n") - } - def setProxyHostnamesString(value: String) { - proxyHostnames.clear() - proxyHostnames ++= value.split("\n") - } - def setSubnetsString(value: String) { - subnets.clear() - subnets ++= value.split("\n").map(Subnet.fromString) - } - def setUrlRules(value: String) { - urls.clear() - urls ++= value.split("\n") - } - def fromAcl(other: Acl): Acl = { bypassHostnames.clear() bypassHostnames ++= other.bypassHostnames @@ -71,17 +51,10 @@ class Acl { var in_urls = false for (line <- value.getLines()) (line.indexOf('#') match { case -1 => if (!in_urls) line else "" - case index => { - line.indexOf("URLS_BEGIN") match { - case -1 => - case index => in_urls = true - } - line.indexOf("URLS_END") match { - case -1 => - case index => in_urls = false - } - "" // ignore any comment lines - } + case index => + if (line.contains("URLS_BEGIN")) in_urls = true + if (line.contains("URLS_END")) in_urls = false + line.substring(0, index) // trim comments }).trim match { case "[outbound_block_list]" => hostnames = null @@ -95,10 +68,8 @@ class Acl { case "[reject_all]" | "[bypass_all]" => bypass = true case "[accept_all]" | "[proxy_all]" => bypass = false case input if subnets != null && input.nonEmpty => try subnets += Subnet.fromString(input) catch { - case _: IllegalArgumentException => if (input.startsWith("http://") || input.startsWith("https://")) { - urls += input - } - hostnames += input + case _: IllegalArgumentException => if (input.startsWith("http://") || input.startsWith("https://")) + urls += input else hostnames += input } case _ => } @@ -116,7 +87,7 @@ class Acl { try { urls.foreach((url: String) => result.append(Source.fromURL(url).mkString)) } catch { - case e: IOException => // ignore + case _: IOException => // ignore } } result.append("#URLS_END\n") @@ -141,9 +112,7 @@ class Acl { result.toString } - override def toString: String = { - getAclString(false) - } + override def toString: String = getAclString(false) def isValidCustomRules: Boolean = bypass && bypassHostnames.isEmpty