Commit ea738d17 authored by Mygod's avatar Mygod

Refine #1217

parent 915b1a74
...@@ -315,13 +315,6 @@ trait BaseService extends Service { ...@@ -315,13 +315,6 @@ trait BaseService extends Service {
file file
} }
private final def buildRemoteDns(remoteDns: String): String = {
val addr = InetAddress.getByName(remoteDns)
if (addr.isInstanceOf[Inet6Address])
return "[" + remoteDns + "]"
remoteDns
}
protected final def buildOvertureConfig(file: String): String = { protected final def buildOvertureConfig(file: String): String = {
val config = new JSONObject() val config = new JSONObject()
.put("BindAddress", ":" + (profile.localPort + 53)) .put("BindAddress", ":" + (profile.localPort + 53))
...@@ -333,7 +326,10 @@ trait BaseService extends Service { ...@@ -333,7 +326,10 @@ trait BaseService extends Service {
def makeDns(name: String, address: String, edns: Boolean = true) = { def makeDns(name: String, address: String, edns: Boolean = true) = {
val dns = new JSONObject() val dns = new JSONObject()
.put("Name", name) .put("Name", name)
.put("Address", address + ":53") .put("Address", (Utils.parseNumericAddress(address) match {
case _: Inet6Address => '[' + address + ']'
case _ => address
}) + ":53")
.put("Timeout", 6) .put("Timeout", 6)
.put("EDNSClientSubnet", new JSONObject().put("Policy", "disable")) .put("EDNSClientSubnet", new JSONObject().put("Policy", "disable"))
if (edns) dns if (edns) dns
...@@ -342,29 +338,23 @@ trait BaseService extends Service { ...@@ -342,29 +338,23 @@ trait BaseService extends Service {
else dns.put("Protocol", "udp") else dns.put("Protocol", "udp")
dns dns
} }
val remoteDns = new JSONArray(profile.remoteDns.split(",").zipWithIndex.map {
case (dns, i) => makeDns("UserDef-" + i, dns.trim)
})
profile.route match { profile.route match {
case Acl.BYPASS_CHN | Acl.BYPASS_LAN_CHN | Acl.GFWLIST | Acl.CUSTOM_RULES => config case Acl.BYPASS_CHN | Acl.BYPASS_LAN_CHN | Acl.GFWLIST | Acl.CUSTOM_RULES => config
.put("PrimaryDNS", new JSONArray(Array( .put("PrimaryDNS", new JSONArray(Array(
makeDns("Primary-1", "119.29.29.29", edns = false), makeDns("Primary-1", "119.29.29.29", edns = false),
makeDns("Primary-2", "114.114.114.114", edns = false) makeDns("Primary-2", "114.114.114.114", edns = false)
))) )))
.put("AlternativeDNS", new JSONArray( .put("AlternativeDNS", remoteDns)
for (remoteDns <- profile.remoteDns.split(","))
yield makeDns(remoteDns.trim, buildRemoteDns(remoteDns.trim)
)))
.put("IPNetworkFile", "china_ip_list.txt") .put("IPNetworkFile", "china_ip_list.txt")
.put("DomainFile", "gfwlist.txt") .put("DomainFile", "gfwlist.txt")
case Acl.CHINALIST => config case Acl.CHINALIST => config
.put("PrimaryDNS", new JSONArray().put(makeDns("Primary", "119.29.29.29"))) .put("PrimaryDNS", new JSONArray().put(makeDns("Primary", "119.29.29.29")))
.put("AlternativeDNS", new JSONArray( .put("AlternativeDNS", remoteDns)
for (remoteDns <- profile.remoteDns.split(","))
yield makeDns(remoteDns.trim, buildRemoteDns(remoteDns.trim)
)))
case _ => config case _ => config
.put("PrimaryDNS", new JSONArray( .put("PrimaryDNS", new JSONArray(remoteDns))
for (remoteDns <- profile.remoteDns.split(","))
yield makeDns(remoteDns.trim, buildRemoteDns(remoteDns.trim)
)))
// no need to setup AlternativeDNS in Acl.ALL/BYPASS_LAN mode // no need to setup AlternativeDNS in Acl.ALL/BYPASS_LAN mode
.put("OnlyPrimaryDNS", true) .put("OnlyPrimaryDNS", true)
} }
......
...@@ -74,7 +74,7 @@ class ShadowsocksNatService extends BaseService { ...@@ -74,7 +74,7 @@ class ShadowsocksNatService extends BaseService {
"-t", "10", "-t", "10",
"-b", "127.0.0.1", "-b", "127.0.0.1",
"-l", (profile.localPort + 63).toString, "-l", (profile.localPort + 63).toString,
"-L", profile.remoteDns.split(",")(0).trim + ":53", "-L", profile.remoteDns.split(",").head.trim + ":53",
"-c", buildShadowsocksConfig("ss-tunnel-nat.conf")) "-c", buildShadowsocksConfig("ss-tunnel-nat.conf"))
if (profile.udpdns) cmd.append("-u") if (profile.udpdns) cmd.append("-u")
......
...@@ -22,8 +22,6 @@ package com.github.shadowsocks ...@@ -22,8 +22,6 @@ package com.github.shadowsocks
import java.io.File import java.io.File
import java.util.Locale import java.util.Locale
import java.net.InetAddress
import java.net.Inet6Address
import android.content._ import android.content._
import android.content.pm.PackageManager.NameNotFoundException import android.content.pm.PackageManager.NameNotFoundException
...@@ -223,13 +221,8 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -223,13 +221,8 @@ class ShadowsocksVpnService extends VpnService with BaseService {
val subnet = Subnet.fromString(cidr) val subnet = Subnet.fromString(cidr)
builder.addRoute(subnet.address.getHostAddress, subnet.prefixSize) builder.addRoute(subnet.address.getHostAddress, subnet.prefixSize)
}) })
profile.remoteDns.split(",").foreach(remoteDns => { profile.remoteDns.split(",").map(dns => Utils.parseNumericAddress(dns.trim)).foreach(dns =>
val addr = InetAddress.getByName(remoteDns.trim) builder.addRoute(dns, dns.getAddress.length << 3))
if (addr.isInstanceOf[Inet6Address])
builder.addRoute(addr, 128)
else if (addr.isInstanceOf[InetAddress])
builder.addRoute(addr, 32)
})
} }
conn = builder.establish() conn = builder.establish()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment