123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- import 'dart:convert';
- import 'package:dio/dio.dart';
- import 'package:get/get.dart';
- import 'package:naiyouwl/app/bean/clash_core.dart';
- import 'package:naiyouwl/app/bean/connect.dart';
- import 'package:naiyouwl/app/bean/proxie.dart';
- import 'package:naiyouwl/app/bean/rule.dart';
- import 'package:naiyouwl/app/common/LogHelper.dart';
- import 'package:naiyouwl/app/controller/controllers.dart';
- import 'package:naiyouwl/app/controller/service.dart';
- import 'package:naiyouwl/app/utils/system_proxy.dart';
- import 'package:web_socket_channel/io.dart';
- import 'package:http/http.dart' as http;
- class CoreController extends GetxController {
- // late final dio = Dio(BaseOptions(
- // baseUrl: 'http://127.0.0.1:9799',
- // ));
- var ip = "127.0.0.1";
- var url = "";
- var client = http.Client();
- var headers = Map<String,String>();
- late final LogHelper _logger = LogHelper();
- var version = ClashCoreVersion(premium: true, version: '').obs;
- var address = ''.obs;
- var secret = ''.obs;
- var config = ClashCoreConfig(
- port: 0,
- socksPort: 0,
- redirPort: 0,
- tproxyPort: 0,
- mixedPort: 0,
- allowLan: false,
- bindAddress: '',
- mode: 'rule',
- logLevel: '',
- ipv6: false,
- ).obs;
- var ruleProvider = RuleProvider(providers: {}).obs;
- var rule = Rule(rules: []).obs;
- CoreController(){
- // dio.interceptors.add(LogInterceptor(
- // request: true,
- // requestBody: true,
- // responseBody: true,
- // error: true,
- // logPrint: _logger.d, // 使用 Logger 插件打印日志
- // ));
- //dio.interceptors.add(FriendlyErrorInterceptor());
- }
- SystemProxyConfig get proxyConfig {
- final mixedPort = config.value.mixedPort == 0 ? null : config.value.mixedPort;
- final httpPort = mixedPort ?? config.value.port;
- final httpsPort = mixedPort ?? config.value.port;
- final socksPort = mixedPort ?? config.value.socksPort;
- return SystemProxyConfig(
- http: httpPort == 0 ? null : '127.0.0.1:$httpPort',
- https: httpsPort == 0 ? null : '127.0.0.1:$httpsPort',
- socks: socksPort == 0 ? null : '127.0.0.1:$socksPort',
- );
- }
- setApi(String apiAddress, String apiSecret) {
- address.value = apiAddress;
- secret.value = apiSecret;
- // dio.close(force: true);
- url = "${address.value}";
- headers["Authorization"] = 'Bearer ${secret.value}';
- //client.head(Uri.http(url,'info'))
- // dio.options.baseUrl = 'http://${address.value}';
- // print("dio baseUrl api ${dio.options.baseUrl}");
- // dio.options.headers['Authorization'] = 'Bearer ${secret.value}';
- }
- Future<dynamic> fetchHello() async {
- try {
- var ut = Uri.parse('http://$url/info');
- final res = await client.get(ut,headers: headers);
- if(res.statusCode == 200) {
- var jsonResponse =
- jsonDecode(res.body) as Map<String, dynamic>;
- return jsonResponse;
- }
- } on http.ClientException catch (e) {
- // 处理客户端异常,例如没有网络连接
- print('Client fetchHello Exception: ${e.message}');
- return null;
- } on Exception catch (e) {
- // 处理其他类型的异常
- print('Exception: $e');
- return null;
- }
- }
- Future<ClashCoreVersion?> updateVersion() async {
- try {
- var ut = Uri.parse('http://$url/version');
- final res = await client.get(ut,headers: headers);
- if(res.statusCode == 200) {
- var jsonResponse =
- jsonDecode(res.body) as Map<String, dynamic>;
- version.value = ClashCoreVersion.fromJson(jsonResponse);
- return version.value;
- }
- } on http.ClientException catch (e) {
- // 处理客户端异常,例如没有网络连接
- print('Client updateVersion Exception: ${e.message}');
- return null;
- } on Exception catch (e) {
- // 处理其他类型的异常
- print('Exception: $e');
- return null;
- }
- return null;
- }
- Future<void> updateConfig() async {
- try {
- var ut = Uri.parse('http://$url/configs');
- final res = await client.get(ut,headers: headers);
- print("updateConfig ---- ${res.statusCode}");
- if(res.statusCode == 200) {
- var jsonResponse =
- jsonDecode(res.body) as Map<String, dynamic>;
- config.value = ClashCoreConfig.fromJson(jsonResponse);
- }
- } on http.ClientException catch (e) {
- // 处理客户端异常,例如没有网络连接
- print('Client updateConfig Exception: ${e.message}');
- } on Exception catch (e) {
- // 处理其他类型的异常
- print('Exception updateConfig: $e');
- }
- }
- Future<void> fetchConfigUpdate(Map<String, dynamic> config) async {
- try {
- var ut = Uri.parse('http://$url/configs');
- final res = await client.patch(ut,headers: headers);
- print("fetchConfigUpdate ---- ${res.statusCode}");
- // var jsonResponse =
- // jsonDecode(res.body) as Map<String, dynamic>;
- // print(jsonResponse);
- await updateConfig();
- } on http.ClientException catch (e) {
- // 处理客户端异常,例如没有网络连接
- print('Client fetchConfigUpdate Exception: ${e.message}');
- } on Exception catch (e) {
- // 处理其他类型的异常
- print('Exception fetchConfigUpdate: $e');
- }
- }
- Future<void> changeConfig(String configPath) async{
- for (var i = 0 ; i< 5; i++) {
- try {
- final body = json.encode({
- "path": configPath
- });
- var ut = Uri.parse('http://$url/configs');
- final res = await client.put(ut,body:body,headers: headers);
- print("changeConfig ---- ${res.statusCode}");
- if(res.statusCode == 204)
- {
- await updateConfig();
- break;
- }
- } on http.ClientException catch (e) {
- // 处理客户端异常,例如没有网络连接
- print('Client changeConfig Exception: ${e.message}');
- continue;
- } on Exception catch (e) {
- // 处理其他类型的异常
- print('Exception changeConfig : $e');
- continue;
- }
- }
- }
- // type updateConfigRequest struct {
- // Path string `json:"path"`
- // Payload string `json:"payload"`
- // }
- // https://github.com/Dreamacro/clash/blob/c231fd14666d6ea05d6a75eaba6db69f9eee5ae9/hub/route/configs.go#L95
- // Future<void> fetchReloadConfig(Map<String, String> config) async {
- // print("fetchReloadConfig $config");
- // await dio.put('/configs', data: config);
- // }
- Future<void> fetchCloseConnections(String id) async {
- //await dio.delete('/connections/${Uri.encodeComponent(id)}');
- try {
- var ut = Uri.parse('http://$url/connections/${Uri.encodeComponent(id)}');
- final res = await client.delete(ut,headers: headers);
- print("fetchCloseConnections ---- ${res.statusCode}");
- if(res.statusCode == 204){
- var jsonResponse =
- jsonDecode(res.body) as Map<String, dynamic>;
- print(jsonResponse);
- await updateConfig();
- }
- } on http.ClientException catch (e) {
- // 处理客户端异常,例如没有网络连接
- print('Client fetchCloseConnections Exception: ${e.message}');
- } on Exception catch (e) {
- // 处理其他类型的异常
- print('Exception: $e');
- }
- }
- IOWebSocketChannel fetchConnectionsWs() {
- return IOWebSocketChannel.connect(
- Uri.parse('ws://${address.value}/connections'),
- headers: headers,
- );
- }
- Future updateRuleProvider() async {
- // final res = await dio.get('/providers/rules');
- // ruleProvider.value = RuleProvider.fromJson(res.data);
- // ruleProvider.refresh();
- try {
- var ut = Uri.parse('http://$url/rpoviders/rules');
- final res = await client.get(ut,headers: headers);
- if(res.statusCode == 200){
- var jsonResponse =
- jsonDecode(res.body) as Map<String, dynamic>;
- ruleProvider.value = RuleProvider.fromJson(jsonResponse);
- ruleProvider.refresh();
- }
- } on http.ClientException catch (e) {
- // 处理客户端异常,例如没有网络连接
- print('Client updateRuleProvider Exception: ${e.message}');
- } on Exception catch (e) {
- // 处理其他类型的异常
- print('Exception: $e');
- }
- }
- Future updateRule() async {
- // final res = await dio.get('/rules');
- // rule.value = Rule.fromJson(res.data);
- // rule.refresh();
- try {
- var ut = Uri.parse('http://$url/rules');
- final res = await client.get(ut,headers: headers);
- if(res.statusCode == 200){
- var jsonResponse =
- jsonDecode(res.body) as Map<String, dynamic>;
- rule.value = Rule.fromJson(jsonResponse);
- rule.refresh();
- }
- } on http.ClientException catch (e) {
- // 处理客户端异常,例如没有网络连接
- print('Client updateRule Exception: ${e.message}');
- } on Exception catch (e) {
- // 处理其他类型的异常
- print('Exception: $e');
- }
- }
- // Future<void> fetchRuleProviderUpdate(String name) async {
- // await dio.put('/providers/rules/${Uri.encodeComponent(name)}');
- // }
- // Future<Proxie> fetchProxie() async {
- // final res = await dio.get('/proxies');
- // return Proxie.fromJson(res.data);
- // }
- // Future<ProxieProvider> fetchProxieProvider() async {
- // final res = await dio.get('/providers/proxies');
- // return ProxieProvider.fromJson(res.data);
- // }
- // Future<void> fetchProxieProviderHealthCheck(String provider) async {
- // await dio.get('/providers/proxies/${Uri.encodeComponent(provider)}/healthcheck');
- // }
- Future<void> fetchSetProxieGroup(String group, String value) async {
- // await dio.put('/proxies/${Uri.encodeComponent(group)}', data: {'name': value});
- try {
- final body = json.encode({
- 'name': value
- });
- var ut = Uri.parse('http://$url/proxies/$group');
- final ret = await client.put(ut,body:body,headers: headers);
- print("fetchSetProxieGroup ${ret.statusCode}");
- } on http.ClientException catch (e) {
- // 处理客户端异常,例如没有网络连接
- print('Client fetchSetProxieGroup Exception: ${e.message}');
- } on Exception catch (e) {
- // 处理其他类型的异常
- print('fetchSetProxieGroup Exception: $e');
- }
- }
- //
- // Future<void> fetchProxieProviderUpdate(String name) async {
- // await dio.put('/providers/proxies/${Uri.encodeComponent(name)}');
- // }
- // Future<int> fetchProxieDelay(String name) async {
- // final query = {'timeout': 5000, 'url': 'http://www.gstatic.com/generate_204'};
- // final res = await dio.get('/proxies/${Uri.encodeComponent(name)}/delay', queryParameters: query);
- // return res.data['delay'] ?? 0;
- // }
- //
- Future<Connect?> fetchConnection() async {
- // final res = await dio.get('/connections');
- // if(res.data !=null){
- // // LogHelper().d("没有连接");
- // //return Connect();
- // }
- try {
- var ut = Uri.parse('http://$url/connections');
- final res = await client.get(ut,headers: headers);
- if(res.statusCode == 200){
- var jsonResponse =
- jsonDecode(res.body) as Map<String, dynamic>;
- return Connect.fromJson(jsonResponse);
- }
- } on http.ClientException catch (e) {
- // 处理客户端异常,例如没有网络连接
- print('Client fetchConnection Exception: ${e.message}');
- return null;
- } on Exception catch (e) {
- // 处理其他类型的异常
- print('fetchConnection Exception: $e');
- return null;
- }
- }
- }
|