diff --git a/lib/common/ip.dart b/lib/common/ip.dart new file mode 100644 index 0000000..e69de29 diff --git a/lib/common/request.dart b/lib/common/request.dart index 619e95e..00aedcf 100644 --- a/lib/common/request.dart +++ b/lib/common/request.dart @@ -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 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)> _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 checkIp() async { + for (final source in _ipInfoSources.entries) { + try { + final response = await _dio.get>( + source.key, + ); + if (response.statusCode == 200 && response.data != null) { + return source.value(response.data!); + } + } catch (e) { + continue; + } + } + throw "无法检索ip"; + } } final request = Request(); diff --git a/lib/models/ip.dart b/lib/models/ip.dart new file mode 100644 index 0000000..19a7877 --- /dev/null +++ b/lib/models/ip.dart @@ -0,0 +1,65 @@ +class IpInfo { + final String ip; + final String countryCode; + + const IpInfo({ + required this.ip, + required this.countryCode, + }); + + static IpInfo fromIpInfoIoJson(Map 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 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 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 json) { + return switch (json) { + { + "ip": final String ip, + "country_code": final String countryCode, + } => + IpInfo( + ip: ip, + countryCode: countryCode, + ), + _ => throw const FormatException("invalid json"), + }; + } +} diff --git a/lib/models/models.dart b/lib/models/models.dart index cd91889..370aa3d 100644 --- a/lib/models/models.dart +++ b/lib/models/models.dart @@ -12,4 +12,5 @@ export 'package.dart'; export 'ffi.dart'; export 'selector.dart'; export 'navigation.dart'; -export 'dav.dart'; \ No newline at end of file +export 'dav.dart'; +export 'ip.dart'; \ No newline at end of file