Commit 45fea940 authored by Mygod's avatar Mygod

Use hosts for resolving remote

Fix #2256.
parent 44f37d14
...@@ -36,12 +36,10 @@ import com.github.shadowsocks.aidl.IShadowsocksService ...@@ -36,12 +36,10 @@ import com.github.shadowsocks.aidl.IShadowsocksService
import com.github.shadowsocks.aidl.IShadowsocksServiceCallback import com.github.shadowsocks.aidl.IShadowsocksServiceCallback
import com.github.shadowsocks.aidl.TrafficStats import com.github.shadowsocks.aidl.TrafficStats
import com.github.shadowsocks.core.R import com.github.shadowsocks.core.R
import com.github.shadowsocks.net.HostsFile
import com.github.shadowsocks.plugin.PluginManager import com.github.shadowsocks.plugin.PluginManager
import com.github.shadowsocks.preference.DataStore import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.Action import com.github.shadowsocks.utils.*
import com.github.shadowsocks.utils.broadcastReceiver
import com.github.shadowsocks.utils.printLog
import com.github.shadowsocks.utils.readableMessage
import com.google.firebase.analytics.FirebaseAnalytics import com.google.firebase.analytics.FirebaseAnalytics
import kotlinx.coroutines.* import kotlinx.coroutines.*
import java.io.File import java.io.File
...@@ -232,7 +230,7 @@ object BaseService { ...@@ -232,7 +230,7 @@ object BaseService {
fun buildAdditionalArguments(cmd: ArrayList<String>): ArrayList<String> = cmd fun buildAdditionalArguments(cmd: ArrayList<String>): ArrayList<String> = cmd
suspend fun startProcesses() { suspend fun startProcesses(hosts: HostsFile) {
val configRoot = (if (Build.VERSION.SDK_INT < 24 || app.getSystemService<UserManager>() val configRoot = (if (Build.VERSION.SDK_INT < 24 || app.getSystemService<UserManager>()
?.isUserUnlocked != false) app else Core.deviceStorage).noBackupFilesDir ?.isUserUnlocked != false) app else Core.deviceStorage).noBackupFilesDir
val udpFallback = data.udpFallback val udpFallback = data.udpFallback
...@@ -343,14 +341,15 @@ object BaseService { ...@@ -343,14 +341,15 @@ object BaseService {
try { try {
Executable.killAll() // clean up old processes Executable.killAll() // clean up old processes
preInit() preInit()
proxy.init(this@Interface) val hosts = HostsFile(DataStore.publicStore.getString(Key.hosts) ?: "")
data.udpFallback?.init(this@Interface) proxy.init(this@Interface, hosts)
data.udpFallback?.init(this@Interface, hosts)
data.processes = GuardedProcessPool { data.processes = GuardedProcessPool {
printLog(it) printLog(it)
stopRunner(false, it.readableMessage) stopRunner(false, it.readableMessage)
} }
startProcesses() startProcesses(hosts)
proxy.scheduleUpdate() proxy.scheduleUpdate()
data.udpFallback?.scheduleUpdate() data.udpFallback?.scheduleUpdate()
......
...@@ -28,7 +28,6 @@ import com.github.shadowsocks.net.LocalDnsServer ...@@ -28,7 +28,6 @@ import com.github.shadowsocks.net.LocalDnsServer
import com.github.shadowsocks.net.Socks5Endpoint import com.github.shadowsocks.net.Socks5Endpoint
import com.github.shadowsocks.net.Subnet import com.github.shadowsocks.net.Subnet
import com.github.shadowsocks.preference.DataStore import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.Key
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import java.net.InetSocketAddress import java.net.InetSocketAddress
import java.net.URI import java.net.URI
...@@ -45,14 +44,14 @@ object LocalDnsService { ...@@ -45,14 +44,14 @@ object LocalDnsService {
private val servers = WeakHashMap<Interface, LocalDnsServer>() private val servers = WeakHashMap<Interface, LocalDnsServer>()
interface Interface : BaseService.Interface { interface Interface : BaseService.Interface {
override suspend fun startProcesses() { override suspend fun startProcesses(hosts: HostsFile) {
super.startProcesses() super.startProcesses(hosts)
val profile = data.proxy!!.profile val profile = data.proxy!!.profile
val dns = URI("dns://${profile.remoteDns}") val dns = URI("dns://${profile.remoteDns}")
LocalDnsServer(this::resolver, LocalDnsServer(this::resolver,
Socks5Endpoint(dns.host, if (dns.port < 0) 53 else dns.port), Socks5Endpoint(dns.host, if (dns.port < 0) 53 else dns.port),
DataStore.proxyAddress, DataStore.proxyAddress,
HostsFile(DataStore.publicStore.getString(Key.hosts) ?: "")).apply { hosts).apply {
tcp = !profile.udpdns tcp = !profile.udpdns
when (profile.route) { when (profile.route) {
Acl.BYPASS_CHN, Acl.BYPASS_LAN_CHN, Acl.GFWLIST, Acl.CUSTOM_RULES -> { Acl.BYPASS_CHN, Acl.BYPASS_LAN_CHN, Acl.GFWLIST, Acl.CUSTOM_RULES -> {
......
...@@ -26,6 +26,7 @@ import com.github.shadowsocks.Core ...@@ -26,6 +26,7 @@ import com.github.shadowsocks.Core
import com.github.shadowsocks.acl.Acl import com.github.shadowsocks.acl.Acl
import com.github.shadowsocks.acl.AclSyncer import com.github.shadowsocks.acl.AclSyncer
import com.github.shadowsocks.database.Profile import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.net.HostsFile
import com.github.shadowsocks.plugin.PluginConfiguration import com.github.shadowsocks.plugin.PluginConfiguration
import com.github.shadowsocks.plugin.PluginManager import com.github.shadowsocks.plugin.PluginManager
import com.github.shadowsocks.preference.DataStore import com.github.shadowsocks.preference.DataStore
...@@ -51,7 +52,7 @@ class ProxyInstance(val profile: Profile, private val route: String = profile.ro ...@@ -51,7 +52,7 @@ class ProxyInstance(val profile: Profile, private val route: String = profile.ro
val pluginPath by lazy { PluginManager.init(plugin) } val pluginPath by lazy { PluginManager.init(plugin) }
private var scheduleConfigUpdate = false private var scheduleConfigUpdate = false
suspend fun init(service: BaseService.Interface) { suspend fun init(service: BaseService.Interface, hosts: HostsFile) {
if (profile.host == "198.199.101.152") { if (profile.host == "198.199.101.152") {
scheduleConfigUpdate = true scheduleConfigUpdate = true
val mdg = MessageDigest.getInstance("SHA-1") val mdg = MessageDigest.getInstance("SHA-1")
...@@ -85,7 +86,8 @@ class ProxyInstance(val profile: Profile, private val route: String = profile.ro ...@@ -85,7 +86,8 @@ class ProxyInstance(val profile: Profile, private val route: String = profile.ro
// it's hard to resolve DNS on a specific interface so we'll do it here // it's hard to resolve DNS on a specific interface so we'll do it here
if (profile.host.parseNumericAddress() == null) { if (profile.host.parseNumericAddress() == null) {
profile.host = service.resolver(profile.host).firstOrNull()?.hostAddress ?: throw UnknownHostException() profile.host = (hosts.resolve(profile.host).firstOrNull() ?: service.resolver(profile.host).firstOrNull())
?.hostAddress ?: throw UnknownHostException()
} }
} }
......
...@@ -23,6 +23,7 @@ package com.github.shadowsocks.bg ...@@ -23,6 +23,7 @@ package com.github.shadowsocks.bg
import android.app.Service import android.app.Service
import android.content.Intent import android.content.Intent
import com.github.shadowsocks.Core import com.github.shadowsocks.Core
import com.github.shadowsocks.net.HostsFile
import com.github.shadowsocks.preference.DataStore import com.github.shadowsocks.preference.DataStore
import java.io.File import java.io.File
...@@ -56,9 +57,9 @@ redsocks { ...@@ -56,9 +57,9 @@ redsocks {
File(applicationInfo.nativeLibraryDir, Executable.REDSOCKS).absolutePath, "-c", "redsocks.conf")) File(applicationInfo.nativeLibraryDir, Executable.REDSOCKS).absolutePath, "-c", "redsocks.conf"))
} }
override suspend fun startProcesses() { override suspend fun startProcesses(hosts: HostsFile) {
startRedsocksDaemon() startRedsocksDaemon()
super.startProcesses() super.startProcesses(hosts)
} }
override fun onDestroy() { override fun onDestroy() {
......
...@@ -36,6 +36,7 @@ import com.github.shadowsocks.acl.Acl ...@@ -36,6 +36,7 @@ import com.github.shadowsocks.acl.Acl
import com.github.shadowsocks.core.R import com.github.shadowsocks.core.R
import com.github.shadowsocks.net.ConcurrentLocalSocketListener import com.github.shadowsocks.net.ConcurrentLocalSocketListener
import com.github.shadowsocks.net.DefaultNetworkListener import com.github.shadowsocks.net.DefaultNetworkListener
import com.github.shadowsocks.net.HostsFile
import com.github.shadowsocks.net.Subnet import com.github.shadowsocks.net.Subnet
import com.github.shadowsocks.preference.DataStore import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.Key import com.github.shadowsocks.utils.Key
...@@ -142,9 +143,9 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface { ...@@ -142,9 +143,9 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface {
override suspend fun resolver(host: String) = DnsResolverCompat.resolve(DefaultNetworkListener.get(), host) override suspend fun resolver(host: String) = DnsResolverCompat.resolve(DefaultNetworkListener.get(), host)
override suspend fun openConnection(url: URL) = DefaultNetworkListener.get().openConnection(url) override suspend fun openConnection(url: URL) = DefaultNetworkListener.get().openConnection(url)
override suspend fun startProcesses() { override suspend fun startProcesses(hosts: HostsFile) {
worker = ProtectWorker().apply { start() } worker = ProtectWorker().apply { start() }
super.startProcesses() super.startProcesses(hosts)
sendFd(startVpn()) sendFd(startVpn())
} }
......
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