Skip to content

Commit

Permalink
Refine DNS settings
Browse files Browse the repository at this point in the history
  • Loading branch information
madeye committed Dec 2, 2014
1 parent 5547b42 commit 01306d5
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ShadowsocksNatService extends Service with BaseService {
private val mSetForegroundSignature = Array[Class[_]](classOf[Boolean])

var closeReceiver: BroadcastReceiver = null
var conReceiver: BroadcastReceiver = null
var connReceiver: BroadcastReceiver = null
var notificationManager: NotificationManager = null
var config: Config = null
var apps: Array[ProxiedApp] = null
Expand Down Expand Up @@ -110,23 +110,41 @@ class ShadowsocksNatService extends Service with BaseService {
setDnsForAllNetwork("114.114.114.114 8.8.8.8 208.67.222.222")
}

def flushDns() {
if (Utils.isLollipopOrAbove) {
val manager = getSystemService(Context.CONNECTIVITY_SERVICE).asInstanceOf[ConnectivityManager]
val networks = manager.getAllNetworks
val cmdBuf = new ArrayBuffer[String]()
networks.foreach(network => {
val networkInfo = manager.getNetworkInfo(network)
if (networkInfo.isAvailable) {
val netId = network.getClass.getDeclaredField("netId").get(network).asInstanceOf[Int]
cmdBuf.append("ndc resolver flushnet %d".format(netId))
}
})
Console.runRootCommand(cmdBuf.toArray)
} else {
Console.runRootCommand(Array("ndc resolver flushdefaultif", "ndc resolver flushif wlan0"))
}
}


def destroyConnectionReceiver() {
if (conReceiver != null) {
unregisterReceiver(conReceiver)
conReceiver = null
if (connReceiver != null) {
unregisterReceiver(connReceiver)
connReceiver = null
}
resetDns()
}

def initConnectionReceiver() {
setupDns()
val filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
conReceiver = new BroadcastReceiver {
connReceiver = new BroadcastReceiver {
override def onReceive(context: Context, intent: Intent) = {
setupDns()
}
}
registerReceiver(conReceiver, filter)
registerReceiver(connReceiver, filter)
}

def startShadowsocksDaemon() {
Expand Down Expand Up @@ -266,7 +284,6 @@ class ShadowsocksNatService extends Service with BaseService {
startRedsocksDaemon()
startShadowsocksDaemon()
setupIptables()
flushDNS()

true
}
Expand Down Expand Up @@ -364,10 +381,6 @@ class ShadowsocksNatService extends Service with BaseService {
Console.runRootCommand(Utils.getIptables + " -t nat -F OUTPUT")
}

def flushDNS() {
Console.runRootCommand(Array("ndc resolver flushdefaultif", "ndc resolver flushif wlan0"))
}

def setupIptables() = {
val init_sb = new ArrayBuffer[String]
val http_sb = new ArrayBuffer[String]
Expand Down Expand Up @@ -454,6 +467,9 @@ class ShadowsocksNatService extends Service with BaseService {

config = c

// register connection receiver
if (Utils.isLollipopOrAbove) initConnectionReceiver()

// register close receiver
val filter = new IntentFilter()
filter.addAction(Intent.ACTION_SHUTDOWN)
Expand All @@ -466,9 +482,6 @@ class ShadowsocksNatService extends Service with BaseService {
}
registerReceiver(closeReceiver, filter)

// register connection receiver
if (Utils.isLollipopOrAbove) initConnectionReceiver()

// send event
application.tracker.send(new HitBuilders.EventBuilder()
.setCategory(TAG)
Expand All @@ -486,7 +499,6 @@ class ShadowsocksNatService extends Service with BaseService {
config = ConfigUtils.getPublicConfig(getBaseContext, holder.getContainer, config)
} catch {
case ex: Exception =>
Log.e(TAG, ex.getMessage)
changeState(State.STOPPED, getString(R.string.service_failed))
stopRunner()
config = null
Expand All @@ -495,8 +507,13 @@ class ShadowsocksNatService extends Service with BaseService {

if (config != null) {

// Clean up
killProcesses()

// Set DNS
if (Utils.isLollipopOrAbove) setupDns()
flushDns()

var resolved: Boolean = false
if (!InetAddressUtils.isIPv4Address(config.proxy) &&
!InetAddressUtils.isIPv6Address(config.proxy)) {
Expand Down

0 comments on commit 01306d5

Please sign in to comment.