diff --git a/src/main/scala/com/github/shadowsocks/BaseService.scala b/src/main/scala/com/github/shadowsocks/BaseService.scala index 3709ddf7..fb78c896 100644 --- a/src/main/scala/com/github/shadowsocks/BaseService.scala +++ b/src/main/scala/com/github/shadowsocks/BaseService.scala @@ -112,7 +112,7 @@ trait BaseService extends Service { startService(new Intent(getContext, getClass)) TrafficMonitor.reset() - trafficMonitorThread = new TrafficMonitorThread() + trafficMonitorThread = new TrafficMonitorThread(getApplicationContext) trafficMonitorThread.start() } diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala index 5afb5937..2c0a7225 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala @@ -60,7 +60,6 @@ object ShadowsocksApplication { lazy val tracker = GoogleAnalytics.getInstance(instance).newTracker(R.xml.tracker) lazy val settings = PreferenceManager.getDefaultSharedPreferences(instance) lazy val profileManager = new ProfileManager(settings, dbHelper) - lazy val dataDir = instance.getApplicationInfo().dataDir def isVpnEnabled = !settings.getBoolean(Key.isNAT, false) diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala index 1cd14db3..7cfa55df 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala @@ -168,7 +168,7 @@ class ShadowsocksNatService extends BaseService { case Route.BYPASS_LAN => getResources.getStringArray(R.array.private_route) case Route.BYPASS_CHN => getResources.getStringArray(R.array.chn_route_full) } - ConfigUtils.printToFile(new File(getApplicationInfo().dataDir + "/acl.list"))(p => { + ConfigUtils.printToFile(new File(getApplicationInfo.dataDir + "/acl.list"))(p => { acl.foreach(item => p.println(item)) }) } @@ -176,21 +176,22 @@ class ShadowsocksNatService extends BaseService { val conf = ConfigUtils .SHADOWSOCKS.formatLocal(Locale.ENGLISH, config.proxy, config.remotePort, config.localPort, config.sitekey, config.encMethod, 600) - ConfigUtils.printToFile(new File(getApplicationInfo().dataDir + "/ss-local-nat.conf"))(p => { + ConfigUtils.printToFile(new File(getApplicationInfo.dataDir + "/ss-local-nat.conf"))(p => { p.println(conf) }) val cmd = new ArrayBuffer[String] - cmd += (getApplicationInfo().dataDir + "/ss-local" + cmd += (getApplicationInfo.dataDir + "/ss-local" , "-b" , "127.0.0.1" , "-t" , "600" - , "-c" , getApplicationInfo().dataDir + "/ss-local-nat.conf") + , "-P", getApplicationInfo.dataDir + , "-c" , getApplicationInfo.dataDir + "/ss-local-nat.conf") if (config.isAuth) cmd += "-A" if (config.route != Route.ALL) { cmd += "--acl" - cmd += (getApplicationInfo().dataDir + "/acl.list") + cmd += (getApplicationInfo.dataDir + "/acl.list") } if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) @@ -205,16 +206,17 @@ class ShadowsocksNatService extends BaseService { val conf = ConfigUtils .SHADOWSOCKS.formatLocal(Locale.ENGLISH, config.proxy, config.remotePort, 8153, config.sitekey, config.encMethod, 10) - ConfigUtils.printToFile(new File(getApplicationInfo().dataDir + "/ss-tunnel-nat.conf"))(p => { + ConfigUtils.printToFile(new File(getApplicationInfo.dataDir + "/ss-tunnel-nat.conf"))(p => { p.println(conf) }) val cmd = new ArrayBuffer[String] - cmd += (getApplicationInfo().dataDir + "/ss-tunnel" + cmd += (getApplicationInfo.dataDir + "/ss-tunnel" , "-u" , "-t" , "10" , "-b" , "127.0.0.1" , "-L" , "8.8.8.8:53" - , "-c" , getApplicationInfo().dataDir + "/ss-tunnel-nat.conf") + , "-P" , getApplicationInfo.dataDir + , "-c" , getApplicationInfo.dataDir + "/ss-tunnel-nat.conf") cmd += ("-l" , "8153") @@ -231,17 +233,18 @@ class ShadowsocksNatService extends BaseService { val conf = ConfigUtils .SHADOWSOCKS.formatLocal(Locale.ENGLISH, config.proxy, config.remotePort, 8163, config.sitekey, config.encMethod, 10) - ConfigUtils.printToFile(new File(getApplicationInfo().dataDir + "/ss-tunnel-nat.conf"))(p => { + ConfigUtils.printToFile(new File(getApplicationInfo.dataDir + "/ss-tunnel-nat.conf"))(p => { p.println(conf) }) val cmdBuf = new ArrayBuffer[String] - cmdBuf += (getApplicationInfo().dataDir + "/ss-tunnel" + cmdBuf += (getApplicationInfo.dataDir + "/ss-tunnel" , "-u" , "-t" , "10" , "-b" , "127.0.0.1" , "-l" , "8163" , "-L" , "8.8.8.8:53" - , "-c" , getApplicationInfo().dataDir + "/ss-tunnel-nat.conf") + , "-P", getApplicationInfo.dataDir + , "-c" , getApplicationInfo.dataDir + "/ss-tunnel-nat.conf") if (config.isAuth) cmdBuf += "-A" @@ -259,17 +262,17 @@ class ShadowsocksNatService extends BaseService { val conf = if (config.route == Route.BYPASS_CHN) { val reject = ConfigUtils.getRejectList(getContext) val blackList = ConfigUtils.getBlackList(getContext) - ConfigUtils.PDNSD_DIRECT.formatLocal(Locale.ENGLISH, "127.0.0.1", 8153, - reject, blackList, 8163, "") + ConfigUtils.PDNSD_DIRECT.formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, + "127.0.0.1", 8153, reject, blackList, 8163, "") } else { - ConfigUtils.PDNSD_LOCAL.formatLocal(Locale.ENGLISH, "127.0.0.1", 8153, - 8163, "") + ConfigUtils.PDNSD_LOCAL.formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, + "127.0.0.1", 8153, 8163, "") } - ConfigUtils.printToFile(new File(getApplicationInfo().dataDir + "/pdnsd-nat.conf"))(p => { + ConfigUtils.printToFile(new File(getApplicationInfo.dataDir + "/pdnsd-nat.conf"))(p => { p.println(conf) }) - val cmd = getApplicationInfo().dataDir + "/pdnsd -c " + getApplicationInfo().dataDir + "/pdnsd-nat.conf" + val cmd = getApplicationInfo.dataDir + "/pdnsd -c " + getApplicationInfo.dataDir + "/pdnsd-nat.conf" if (BuildConfig.DEBUG) Log.d(TAG, cmd) @@ -294,8 +297,8 @@ class ShadowsocksNatService extends BaseService { def startRedsocksDaemon() { val conf = ConfigUtils.REDSOCKS.formatLocal(Locale.ENGLISH, config.localPort) val cmd = "%s/redsocks -c %s/redsocks-nat.conf" - .formatLocal(Locale.ENGLISH, getApplicationInfo().dataDir, getApplicationInfo().dataDir) - ConfigUtils.printToFile(new File(getApplicationInfo().dataDir + "/redsocks-nat.conf"))(p => { + .formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, getApplicationInfo.dataDir) + ConfigUtils.printToFile(new File(getApplicationInfo.dataDir + "/redsocks-nat.conf"))(p => { p.println(conf) }) diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala index 098b18bb..382aab03 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala @@ -261,7 +261,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { var tries = 1 while (tries < 5) { Thread.sleep(1000 * tries) - if (System.sendfd(fd, getApplicationInfo().dataDir + "/sock_path") != -1) { + if (System.sendfd(fd, getApplicationInfo.dataDir + "/sock_path") != -1) { return true } tries += 1 @@ -277,7 +277,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { case Route.BYPASS_LAN => getResources.getStringArray(R.array.private_route) case Route.BYPASS_CHN => getResources.getStringArray(R.array.chn_route_full) } - ConfigUtils.printToFile(new File(getApplicationInfo().dataDir + "/acl.list"))(p => { + ConfigUtils.printToFile(new File(getApplicationInfo.dataDir + "/acl.list"))(p => { acl.foreach(item => p.println(item)) }) } @@ -285,22 +285,22 @@ class ShadowsocksVpnService extends VpnService with BaseService { val conf = ConfigUtils .SHADOWSOCKS.formatLocal(Locale.ENGLISH, config.proxy, config.remotePort, config.localPort, config.sitekey, config.encMethod, 600) - ConfigUtils.printToFile(new File(getApplicationInfo().dataDir + "/ss-local-vpn.conf"))(p => { + ConfigUtils.printToFile(new File(getApplicationInfo.dataDir + "/ss-local-vpn.conf"))(p => { p.println(conf) }) val cmd = new ArrayBuffer[String] - cmd += (getApplicationInfo().dataDir + "/ss-local", "-V", "-u" + cmd += (getApplicationInfo.dataDir + "/ss-local", "-V", "-u" , "-b", "127.0.0.1" , "-t", "600" - , "-P", ShadowsocksApplication.dataDir - , "-c", getApplicationInfo().dataDir + "/ss-local-vpn.conf") + , "-P", getApplicationInfo.dataDir + , "-c", getApplicationInfo.dataDir + "/ss-local-vpn.conf") if (config.isAuth) cmd += "-A" if (config.route != Route.ALL) { cmd += "--acl" - cmd += (getApplicationInfo().dataDir + "/acl.list") + cmd += (getApplicationInfo.dataDir + "/acl.list") } if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) @@ -315,19 +315,19 @@ class ShadowsocksVpnService extends VpnService with BaseService { val conf = ConfigUtils .SHADOWSOCKS.formatLocal(Locale.ENGLISH, config.proxy, config.remotePort, 8163, config.sitekey, config.encMethod, 10) - ConfigUtils.printToFile(new File(getApplicationInfo().dataDir + "/ss-tunnel-vpn.conf"))(p => { + ConfigUtils.printToFile(new File(getApplicationInfo.dataDir + "/ss-tunnel-vpn.conf"))(p => { p.println(conf) }) val cmd = new ArrayBuffer[String] - cmd += (getApplicationInfo().dataDir + "/ss-tunnel" + cmd += (getApplicationInfo.dataDir + "/ss-tunnel" , "-V" , "-u" , "-t", "10" , "-b", "127.0.0.1" , "-l", "8163" , "-L", "8.8.8.8:53" - , "-P", ShadowsocksApplication.dataDir - , "-c", getApplicationInfo().dataDir + "/ss-tunnel-vpn.conf") + , "-P", getApplicationInfo.dataDir + , "-c", getApplicationInfo.dataDir + "/ss-tunnel-vpn.conf") if (config.isAuth) cmd += "-A" @@ -345,17 +345,17 @@ class ShadowsocksVpnService extends VpnService with BaseService { if (config.route == Route.BYPASS_CHN) { val reject = ConfigUtils.getRejectList(getContext) val blackList = ConfigUtils.getBlackList(getContext) - ConfigUtils.PDNSD_DIRECT.formatLocal(Locale.ENGLISH, "0.0.0.0", 8153, - reject, blackList, 8163, ipv6) + ConfigUtils.PDNSD_DIRECT.formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, + "0.0.0.0", 8153, reject, blackList, 8163, ipv6) } else { - ConfigUtils.PDNSD_LOCAL.formatLocal(Locale.ENGLISH, "0.0.0.0", 8153, - 8163, ipv6) + ConfigUtils.PDNSD_LOCAL.formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, + "0.0.0.0", 8153, 8163, ipv6) } } - ConfigUtils.printToFile(new File(getApplicationInfo().dataDir + "/pdnsd-vpn.conf"))(p => { + ConfigUtils.printToFile(new File(getApplicationInfo.dataDir + "/pdnsd-vpn.conf"))(p => { p.println(conf) }) - val cmd = getApplicationInfo().dataDir + "/pdnsd -c " + getApplicationInfo().dataDir + "/pdnsd-vpn.conf" + val cmd = getApplicationInfo.dataDir + "/pdnsd -c " + getApplicationInfo.dataDir + "/pdnsd-vpn.conf" if (BuildConfig.DEBUG) Log.d(TAG, cmd) @@ -430,7 +430,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { val fd = conn.getFd - var cmd = (getApplicationInfo().dataDir + + var cmd = (getApplicationInfo.dataDir + "/tun2socks --netif-ipaddr %s " + "--netif-netmask 255.255.255.0 " + "--socks-server-addr 127.0.0.1:%d " @@ -440,7 +440,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { + "--loglevel 3") .formatLocal(Locale.ENGLISH, PRIVATE_VLAN.formatLocal(Locale.ENGLISH, "2"), - config.localPort, fd, VPN_MTU, getApplicationInfo().dataDir + "/sock_path") + config.localPort, fd, VPN_MTU, getApplicationInfo.dataDir + "/sock_path") if (config.isIpv6) cmd += " --netif-ip6addr " + PRIVATE_VLAN6.formatLocal(Locale.ENGLISH, "2") diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnThread.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnThread.scala index 54328e34..fcb99498 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnThread.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnThread.scala @@ -53,7 +53,7 @@ class ShadowsocksVpnThread(vpnService: ShadowsocksVpnService) extends Thread { import ShadowsocksVpnThread._ val TAG = "ShadowsocksVpnService" - lazy val PATH = ShadowsocksApplication.dataDir + "/protect_path" + lazy val PATH = vpnService.getApplicationInfo.dataDir + "/protect_path" @volatile var isRunning: Boolean = true @volatile var serverSocket: LocalServerSocket = null diff --git a/src/main/scala/com/github/shadowsocks/utils/ConfigUtils.scala b/src/main/scala/com/github/shadowsocks/utils/ConfigUtils.scala index b118b2c6..96b9cd00 100644 --- a/src/main/scala/com/github/shadowsocks/utils/ConfigUtils.scala +++ b/src/main/scala/com/github/shadowsocks/utils/ConfigUtils.scala @@ -66,7 +66,7 @@ object ConfigUtils { """ |global { | perm_cache = 2048; - | cache_dir = "/data/data/com.github.shadowsocks"; + | cache_dir = "%s"; | server_ip = %s; | server_port = %d; | query_method = tcp_only; @@ -100,7 +100,7 @@ object ConfigUtils { """ |global { | perm_cache = 2048; - | cache_dir = "/data/data/com.github.shadowsocks"; + | cache_dir = "%s"; | server_ip = %s; | server_port = %d; | query_method = tcp_only; @@ -143,7 +143,7 @@ object ConfigUtils { """ |global { | perm_cache = 2048; - | cache_dir = "/data/data/com.github.shadowsocks"; + | cache_dir = "%s"; | server_ip = %s; | server_port = %d; | query_method = tcp_only; diff --git a/src/main/scala/com/github/shadowsocks/utils/TrafficMonitorThread.scala b/src/main/scala/com/github/shadowsocks/utils/TrafficMonitorThread.scala index ec4b890e..d17eddb1 100644 --- a/src/main/scala/com/github/shadowsocks/utils/TrafficMonitorThread.scala +++ b/src/main/scala/com/github/shadowsocks/utils/TrafficMonitorThread.scala @@ -43,13 +43,14 @@ import java.io.{File, IOException} import java.nio.{ByteBuffer, ByteOrder} import java.util.concurrent.Executors +import android.content.Context import android.net.{LocalServerSocket, LocalSocket, LocalSocketAddress} import android.util.Log -class TrafficMonitorThread() extends Thread { +class TrafficMonitorThread(context: Context) extends Thread { val TAG = "TrafficMonitorThread" - val PATH = "/data/data/com.github.shadowsocks/stat_path" + lazy val PATH = context.getApplicationInfo.dataDir + "/stat_path" @volatile var serverSocket: LocalServerSocket = null @volatile var isRunning: Boolean = true