Skip to content

Commit 4e31954

Browse files
committed
Fix fallback issues
Optimize change proxy
1 parent 25a3f03 commit 4e31954

File tree

14 files changed

+125
-85
lines changed

14 files changed

+125
-85
lines changed

core/hub.go

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"github.com/metacubex/mihomo/adapter"
88
"github.com/metacubex/mihomo/adapter/outboundgroup"
9-
"github.com/metacubex/mihomo/adapter/provider"
109
"github.com/metacubex/mihomo/common/observable"
1110
"github.com/metacubex/mihomo/common/utils"
1211
"github.com/metacubex/mihomo/component/updater"
@@ -108,36 +107,43 @@ func handleGetProxies() string {
108107
return string(data)
109108
}
110109

111-
func handleChangeProxy(data string) bool {
110+
func handleChangeProxy(data string, fn func(string string)) {
112111
runLock.Lock()
113-
defer runLock.Unlock()
114-
var params = &ChangeProxyParams{}
115-
err := json.Unmarshal([]byte(data), params)
116-
if err != nil {
117-
log.Infoln("Unmarshal ChangeProxyParams %v", err)
118-
}
119-
groupName := *params.GroupName
120-
proxyName := *params.ProxyName
121-
proxies := tunnel.ProxiesWithProviders()
122-
group, ok := proxies[groupName]
123-
if !ok {
124-
return false
125-
}
126-
adapterProxy := group.(*adapter.Proxy)
127-
selector, ok := adapterProxy.ProxyAdapter.(outboundgroup.SelectAble)
128-
if !ok {
129-
return false
130-
}
131-
if proxyName == "" {
132-
selector.ForceSet(proxyName)
133-
} else {
134-
err = selector.Set(proxyName)
135-
}
136-
if err == nil {
137-
log.Infoln("[SelectAble] %s selected %s", groupName, proxyName)
138-
return false
139-
}
140-
return true
112+
go func() {
113+
defer runLock.Unlock()
114+
var params = &ChangeProxyParams{}
115+
err := json.Unmarshal([]byte(data), params)
116+
if err != nil {
117+
fn(err.Error())
118+
return
119+
}
120+
groupName := *params.GroupName
121+
proxyName := *params.ProxyName
122+
proxies := tunnel.ProxiesWithProviders()
123+
group, ok := proxies[groupName]
124+
if !ok {
125+
fn("Not found group")
126+
return
127+
}
128+
adapterProxy := group.(*adapter.Proxy)
129+
selector, ok := adapterProxy.ProxyAdapter.(outboundgroup.SelectAble)
130+
if !ok {
131+
fn("Group is not selectable")
132+
return
133+
}
134+
if proxyName == "" {
135+
selector.ForceSet(proxyName)
136+
} else {
137+
err = selector.Set(proxyName)
138+
}
139+
if err != nil {
140+
fn(err.Error())
141+
return
142+
}
143+
144+
fn("")
145+
return
146+
}()
141147
}
142148

143149
func handleGetTraffic(onlyProxy bool) string {
@@ -339,8 +345,6 @@ func handleUpdateGeoData(geoType string, geoName string, fn func(value string))
339345

340346
func handleUpdateExternalProvider(providerName string, fn func(value string)) {
341347
go func() {
342-
runLock.Lock()
343-
defer runLock.Unlock()
344348
externalProvider, exist := externalProviders[providerName]
345349
if !exist {
346350
fn("external provider is not exist")
@@ -401,7 +405,7 @@ func handleStopLog() {
401405
}
402406

403407
func init() {
404-
provider.HealthcheckHook = func(name string, delay uint16) {
408+
adapter.UrlTestHook = func(name string, delay uint16) {
405409
delayData := &Delay{
406410
Name: name,
407411
}

core/lib.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,12 @@ func getProxies() *C.char {
7979
}
8080

8181
//export changeProxy
82-
func changeProxy(s *C.char) bool {
82+
func changeProxy(s *C.char, port C.longlong) {
83+
i := int64(port)
8384
paramsString := C.GoString(s)
84-
return handleChangeProxy(paramsString)
85+
handleChangeProxy(paramsString, func(value string) {
86+
bridge.SendToPort(i, value)
87+
})
8588
}
8689

8790
//export getTraffic

core/server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ func handleAction(action *Action) {
7373
return
7474
case changeProxyMethod:
7575
data := action.Data.(string)
76-
action.callback(handleChangeProxy(data))
76+
handleChangeProxy(data, func(value string) {
77+
action.callback(value)
78+
})
7779
return
7880
case getTrafficMethod:
7981
data := action.Data.(bool)

lib/clash/core.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class ClashCore {
113113
});
114114
}
115115

116-
FutureOr<bool> changeProxy(ChangeProxyParams changeProxyParams) async {
116+
FutureOr<String> changeProxy(ChangeProxyParams changeProxyParams) async {
117117
return await clashInterface.changeProxy(changeProxyParams);
118118
}
119119

lib/clash/generated/clash_ffi.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,19 +2501,22 @@ class ClashFFI {
25012501
late final _getProxies =
25022502
_getProxiesPtr.asFunction<ffi.Pointer<ffi.Char> Function()>();
25032503

2504-
int changeProxy(
2504+
void changeProxy(
25052505
ffi.Pointer<ffi.Char> s,
2506+
int port,
25062507
) {
25072508
return _changeProxy(
25082509
s,
2510+
port,
25092511
);
25102512
}
25112513

2512-
late final _changeProxyPtr =
2513-
_lookup<ffi.NativeFunction<GoUint8 Function(ffi.Pointer<ffi.Char>)>>(
2514-
'changeProxy');
2514+
late final _changeProxyPtr = _lookup<
2515+
ffi.NativeFunction<
2516+
ffi.Void Function(
2517+
ffi.Pointer<ffi.Char>, ffi.LongLong)>>('changeProxy');
25152518
late final _changeProxy =
2516-
_changeProxyPtr.asFunction<int Function(ffi.Pointer<ffi.Char>)>();
2519+
_changeProxyPtr.asFunction<void Function(ffi.Pointer<ffi.Char>, int)>();
25172520

25182521
ffi.Pointer<ffi.Char> getTraffic(
25192522
int port,

lib/clash/interface.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mixin ClashInterface {
1919

2020
FutureOr<String> getProxies();
2121

22-
FutureOr<bool> changeProxy(ChangeProxyParams changeProxyParams);
22+
FutureOr<String> changeProxy(ChangeProxyParams changeProxyParams);
2323

2424
Future<bool> startListener();
2525

lib/clash/lib.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,23 @@ class ClashLib with ClashInterface {
194194
}
195195

196196
@override
197-
changeProxy(ChangeProxyParams changeProxyParams) {
197+
Future<String> changeProxy(ChangeProxyParams changeProxyParams) {
198+
final completer = Completer<String>();
199+
final receiver = ReceivePort();
200+
receiver.listen((message) {
201+
if (!completer.isCompleted) {
202+
completer.complete(message);
203+
receiver.close();
204+
}
205+
});
198206
final params = json.encode(changeProxyParams);
199207
final paramsChar = params.toNativeUtf8().cast<Char>();
200-
final res = clashFFI.changeProxy(paramsChar);
208+
clashFFI.changeProxy(
209+
paramsChar,
210+
receiver.sendPort.nativePort,
211+
);
201212
malloc.free(paramsChar);
202-
return res == 1;
213+
return completer.future;
203214
}
204215

205216
@override

lib/clash/service.dart

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ class ClashService with ClashInterface {
123123
case ActionMethod.resetTraffic:
124124
case ActionMethod.closeConnections:
125125
case ActionMethod.closeConnection:
126-
case ActionMethod.changeProxy:
127126
case ActionMethod.stopListener:
128127
completer?.complete(action.data as bool);
129128
return;
129+
case ActionMethod.changeProxy:
130130
case ActionMethod.getProxies:
131131
case ActionMethod.getTraffic:
132132
case ActionMethod.getTotalTraffic:
@@ -255,8 +255,8 @@ class ClashService with ClashInterface {
255255
}
256256

257257
@override
258-
FutureOr<bool> changeProxy(ChangeProxyParams changeProxyParams) {
259-
return _invoke<bool>(
258+
FutureOr<String> changeProxy(ChangeProxyParams changeProxyParams) {
259+
return _invoke<String>(
260260
method: ActionMethod.changeProxy,
261261
data: json.encode(changeProxyParams),
262262
);
@@ -284,10 +284,12 @@ class ClashService with ClashInterface {
284284
}) {
285285
return _invoke<String>(
286286
method: ActionMethod.updateGeoData,
287-
data: {
288-
"geoType": geoType,
289-
"geoName": geoName,
290-
},
287+
data: json.encode(
288+
{
289+
"geoType": geoType,
290+
"geoName": geoName,
291+
},
292+
),
291293
);
292294
}
293295

@@ -298,10 +300,10 @@ class ClashService with ClashInterface {
298300
}) {
299301
return _invoke<String>(
300302
method: ActionMethod.sideLoadExternalProvider,
301-
data: {
303+
data: json.encode({
302304
"providerName": providerName,
303305
"data": data,
304-
},
306+
}),
305307
);
306308
}
307309

lib/common/lock.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ class SingleInstanceLock {
1414
}
1515

1616
Future<bool> acquire() async {
17-
final lockFilePath = await appPath.getLockFilePath();
18-
final lockFile = File(lockFilePath);
19-
await lockFile.create();
2017
try {
18+
final lockFilePath = await appPath.getLockFilePath();
19+
final lockFile = File(lockFilePath);
20+
await lockFile.create();
2121
_accessFile = await lockFile.open(mode: FileMode.write);
22-
_accessFile?.lock();
22+
await _accessFile?.lock();
2323
return true;
2424
} catch (_) {
2525
return false;

0 commit comments

Comments
 (0)