Skip to content

Commit 2fbc417

Browse files
committed
Add some scenes auto close connections
Support proxies query Optimize more details
1 parent 76c9f08 commit 2fbc417

Some content is hidden

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

63 files changed

+1097
-690
lines changed

android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ android {
7373
applicationIdSuffix '.debug'
7474
}
7575
release {
76+
debuggable false
7677
if (isRelease) {
7778
signingConfig signingConfigs.release
7879
} else {

android/core/build.gradle.kts

Lines changed: 3 additions & 0 deletions
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")
@@ -15,6 +17,7 @@ android {
1517

1618
buildTypes {
1719
release {
20+
isJniDebuggable = false
1821
proguardFiles(
1922
getDefaultProguardFile("proguard-android-optimize.txt"),
2023
"proguard-rules.pro"

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
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#pragma once
22

33
#include <jni.h>
4-
#include <cstdint>
5-
#include <cstdlib>
6-
#include <malloc.h>
74

85
struct scoped_jni {
96
JNIEnv *env;
@@ -18,14 +15,14 @@ extern char *jni_get_string(JNIEnv *env, jstring str);
1815

1916
extern int jni_catch_exception(JNIEnv *env);
2017

21-
extern void jni_attach_thread(struct scoped_jni *jni);
18+
extern void jni_attach_thread(scoped_jni *jni);
2219

23-
extern void jni_detach_thread(struct scoped_jni *env);
20+
extern void jni_detach_thread(const scoped_jni *env);
2421

2522
extern void release_string(char **str);
2623

2724
#define ATTACH_JNI() __attribute__((unused, cleanup(jni_detach_thread))) \
28-
struct scoped_jni _jni; \
25+
scoped_jni _jni{}; \
2926
jni_attach_thread(&_jni); \
3027
JNIEnv *env = _jni.env
3128

@@ -36,4 +33,4 @@ extern void release_string(char **str);
3633
#define new_global(obj) env->NewGlobalRef(obj)
3734
#define del_global(obj) env->DeleteGlobalRef(obj)
3835
#define get_string(jstr) jni_get_string(env, jstr)
39-
#define new_string(cstr) jni_new_string(env, cstr)
36+
#define new_string(cstr) jni_new_string(env, cstr)

android/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ org.gradle.jvmargs=-Xmx4G
22
android.useAndroidX=true
33
android.enableJetifier=true
44
kotlin_version=1.9.22
5-
agp_version=8.9.1
5+
agp_version=8.9.2

core/hub.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func handleStartListener() bool {
5353
defer runLock.Unlock()
5454
isRunning = true
5555
updateListeners(true)
56+
closeConnections()
5657
return true
5758
}
5859

@@ -276,6 +277,16 @@ func handleCloseConnections() bool {
276277
return true
277278
}
278279

280+
func closeConnections() {
281+
statistic.DefaultManager.Range(func(c statistic.Tracker) bool {
282+
err := c.Close()
283+
if err != nil {
284+
return false
285+
}
286+
return true
287+
})
288+
}
289+
279290
func handleCloseConnection(connectionId string) bool {
280291
runLock.Lock()
281292
defer runLock.Unlock()

lib/application.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ class ApplicationState extends ConsumerState<Application> {
100100
return AppStateManager(
101101
child: ClashManager(
102102
child: ConnectivityManager(
103-
onConnectivityChanged: () {
103+
onConnectivityChanged: () async {
104+
await clashCore.closeConnections();
104105
globalState.appController.updateLocalIp();
105106
globalState.appController.addCheckIpNumDebounce();
106107
},

lib/common/common.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ export 'window.dart';
3636
export 'windows.dart';
3737
export 'render.dart';
3838
export 'mixin.dart';
39-
export 'print.dart';
39+
export 'print.dart';

0 commit comments

Comments
 (0)