| 
									
										
										
										
											2022-07-25 16:26:51 +08:00
										 |  |  | import 'dart:convert'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import 'package:flutter/material.dart'; | 
					
						
							|  |  |  | import 'package:flutter_hbb/models/model.dart'; | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  | import 'package:flutter_hbb/models/peer_model.dart'; | 
					
						
							| 
									
										
										
										
											2022-08-03 22:03:31 +08:00
										 |  |  | import 'package:flutter_hbb/models/platform_model.dart'; | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  | import 'package:get/get.dart'; | 
					
						
							| 
									
										
										
										
											2023-06-23 15:10:10 +08:00
										 |  |  | import 'package:bot_toast/bot_toast.dart'; | 
					
						
							| 
									
										
										
										
											2022-07-25 16:26:51 +08:00
										 |  |  | import 'package:http/http.dart' as http; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 17:52:36 +08:00
										 |  |  | import '../common.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-08 17:13:24 +09:00
										 |  |  | class AbModel { | 
					
						
							| 
									
										
										
										
											2023-06-21 16:08:45 +08:00
										 |  |  |   final abLoading = false.obs; | 
					
						
							|  |  |  |   final abError = "".obs; | 
					
						
							|  |  |  |   final tags = [].obs; | 
					
						
							|  |  |  |   final peers = List<Peer>.empty(growable: true).obs; | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 16:08:45 +08:00
										 |  |  |   final selectedTags = List<String>.empty(growable: true).obs; | 
					
						
							| 
									
										
										
										
											2023-06-23 15:10:10 +08:00
										 |  |  |   var initialized = false; | 
					
						
							| 
									
										
										
										
											2022-07-25 16:26:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   WeakReference<FFI> parent; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   AbModel(this.parent); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-23 15:10:10 +08:00
										 |  |  |   Future<void> pullAb({force = true, quiet = false}) async { | 
					
						
							| 
									
										
										
										
											2022-12-11 21:40:35 +08:00
										 |  |  |     if (gFFI.userModel.userName.isEmpty) return; | 
					
						
							| 
									
										
										
										
											2023-06-23 15:10:10 +08:00
										 |  |  |     if (abLoading.value) return; | 
					
						
							|  |  |  |     if (!force && initialized) return; | 
					
						
							|  |  |  |     if (!quiet) { | 
					
						
							|  |  |  |       abLoading.value = true; | 
					
						
							|  |  |  |       abError.value = ""; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |     final api = "${await bind.mainGetApiServer()}/api/ab/get"; | 
					
						
							| 
									
										
										
										
											2022-07-26 17:14:52 +08:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2023-03-27 17:02:46 +02:00
										 |  |  |       var authHeaders = getHttpHeaders(); | 
					
						
							|  |  |  |       authHeaders['Content-Type'] = "application/json"; | 
					
						
							|  |  |  |       final resp = await http.post(Uri.parse(api), headers: authHeaders); | 
					
						
							| 
									
										
										
										
											2022-09-15 11:06:44 +08:00
										 |  |  |       if (resp.body.isNotEmpty && resp.body.toLowerCase() != "null") { | 
					
						
							|  |  |  |         Map<String, dynamic> json = jsonDecode(resp.body); | 
					
						
							|  |  |  |         if (json.containsKey('error')) { | 
					
						
							| 
									
										
										
										
											2022-11-30 11:13:02 +08:00
										 |  |  |           abError.value = json['error']; | 
					
						
							| 
									
										
										
										
											2022-09-15 11:06:44 +08:00
										 |  |  |         } else if (json.containsKey('data')) { | 
					
						
							|  |  |  |           final data = jsonDecode(json['data']); | 
					
						
							| 
									
										
										
										
											2022-11-30 11:13:02 +08:00
										 |  |  |           if (data != null) { | 
					
						
							|  |  |  |             tags.clear(); | 
					
						
							|  |  |  |             peers.clear(); | 
					
						
							|  |  |  |             if (data['tags'] is List) { | 
					
						
							|  |  |  |               tags.value = data['tags']; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (data['peers'] is List) { | 
					
						
							|  |  |  |               for (final peer in data['peers']) { | 
					
						
							|  |  |  |                 peers.add(Peer.fromJson(peer)); | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2022-09-15 11:06:44 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-07-26 17:14:52 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } catch (err) { | 
					
						
							| 
									
										
										
										
											2022-10-08 17:13:24 +09:00
										 |  |  |       abError.value = err.toString(); | 
					
						
							| 
									
										
										
										
											2022-07-26 17:14:52 +08:00
										 |  |  |     } finally { | 
					
						
							| 
									
										
										
										
											2022-10-08 17:13:24 +09:00
										 |  |  |       abLoading.value = false; | 
					
						
							| 
									
										
										
										
											2023-06-23 15:10:10 +08:00
										 |  |  |       initialized = true; | 
					
						
							| 
									
										
										
										
											2022-07-25 16:26:51 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-11 21:40:35 +08:00
										 |  |  |   Future<void> reset() async { | 
					
						
							|  |  |  |     await bind.mainSetLocalOption(key: "selected-tags", value: ''); | 
					
						
							| 
									
										
										
										
											2022-07-25 16:26:51 +08:00
										 |  |  |     tags.clear(); | 
					
						
							|  |  |  |     peers.clear(); | 
					
						
							| 
									
										
										
										
											2023-06-23 15:10:10 +08:00
										 |  |  |     initialized = false; | 
					
						
							| 
									
										
										
										
											2022-07-25 16:26:51 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-27 12:16:45 +08:00
										 |  |  |   void addId(String id, String alias, List<dynamic> tags) { | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |     if (idContainBy(id)) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-11-27 12:16:45 +08:00
										 |  |  |     final peer = Peer.fromJson({ | 
					
						
							|  |  |  |       'id': id, | 
					
						
							|  |  |  |       'alias': alias, | 
					
						
							|  |  |  |       'tags': tags, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     peers.add(peer); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void addPeer(Peer peer) { | 
					
						
							|  |  |  |     peers.removeWhere((e) => e.id == peer.id); | 
					
						
							|  |  |  |     peers.add(peer); | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void addTag(String tag) async { | 
					
						
							|  |  |  |     if (tagContainBy(tag)) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     tags.add(tag); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void changeTagForPeer(String id, List<dynamic> tags) { | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |     final it = peers.where((element) => element.id == id); | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |     if (it.isEmpty) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |     it.first.tags = tags; | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-08 17:13:24 +09:00
										 |  |  |   Future<void> pushAb() async { | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |     final api = "${await bind.mainGetApiServer()}/api/ab"; | 
					
						
							| 
									
										
										
										
											2023-01-06 19:26:19 +09:00
										 |  |  |     var authHeaders = getHttpHeaders(); | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |     authHeaders['Content-Type'] = "application/json"; | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |     final peersJsonData = peers.map((e) => e.toJson()).toList(); | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |     final body = jsonEncode({ | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |       "data": jsonEncode({"tags": tags, "peers": peersJsonData}) | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2022-09-19 10:22:40 +08:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2023-06-23 15:10:10 +08:00
										 |  |  |       await http.post(Uri.parse(api), headers: authHeaders, body: body); | 
					
						
							|  |  |  |       await pullAb(quiet: true); | 
					
						
							| 
									
										
										
										
											2022-09-19 10:22:40 +08:00
										 |  |  |     } catch (e) { | 
					
						
							| 
									
										
										
										
											2023-06-23 15:10:10 +08:00
										 |  |  |       BotToast.showText(contentColor: Colors.red, text: e.toString()); | 
					
						
							|  |  |  |     } finally {} | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 18:16:29 +08:00
										 |  |  |   Peer? find(String id) { | 
					
						
							|  |  |  |     return peers.firstWhereOrNull((e) => e.id == id); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |   bool idContainBy(String id) { | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |     return peers.where((element) => element.id == id).isNotEmpty; | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bool tagContainBy(String tag) { | 
					
						
							|  |  |  |     return tags.where((element) => element == tag).isNotEmpty; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void deletePeer(String id) { | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |     peers.removeWhere((element) => element.id == id); | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void deleteTag(String tag) { | 
					
						
							| 
									
										
										
										
											2022-10-08 19:56:04 +09:00
										 |  |  |     gFFI.abModel.selectedTags.remove(tag); | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |     tags.removeWhere((element) => element == tag); | 
					
						
							|  |  |  |     for (var peer in peers) { | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |       if (peer.tags.isEmpty) { | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |         continue; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |       if (peer.tags.contains(tag)) { | 
					
						
							|  |  |  |         ((peer.tags)).remove(tag); | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void unsetSelectedTags() { | 
					
						
							|  |  |  |     selectedTags.clear(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   List<dynamic> getPeerTags(String id) { | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |     final it = peers.where((p0) => p0.id == id); | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |     if (it.isEmpty) { | 
					
						
							|  |  |  |       return []; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |       return it.first.tags; | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-07-27 14:29:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 18:16:29 +08:00
										 |  |  |   Future<void> setPeerAlias(String id, String value) async { | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |     final it = peers.where((p0) => p0.id == id); | 
					
						
							| 
									
										
										
										
											2022-11-28 18:16:29 +08:00
										 |  |  |     if (it.isNotEmpty) { | 
					
						
							| 
									
										
										
										
											2022-10-08 16:53:03 +09:00
										 |  |  |       it.first.alias = value; | 
					
						
							| 
									
										
										
										
											2022-11-28 18:16:29 +08:00
										 |  |  |       await pushAb(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<void> setPeerForceAlwaysRelay(String id, bool value) async { | 
					
						
							|  |  |  |     final it = peers.where((p0) => p0.id == id); | 
					
						
							|  |  |  |     if (it.isNotEmpty) { | 
					
						
							|  |  |  |       it.first.forceAlwaysRelay = value; | 
					
						
							|  |  |  |       await pushAb(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<void> setRdp(String id, String port, String username) async { | 
					
						
							|  |  |  |     final it = peers.where((p0) => p0.id == id); | 
					
						
							|  |  |  |     if (it.isNotEmpty) { | 
					
						
							|  |  |  |       it.first.rdpPort = port; | 
					
						
							|  |  |  |       it.first.rdpUsername = username; | 
					
						
							|  |  |  |       await pushAb(); | 
					
						
							| 
									
										
										
										
											2022-07-29 12:03:24 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-07-25 16:26:51 +08:00
										 |  |  | } |