Compare commits

...

2 Commits

Author SHA1 Message Date
chen08209
513ee9ff7a Fix quickStart change proxy error 2024-06-05 17:59:21 +08:00
chen08209
ddad3e40b9 cache 2024-06-05 16:19:23 +08:00
6 changed files with 111 additions and 23 deletions

0
lib/common/ip.dart Normal file
View File

View File

@@ -3,6 +3,7 @@ import 'dart:io';
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/models/ip.dart';
import 'package:fl_clash/state.dart';
class Request {
@@ -26,7 +27,7 @@ class Request {
));
}
_syncProxy(){
_syncProxy() {
final port = globalState.appController.clashConfig.mixedPort;
if (_port != port) {
_port = port;
@@ -45,14 +46,14 @@ class Request {
Future<Response> getFileResponseForUrl(String url) async {
final response = await _dio
.get(
url,
options: Options(
responseType: ResponseType.bytes,
),
)
url,
options: Options(
responseType: ResponseType.bytes,
),
)
.timeout(
httpTimeoutDuration,
);
httpTimeoutDuration,
);
return response;
}
@@ -73,6 +74,29 @@ class Request {
if (!hasUpdate) return null;
return data;
}
final Map<String, IpInfo Function(Map<String, dynamic>)> _ipInfoSources = {
"https://ipwho.is/": IpInfo.fromIpwhoIsJson,
"https://api.ip.sb/geoip/": IpInfo.fromIpSbJson,
"https://ipapi.co/json/": IpInfo.fromIpApiCoJson,
"https://ipinfo.io/json/": IpInfo.fromIpInfoIoJson,
};
Future<IpInfo> checkIp() async {
for (final source in _ipInfoSources.entries) {
try {
final response = await _dio.get<Map<String, dynamic>>(
source.key,
);
if (response.statusCode == 200 && response.data != null) {
return source.value(response.data!);
}
} catch (e) {
continue;
}
}
throw "无法检索ip";
}
}
final request = Request();

65
lib/models/ip.dart Normal file
View File

@@ -0,0 +1,65 @@
class IpInfo {
final String ip;
final String countryCode;
const IpInfo({
required this.ip,
required this.countryCode,
});
static IpInfo fromIpInfoIoJson(Map<String, dynamic> json) {
return switch (json) {
{
"ip": final String ip,
"country": final String country,
} =>
IpInfo(
ip: ip,
countryCode: country,
),
_ => throw const FormatException("invalid json"),
};
}
static IpInfo fromIpApiCoJson(Map<String, dynamic> json) {
return switch (json) {
{
"ip": final String ip,
"country_code": final String countryCode,
} =>
IpInfo(
ip: ip,
countryCode: countryCode,
),
_ => throw const FormatException("invalid json"),
};
}
static IpInfo fromIpSbJson(Map<String, dynamic> json) {
return switch (json) {
{
"ip": final String ip,
"country_code": final String countryCode,
} =>
IpInfo(
ip: ip,
countryCode: countryCode,
),
_ => throw const FormatException("invalid json"),
};
}
static IpInfo fromIpwhoIsJson(Map<String, dynamic> json) {
return switch (json) {
{
"ip": final String ip,
"country_code": final String countryCode,
} =>
IpInfo(
ip: ip,
countryCode: countryCode,
),
_ => throw const FormatException("invalid json"),
};
}
}

View File

@@ -12,4 +12,5 @@ export 'package.dart';
export 'ffi.dart';
export 'selector.dart';
export 'navigation.dart';
export 'dav.dart';
export 'dav.dart';
export 'ip.dart';

View File

@@ -121,19 +121,17 @@ class GlobalState {
required Config config,
required ClashConfig clashConfig,
}) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (config.profiles.isEmpty) {
stopSystemProxy();
return;
}
config.currentSelectedMap.forEach((key, value) {
clashCore.changeProxy(
ChangeProxyParams(
groupName: key,
proxyName: value,
),
);
});
if (config.profiles.isEmpty) {
stopSystemProxy();
return;
}
config.currentSelectedMap.forEach((key, value) {
clashCore.changeProxy(
ChangeProxyParams(
groupName: key,
proxyName: value,
),
);
});
}

View File

@@ -1,7 +1,7 @@
name: fl_clash
description: A multi-platform proxy client based on ClashMeta, simple and easy to use, open-source and ad-free.
publish_to: 'none'
version: 0.8.10
version: 0.8.11
environment:
sdk: '>=3.1.0 <4.0.0'