forked from eycorsican/kitsunebi-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2fec94
commit 6ae4ffe
Showing
35 changed files
with
1,382 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
apply plugin: 'kotlin-android' | ||
|
||
apply plugin: 'kotlin-android-extensions' | ||
|
||
android { | ||
compileSdkVersion 28 | ||
defaultConfig { | ||
applicationId 'fun.kitsunebi.kitsunebi4android' | ||
minSdkVersion 17 | ||
targetSdkVersion 28 | ||
versionCode 2 | ||
versionName "0.2.0" | ||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
productFlavors { | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(include: ['*.jar'], dir: 'libs') | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
implementation 'com.android.support:appcompat-v7:28.0.0' | ||
implementation 'com.android.support.constraint:constraint-layout:1.0.2' | ||
implementation 'com.android.support:design:28.0.0' | ||
testImplementation 'junit:junit:4.12' | ||
androidTestImplementation 'com.android.support.test:runner:1.0.1' | ||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' | ||
implementation 'com.beust:klaxon:3.0.1' | ||
implementation(name: 'tun2socks', ext: 'aar') | ||
} | ||
|
||
repositories { | ||
flatDir { | ||
dirs 'libs' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
24 changes: 24 additions & 0 deletions
24
app/src/androidTest/java/fun/kitsunebi/kitsunebi4android/ExampleInstrumentedTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package `fun`.kitsunebi.kitsunebi4android | ||
|
||
import android.support.test.InstrumentationRegistry | ||
import android.support.test.runner.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getTargetContext() | ||
assertEquals("fun.kitsunebi.kitsunebi4android", appContext.packageName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="fun.kitsunebi.kitsunebi4android"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET"/> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity | ||
android:name="fun.kitsunebi.kitsunebi4android.MainActivity" | ||
android:label="@string/app_name" | ||
android:theme="@style/AppTheme.NoActionBar"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
|
||
<service | ||
android:name="fun.kitsunebi.kitsunebi4android.KitsunebiVpnService" | ||
android:permission="android.permission.BIND_VPN_SERVICE" > | ||
<intent-filter> | ||
<action android:name="android.net.VpnService" /> | ||
</intent-filter> | ||
</service> | ||
|
||
</application> | ||
|
||
</manifest> |
179 changes: 179 additions & 0 deletions
179
app/src/main/java/fun/kitsunebi/kitsunebi4android/KitsunebiVpnService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package `fun`.kitsunebi.kitsunebi4android | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.net.VpnService | ||
import android.os.Build | ||
import android.os.ParcelFileDescriptor | ||
import tun2socks.PacketFlow | ||
import tun2socks.VpnService as Tun2socksVpnService | ||
import kotlin.concurrent.thread | ||
import com.beust.klaxon.Klaxon | ||
import java.io.FileInputStream | ||
import java.io.FileOutputStream | ||
import java.net.InetAddress | ||
import java.nio.ByteBuffer | ||
|
||
open class KitsunebiVpnService: VpnService() { | ||
var configString: String = "" | ||
var proxyDomainIPMap: HashMap<String, String> = HashMap<String, String>() | ||
var pfd: ParcelFileDescriptor? = null | ||
var inputStream: FileInputStream? = null | ||
var outputStream: FileOutputStream? = null | ||
var buffer = ByteBuffer.allocate(1501) | ||
var isStopped = false | ||
|
||
data class Config(val outbounds: List<Outbound>?) | ||
data class Outbound(val protocol: String = "", val settings: Settings? = null) | ||
data class Settings(val vnext: List<Server?>? = emptyList()) | ||
data class Server(val address: String? = null) | ||
|
||
val broadcastReceiver = object : BroadcastReceiver() { | ||
override fun onReceive(contxt: Context?, intent: Intent?) { | ||
when (intent?.action) { | ||
"stop_vpn" -> { | ||
stopVPN() | ||
} | ||
"ping" -> { | ||
if (!isStopped) { | ||
sendBroadcast(Intent("pong")) | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
private fun stopVPN() { | ||
isStopped = true | ||
tun2socks.Tun2socks.stopV2Ray() | ||
pfd?.close() | ||
pfd = null | ||
inputStream = null | ||
outputStream = null | ||
sendBroadcast(Intent("vpn_stopped")) | ||
stopSelf() | ||
} | ||
|
||
class Flow(stream: FileOutputStream?): PacketFlow { | ||
val flowOutputStream = stream | ||
override fun writePacket(pkt: ByteArray?) { | ||
flowOutputStream?.write(pkt) | ||
} | ||
} | ||
|
||
class Service(service: VpnService): Tun2socksVpnService { | ||
val vpnService = service | ||
override fun protect(fd: Long) { | ||
vpnService.protect(fd.toInt()) | ||
} | ||
} | ||
|
||
fun handlePackets() { | ||
while (!isStopped) { | ||
val n = inputStream?.read(buffer.array()) | ||
n?.let { it } ?: return | ||
if (n > 0) { | ||
buffer.limit(n) | ||
tun2socks.Tun2socks.inputPacket(buffer.array()) | ||
buffer.clear() | ||
} | ||
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { | ||
// In non-blocking mode | ||
Thread.sleep(50) | ||
} | ||
} | ||
} | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
registerReceiver(broadcastReceiver, IntentFilter("stop_vpn")) | ||
registerReceiver(broadcastReceiver, IntentFilter("ping")) | ||
} | ||
|
||
override public fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | ||
configString = intent?.extras?.get("config").toString() | ||
|
||
thread (start = true) { | ||
var error = false | ||
|
||
val config = Klaxon().parse<Config>(configString) | ||
if (config != null) { | ||
config.outbounds?.forEach { | ||
if (it.protocol == "vmess") { | ||
it.settings?.vnext?.forEach { | ||
if (it != null && it.address != null) { | ||
println("vmess server address: ${it.address}") | ||
try { | ||
val addr = InetAddress.getByName(it.address) | ||
val ip = addr.getHostAddress() | ||
if (it.address != ip) { | ||
// address is a domain name | ||
proxyDomainIPMap.put(it.address, ip) | ||
} | ||
} catch (e: Exception) { | ||
error = true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
println("parsing v2ray config failed") | ||
error = true | ||
} | ||
|
||
if (error) { | ||
sendBroadcast(Intent("vpn_start_err")) | ||
return@thread | ||
} | ||
|
||
val builder = Builder() | ||
builder.setSession("vv") | ||
.setMtu(1500) | ||
.addAddress("10.233.233.233", 30) | ||
.addDnsServer("223.5.5.5") | ||
.addSearchDomain("local") | ||
.addRoute("0.0.0.0", 0) | ||
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { | ||
builder.setBlocking(true) | ||
} | ||
pfd = builder.establish() | ||
|
||
inputStream = FileInputStream(pfd?.fileDescriptor) | ||
outputStream = FileOutputStream(pfd?.fileDescriptor) | ||
|
||
val flow = Flow(outputStream) | ||
val service = Service(this) | ||
|
||
val files = filesDir.list() | ||
if (!files.contains("geoip.dat") || !files.contains("geosite.dat")) { | ||
val geoipBytes = resources.openRawResource(R.raw.geoip).readBytes() | ||
val fos = openFileOutput("geoip.dat", Context.MODE_PRIVATE) | ||
fos.write(geoipBytes) | ||
fos.close() | ||
|
||
val geositeBytes = resources.openRawResource(R.raw.geosite).readBytes() | ||
val fos2 = openFileOutput("geosite.dat", Context.MODE_PRIVATE) | ||
fos2.write(geositeBytes) | ||
fos2.close() | ||
} | ||
val serverDomains = proxyDomainIPMap.keys.joinToString(separator = ",") | ||
val serverIPs = proxyDomainIPMap.values.joinToString(separator = ",") | ||
tun2socks.Tun2socks.startV2Ray(flow, service, configString.toByteArray(), filesDir.absolutePath, serverDomains, serverIPs) | ||
|
||
sendBroadcast(Intent("vpn_started")) | ||
|
||
handlePackets() | ||
} | ||
|
||
return START_STICKY | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
unregisterReceiver(broadcastReceiver) | ||
} | ||
} |
Oops, something went wrong.