Skip to content

Commit ec30e3a

Browse files
committed
Add some scenes auto close connections
Support proxies search Update core Optimize more details
1 parent 76c9f08 commit ec30e3a

File tree

90 files changed

+1595
-1056
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+1595
-1056
lines changed

android/app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ android {
7373
applicationIdSuffix '.debug'
7474
}
7575
release {
76+
minifyEnabled true
77+
debuggable false
78+
7679
if (isRelease) {
7780
signingConfig signingConfigs.release
7881
} else {

android/app/src/main/kotlin/com/follow/clash/GlobalState.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import com.follow.clash.plugins.VpnPlugin
77
import io.flutter.FlutterInjector
88
import io.flutter.embedding.engine.FlutterEngine
99
import io.flutter.embedding.engine.dart.DartExecutor
10+
import kotlinx.coroutines.CoroutineScope
11+
import kotlinx.coroutines.Dispatchers
12+
import kotlinx.coroutines.launch
13+
import kotlinx.coroutines.withContext
1014
import java.util.concurrent.locks.ReentrantLock
1115
import kotlin.concurrent.withLock
1216

@@ -33,6 +37,15 @@ object GlobalState {
3337
return currentEngine?.plugins?.get(AppPlugin::class.java) as AppPlugin?
3438
}
3539

40+
fun syncStatus() {
41+
CoroutineScope(Dispatchers.Default).launch {
42+
val status = getCurrentVPNPlugin()?.getStatus() ?: false
43+
withContext(Dispatchers.Main){
44+
runState.value = if (status) RunState.START else RunState.STOP
45+
}
46+
}
47+
}
48+
3649
suspend fun getText(text: String): String {
3750
return getCurrentAppPlugin()?.getText(text) ?: ""
3851
}

android/app/src/main/kotlin/com/follow/clash/plugins/ServicePlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ data object ServicePlugin : FlutterPlugin, MethodChannel.MethodCallHandler {
5151
}
5252
}
5353

54+
5455
private fun handleDestroy() {
5556
GlobalState.destroyServiceEngine()
5657
}

android/app/src/main/kotlin/com/follow/clash/plugins/VpnPlugin.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ data object VpnPlugin : FlutterPlugin, MethodChannel.MethodCallHandler {
200200
timerJob = null
201201
}
202202

203+
204+
suspend fun getStatus(): Boolean? {
205+
return withContext(Dispatchers.Default) {
206+
flutterMethodChannel.awaitResult<Boolean>("status", null)
207+
}
208+
}
209+
203210
private fun handleStartService() {
204211
if (flClashService == null) {
205212
bindService()
@@ -248,9 +255,9 @@ data object VpnPlugin : FlutterPlugin, MethodChannel.MethodCallHandler {
248255
GlobalState.runLock.withLock {
249256
if (GlobalState.runState.value == RunState.STOP) return
250257
GlobalState.runState.value = RunState.STOP
258+
flClashService?.stop()
251259
stopForegroundJob()
252260
Core.stopTun()
253-
flClashService?.stop()
254261
GlobalState.handleTryDestroy()
255262
}
256263
}

android/app/src/main/kotlin/com/follow/clash/services/FlClashTileService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class FlClashTileService : TileService() {
3333

3434
override fun onStartListening() {
3535
super.onStartListening()
36+
GlobalState.syncStatus()
3637
GlobalState.runState.value?.let { updateTile(it) }
3738
GlobalState.runState.observeForever(observer)
3839
}

android/core/build.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.argumentsWithVarargAsSingleArray
2+
13
plugins {
24
id("com.android.library")
35
id("org.jetbrains.kotlin.android")
@@ -10,11 +12,11 @@ android {
1012

1113
defaultConfig {
1214
minSdk = 21
13-
consumerProguardFiles("consumer-rules.pro")
1415
}
1516

1617
buildTypes {
1718
release {
19+
isJniDebuggable = false
1820
proguardFiles(
1921
getDefaultProguardFile("proguard-android-optimize.txt"),
2022
"proguard-rules.pro"

android/core/consumer-rules.pro

Whitespace-only changes.

android/core/src/main/cpp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ project("core")
44

55
message("CMAKE_SOURCE_DIR ${CMAKE_SOURCE_DIR}")
66

7+
message("CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}")
8+
79
if (NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
810
add_compile_options(-O3)
911

android/core/src/main/cpp/core.cpp

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
#include <jni.h>
2-
31
#ifdef LIBCLASH
42
#include <jni.h>
5-
#include <string>
63
#include "jni_helper.h"
74
#include "libclash.h"
85

96
extern "C"
107
JNIEXPORT void JNICALL
11-
Java_com_follow_clash_core_Core_startTun(JNIEnv *env, jobject thiz, jint fd, jobject cb) {
12-
auto interface = new_global(cb);
8+
Java_com_follow_clash_core_Core_startTun(JNIEnv *env, jobject, const jint fd, jobject cb) {
9+
const auto interface = new_global(cb);
1310
startTUN(fd, interface);
1411
}
1512

1613
extern "C"
1714
JNIEXPORT void JNICALL
18-
Java_com_follow_clash_core_Core_stopTun(JNIEnv *env, jobject thiz) {
15+
Java_com_follow_clash_core_Core_stopTun(JNIEnv *) {
1916
stopTun();
2017
}
2118

@@ -26,50 +23,50 @@ static jmethodID m_tun_interface_resolve_process;
2623

2724
static void release_jni_object_impl(void *obj) {
2825
ATTACH_JNI();
29-
del_global((jobject) obj);
26+
del_global(static_cast<jobject>(obj));
3027
}
3128

32-
static void call_tun_interface_protect_impl(void *tun_interface, int fd) {
29+
static void call_tun_interface_protect_impl(void *tun_interface, const int fd) {
3330
ATTACH_JNI();
34-
env->CallVoidMethod((jobject) tun_interface,
35-
(jmethodID) m_tun_interface_protect,
36-
(jint) fd);
31+
env->CallVoidMethod(static_cast<jobject>(tun_interface),
32+
m_tun_interface_protect,
33+
fd);
3734
}
3835

39-
static const char*
36+
static const char *
4037
call_tun_interface_resolve_process_impl(void *tun_interface, int protocol,
41-
const char *source,
42-
const char *target,
43-
int uid) {
38+
const char *source,
39+
const char *target,
40+
const int uid) {
4441
ATTACH_JNI();
45-
jstring packageName = (jstring)env->CallObjectMethod((jobject) tun_interface,
46-
(jmethodID) m_tun_interface_resolve_process,
47-
(jint) protocol,
48-
(jstring) new_string(source),
49-
(jstring) new_string(target),
50-
(jint) uid);
42+
const auto packageName = reinterpret_cast<jstring>(env->CallObjectMethod(static_cast<jobject>(tun_interface),
43+
m_tun_interface_resolve_process,
44+
protocol,
45+
new_string(source),
46+
new_string(target),
47+
uid));
5148
return get_string(packageName);
5249
}
5350

5451
extern "C"
5552
JNIEXPORT jint JNICALL
56-
JNI_OnLoad(JavaVM *vm, void *reserved) {
53+
JNI_OnLoad(JavaVM *vm, void *) {
5754
JNIEnv *env = nullptr;
58-
if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
55+
if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) {
5956
return JNI_ERR;
6057
}
6158

6259
initialize_jni(vm, env);
6360

64-
jclass c_tun_interface = find_class("com/follow/clash/core/TunInterface");
61+
const auto c_tun_interface = find_class("com/follow/clash/core/TunInterface");
6562

6663
m_tun_interface_protect = find_method(c_tun_interface, "protect", "(I)V");
6764
m_tun_interface_resolve_process = find_method(c_tun_interface, "resolverProcess",
68-
"(ILjava/lang/String;Ljava/lang/String;I)Ljava/lang/String;");
65+
"(ILjava/lang/String;Ljava/lang/String;I)Ljava/lang/String;");
6966

7067
registerCallbacks(&call_tun_interface_protect_impl,
7168
&call_tun_interface_resolve_process_impl,
7269
&release_jni_object_impl);
7370
return JNI_VERSION_1_6;
7471
}
75-
#endif
72+
#endif

android/core/src/main/cpp/jni_helper.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "jni_helper.h"
22

3+
#include <cstdlib>
34
#include <malloc.h>
45
#include <cstring>
56

@@ -12,7 +13,7 @@ static jmethodID m_get_bytes;
1213
void initialize_jni(JavaVM *vm, JNIEnv *env) {
1314
global_vm = vm;
1415

15-
c_string = (jclass) new_global(find_class("java/lang/String"));
16+
c_string = reinterpret_cast<jclass>(new_global(find_class("java/lang/String")));
1617
m_new_string = find_method(c_string, "<init>", "([B)V");
1718
m_get_bytes = find_method(c_string, "getBytes", "()[B");
1819
}
@@ -22,33 +23,33 @@ JavaVM *global_java_vm() {
2223
}
2324

2425
char *jni_get_string(JNIEnv *env, jstring str) {
25-
auto array = (jbyteArray) env->CallObjectMethod(str, m_get_bytes);
26-
int length = env->GetArrayLength(array);
27-
char *content = (char *) malloc(length + 1);
28-
env->GetByteArrayRegion(array, 0, length, (jbyte *) content);
26+
const auto array = reinterpret_cast<jbyteArray>(env->CallObjectMethod(str, m_get_bytes));
27+
const int length = env->GetArrayLength(array);
28+
const auto content = static_cast<char *>(malloc(length + 1));
29+
env->GetByteArrayRegion(array, 0, length, reinterpret_cast<jbyte *>(content));
2930
content[length] = 0;
3031
return content;
3132
}
3233

3334
jstring jni_new_string(JNIEnv *env, const char *str) {
34-
auto length = (int) strlen(str);
35-
jbyteArray array = env->NewByteArray(length);
36-
env->SetByteArrayRegion(array, 0, length, (const jbyte *) str);
37-
return (jstring) env->NewObject(c_string, m_new_string, array);
35+
const auto length = static_cast<int>(strlen(str));
36+
const auto array = env->NewByteArray(length);
37+
env->SetByteArrayRegion(array, 0, length, reinterpret_cast<const jbyte *>(str));
38+
return reinterpret_cast<jstring>(env->NewObject(c_string, m_new_string, array));
3839
}
3940

4041
int jni_catch_exception(JNIEnv *env) {
41-
int result = env->ExceptionCheck();
42+
const int result = env->ExceptionCheck();
4243
if (result) {
4344
env->ExceptionDescribe();
4445
env->ExceptionClear();
4546
}
4647
return result;
4748
}
4849

49-
void jni_attach_thread(struct scoped_jni *jni) {
50+
void jni_attach_thread(scoped_jni *jni) {
5051
JavaVM *vm = global_java_vm();
51-
if (vm->GetEnv((void **) &jni->env, JNI_VERSION_1_6) == JNI_OK) {
52+
if (vm->GetEnv(reinterpret_cast<void **>(&jni->env), JNI_VERSION_1_6) == JNI_OK) {
5253
jni->require_release = 0;
5354
return;
5455
}
@@ -58,9 +59,9 @@ void jni_attach_thread(struct scoped_jni *jni) {
5859
jni->require_release = 1;
5960
}
6061

61-
void jni_detach_thread(struct scoped_jni *jni) {
62+
void jni_detach_thread(const scoped_jni *env) {
6263
JavaVM *vm = global_java_vm();
63-
if (jni->require_release) {
64+
if (env->require_release) {
6465
vm->DetachCurrentThread();
6566
}
6667
}

0 commit comments

Comments
 (0)