Skip to content

Commit

Permalink
Optimize ip detection
Browse files Browse the repository at this point in the history
Support android vpn ipv6 inbound switch

Support log export

Optimize more details
  • Loading branch information
chen08209 committed Sep 26, 2024
1 parent d522a64 commit 23e3baf
Show file tree
Hide file tree
Showing 55 changed files with 35,205 additions and 30,525 deletions.
15 changes: 7 additions & 8 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ def isRelease = defStoreFile.exists() && defStorePassword != null && defKeyAlias
android {
namespace "com.follow.clash"
compileSdkVersion 34
ndkVersion "25.1.8937393"
ndkVersion "27.1.12297006"

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = '1.8'
jvmTarget = '17'
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
signingConfigs {
if (isRelease){
if (isRelease) {
release {
storeFile defStoreFile
storePassword defStorePassword
Expand All @@ -74,10 +74,9 @@ android {
applicationIdSuffix '.debug'
}
release {
minifyEnabled true
if(isRelease){
if (isRelease) {
signingConfig signingConfigs.release
}else{
} else {
signingConfig signingConfigs.debug
}
proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
Expand Down
40 changes: 23 additions & 17 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,22 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"
tools:ignore="SystemPermissionTypo" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission
android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"
tools:ignore="SystemPermissionTypo" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />

<application
android:name="${applicationName}"
android:enableOnBackInvokedCallback="true"
android:extractNativeLibs="true"
android:icon="@mipmap/ic_launcher"
android:label="FlClash"
android:networkSecurityConfig="@xml/network_security_config"
tools:targetApi="tiramisu">
android:hardwareAccelerated="true"
android:label="FlClash">
<activity
android:name="com.follow.clash.MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
Expand Down Expand Up @@ -64,6 +61,16 @@

<data android:host="install-config" />
</intent-filter>
</activity>

<!-- <meta-data-->
<!-- android:name="io.flutter.embedding.android.EnableImpeller"-->
<!-- android:value="true" />-->

<activity
android:name=".TempActivity"
android:exported="true"
android:theme="@style/TransparentTheme">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="com.follow.clash.action.START" />
Expand All @@ -74,14 +81,6 @@
</intent-filter>
</activity>

<!-- <meta-data-->
<!-- android:name="io.flutter.embedding.android.EnableImpeller"-->
<!-- android:value="true" />-->

<activity
android:name=".TempActivity"
android:theme="@style/TransparentTheme" />

<service
android:name=".services.FlClashTileService"
android:exported="true"
Expand Down Expand Up @@ -127,12 +126,19 @@
<intent-filter>
<action android:name="android.net.VpnService" />
</intent-filter>
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="vpn" />
</service>

<service
android:name=".services.FlClashService"
android:exported="false"
android:foregroundServiceType="specialUse" />
android:foregroundServiceType="specialUse">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="service" />
</service>

<meta-data
android:name="flutterEmbedding"
Expand Down
13 changes: 0 additions & 13 deletions android/app/src/main/kotlin/com/follow/clash/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@ import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine

class MainActivity : FlutterActivity() {
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
when (intent.action) {
"com.follow.clash.action.START" -> {
GlobalState.getCurrentTilePlugin()?.handleStart()
}

"com.follow.clash.action.STOP" -> {
GlobalState.getCurrentTilePlugin()?.handleStop()
}
}
}

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
flutterEngine.plugins.add(AppPlugin())
Expand Down
9 changes: 9 additions & 0 deletions android/app/src/main/kotlin/com/follow/clash/TempActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import android.os.Bundle
class TempActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
when (intent.action) {
"com.follow.clash.action.START" -> {
GlobalState.getCurrentTilePlugin()?.handleStart()
}

"com.follow.clash.action.STOP" -> {
GlobalState.getCurrentTilePlugin()?.handleStop()
}
}
finishAndRemoveTask()
}
}
26 changes: 0 additions & 26 deletions android/app/src/main/kotlin/com/follow/clash/models/Dns.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ data class Props(
val accessControl: AccessControl?,
val allowBypass: Boolean?,
val systemProxy: Boolean?,
val ipv6: Boolean?,
)

data class TunProps(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.follow.clash.plugins

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -163,8 +162,7 @@ class VpnPlugin : FlutterPlugin, MethodChannel.MethodCallHandler {
}
}

@SuppressLint("ForegroundServiceType")
fun handleStartVpn() {
private fun handleStartVpn() {
GlobalState.getCurrentAppPlugin()?.requestVpnPermission(context) {
start()
}
Expand Down Expand Up @@ -228,7 +226,6 @@ class VpnPlugin : FlutterPlugin, MethodChannel.MethodCallHandler {
onUpdateNetwork()
}

@SuppressLint("ForegroundServiceType")
private fun startForeground(title: String, content: String) {
GlobalState.runLock.withLock {
if (GlobalState.runState.value != RunState.START) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import com.follow.clash.BaseServiceInterface
import com.follow.clash.MainActivity
import com.follow.clash.models.Props


@SuppressLint("WrongConstant")

class FlClashService : Service(), BaseServiceInterface {

private val binder = LocalBinder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.follow.clash.services

import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Intent
import android.os.Build
Expand Down Expand Up @@ -37,6 +38,7 @@ class FlClashTileService : TileService() {
GlobalState.runState.observeForever(observer)
}

@SuppressLint("StartActivityAndCollapseDeprecated")
private fun activityTransfer() {
val intent = Intent(this, TempActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import android.os.Build
import android.os.IBinder
import android.os.Parcel
import android.os.RemoteException
import android.util.Log
import androidx.core.app.NotificationCompat
import com.follow.clash.BaseServiceInterface
import com.follow.clash.GlobalState
import com.follow.clash.MainActivity
import com.follow.clash.R
import com.follow.clash.TempActivity
import com.follow.clash.models.AccessControlMode
import com.follow.clash.models.Props
import com.follow.clash.models.TunProps
Expand All @@ -29,7 +29,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


@SuppressLint("WrongConstant")
class FlClashVpnService : VpnService(), BaseServiceInterface {

companion object {
Expand Down Expand Up @@ -73,11 +72,19 @@ class FlClashVpnService : VpnService(), BaseServiceInterface {
override fun start(port: Int, props: Props?): TunProps {
return with(Builder()) {
addAddress(TUN_GATEWAY, TUN_SUBNET_PREFIX)
addAddress(TUN_GATEWAY6, TUN_SUBNET_PREFIX6)
addRoute(NET_ANY, 0)
addRoute(NET_ANY6, 0)
addDnsServer(TUN_DNS)
addDnsServer(TUN_DNS6)


if (props?.ipv6 == true) {
try {
addAddress(TUN_GATEWAY6, TUN_SUBNET_PREFIX6)
addRoute(NET_ANY6, 0)
addDnsServer(TUN_DNS6)
} catch (_: Exception) {

}
}
setMtu(TUN_MTU)
props?.accessControl?.let { accessControl ->
when (accessControl.mode) {
Expand Down Expand Up @@ -115,16 +122,16 @@ class FlClashVpnService : VpnService(), BaseServiceInterface {
fd = establish()?.detachFd()
?: throw NullPointerException("Establish VPN rejected by system"),
gateway = "$TUN_GATEWAY/$TUN_SUBNET_PREFIX",
gateway6 = "$TUN_GATEWAY6/$TUN_SUBNET_PREFIX6",
gateway6 = if (props?.ipv6 == true) "$TUN_GATEWAY6/$TUN_SUBNET_PREFIX6" else "",
portal = TUN_PORTAL,
portal6 = TUN_PORTAL6,
portal6 = if (props?.ipv6 == true) TUN_PORTAL6 else "",
dns = TUN_DNS,
dns6 = TUN_DNS6
dns6 = if (props?.ipv6 == true) TUN_DNS6 else ""
)
}
}

fun updateUnderlyingNetworks( networks: Array<Network>){
fun updateUnderlyingNetworks(networks: Array<Network>) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
this.setUnderlyingNetworks(networks)
}
Expand Down Expand Up @@ -160,18 +167,23 @@ class FlClashVpnService : VpnService(), BaseServiceInterface {
)
}

val stopIntent = Intent(this, TempActivity::class.java)
stopIntent.action = "com.follow.clash.action.STOP"
stopIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)


val stopPendingIntent = if (Build.VERSION.SDK_INT >= 31) {
PendingIntent.getActivity(
this,
0,
Intent("com.follow.clash.action.STOP"),
stopIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
} else {
PendingIntent.getActivity(
this,
0,
Intent("com.follow.clash.action.STOP"),
stopIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
}
Expand Down
Binary file modified assets/data/ASN.mmdb
Binary file not shown.
Binary file modified assets/data/GeoIP.dat
Binary file not shown.
Loading

0 comments on commit 23e3baf

Please sign in to comment.