Skip to content

Commit 01b50e1

Browse files
committed
Optimize performance
Fix some issues
1 parent 96328f6 commit 01b50e1

File tree

117 files changed

+12409
-8356
lines changed

Some content is hidden

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

117 files changed

+12409
-8356
lines changed

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@
66
path = plugins/flutter_distributor
77
url = git@github.com:chen08209/flutter_distributor.git
88
branch = FlClash
9+
[submodule "plugins/tray_manager"]
10+
path = plugins/tray_manager
11+
url = git@github.com:chen08209/tray_manager.git
12+
branch = main
13+
14+

analysis_options.yaml

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
1-
# This file configures the analyzer, which statically analyzes Dart code to
2-
# check for errors, warnings, and lints.
3-
#
4-
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
5-
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
6-
# invoked from the command line by running `flutter analyze`.
7-
8-
# The following line activates a set of recommended lints for Flutter apps,
9-
# packages, and plugins designed to encourage good coding practices.
101
include: package:flutter_lints/flutter.yaml
112

123
linter:
13-
# The lint rules applied to this project can be customized in the
14-
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
15-
# included above or to enable additional rules. A list of all available lints
16-
# and their documentation is published at
17-
# https://dart-lang.github.io/linter/lints/index.html.
18-
#
19-
# Instead of disabling a lint rule for the entire project in the
20-
# section below, it can also be suppressed for a single line of code
21-
# or a specific dart file by using the `// ignore: name_of_lint` and
22-
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
23-
# producing the lint.
244
rules:
25-
# avoid_print: false # Uncomment to disable the `avoid_print` rule
26-
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
275

