Skip to content

Commit 809f4c1

Browse files
committed
add management thread
1 parent 0472580 commit 809f4c1

File tree

4 files changed

+124
-86
lines changed

4 files changed

+124
-86
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function try () {
77
try pushd src/main
88

99
# Build
10-
try $ANDROID_NDK_HOME/ndk-build clean
10+
#try $ANDROID_NDK_HOME/ndk-build clean
1111
try $ANDROID_NDK_HOME/ndk-build -j8
1212

1313
# copy executables

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

Lines changed: 10 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -212,89 +212,17 @@ class ShadowsocksVpnService extends VpnService with BaseService {
212212
builder.addDisallowedApplication(pkg)
213213
}
214214
}
215-
216-
if (config.isBypassApps) {
217-
builder.addDisallowedApplication(this.getPackageName)
218-
}
219-
} else {
220-
builder.addDisallowedApplication(this.getPackageName)
221215
}
222216
}
223217

224-
if (InetAddressUtils.isIPv6Address(config.proxy)) {
218+
if (config.route == Route.ALL) {
225219
builder.addRoute("0.0.0.0", 0)
226220
} else {
227-
if (!Utils.isLollipopOrAbove) {
228-
config.route match {
229-
case Route.BYPASS_LAN =>
230-
for (i <- 1 to 223) {
231-
if (i != 26 && i != 127) {
232-
val addr = i.toString + ".0.0.0"
233-
val cidr = addr + "/8"
234-
val net = new SubnetUtils(cidr)
235-
236-
if (!isByass(net) && !isPrivateA(i)) {
237-
builder.addRoute(addr, 8)
238-
} else {
239-
for (j <- 0 to 255) {
240-
val subAddr = i.toString + "." + j.toString + ".0.0"
241-
val subCidr = subAddr + "/16"
242-
val subNet = new SubnetUtils(subCidr)
243-
if (!isByass(subNet) && !isPrivateB(i, j)) {
244-
builder.addRoute(subAddr, 16)
245-
}
246-
}
247-
}
248-
}
249-
}
250-
case Route.BYPASS_CHN =>
251-
val list = {
252-
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
253-
getResources.getStringArray(R.array.simple_route)
254-
} else {
255-
getResources.getStringArray(R.array.gfw_route)
256-
}
257-
}
258-
list.foreach(cidr => {
259-
val net = new SubnetUtils(cidr)
260-
if (!isByass(net)) {
261-
val addr = cidr.split('/')
262-
builder.addRoute(addr(0), addr(1).toInt)
263-
}
264-
})
265-
case Route.ALL =>
266-
for (i <- 1 to 223) {
267-
if (i != 26 && i != 127) {
268-
val addr = i.toString + ".0.0.0"
269-
val cidr = addr + "/8"
270-
val net = new SubnetUtils(cidr)
271-
272-
if (!isByass(net)) {
273-
builder.addRoute(addr, 8)
274-
} else {
275-
for (j <- 0 to 255) {
276-
val subAddr = i.toString + "." + j.toString + ".0.0"
277-
val subCidr = subAddr + "/16"
278-
val subNet = new SubnetUtils(subCidr)
279-
if (!isByass(subNet)) {
280-
builder.addRoute(subAddr, 16)
281-
}
282-
}
283-
}
284-
}
285-
}
286-
}
287-
} else {
288-
if (config.route == Route.ALL) {
289-
builder.addRoute("0.0.0.0", 0)
290-
} else {
291-
val privateList = getResources.getStringArray(R.array.bypass_private_route)
292-
privateList.foreach(cidr => {
293-
val addr = cidr.split('/')
294-
builder.addRoute(addr(0), addr(1).toInt)
295-
})
296-
}
297-
}
221+
val privateList = getResources.getStringArray(R.array.bypass_private_route)
222+
privateList.foreach(cidr => {
223+
val addr = cidr.split('/')
224+
builder.addRoute(addr(0), addr(1).toInt)
225+
})
298226
}
299227

