Skip to content

Commit f684184

Browse files
committed
chnroute works perfectly without root on lollipop
1 parent 52cb150 commit f684184

File tree

3 files changed

+110
-31
lines changed

3 files changed

+110
-31
lines changed

src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import java.util.{Timer, TimerTask}
4646
import android.app.{Notification, NotificationManager, PendingIntent, Service}
4747
import android.content._
4848
import android.content.pm.{PackageInfo, PackageManager}
49-
import android.graphics.Color
5049
import android.net.TrafficStats
5150
import android.os._
5251
import android.support.v4.app.NotificationCompat

src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,28 @@ class ShadowsocksVpnService extends VpnService with BaseService {
7575

7676
private lazy val application = getApplication.asInstanceOf[ShadowsocksApplication]
7777

78+
def isACLEnabled: Boolean = {
79+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
80+
true
81+
} else {
82+
false
83+
}
84+
}
85+
7886
def isByass(net: SubnetUtils): Boolean = {
7987
val info = net.getInfo
8088
info.isInRange(config.proxy)
8189
}
8290

8391
def startShadowsocksDaemon() {
92+
93+
if (isACLEnabled && config.isGFWList) {
94+
val chn_list: Array[String] = getResources.getStringArray(R.array.chn_list_full)
95+
ConfigUtils.printToFile(new File(Path.BASE + "chn.acl"))(p => {
96+
chn_list.foreach(item => p.println(item))
97+
})
98+
}
99+
84100
val cmd = new ArrayBuffer[String]
85101
cmd += ("ss-local" , "-u"
86102
, "-b" , "127.0.0.1"
@@ -90,13 +106,34 @@ class ShadowsocksVpnService extends VpnService with BaseService {
90106
, "-k" , config.sitekey
91107
, "-m" , config.encMethod
92108
, "-f" , Path.BASE + "ss-local.pid")
109+
110+
if (config.isGFWList && isACLEnabled) {
111+
cmd += "--acl"
112+
cmd += (Path.BASE + "chn.acl")
113+
}
114+
93115
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
94116
Core.sslocal(cmd.toArray)
95117
}
96118

119+
def startDnsTunnel() = {
120+
val cmd = new ArrayBuffer[String]
121+
cmd += ("ss-tunnel"
122+
, "-b" , "127.0.0.1"
123+
, "-l" , "8163"
124+
, "-L" , "8.8.8.8:53"
125+
, "-s" , config.proxy
126+
, "-p" , config.remotePort.toString
127+
, "-k" , config.sitekey
128+
, "-m" , config.encMethod
129+
, "-f" , Path.BASE + "ss-tunnel.pid")
130+
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
131+
Core.sstunnel(cmd.toArray)
132+
}
133+
97134
def startDnsDaemon() {
98135
val conf = {
99-
ConfigUtils.PDNSD.format("0.0.0.0")
136+
ConfigUtils.PDNSD_LOCAL.format("0.0.0.0", 8163)
100137
}
101138
ConfigUtils.printToFile(new File(Path.BASE + "pdnsd.conf"))(p => {
102139
p.println(conf)
@@ -144,15 +181,17 @@ class ShadowsocksVpnService extends VpnService with BaseService {
144181
}
145182
}
146183

147-
if (!config.isBypassApps) {
148-
builder.addAllowedApplication(this.getPackageName)
184+
if (config.isBypassApps) {
185+
builder.addDisallowedApplication(this.getPackageName)
149186
}
187+
} else {
188+
builder.addDisallowedApplication(this.getPackageName)
150189
}
151190
}
152191

153192
if (InetAddressUtils.isIPv6Address(config.proxy)) {
154193
builder.addRoute("0.0.0.0", 0)
155-
} else if (config.isGFWList) {
194+
} else if (!isACLEnabled && config.isGFWList) {
156195
val gfwList = {
157196
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
158197
getResources.getStringArray(R.array.simple_list)
@@ -168,24 +207,28 @@ class ShadowsocksVpnService extends VpnService with BaseService {
168207
}
169208
})
170209
} else {
171-
for (i <- 1 to 223) {
172-
if (i != 26 && i != 127) {
173-
val addr = i.toString + ".0.0.0"
174-
val cidr = addr + "/8"
175-
val net = new SubnetUtils(cidr)
176-
177-
if (!isByass(net)) {
178-
if (!InetAddress.getByName(addr).isSiteLocalAddress) {
179-
builder.addRoute(addr, 8)
180-
}
181-
} else {
182-
for (j <- 0 to 255) {
183-
val subAddr = i.toString + "." + j.toString + ".0.0"
184-
val subCidr = subAddr + "/16"
185-
val subNet = new SubnetUtils(subCidr)
186-
if (!isByass(subNet)) {
187-
if (!InetAddress.getByName(subAddr).isSiteLocalAddress) {
188-
builder.addRoute(subAddr, 16)
210+
if (isACLEnabled) {
211+
builder.addRoute("0.0.0.0", 0)
212+
} else {
213+
for (i <- 1 to 223) {
214+
if (i != 26 && i != 127) {
215+
val addr = i.toString + ".0.0.0"
216+
val cidr = addr + "/8"
217+
val net = new SubnetUtils(cidr)
218+
219+
if (!isByass(net)) {
220+
if (!InetAddress.getByName(addr).isSiteLocalAddress) {
221+
builder.addRoute(addr, 8)
222+
}
223+
} else {
224+
for (j <- 0 to 255) {
225+
val subAddr = i.toString + "." + j.toString + ".0.0"
226+
val subCidr = subAddr + "/16"
227+
val subNet = new SubnetUtils(subCidr)
228+
if (!isByass(subNet)) {
229+
if (!InetAddress.getByName(subAddr).isSiteLocalAddress) {
230+
builder.addRoute(subAddr, 16)
231+
}
189232
}
190233
}
191234
}
@@ -236,7 +279,10 @@ class ShadowsocksVpnService extends VpnService with BaseService {
236279
def handleConnection: Boolean = {
237280
startVpn()
238281
startShadowsocksDaemon()
239-
if (!config.isUdpDns) startDnsDaemon()
282+
if (!config.isUdpDns) {
283+
startDnsDaemon()
284+
startDnsTunnel()
285+
}
240286
true
241287
}
242288

@@ -264,13 +310,15 @@ class ShadowsocksVpnService extends VpnService with BaseService {
264310
}
265311

266312
def killProcesses() {
267-
val ab = new ArrayBuffer[String]
268-
269-
ab.append("kill -9 `cat " + Path.BASE + "ss-local.pid`")
270-
ab.append("kill -9 `cat " + Path.BASE + "tun2socks.pid`")
271-
ab.append("kill -15 `cat " + Path.BASE + "pdnsd.pid`")
272-
273-
Console.runCommand(ab.toArray)
313+
for (task <- Array("ss-local", "ss-tunnel", "tun2socks", "pdnsd")) {
314+
try {
315+
val pid = scala.io.Source.fromFile(Path.BASE + task + ".pid").mkString.trim.toInt
316+
Process.killProcess(pid)
317+
Log.d(TAG, "kill pid: " + pid)
318+
} catch {
319+
case e: Throwable => Log.e(TAG, "unable to kill " + task, e)
320+
}
321+
}
274322
}
275323

276324
override def startRunner(c: Config) {

src/main/scala/com/github/shadowsocks/utils/ConfigUtils.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,38 @@ object ConfigUtils {
9292
|}
9393
""".stripMargin
9494

95+
val PDNSD_LOCAL =
96+
"""
97+
|global {
98+
| perm_cache = 2048;
99+
| cache_dir = "/data/data/com.github.shadowsocks";
100+
| server_ip = %s;
101+
| server_port = 8153;
102+
| query_method = tcp_only;
103+
| run_ipv4 = on;
104+
| min_ttl = 15m;
105+
| max_ttl = 1w;
106+
| timeout = 10;
107+
| daemon = on;
108+
| pid_file = "/data/data/com.github.shadowsocks/pdnsd.pid";
109+
|}
110+
|
111+
|server {
112+
| label = "local";
113+
| ip = 127.0.0.1;
114+
| port = %d;
115+
| timeout = 5;
116+
|}
117+
|
118+
|rr {
119+
| name=localhost;
120+
| reverse=on;
121+
| a=127.0.0.1;
122+
| owner=localhost;
123+
| soa=localhost,root.localhost,42,86400,900,86400,86400;
124+
|}
125+
""".stripMargin
126+
95127
val PDNSD_BYPASS =
96128
"""
97129
|global {

0 commit comments

Comments
 (0)