mirror of
https://github.com/yggdrasil-network/crispa-android.git
synced 2025-01-22 07:56:30 +00:00
1. set blocking read
This commit is contained in:
parent
df0733f6b6
commit
4865e16854
@ -31,6 +31,10 @@ import java.net.Inet6Address
|
|||||||
class YggdrasilTunService : VpnService() {
|
class YggdrasilTunService : VpnService() {
|
||||||
|
|
||||||
private lateinit var ygg: Yggdrasil
|
private lateinit var ygg: Yggdrasil
|
||||||
|
private lateinit var tunInterface: ParcelFileDescriptor
|
||||||
|
private lateinit var tunInputStream: InputStream
|
||||||
|
private lateinit var tunOutputStream: OutputStream
|
||||||
|
private lateinit var address: String
|
||||||
private var isClosed = false
|
private var isClosed = false
|
||||||
|
|
||||||
/** Maximum packet size is constrained by the MTU, which is given as a signed short/2 */
|
/** Maximum packet size is constrained by the MTU, which is given as a signed short/2 */
|
||||||
@ -39,21 +43,11 @@ class YggdrasilTunService : VpnService() {
|
|||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "Yggdrasil-service"
|
private const val TAG = "Yggdrasil-service"
|
||||||
}
|
}
|
||||||
private var tunInterface: ParcelFileDescriptor? = null
|
|
||||||
private var tunInputStream: InputStream? = null
|
|
||||||
private var tunOutputStream: OutputStream? = null
|
|
||||||
private var scope: CoroutineScope? = null
|
|
||||||
private var address: String? = null
|
|
||||||
|
|
||||||
private var mNotificationManager: NotificationManager? = null
|
private var scope: CoroutineScope? = null
|
||||||
|
|
||||||
private val FOREGROUND_ID = 1338
|
private val FOREGROUND_ID = 1338
|
||||||
|
|
||||||
override fun onCreate() {
|
|
||||||
super.onCreate()
|
|
||||||
mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
val pi: PendingIntent? = intent?.getParcelableExtra(MainActivity.PARAM_PINTENT)
|
val pi: PendingIntent? = intent?.getParcelableExtra(MainActivity.PARAM_PINTENT)
|
||||||
when(intent?.getStringExtra(MainActivity.COMMAND)){
|
when(intent?.getStringExtra(MainActivity.COMMAND)){
|
||||||
@ -86,14 +80,14 @@ class YggdrasilTunService : VpnService() {
|
|||||||
|
|
||||||
var builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
var builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||||
Builder()
|
Builder()
|
||||||
.addAddress(address!!, 7)
|
.addAddress(address, 7)
|
||||||
.allowFamily(OsConstants.AF_INET)
|
.allowFamily(OsConstants.AF_INET)
|
||||||
.allowBypass()
|
.allowBypass()
|
||||||
.setBlocking(true)
|
.setBlocking(true)
|
||||||
.setMtu(MAX_PACKET_SIZE)
|
.setMtu(MAX_PACKET_SIZE)
|
||||||
} else {
|
} else {
|
||||||
Builder()
|
Builder()
|
||||||
.addAddress(address!!, 7)
|
.addAddress(address, 7)
|
||||||
.addRoute("200::", 7)
|
.addRoute("200::", 7)
|
||||||
.setMtu(MAX_PACKET_SIZE)
|
.setMtu(MAX_PACKET_SIZE)
|
||||||
}
|
}
|
||||||
@ -108,15 +102,9 @@ class YggdrasilTunService : VpnService() {
|
|||||||
if(!hasIpv6DefaultRoute()){
|
if(!hasIpv6DefaultRoute()){
|
||||||
builder.addRoute("2000::",3)
|
builder.addRoute("2000::",3)
|
||||||
}
|
}
|
||||||
if(tunInterface!=null){
|
|
||||||
tunInterface!!.close()
|
|
||||||
tunInputStream!!.close()
|
|
||||||
tunOutputStream!!.close()
|
|
||||||
}
|
|
||||||
tunInterface = builder.establish()
|
tunInterface = builder.establish()
|
||||||
tunInputStream = FileInputStream(tunInterface!!.fileDescriptor)
|
tunInputStream = FileInputStream(tunInterface.fileDescriptor)
|
||||||
tunOutputStream = FileOutputStream(tunInterface!!.fileDescriptor)
|
tunOutputStream = FileOutputStream(tunInterface.fileDescriptor)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupTunInterface(
|
private fun setupTunInterface(
|
||||||
@ -140,7 +128,7 @@ class YggdrasilTunService : VpnService() {
|
|||||||
val job = SupervisorJob()
|
val job = SupervisorJob()
|
||||||
scope = CoroutineScope(Dispatchers.Default + job)
|
scope = CoroutineScope(Dispatchers.Default + job)
|
||||||
scope!!.launch {
|
scope!!.launch {
|
||||||
val buffer = ByteArray(2048)
|
val buffer = ByteArray(1024)
|
||||||
while (!isClosed) {
|
while (!isClosed) {
|
||||||
readPacketsFromTun(yggConduitEndpoint, buffer)
|
readPacketsFromTun(yggConduitEndpoint, buffer)
|
||||||
}
|
}
|
||||||
@ -227,7 +215,7 @@ class YggdrasilTunService : VpnService() {
|
|||||||
private fun readPacketsFromTun(yggConduitEndpoint: ConduitEndpoint, buffer: ByteArray) {
|
private fun readPacketsFromTun(yggConduitEndpoint: ConduitEndpoint, buffer: ByteArray) {
|
||||||
try {
|
try {
|
||||||
// Read the outgoing packet from the input stream.
|
// Read the outgoing packet from the input stream.
|
||||||
val length = tunInputStream?.read(buffer) ?: 1
|
val length = tunInputStream.read(buffer)
|
||||||
yggConduitEndpoint.send(buffer.sliceArray(IntRange(0, length - 1)))
|
yggConduitEndpoint.send(buffer.sliceArray(IntRange(0, length - 1)))
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
@ -238,7 +226,7 @@ class YggdrasilTunService : VpnService() {
|
|||||||
val buffer = yggConduitEndpoint.recv()
|
val buffer = yggConduitEndpoint.recv()
|
||||||
if(buffer!=null) {
|
if(buffer!=null) {
|
||||||
try {
|
try {
|
||||||
tunOutputStream?.write(buffer)
|
tunOutputStream.write(buffer)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
@ -248,10 +236,9 @@ class YggdrasilTunService : VpnService() {
|
|||||||
private fun stopVpn(pi: PendingIntent?) {
|
private fun stopVpn(pi: PendingIntent?) {
|
||||||
isClosed = true;
|
isClosed = true;
|
||||||
scope!!.coroutineContext.cancelChildren()
|
scope!!.coroutineContext.cancelChildren()
|
||||||
tunInputStream!!.close()
|
tunInputStream.close()
|
||||||
tunOutputStream!!.close()
|
tunOutputStream.close()
|
||||||
tunInterface!!.close()
|
tunInterface.close()
|
||||||
tunInterface = null
|
|
||||||
Log.d(TAG,"Stop is running from service")
|
Log.d(TAG,"Stop is running from service")
|
||||||
ygg.stop()
|
ygg.stop()
|
||||||
val intent: Intent = Intent()
|
val intent: Intent = Intent()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user