300228
builder.addRoute("8.8.0.0", 16)
@@ -330,15 +258,11 @@ class ShadowsocksVpnService extends VpnService with BaseService {
330258
else
331259
cmd += " --dnsgw %s:8153".formatLocal(Locale.ENGLISH, PRIVATE_VLAN.formatLocal(Locale.ENGLISH, "1"))
332260

333-
// if (Utils.isLollipopOrAbove) {
334-
// cmd += " --fake-proc"
335-
// }
336-
337261
if (BuildConfig.DEBUG) Log.d(TAG, cmd)
338262

339263
Console.runCommand(cmd)
340264

341-
return fd;
265+
return fd
342266
}
343267

344268
/** Called when the activity is first created. */
@@ -350,7 +274,6 @@ class ShadowsocksVpnService extends VpnService with BaseService {
350274
}
351275

352276
val fd = startVpn()
353-
354277
if (fd == -1) {
355278
false
356279
} else {
@@ -361,6 +284,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
361284
true
362285
}
363286
}
287+
364288
}
365289

366290
override def onBind(intent: Intent): IBinder = {
@@ -380,6 +304,8 @@ class ShadowsocksVpnService extends VpnService with BaseService {
380304

381305
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE)
382306
.asInstanceOf[NotificationManager]
307+
308+
new Thread(new ShadowsocksVpnThread(this)).start()
383309
}
384310

385311
override def onRevoke() {
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Shadowsocks - A shadowsocks client for Android
3+
* Copyright (C) 2015 <max.c.lv@gmail.com>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*
18+
*
19+
* ___====-_ _-====___
20+
* _--^^^#####// \\#####^^^--_
21+
* _-^##########// ( ) \\##########^-_
22+
* -############// |\^^/| \\############-
23+
* _/############// (@::@) \\############\_
24+
* /#############(( \\// ))#############\
25+
* -###############\\ (oo) //###############-
26+
* -#################\\ / VV \ //#################-
27+
* -###################\\/ \//###################-
28+
* _#/|##########/\######( /\ )######/\##########|\#_
29+
* |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \|
30+
* ` |/ V V ` V \#\| | | |/#/ V ' V V \| '
31+
* ` ` ` ` / | | | | \ ' ' ' '
32+
* ( | | | | )
33+
* __\ | | | | /__
34+
* (vvv(VVV)(VVV)vvv)
35+
*
36+
* HERE BE DRAGONS
37+
*
38+
*/
39+
40+
package com.github.shadowsocks
41+
42+
import java.io.{File, FileDescriptor, IOException}
43+
44+
import android.net.{LocalServerSocket, LocalSocket, LocalSocketAddress}
45+
import android.util.Log
46+
47+
class ShadowsocksVpnThread(vpnService: ShadowsocksVpnService) extends Runnable {
48+
49+
val TAG = "ShadowsocksVpnService"
50+
val PATH = "/data/data/com.github.shadowsocks/protect_path"
51+
52+
override def run(): Unit = {
53+
54+
var serverSocket: LocalServerSocket = null
55+
56+
try {
57+
new File(PATH).delete()
58+
} catch {
59+
case _ => // ignore
60+
}
61+
62+
try {
63+
val localSocket = new LocalSocket
64+
65+
localSocket.bind(new LocalSocketAddress(PATH, LocalSocketAddress.Namespace.FILESYSTEM))
66+
serverSocket = new LocalServerSocket(localSocket.getFileDescriptor)
67+
} catch {
68+
case e: IOException =>
69+
Log.e(TAG, "unable to bind", e)
70+
vpnService.stopBackgroundService()
71+
return
72+
}
73+
74+
Log.d(TAG, "start to accept connections")
75+
76+
while (true) {
77+
78+
try {
79+
val socket = serverSocket.accept()
80+
81+
val input = socket.getInputStream
82+
val output = socket.getOutputStream
83+
84+
input.read()
85+
86+
Log.d(TAG, "test")
87+
88+
val fds = socket.getAncillaryFileDescriptors
89+
90+
if (fds.length > 0) {
91+
var ret = false
92+
93+
val getInt = classOf[FileDescriptor].getDeclaredMethod("getInt$")
94+
val fd = getInt.invoke(fds(0)).asInstanceOf[Int]
95+
ret = vpnService.protect(fd)
96+
97+
if (ret) {
98+
socket.setFileDescriptorsForSend(fds)
99+
output.write(0)
100+
}
101+
102+
input.close()
103+
output.close()
104+
}
105+
} catch {
106+
case e: Exception =>
107+
Log.e(TAG, "Error when protect socket", e)
108+
}
109+
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)