Commit abf050a4 authored by Max Lv's avatar Max Lv

refine the service shutdown

parent b4937785
...@@ -51,13 +51,11 @@ import java.io._ ...@@ -51,13 +51,11 @@ import java.io._
import android.net.VpnService import android.net.VpnService
import org.apache.http.conn.util.InetAddressUtils import org.apache.http.conn.util.InetAddressUtils
import android.os.Message import android.os.Message
import scala.Some
import scala.concurrent.ops._ import scala.concurrent.ops._
import org.apache.commons.net.util.SubnetUtils import org.apache.commons.net.util.SubnetUtils
import java.net.InetAddress import java.net.InetAddress
import com.github.shadowsocks.utils._ import com.github.shadowsocks.utils._
import scala.Some import scala.Some
import com.github.shadowsocks.ProxiedApp
object ShadowVpnService { object ShadowVpnService {
def isServiceStarted(context: Context): Boolean = { def isServiceStarted(context: Context): Boolean = {
...@@ -68,7 +66,6 @@ object ShadowVpnService { ...@@ -68,7 +66,6 @@ object ShadowVpnService {
class ShadowVpnService extends VpnService { class ShadowVpnService extends VpnService {
val TAG = "ShadowVpnService" val TAG = "ShadowVpnService"
val BASE = "/data/data/com.github.shadowsocks/"
val MSG_CONNECT_FINISH = 1 val MSG_CONNECT_FINISH = 1
val MSG_CONNECT_SUCCESS = 2 val MSG_CONNECT_SUCCESS = 2
...@@ -114,7 +111,6 @@ class ShadowVpnService extends VpnService { ...@@ -114,7 +111,6 @@ class ShadowVpnService extends VpnService {
case MSG_VPN_ERROR => case MSG_VPN_ERROR =>
if (msg.obj != null) changeState(State.STOPPED, msg.obj.asInstanceOf[String]) if (msg.obj != null) changeState(State.STOPPED, msg.obj.asInstanceOf[String])
case MSG_STOP_SELF => case MSG_STOP_SELF =>
destroy()
stopSelf() stopSelf()
case _ => case _ =>
} }
...@@ -124,7 +120,7 @@ class ShadowVpnService extends VpnService { ...@@ -124,7 +120,7 @@ class ShadowVpnService extends VpnService {
def getPid(name: String): Int = { def getPid(name: String): Int = {
try { try {
val reader: BufferedReader = new BufferedReader(new FileReader(BASE + name + ".pid")) val reader: BufferedReader = new BufferedReader(new FileReader(Path.BASE + name + ".pid"))
val line = reader.readLine val line = reader.readLine
return Integer.valueOf(line) return Integer.valueOf(line)
} catch { } catch {
...@@ -142,18 +138,18 @@ class ShadowVpnService extends VpnService { ...@@ -142,18 +138,18 @@ class ShadowVpnService extends VpnService {
} }
def startShadowsocksDaemon() { def startShadowsocksDaemon() {
val cmd: String = (BASE + val cmd: String = (Path.BASE +
"shadowsocks -b 127.0.0.1 -s \"%s\" -p \"%d\" -l \"%d\" -k \"%s\" -m \"%s\" -f " + "shadowsocks -b 127.0.0.1 -s \"%s\" -p \"%d\" -l \"%d\" -k \"%s\" -m \"%s\" -f " +
BASE + "shadowsocks.pid") Path.BASE + "shadowsocks.pid")
.format(config.proxy, config.remotePort, config.localPort, config.sitekey, config.encMethod) .format(config.proxy, config.remotePort, config.localPort, config.sitekey, config.encMethod)
if (BuildConfig.DEBUG) Log.d(TAG, cmd) if (BuildConfig.DEBUG) Log.d(TAG, cmd)
System.exec(cmd) System.exec(cmd)
} }
def startDnsDaemon() { def startDnsDaemon() {
val cmd: String = BASE + "pdnsd -c " + BASE + "pdnsd.conf" val cmd: String = Path.BASE + "pdnsd -c " + Path.BASE + "pdnsd.conf"
val conf: String = Config.PDNSD.format("0.0.0.0") val conf: String = Config.PDNSD.format("0.0.0.0")
Config.printToFile(new File(BASE + "pdnsd.conf"))(p => { Config.printToFile(new File(Path.BASE + "pdnsd.conf"))(p => {
p.println(conf) p.println(conf)
}) })
Utils.runCommand(cmd) Utils.runCommand(cmd)
...@@ -329,7 +325,7 @@ class ShadowVpnService extends VpnService { ...@@ -329,7 +325,7 @@ class ShadowVpnService extends VpnService {
val fd = conn.getFd val fd = conn.getFd
val cmd = (BASE + val cmd = (Path.BASE +
"tun2socks --netif-ipaddr %s " "tun2socks --netif-ipaddr %s "
+ "--dnsgw %s:8153 " + "--dnsgw %s:8153 "
+ "--netif-netmask 255.255.255.0 " + "--netif-netmask 255.255.255.0 "
...@@ -339,7 +335,7 @@ class ShadowVpnService extends VpnService { ...@@ -339,7 +335,7 @@ class ShadowVpnService extends VpnService {
+ "--loglevel 3 " + "--loglevel 3 "
+ "--pid %stun2socks.pid") + "--pid %stun2socks.pid")
.format(PRIVATE_VLAN.format("2"), PRIVATE_VLAN.format("1"), config.localPort, fd, VPN_MTU, .format(PRIVATE_VLAN.format("2"), PRIVATE_VLAN.format("1"), config.localPort, fd, VPN_MTU,
BASE) Path.BASE)
if (BuildConfig.DEBUG) Log.d(TAG, cmd) if (BuildConfig.DEBUG) Log.d(TAG, cmd)
System.exec(cmd) System.exec(cmd)
} }
...@@ -402,14 +398,13 @@ class ShadowVpnService extends VpnService { ...@@ -402,14 +398,13 @@ class ShadowVpnService extends VpnService {
filter.addAction(Action.CLOSE) filter.addAction(Action.CLOSE)
receiver = new BroadcastReceiver { receiver = new BroadcastReceiver {
def onReceive(p1: Context, p2: Intent) { def onReceive(p1: Context, p2: Intent) {
destroy()
stopSelf() stopSelf()
} }
} }
registerReceiver(receiver, filter) registerReceiver(receiver, filter)
} }
def destroy() { override def onDestroy() {
killProcesses() killProcesses()
changeState(State.STOPPED) changeState(State.STOPPED)
...@@ -425,16 +420,14 @@ class ShadowVpnService extends VpnService { ...@@ -425,16 +420,14 @@ class ShadowVpnService extends VpnService {
unregisterReceiver(receiver) unregisterReceiver(receiver)
receiver = null receiver = null
} }
if (conn != null) { if (conn != null) {
conn.close() conn.close()
conn = null conn = null
} }
notificationManager.cancel(1) notificationManager.cancel(1)
}
/** Called when the activity is closed. */
override def onDestroy() {
destroy()
super.onDestroy() super.onDestroy()
} }
...@@ -464,7 +457,4 @@ class ShadowVpnService extends VpnService { ...@@ -464,7 +457,4 @@ class ShadowVpnService extends VpnService {
Service.START_STICKY Service.START_STICKY
} }
override def onRevoke() {
stopSelf()
}
} }
...@@ -355,7 +355,7 @@ class Shadowsocks ...@@ -355,7 +355,7 @@ class Shadowsocks
} else { } else {
in = assetManager.open(file) in = assetManager.open(file)
} }
out = new FileOutputStream("/data/data/com.github.shadowsocks/" + file) out = new FileOutputStream(Path.BASE + file)
copyFile(in, out) copyFile(in, out)
in.close() in.close()
in = null in = null
......
...@@ -57,13 +57,10 @@ import org.apache.http.conn.util.InetAddressUtils ...@@ -57,13 +57,10 @@ import org.apache.http.conn.util.InetAddressUtils
import scala.collection._ import scala.collection._
import java.util.{TimerTask, Timer} import java.util.{TimerTask, Timer}
import android.net.TrafficStats import android.net.TrafficStats
import android.graphics._
import scala.concurrent.ops._ import scala.concurrent.ops._
import scala.Some
import com.github.shadowsocks.utils._ import com.github.shadowsocks.utils._
import scala.Some import scala.Some
import com.github.shadowsocks.ProxiedApp import android.graphics.Color
import com.github.shadowsocks.TrafficStat
case class TrafficStat(tx: Long, rx: Long, timestamp: Long) case class TrafficStat(tx: Long, rx: Long, timestamp: Long)
...@@ -76,7 +73,6 @@ object ShadowsocksService { ...@@ -76,7 +73,6 @@ object ShadowsocksService {
class ShadowsocksService extends Service { class ShadowsocksService extends Service {
val TAG = "ShadowsocksService" val TAG = "ShadowsocksService"
val BASE = "/data/data/com.github.shadowsocks/"
val CMD_IPTABLES_RETURN = " -t nat -A OUTPUT -p tcp -d 0.0.0.0 -j RETURN\n" val CMD_IPTABLES_RETURN = " -t nat -A OUTPUT -p tcp -d 0.0.0.0 -j RETURN\n"
val CMD_IPTABLES_REDIRECT_ADD_SOCKS = " -t nat -A OUTPUT -p tcp " + "-j REDIRECT --to 8123\n" val CMD_IPTABLES_REDIRECT_ADD_SOCKS = " -t nat -A OUTPUT -p tcp " + "-j REDIRECT --to 8123\n"
...@@ -141,7 +137,7 @@ class ShadowsocksService extends Service { ...@@ -141,7 +137,7 @@ class ShadowsocksService extends Service {
def getPid(name: String): Int = { def getPid(name: String): Int = {
try { try {
val reader: BufferedReader = new BufferedReader(new FileReader(BASE + name + ".pid")) val reader: BufferedReader = new BufferedReader(new FileReader(Path.BASE + name + ".pid"))
val line = reader.readLine val line = reader.readLine
return Integer.valueOf(line) return Integer.valueOf(line)
} catch { } catch {
...@@ -159,18 +155,18 @@ class ShadowsocksService extends Service { ...@@ -159,18 +155,18 @@ class ShadowsocksService extends Service {
} }
def startShadowsocksDaemon() { def startShadowsocksDaemon() {
val cmd: String = (BASE + val cmd: String = (Path.BASE +
"shadowsocks -b 127.0.0.1 -s \"%s\" -p \"%d\" -l \"%d\" -k \"%s\" -m \"%s\" -f " + "shadowsocks -b 127.0.0.1 -s \"%s\" -p \"%d\" -l \"%d\" -k \"%s\" -m \"%s\" -f " +
BASE + "shadowsocks.pid") Path.BASE + "shadowsocks.pid")
.format(config.proxy, config.remotePort, config.localPort, config.sitekey, config.encMethod) .format(config.proxy, config.remotePort, config.localPort, config.sitekey, config.encMethod)
if (BuildConfig.DEBUG) Log.d(TAG, cmd) if (BuildConfig.DEBUG) Log.d(TAG, cmd)
Utils.runCommand(cmd) Utils.runCommand(cmd)
} }
def startDnsDaemon() { def startDnsDaemon() {
val cmd: String = BASE + "pdnsd -c " + BASE + "pdnsd.conf" val cmd: String = Path.BASE + "pdnsd -c " + Path.BASE + "pdnsd.conf"
val conf: String = Config.PDNSD.format("127.0.0.1") val conf: String = Config.PDNSD.format("127.0.0.1")
Config.printToFile(new File(BASE + "pdnsd.conf"))(p => { Config.printToFile(new File(Path.BASE + "pdnsd.conf"))(p => {
p.println(conf) p.println(conf)
}) })
Utils.runCommand(cmd) Utils.runCommand(cmd)
...@@ -275,8 +271,8 @@ class ShadowsocksService extends Service { ...@@ -275,8 +271,8 @@ class ShadowsocksService extends Service {
def startRedsocksDaemon() { def startRedsocksDaemon() {
val conf = Config.REDSOCKS.format(config.localPort) val conf = Config.REDSOCKS.format(config.localPort)
val cmd = "%sredsocks -p %sredsocks.pid -c %sredsocks.conf".format(BASE, BASE, BASE) val cmd = "%sredsocks -p %sredsocks.pid -c %sredsocks.conf".format(Path.BASE, Path.BASE, Path.BASE)
Config.printToFile(new File(BASE + "redsocks.conf"))(p => { Config.printToFile(new File(Path.BASE + "redsocks.conf"))(p => {
p.println(conf) p.println(conf)
}) })
Utils.runRootCommand(cmd) Utils.runRootCommand(cmd)
......
...@@ -2,6 +2,10 @@ package com.github.shadowsocks.utils ...@@ -2,6 +2,10 @@ package com.github.shadowsocks.utils
import android.content.{Intent, SharedPreferences} import android.content.{Intent, SharedPreferences}
object Path {
val BASE = "/data/data/com.github.shadowsocks/"
}
object Key { object Key {
val profileId = "profileId" val profileId = "profileId"
val profileName = "profileName" val profileName = "profileName"
......
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