28-
# Additional information about this file can be found at
29-
# https://dart.dev/guides/language/analysis-options
6+
analyzer:
7+
plugins:
8+
- custom_lint

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.net.NetworkCapabilities
1010
import android.net.NetworkRequest
1111
import android.os.Build
1212
import android.os.IBinder
13+
import android.util.Log
1314
import androidx.core.content.getSystemService
1415
import com.follow.clash.FlClashApplication
1516
import com.follow.clash.GlobalState
@@ -92,11 +93,13 @@ data object VpnPlugin : FlutterPlugin, MethodChannel.MethodCallHandler {
9293

9394
"setProtect" -> {
9495
val fd = call.argument<Int>("fd")
95-
if (fd != null) {
96-
if (flClashService is FlClashVpnService) {
96+
if (fd != null && flClashService is FlClashVpnService) {
97+
try {
9798
(flClashService as FlClashVpnService).protect(fd)
99+
result.success(true)
100+
} catch (e: RuntimeException) {
101+
result.success(false)
98102
}
99-
result.success(true)
100103
} else {
101104
result.success(false)
102105
}

build.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ targets:
55
options:
66
build_extensions:
77
'^lib/models/{{}}.dart': 'lib/models/generated/{{}}.g.dart'
8+
'^lib/providers/{{}}.dart': 'lib/providers/generated/{{}}.g.dart'
89
freezed:
910
options:
1011
build_extensions:

core/action.go

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,146 +22,156 @@ func (result ActionResult) Json() ([]byte, error) {
2222
return data, err
2323
}
2424

25-
func (action Action) wrapMessage(data interface{}) []byte {
26-
sendAction := ActionResult{
25+
func (action Action) getResult(data interface{}) []byte {
26+
resultAction := ActionResult{
2727
Id: action.Id,
2828
Method: action.Method,
2929
Data: data,
3030
}
31-
res, _ := sendAction.Json()
31+
res, _ := resultAction.Json()
3232
return res
3333
}
3434

35-
func handleAction(action *Action, send func([]byte)) {
35+
func handleAction(action *Action, result func(data interface{})) {
3636
switch action.Method {
3737
case initClashMethod:
3838
data := action.Data.(string)
39-
send(action.wrapMessage(handleInitClash(data)))
39+
result(handleInitClash(data))
4040
return
4141
case getIsInitMethod:
42-
send(action.wrapMessage(handleGetIsInit()))
42+
result(handleGetIsInit())
4343
return
4444
case forceGcMethod:
4545
handleForceGc()
46-
send(action.wrapMessage(true))
46+
result(true)
4747
return
4848
case shutdownMethod:
49-
send(action.wrapMessage(handleShutdown()))
49+
result(handleShutdown())
5050
return
5151
case validateConfigMethod:
5252
data := []byte(action.Data.(string))
53-
send(action.wrapMessage(handleValidateConfig(data)))
53+
result(handleValidateConfig(data))
5454
return
5555
case updateConfigMethod:
5656
data := []byte(action.Data.(string))
57-
send(action.wrapMessage(handleUpdateConfig(data)))
57+
result(handleUpdateConfig(data))
5858
return
5959
case getProxiesMethod:
60-
send(action.wrapMessage(handleGetProxies()))
60+
result(handleGetProxies())
6161
return
6262
case changeProxyMethod:
6363
data := action.Data.(string)
6464
handleChangeProxy(data, func(value string) {
65-
send(action.wrapMessage(value))
65+
result(value)
6666
})
6767
return
6868
case getTrafficMethod:
69-
send(action.wrapMessage(handleGetTraffic()))
69+
result(handleGetTraffic())
7070
return
7171
case getTotalTrafficMethod:
72-
send(action.wrapMessage(handleGetTotalTraffic()))
72+
result(handleGetTotalTraffic())
7373
return
7474
case resetTrafficMethod:
7575
handleResetTraffic()
76-
send(action.wrapMessage(true))
76+
result(true)
7777
return
7878
case asyncTestDelayMethod:
7979
data := action.Data.(string)
8080
handleAsyncTestDelay(data, func(value string) {
81-
send(action.wrapMessage(value))
81+
result(value)
8282
})
8383
return
8484
case getConnectionsMethod:
85-
send(action.wrapMessage(handleGetConnections()))
85+
result(handleGetConnections())
8686
return
8787
case closeConnectionsMethod:
88-
send(action.wrapMessage(handleCloseConnections()))
88+
result(handleCloseConnections())
8989
return
9090
case closeConnectionMethod:
9191
id := action.Data.(string)
92-
send(action.wrapMessage(handleCloseConnection(id)))
92+
result(handleCloseConnection(id))
9393
return
9494
case getExternalProvidersMethod:
95-
send(action.wrapMessage(handleGetExternalProviders()))
95+
result(handleGetExternalProviders())
9696
return
9797
case getExternalProviderMethod:
9898
externalProviderName := action.Data.(string)
99-
send(action.wrapMessage(handleGetExternalProvider(externalProviderName)))
99+
result(handleGetExternalProvider(externalProviderName))
100100
case updateGeoDataMethod:
101101
paramsString := action.Data.(string)
102102
var params = map[string]string{}
103103
err := json.Unmarshal([]byte(paramsString), &params)
104104
if err != nil {
105-
send(action.wrapMessage(err.Error()))
105+
result(err.Error())
106106
return
107107
}
108108
geoType := params["geo-type"]
109109
geoName := params["geo-name"]
110110
handleUpdateGeoData(geoType, geoName, func(value string) {
111-
send(action.wrapMessage(value))
111+
result(value)
112112
})
113113
return
114114
case updateExternalProviderMethod:
115115
providerName := action.Data.(string)
116116
handleUpdateExternalProvider(providerName, func(value string) {
117-
send(action.wrapMessage(value))
117+
result(value)
118118
})
119119
return
120120
case sideLoadExternalProviderMethod:
121121
paramsString := action.Data.(string)
122122
var params = map[string]string{}
123123
err := json.Unmarshal([]byte(paramsString), &params)
124124
if err != nil {
125-
send(action.wrapMessage(err.Error()))
125+
result(err.Error())
126126
return
127127
}
128128
providerName := params["providerName"]
129129
data := params["data"]
130130
handleSideLoadExternalProvider(providerName, []byte(data), func(value string) {
131-
send(action.wrapMessage(value))
131+
result(value)
132132
})
133133
return
134134
case startLogMethod:
135135
handleStartLog()
136-
send(action.wrapMessage(true))
136+
result(true)
137137
return
138138
case stopLogMethod:
139139
handleStopLog()
140-
send(action.wrapMessage(true))
140+
result(true)
141141
return
142142
case startListenerMethod:
143-
send(action.wrapMessage(handleStartListener()))
143+
result(handleStartListener())
144144
return
145145
case stopListenerMethod:
146-
send(action.wrapMessage(handleStopListener()))
146+
result(handleStopListener())
147147
return
148148
case getCountryCodeMethod:
149149
ip := action.Data.(string)
150150
handleGetCountryCode(ip, func(value string) {
151-
send(action.wrapMessage(value))
151+
result(value)
152152
})
153153
return
154154
case getMemoryMethod:
155155
handleGetMemory(func(value string) {
156-
send(action.wrapMessage(value))
156+
result(value)
157157
})
158158
return
159+
case getProfileMethod:
160+
profileId := action.Data.(string)
161+
handleGetMemory(func(value string) {
162+
result(handleGetProfile(profileId))
163+
})
164+
return
165+
case setStateMethod:
166+
data := action.Data.(string)
167+
handleSetState(data)
168+
result(true)
159169
default:
160-
handle := nextHandle(action, send)
170+
handle := nextHandle(action, result)
161171
if handle {
162172
return
163173
} else {
164-
send(action.wrapMessage(action.DefaultValue))
174+
result(action.DefaultValue)
165175
}
166176
}
167177
}

core/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ func overwriteConfig(targetConfig *config.RawConfig, patchConfig config.RawConfi
215215
targetConfig.Tun.Device = patchConfig.Tun.Device
216216
targetConfig.Tun.DNSHijack = patchConfig.Tun.DNSHijack
217217
targetConfig.Tun.Stack = patchConfig.Tun.Stack
218+
targetConfig.Tun.RouteAddress = patchConfig.Tun.RouteAddress
218219
targetConfig.GeodataLoader = patchConfig.GeodataLoader
219220
targetConfig.Profile.StoreSelected = false
220221
targetConfig.GeoXUrl = patchConfig.GeoXUrl

core/constant.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import (
88
)
99

1010
type ConfigExtendedParams struct {
11-
IsPatch bool `json:"is-patch"`
12-
IsCompatible bool `json:"is-compatible"`
13-
SelectedMap map[string]string `json:"selected-map"`
14-
TestURL *string `json:"test-url"`
15-
OverrideDns bool `json:"override-dns"`
16-
OnlyStatisticsProxy bool `json:"only-statistics-proxy"`
11+
IsPatch bool `json:"is-patch"`
12+
IsCompatible bool `json:"is-compatible"`
13+
SelectedMap map[string]string `json:"selected-map"`
14+
TestURL *string `json:"test-url"`
15+
OverrideDns bool `json:"override-dns"`
1716
}
1817

1918
type GenerateConfigParams struct {
@@ -80,6 +79,7 @@ const (
8079
getAndroidVpnOptionsMethod Method = "getAndroidVpnOptions"
8180
getRunTimeMethod Method = "getRunTime"
8281
getCurrentProfileNameMethod Method = "getCurrentProfileName"
82+
getProfileMethod Method = "getProfile"
8383
)
8484

8585
type Method string

core/hub.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"core/state"
56
"encoding/json"
67
"fmt"
78
"github.com/metacubex/mihomo/adapter"
@@ -26,10 +27,8 @@ import (
2627
)
2728

2829
var (
29-
isInit = false
30-
configParams = ConfigExtendedParams{
31-
OnlyStatisticsProxy: false,
32-
}
30+
isInit = false
31+
configParams = ConfigExtendedParams{}
3332
externalProviders = map[string]cp.Provider{}
3433
logSubscriber observable.Subscription[log.Event]
3534
currentConfig *config.Config
@@ -152,7 +151,7 @@ func handleChangeProxy(data string, fn func(string string)) {
152151
}
153152

154153
func handleGetTraffic() string {
155-
up, down := statistic.DefaultManager.Current(configParams.OnlyStatisticsProxy)
154+
up, down := statistic.DefaultManager.Current(state.CurrentState.OnlyStatisticsProxy)
156155
traffic := map[string]int64{
157156
"up": up,
158157
"down": down,
@@ -166,7 +165,7 @@ func handleGetTraffic() string {
166165
}
167166

168167
func handleGetTotalTraffic() string {
169-
up, down := statistic.DefaultManager.Total(configParams.OnlyStatisticsProxy)
168+
up, down := statistic.DefaultManager.Total(state.CurrentState.OnlyStatisticsProxy)
170169
traffic := map[string]int64{
171170
"up": up,
172171
"down": down,
@@ -179,6 +178,15 @@ func handleGetTotalTraffic() string {
179178
return string(data)
180179
}
181180

181+
func handleGetProfile(profileId string) string {
182+
prof := getRawConfigWithId(profileId)
183+
data, err := json.Marshal(prof)
184+
if err != nil {
185+
return ""
186+
}
187+
return string(data)
188+
}
189+
182190
func handleResetTraffic() {
183191
statistic.DefaultManager.ResetStatistic()
184192
}
@@ -220,6 +228,7 @@ func handleAsyncTestDelay(paramsString string, fn func(string)) {
220228
if params.TestUrl != "" {
221229
testUrl = params.TestUrl
222230
}
231+
delayData.Url = testUrl
223232

224233
delay, err := proxy.URLTest(ctx, testUrl, expectedStatus)
225234
if err != nil || delay == 0 {
@@ -423,6 +432,10 @@ func handleGetMemory(fn func(value string)) {
423432
}()
424433
}
425434

435+
func handleSetState(params string) {
436+
_ = json.Unmarshal([]byte(params), state.CurrentState)
437+
}
438+
426439
func init() {
427440
adapter.UrlTestHook = func(url string, name string, delay uint16) {
428441
delayData := &Delay{

core/lib.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func invokeAction(paramsChar *C.char, port C.longlong) {
4949
bridge.SendToPort(i, err.Error())
5050
return
5151
}
52-
go handleAction(action, func(bytes []byte) {
53-
bridge.SendToPort(i, string(bytes))
52+
go handleAction(action, func(data interface{}) {
53+
bridge.SendToPort(i, string(action.getResult(data)))
5454
})
5555
}
5656

@@ -64,7 +64,7 @@ func sendMessage(message Message) {
6464
}
6565
bridge.SendToPort(messagePort, string(Action{
6666
Method: messageMethod,
67-
}.wrapMessage(res)))
67+
}.getResult(res)))
6868
}
6969

7070
//export startListener

0 commit comments

Comments
 (0)