From 4f0e527df79ecb38be809fd54cb940af6d51dcc5 Mon Sep 17 00:00:00 2001 From: Max Lv Date: Fri, 24 Feb 2017 21:00:40 +0800 Subject: [PATCH] Replace PDNSD with overture --- .gitmodules | 4 - mobile/build.sbt | 4 +- mobile/src/main/assets/overture/hosts | 1 + mobile/src/main/jni/Android.mk | 17 -- mobile/src/main/jni/pdnsd | 1 - .../com/github/shadowsocks/BaseService.scala | 13 ++ .../shadowsocks/ShadowsocksNatService.scala | 53 ++++-- .../shadowsocks/ShadowsocksVpnService.scala | 70 ++++---- .../github/shadowsocks/utils/Constants.scala | 159 ++++++++++-------- 9 files changed, 173 insertions(+), 149 deletions(-) create mode 100644 mobile/src/main/assets/overture/hosts delete mode 160000 mobile/src/main/jni/pdnsd diff --git a/.gitmodules b/.gitmodules index c14024db..d74fc99e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -17,10 +17,6 @@ path = mobile/src/main/jni/redsocks url = https://github.com/shadowsocks/redsocks.git branch = shadowsocks-android -[submodule "mobile/src/main/jni/pdnsd"] - path = mobile/src/main/jni/pdnsd - url = https://github.com/shadowsocks/pdnsd.git - branch = shadowsocks-android [submodule "mobile/src/main/jni/mbedtls"] path = mobile/src/main/jni/mbedtls url = https://github.com/ARMmbed/mbedtls diff --git a/mobile/build.sbt b/mobile/build.sbt index c52a430b..0eb74afc 100644 --- a/mobile/build.sbt +++ b/mobile/build.sbt @@ -2,8 +2,8 @@ enablePlugins(AndroidApp) android.useSupportVectors name := "shadowsocks" -version := "4.0.4" -versionCode := Some(179) +version := "4.1.0-SNAPSHOT" +versionCode := Some(180) proguardOptions ++= "-keep class com.github.shadowsocks.JniHelper { *; }" :: diff --git a/mobile/src/main/assets/overture/hosts b/mobile/src/main/assets/overture/hosts new file mode 100644 index 00000000..75721cd5 --- /dev/null +++ b/mobile/src/main/assets/overture/hosts @@ -0,0 +1 @@ +127.0.0.1 localhost diff --git a/mobile/src/main/jni/Android.mk b/mobile/src/main/jni/Android.mk index 97d899b8..a8089e22 100755 --- a/mobile/src/main/jni/Android.mk +++ b/mobile/src/main/jni/Android.mk @@ -239,23 +239,6 @@ LOCAL_CFLAGS := -O2 -std=gnu99 -DUSE_IPTABLES \ include $(BUILD_SHARED_EXECUTABLE) -######################################################## -## pdnsd -######################################################## - -include $(CLEAR_VARS) - -PDNSD_SOURCES := $(wildcard $(LOCAL_PATH)/pdnsd/src/*.c) - -LOCAL_MODULE := pdnsd -LOCAL_SRC_FILES := $(PDNSD_SOURCES:$(LOCAL_PATH)/%=%) -LOCAL_CFLAGS := -DANDROID -Wall -O2 -I$(LOCAL_PATH)/pdnsd \ - -I$(LOCAL_PATH)/include/pdnsd -I$(LOCAL_PATH)/libancillary -LOCAL_STATIC_LIBRARIES := libancillary -LOCAL_LDLIBS := -llog - -include $(BUILD_SHARED_EXECUTABLE) - ######################################################## ## shadowsocks-libev local ######################################################## diff --git a/mobile/src/main/jni/pdnsd b/mobile/src/main/jni/pdnsd deleted file mode 160000 index f6b0a56c..00000000 --- a/mobile/src/main/jni/pdnsd +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f6b0a56c74ff33e3df4c00859bc7ccefa1354fa4 diff --git a/mobile/src/main/scala/com/github/shadowsocks/BaseService.scala b/mobile/src/main/scala/com/github/shadowsocks/BaseService.scala index 5d5ec16d..994ab3d2 100644 --- a/mobile/src/main/scala/com/github/shadowsocks/BaseService.scala +++ b/mobile/src/main/scala/com/github/shadowsocks/BaseService.scala @@ -123,6 +123,19 @@ trait BaseService extends Service { false } else true + def getExternalIp() { + val url = "http://icanhazip.com" + val client = new OkHttpClient.Builder() + .connectTimeout(10, TimeUnit.SECONDS) + .writeTimeout(10, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .build() + val request = new Request.Builder() + .url(url) + .build() + client.newCall(request).execute().body.string + } + def connect() { if (profile.host == "198.199.101.152") { val holder = app.containerHolder diff --git a/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala b/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala index 4a4204b9..2d202977 100644 --- a/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala +++ b/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala @@ -49,7 +49,7 @@ class ShadowsocksNatService extends BaseService { var sslocalProcess: GuardedProcess = _ var sstunnelProcess: GuardedProcess = _ var redsocksProcess: GuardedProcess = _ - var pdnsdProcess: GuardedProcess = _ + var overtureProcess: GuardedProcess = _ var su: Shell.Interactive = _ def startShadowsocksDaemon() { @@ -83,21 +83,38 @@ class ShadowsocksNatService extends BaseService { } def startDnsDaemon() { - val reject = if (profile.ipv6) "224.0.0.0/3" else "224.0.0.0/3, ::/0" - IOUtils.writeString(new File(getFilesDir, "pdnsd-nat.conf"), profile.route match { + val externalIp = getExternalIp() + IOUtils.writeString(new File(getFilesDir, "overture-nat.conf"), profile.route match { case Acl.BYPASS_CHN | Acl.BYPASS_LAN_CHN | Acl.GFWLIST | Acl.CUSTOM_RULES => - ConfigUtils.PDNSD_DIRECT.formatLocal(Locale.ENGLISH, "", getCacheDir.getAbsolutePath, - "127.0.0.1", profile.localPort + 53, "114.114.114.114, 223.5.5.5, 1.2.4.8", - getBlackList, reject, profile.localPort + 63, reject) + ConfigUtils.OVERTURE_DIRECT.formatLocal(Locale.ENGLISH, + profile.localPort + 53, // Local Port + "119.29.29.29", // Primary DNS 1 + "tcp", // DNS type of Primary DNS 1 + "114.114.114.114", // Primary DNS 2 + "tcp", // DNS type of Primary DNS 2 + profile.remoteDns, // Alternative DNS + "127.0.0.1:" + profile.localPort, // Local SOCKS5 Proxy + externalIp) // External IP case Acl.CHINALIST => - ConfigUtils.PDNSD_DIRECT.formatLocal(Locale.ENGLISH, "", getCacheDir.getAbsolutePath, - "127.0.0.1", profile.localPort + 53, "8.8.8.8, 8.8.4.4, 208.67.222.222", - "", reject, profile.localPort + 63, reject) + ConfigUtils.OVERTURE_LOCAL.formatLocal(Locale.ENGLISH, + profile.localPort + 53, // Local Port + "119.29.29.29", // Primary DNS + "127.0.0.1:" + profile.localPort, // Local SOCKS5 Proxy + externalIp, // External IP + "114.114.114.114", // Alternative DNS + "127.0.0.1:" + profile.localPort, // Local SOCKS5 Proxy + externalIp) // External IP case _ => - ConfigUtils.PDNSD_LOCAL.formatLocal(Locale.ENGLISH, "", getCacheDir.getAbsolutePath, - "127.0.0.1", profile.localPort + 53, profile.localPort + 63, reject) + ConfigUtils.OVERTURE_LOCAL.formatLocal(Locale.ENGLISH, + profile.localPort + 53, // Local Port + profile.remoteDns, // Primary DNS + "127.0.0.1:" + profile.localPort, // Local SOCKS5 Proxy + externalIp, // External IP + "208.67.222.222", // Alternative DNS + "127.0.0.1:" + profile.localPort, // Local SOCKS5 Proxy + externalIp) // External IP }) - pdnsdProcess = new GuardedProcess(getApplicationInfo.nativeLibraryDir + "/libpdnsd.so", "-c", "pdnsd-nat.conf") + overtureProcess = new GuardedProcess(getApplicationInfo.nativeLibraryDir + "/liboverture.so", "-c", "overture-nat.conf") .start() } @@ -111,11 +128,13 @@ class ShadowsocksNatService extends BaseService { /** Called when the activity is first created. */ def handleConnection() { - startDNSTunnel() startRedsocksDaemon() startShadowsocksDaemon() - if (!profile.udpdns) startDnsDaemon() + if (!profile.udpdns) + startDnsDaemon() + else + startDNSTunnel() setupIptables() @@ -143,9 +162,9 @@ class ShadowsocksNatService extends BaseService { redsocksProcess.destroy() redsocksProcess = null } - if (pdnsdProcess != null) { - pdnsdProcess.destroy() - pdnsdProcess = null + if (overtureProcess != null) { + overtureProcess.destroy() + overtureProcess = null } su.addCommand("iptables -t nat -F OUTPUT") diff --git a/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala b/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala index 220d8333..5120d189 100644 --- a/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala +++ b/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala @@ -45,8 +45,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { private var notification: ShadowsocksNotification = _ var sslocalProcess: GuardedProcess = _ - var sstunnelProcess: GuardedProcess = _ - var pdnsdProcess: GuardedProcess = _ + var overtureProcess: GuardedProcess = _ var tun2socksProcess: GuardedProcess = _ override def onBind(intent: Intent): IBinder = { @@ -94,17 +93,13 @@ class ShadowsocksVpnService extends VpnService with BaseService { sslocalProcess.destroy() sslocalProcess = null } - if (sstunnelProcess != null) { - sstunnelProcess.destroy() - sstunnelProcess = null - } if (tun2socksProcess != null) { tun2socksProcess.destroy() tun2socksProcess = null } - if (pdnsdProcess != null) { - pdnsdProcess.destroy() - pdnsdProcess = null + if (overtureProcess != null) { + overtureProcess.destroy() + overtureProcess = null } } @@ -149,15 +144,14 @@ class ShadowsocksVpnService extends VpnService with BaseService { /** Called when the activity is first created. */ def handleConnection() { - val fd = startVpn() - if (!sendFd(fd)) throw new Exception("sendFd failed") - startShadowsocksDaemon() if (!profile.udpdns) { startDnsDaemon() - startDnsTunnel() } + + val fd = startVpn() + if (!sendFd(fd)) throw new Exception("sendFd failed") } override protected def buildPluginCommandLine(): ArrayBuffer[String] = super.buildPluginCommandLine() += "-V" @@ -182,33 +176,41 @@ class ShadowsocksVpnService extends VpnService with BaseService { sslocalProcess = new GuardedProcess(cmd: _*).start() } - def startDnsTunnel(): Unit = - sstunnelProcess = new GuardedProcess(getApplicationInfo.nativeLibraryDir + "/libss-tunnel.so", - "-V", - "-t", "10", - "-b", "127.0.0.1", - "-l", (profile.localPort + 63).toString, - "-L" , profile.remoteDns.trim + ":53", - "-c", buildShadowsocksConfig("ss-tunnel-vpn.conf")).start() - def startDnsDaemon() { - val reject = if (profile.ipv6) "224.0.0.0/3" else "224.0.0.0/3, ::/0" - IOUtils.writeString(new File(getFilesDir, "pdnsd-vpn.conf"), profile.route match { + val externalIp = getExternalIp() + IOUtils.writeString(new File(getFilesDir, "overture-vpn.conf"), profile.route match { case Acl.BYPASS_CHN | Acl.BYPASS_LAN_CHN | Acl.GFWLIST | Acl.CUSTOM_RULES => - ConfigUtils.PDNSD_DIRECT.formatLocal(Locale.ENGLISH, "protect = \"protect_path\";", getCacheDir.getAbsolutePath, - "0.0.0.0", profile.localPort + 53, "114.114.114.114, 223.5.5.5, 1.2.4.8", - getBlackList, reject, profile.localPort + 63, reject) + ConfigUtils.OVERTURE_DIRECT.formatLocal(Locale.ENGLISH, + profile.localPort + 53, // Local Port + "119.29.29.29", // Primary DNS 1 + "udp", // DNS type of Primary DNS 1 + "114.114.114.114", // Primary DNS 2 + "udp", // DNS type of Primary DNS 2 + "208.67.222.222", // Alternative DNS + "127.0.0.1:" + profile.localPort, // Local SOCKS5 Proxy + externalIp) // External IP case Acl.CHINALIST => - ConfigUtils.PDNSD_DIRECT.formatLocal(Locale.ENGLISH, "protect = \"protect_path\";", getCacheDir.getAbsolutePath, - "0.0.0.0", profile.localPort + 53, "8.8.8.8, 8.8.4.4, 208.67.222.222", - "", reject, profile.localPort + 63, reject) + ConfigUtils.OVERTURE_LOCAL.formatLocal(Locale.ENGLISH, + profile.localPort + 53, // Local Port + "119.29.29.29", // Primary DNS + "127.0.0.1:" + profile.localPort, // Local SOCKS5 Proxy + externalIp, // External IP + "114.114.114.114", // Alternative DNS + "127.0.0.1:" + profile.localPort, // Local SOCKS5 Proxy + externalIp) // External IP case _ => - ConfigUtils.PDNSD_LOCAL.formatLocal(Locale.ENGLISH, "protect = \"protect_path\";", getCacheDir.getAbsolutePath, - "0.0.0.0", profile.localPort + 53, profile.localPort + 63, reject) + ConfigUtils.OVERTURE_LOCAL.formatLocal(Locale.ENGLISH, + profile.localPort + 53, // Local Port + profile.remoteDns, // Primary DNS + "127.0.0.1:" + profile.localPort, // Local SOCKS5 Proxy + externalIp, // External IP + "208.67.222.222", // Alternative DNS + "127.0.0.1:" + profile.localPort, // Local SOCKS5 Proxy + externalIp) // External IP }) - val cmd = Array(getApplicationInfo.nativeLibraryDir + "/libpdnsd.so", "-c", "pdnsd-vpn.conf") + val cmd = Array(getApplicationInfo.nativeLibraryDir + "/liboverture.so", "-c", "overture-vpn.conf", "-V") - pdnsdProcess = new GuardedProcess(cmd: _*).start() + overtureProcess = new GuardedProcess(cmd: _*).start() } def startVpn(): Int = { diff --git a/mobile/src/main/scala/com/github/shadowsocks/utils/Constants.scala b/mobile/src/main/scala/com/github/shadowsocks/utils/Constants.scala index d1d9fe21..bdafa8c9 100644 --- a/mobile/src/main/scala/com/github/shadowsocks/utils/Constants.scala +++ b/mobile/src/main/scala/com/github/shadowsocks/utils/Constants.scala @@ -46,84 +46,95 @@ object ConfigUtils { " type = socks5;\n" + "}\n" - val PDNSD_LOCAL = + val OVERTURE_DIRECT = """ - |global { - | perm_cache = 2048; - | %s - | cache_dir = "%s"; - | server_ip = %s; - | server_port = %d; - | query_method = tcp_only; - | min_ttl = 15m; - | max_ttl = 1w; - | timeout = 10; - | daemon = off; - |} - | - |server { - | label = "local"; - | ip = 127.0.0.1; - | port = %d; - | reject = %s; - | reject_policy = negate; - | reject_recursively = on; - |} - | - |rr { - | name=localhost; - | reverse=on; - | a=127.0.0.1; - | owner=localhost; - | soa=localhost,root.localhost,42,86400,900,86400,86400; - |} + |{ + | "BindAddress": ":%d", + | "PrimaryDNS": [ + | { + | "Name": "Primary-1", + | "Address": "%s:53", + | "Protocol": "%s", + | "Socks5Address": "", + | "Timeout": 6, + | "EDNSClientSubnet": { + | "Policy": "disable", + | "ExternalIP": "" + | } + | }, + | { + | "Name": "Primary-2", + | "Address": "%s:53", + | "Protocol": "%s", + | "Socks5Address": "", + | "Timeout": 6, + | "EDNSClientSubnet": { + | "Policy": "disable", + | "ExternalIP": "" + | } + | } + | ], + | "AlternativeDNS":[ + | { + | "Name": "Alternative", + | "Address": "%s:53", + | "Protocol": "tcp", + | "Socks5Address": "%s", + | "Timeout": 6, + | "EDNSClientSubnet":{ + | "Policy": "auto", + | "ExternalIP": "%s" + | } + | } + | ], + | "RedirectIPv6Record": true, + | "IPNetworkFile": "china_ip_list.txt", + | "DomainFile": "gfwlist.txt", + | "DomainBase64Decode": true, + | "HostsFile": "hosts", + | "MinimumTTL": 3600, + | "CacheSize" : 4096 + |} """.stripMargin - val PDNSD_DIRECT = + val OVERTURE_LOCAL = """ - |global { - | perm_cache = 2048; - | %s - | cache_dir = "%s"; - | server_ip = %s; - | server_port = %d; - | query_method = udp_only; - | min_ttl = 15m; - | max_ttl = 1w; - | timeout = 10; - | daemon = off; - | par_queries = 4; - |} - | - |server { - | label = "remote-servers"; - | ip = %s; - | timeout = 3; - | query_method = udp_only; - | %s - | policy = included; - | reject = %s; - | reject_policy = fail; - | reject_recursively = on; - |} - | - |server { - | label = "local-server"; - | ip = 127.0.0.1; - | query_method = tcp_only; - | port = %d; - | reject = %s; - | reject_policy = negate; - | reject_recursively = on; - |} - | - |rr { - | name=localhost; - | reverse=on; - | a=127.0.0.1; - | owner=localhost; - | soa=localhost,root.localhost,42,86400,900,86400,86400; - |} + |{ + | "BindAddress": ":%d", + | "PrimaryDNS": [ + | { + | "Name": "Primary", + | "Address": "%s:53", + | "Protocol": "tcp", + | "Socks5Address": "%s", + | "Timeout": 6, + | "EDNSClientSubnet": { + | "Policy": "auto", + | "ExternalIP": "%s" + | } + | } + | ], + | "AlternativeDNS":[ + | { + | "Name": "Alternative", + | "Address": "%s:53", + | "Protocol": "tcp", + | "Socks5Address": "%s", + | "Timeout": 6, + | "EDNSClientSubnet":{ + | "Policy": "auto", + | "ExternalIP": "%s" + | } + | } + | ], + | "RedirectIPv6Record": true, + | "IPNetworkFile": "", + | "DomainFile": "", + | "DomainBase64Decode": true, + | "HostsFile": "hosts", + | "MinimumTTL": 3600, + | "CacheSize" : 4096 + |} """.stripMargin }