tun device work fine
This commit is contained in:
11 files changed
+165
-50
No files matched your search
@@ -3,5 +3,5 @@ package com.github.kr328.clash;
|
||||
|
||||
interface IClashService {
|
||||
void loadProfile(String path);
|
||||
void startTunDevice(int fd, int mtu);
|
||||
void startTunDevice(in ParcelFileDescriptor fd, int mtu);
|
||||
}
|
||||
@@ -2,10 +2,14 @@ package com.github.kr328.clash
|
||||
|
||||
import android.app.Service
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.IBinder
|
||||
import android.os.Parcel
|
||||
import android.os.ParcelFileDescriptor
|
||||
import android.util.Log
|
||||
import com.github.kr328.clash.core.Clash
|
||||
import com.github.kr328.clash.core.ClashProcess
|
||||
import java.io.FileDescriptor
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
class ClashService : Service() {
|
||||
@@ -15,12 +19,12 @@ class ClashService : Service() {
|
||||
|
||||
private lateinit var clash: Clash
|
||||
private inner class ClashServiceImpl : IClashService.Stub() {
|
||||
override fun loadProfile(path: String?) {
|
||||
|
||||
override fun loadProfile(url: String?) {
|
||||
clash.loadProfile(Uri.parse(url))
|
||||
}
|
||||
|
||||
override fun startTunDevice(fd: Int, mtu: Int) {
|
||||
|
||||
override fun startTunDevice(fd: ParcelFileDescriptor, mtu: Int) {
|
||||
clash.startTunDevice(fd.fileDescriptor, mtu)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +35,7 @@ class ClashService : Service() {
|
||||
filesDir.resolve("clash"),
|
||||
cacheDir.resolve("clash_controller"))
|
||||
|
||||
Log.d(TAG, "ping " + clash.ping())
|
||||
|
||||
thread {
|
||||
clash.exec()
|
||||
@@ -44,8 +49,6 @@ class ClashService : Service() {
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
||||
|
||||
super.onDestroy()
|
||||
}
|
||||
}
|
||||
@@ -8,13 +8,9 @@ import android.content.ServiceConnection
|
||||
import android.net.VpnService
|
||||
import android.os.Bundle
|
||||
import android.os.IBinder
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import java.io.FileOutputStream
|
||||
import java.lang.Exception
|
||||
import java.lang.RuntimeException
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
@@ -23,7 +19,7 @@ class MainActivity : AppCompatActivity() {
|
||||
private const val VPN_REQUEST = 234
|
||||
}
|
||||
|
||||
private val connection = object: ServiceConnection {
|
||||
private val connection = object : ServiceConnection {
|
||||
override fun onServiceDisconnected(name: ComponentName?) {
|
||||
Toast.makeText(this@MainActivity, "Disconnected", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
@@ -34,11 +30,8 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
clash.loadProfile(cacheDir.resolve("config.yaml").absolutePath)
|
||||
|
||||
// VpnService.prepare(this@MainActivity)?.apply {
|
||||
// startActivityForResult(this, VPN_REQUEST)
|
||||
// } ?: startService(Intent(this@MainActivity, TunService::class.java))
|
||||
}
|
||||
catch (e: Exception) {
|
||||
|
||||
} catch (e: Exception) {
|
||||
runOnUiThread {
|
||||
Toast.makeText(this@MainActivity, e.toString(), Toast.LENGTH_LONG).show()
|
||||
}
|
||||
@@ -57,23 +50,28 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
if ( requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK ) {
|
||||
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
|
||||
Toast.makeText(this, intent?.dataString, Toast.LENGTH_LONG).show()
|
||||
|
||||
thread {
|
||||
FileOutputStream(cacheDir.resolve("config.yaml")).use {
|
||||
contentResolver.openInputStream(data?.data ?: throw RuntimeException())?.apply { copyTo(it) }?.close()
|
||||
}
|
||||
bindService(Intent(this, ClashService::class.java), object : ServiceConnection {
|
||||
override fun onServiceDisconnected(name: ComponentName?) {
|
||||
Toast.makeText(this@MainActivity, "Disconnected", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
|
||||
//startService(Intent(this, ClashService::class.java))
|
||||
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
|
||||
IClashService.Stub.asInterface(service)?.loadProfile(data?.dataString)
|
||||
|
||||
bindService(Intent(this, ClashService::class.java), connection, Context.BIND_AUTO_CREATE)
|
||||
VpnService.prepare(this@MainActivity)?.apply {
|
||||
startActivityForResult(this, VPN_REQUEST)
|
||||
} ?: startService(Intent(this@MainActivity, TunService::class.java))
|
||||
}
|
||||
|
||||
}, Context.BIND_AUTO_CREATE)
|
||||
}
|
||||
}
|
||||
else if ( requestCode == VPN_REQUEST && resultCode == Activity.RESULT_OK ) {
|
||||
} else if (requestCode == VPN_REQUEST && resultCode == Activity.RESULT_OK) {
|
||||
startService(Intent(this, TunService::class.java))
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ class TunService : VpnService() {
|
||||
}
|
||||
|
||||
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
|
||||
IClashService.Stub.asInterface(service)?.startTunDevice(fileDescriptor.fd, 1500) ?: stopSelf()
|
||||
IClashService.Stub.asInterface(service)?.startTunDevice(fileDescriptor, 1500) ?: stopSelf()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ class TunService : VpnService() {
|
||||
|
||||
bindService(Intent(this, ClashService::class.java), connection, Context.BIND_AUTO_CREATE)
|
||||
|
||||
return Service.START_STICKY
|
||||
return Service.START_NOT_STICKY
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
||||
Reference in new issue
Block a user