2023-08-02 22:25:54 +08:00
|
|
|
import 'dart:async';
|
2022-07-25 16:26:51 +08:00
|
|
|
import 'dart:convert';
|
2023-07-28 17:53:02 +08:00
|
|
|
import 'dart:io';
|
2022-07-25 16:26:51 +08:00
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hbb/models/model.dart';
|
2022-10-08 15:53:03 +08: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';
|
|
|
|
|
2023-07-26 19:53:57 +08:00
|
|
|
final syncAbOption = 'sync-ab-with-recent-sessions';
|
|
|
|
bool shouldSyncAb() {
|
|
|
|
return bind.mainGetLocalOption(key: syncAbOption).isNotEmpty;
|
|
|
|
}
|
|
|
|
|
|
|
|
final sortAbTagsOption = 'sync-ab-tags';
|
|
|
|
bool shouldSortTags() {
|
|
|
|
return bind.mainGetLocalOption(key: sortAbTagsOption).isNotEmpty;
|
|
|
|
}
|
|
|
|
|
2022-10-08 16:13:24 +08: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;
|
2023-07-26 19:53:57 +08:00
|
|
|
final sortTags = shouldSortTags().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;
|
2023-07-26 20:43:18 +08:00
|
|
|
var licensedDevices = 0;
|
2023-08-02 22:25:54 +08:00
|
|
|
var sync_all_from_recent = true;
|
|
|
|
var _timerCounter = 0;
|
2022-07-25 16:26:51 +08:00
|
|
|
|
|
|
|
WeakReference<FFI> parent;
|
|
|
|
|
2023-08-02 22:25:54 +08:00
|
|
|
AbModel(this.parent) {
|
|
|
|
if (desktopType == DesktopType.main) {
|
|
|
|
Timer.periodic(Duration(milliseconds: 500), (timer) async {
|
|
|
|
if (_timerCounter++ % 6 == 0) syncFromRecent();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 16:26:51 +08:00
|
|
|
|
2023-06-23 15:10:10 +08:00
|
|
|
Future<void> pullAb({force = true, quiet = false}) async {
|
2023-08-02 22:25:54 +08:00
|
|
|
debugPrint("pullAb, force:$force, quite:$quiet");
|
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 = "";
|
|
|
|
}
|
2023-07-31 17:13:39 +08:00
|
|
|
final api = "${await bind.mainGetApiServer()}/api/ab";
|
2023-08-10 17:16:53 +08:00
|
|
|
int? statusCode;
|
2022-07-26 17:14:52 +08:00
|
|
|
try {
|
2023-03-27 23:02:46 +08:00
|
|
|
var authHeaders = getHttpHeaders();
|
|
|
|
authHeaders['Content-Type'] = "application/json";
|
2023-07-28 17:53:02 +08:00
|
|
|
authHeaders['Accept-Encoding'] = "gzip";
|
|
|
|
final resp = await http.get(Uri.parse(api), headers: authHeaders);
|
2023-08-10 17:16:53 +08:00
|
|
|
statusCode = resp.statusCode;
|
2022-09-15 11:06:44 +08:00
|
|
|
if (resp.body.isNotEmpty && resp.body.toLowerCase() != "null") {
|
2023-08-10 17:16:53 +08:00
|
|
|
Map<String, dynamic> json;
|
|
|
|
try {
|
|
|
|
json = jsonDecode(utf8.decode(resp.bodyBytes));
|
|
|
|
} catch (e) {
|
|
|
|
if (resp.statusCode != 200) {
|
|
|
|
BotToast.showText(
|
|
|
|
contentColor: Colors.red, text: 'HTTP ${resp.statusCode}');
|
|
|
|
}
|
|
|
|
rethrow;
|
|
|
|
}
|
2022-09-15 11:06:44 +08:00
|
|
|
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')) {
|
2023-07-26 20:43:18 +08:00
|
|
|
try {
|
|
|
|
gFFI.abModel.licensedDevices = json['licensed_devices'];
|
|
|
|
// ignore: empty_catches
|
|
|
|
} catch (e) {}
|
2022-09-15 11:06:44 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2023-08-10 17:16:53 +08:00
|
|
|
save(); // save on success
|
2022-10-08 15:53:03 +08:00
|
|
|
}
|
2022-09-15 11:06:44 +08:00
|
|
|
}
|
2022-07-26 17:14:52 +08:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2022-10-08 16:13:24 +08:00
|
|
|
abError.value = err.toString();
|
2022-07-26 17:14:52 +08:00
|
|
|
} finally {
|
2022-10-08 16:13:24 +08:00
|
|
|
abLoading.value = false;
|
2023-06-23 15:10:10 +08:00
|
|
|
initialized = true;
|
2023-08-02 22:25:54 +08:00
|
|
|
sync_all_from_recent = true;
|
|
|
|
_timerCounter = 0;
|
2023-08-10 17:16:53 +08:00
|
|
|
if (abError.isNotEmpty) {
|
|
|
|
if (statusCode == 401) {
|
|
|
|
gFFI.userModel.reset(clearAbCache: true);
|
|
|
|
} else {
|
|
|
|
reset(clearCache: false);
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 16:26:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-10 17:16:53 +08:00
|
|
|
Future<void> reset({bool clearCache = false}) async {
|
2022-12-11 21:40:35 +08:00
|
|
|
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;
|
2023-08-10 17:16:53 +08:00
|
|
|
if (clearCache) await bind.mainClearAb();
|
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);
|
|
|
|
}
|
|
|
|
|
2023-07-26 20:43:18 +08:00
|
|
|
bool isFull(bool warn) {
|
|
|
|
final res = licensedDevices > 0 && peers.length >= licensedDevices;
|
|
|
|
if (res && warn) {
|
|
|
|
BotToast.showText(
|
|
|
|
contentColor: Colors.red, text: translate("exceed_max_devices"));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-11-27 12:16:45 +08:00
|
|
|
void addPeer(Peer peer) {
|
|
|
|
peers.removeWhere((e) => e.id == peer.id);
|
|
|
|
peers.add(peer);
|
2022-07-26 17:03:19 +08:00
|
|
|
}
|
|
|
|
|
2023-08-03 16:48:14 +08:00
|
|
|
void addPeers(List<Peer> ps) {
|
|
|
|
for (var p in ps) {
|
|
|
|
addPeer(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 15:53:03 +08:00
|
|
|
final it = peers.where((element) => element.id == id);
|
2022-07-26 17:03:19 +08:00
|
|
|
if (it.isEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-08 15:53:03 +08:00
|
|
|
it.first.tags = tags;
|
2022-07-26 17:03:19 +08:00
|
|
|
}
|
|
|
|
|
2023-08-03 16:48:14 +08:00
|
|
|
void changeTagForPeers(List<String> ids, List<dynamic> tags) {
|
|
|
|
peers.map((e) {
|
|
|
|
if (ids.contains(e.id)) {
|
|
|
|
e.tags = tags;
|
|
|
|
}
|
|
|
|
}).toList();
|
|
|
|
}
|
|
|
|
|
2022-10-08 16:13:24 +08:00
|
|
|
Future<void> pushAb() async {
|
2023-08-02 22:25:54 +08:00
|
|
|
debugPrint("pushAb");
|
2022-10-08 15:53:03 +08:00
|
|
|
final api = "${await bind.mainGetApiServer()}/api/ab";
|
2023-01-06 18:26:19 +08:00
|
|
|
var authHeaders = getHttpHeaders();
|
2022-07-26 17:03:19 +08:00
|
|
|
authHeaders['Content-Type'] = "application/json";
|
2023-08-02 22:25:54 +08:00
|
|
|
final peersJsonData = peers.map((e) => e.toAbUploadJson()).toList();
|
2022-07-26 17:03:19 +08:00
|
|
|
final body = jsonEncode({
|
2022-10-08 15:53:03 +08:00
|
|
|
"data": jsonEncode({"tags": tags, "peers": peersJsonData})
|
2022-07-26 17:03:19 +08:00
|
|
|
});
|
2023-07-28 17:53:02 +08:00
|
|
|
var request = http.Request('POST', Uri.parse(api));
|
2023-07-30 12:16:00 +08:00
|
|
|
// support compression
|
|
|
|
if (licensedDevices > 0 && body.length > 1024) {
|
2023-07-28 17:53:02 +08:00
|
|
|
authHeaders['Content-Encoding'] = "gzip";
|
|
|
|
request.bodyBytes = GZipCodec().encode(utf8.encode(body));
|
|
|
|
} else {
|
|
|
|
request.body = body;
|
|
|
|
}
|
|
|
|
request.headers.addAll(authHeaders);
|
2022-09-19 10:22:40 +08:00
|
|
|
try {
|
2023-07-28 17:53:02 +08:00
|
|
|
await http.Client().send(request);
|
2023-08-02 22:25:54 +08:00
|
|
|
// await pullAb(quiet: true);
|
2023-08-10 17:16:53 +08:00
|
|
|
save(); // save on success
|
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());
|
2023-08-02 22:25:54 +08:00
|
|
|
} finally {
|
|
|
|
sync_all_from_recent = true;
|
|
|
|
_timerCounter = 0;
|
|
|
|
}
|
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 15:53:03 +08: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 15:53:03 +08:00
|
|
|
peers.removeWhere((element) => element.id == id);
|
2022-07-26 17:03:19 +08:00
|
|
|
}
|
|
|
|
|
2023-08-03 16:48:14 +08:00
|
|
|
void deletePeers(List<String> ids) {
|
|
|
|
peers.removeWhere((e) => ids.contains(e.id));
|
|
|
|
}
|
|
|
|
|
2022-07-26 17:03:19 +08:00
|
|
|
void deleteTag(String tag) {
|
2022-10-08 18:56:04 +08: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 15:53:03 +08:00
|
|
|
if (peer.tags.isEmpty) {
|
2022-07-26 17:03:19 +08:00
|
|
|
continue;
|
|
|
|
}
|
2022-10-08 15:53:03 +08:00
|
|
|
if (peer.tags.contains(tag)) {
|
|
|
|
((peer.tags)).remove(tag);
|
2022-07-26 17:03:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-10 10:08:33 +08:00
|
|
|
void renameTag(String oldTag, String newTag) {
|
|
|
|
if (tags.contains(newTag)) return;
|
|
|
|
tags.value = tags.map((e) {
|
|
|
|
if (e == oldTag) {
|
|
|
|
return newTag;
|
|
|
|
} else {
|
2023-08-10 11:12:58 +08:00
|
|
|
return e;
|
2023-08-10 10:08:33 +08:00
|
|
|
}
|
|
|
|
}).toList();
|
|
|
|
selectedTags.value = selectedTags.map((e) {
|
|
|
|
if (e == oldTag) {
|
|
|
|
return newTag;
|
|
|
|
} else {
|
2023-08-10 11:12:58 +08:00
|
|
|
return e;
|
2023-08-10 10:08:33 +08:00
|
|
|
}
|
|
|
|
}).toList();
|
|
|
|
for (var peer in peers) {
|
|
|
|
peer.tags = peer.tags.map((e) {
|
|
|
|
if (e == oldTag) {
|
|
|
|
return newTag;
|
|
|
|
} else {
|
2023-08-10 11:12:58 +08:00
|
|
|
return e;
|
2023-08-10 10:08:33 +08:00
|
|
|
}
|
|
|
|
}).toList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 17:03:19 +08:00
|
|
|
void unsetSelectedTags() {
|
|
|
|
selectedTags.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<dynamic> getPeerTags(String id) {
|
2022-10-08 15:53:03 +08: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 15:53:03 +08:00
|
|
|
return it.first.tags;
|
2022-07-26 17:03:19 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-27 14:29:47 +08:00
|
|
|
|
2023-08-02 22:25:54 +08:00
|
|
|
void syncFromRecent() async {
|
|
|
|
Peer merge(Peer r, Peer p) {
|
|
|
|
return Peer(
|
|
|
|
id: p.id,
|
|
|
|
hash: r.hash.isEmpty ? p.hash : r.hash,
|
|
|
|
username: r.username.isEmpty ? p.username : r.username,
|
|
|
|
hostname: r.hostname.isEmpty ? p.hostname : r.hostname,
|
|
|
|
platform: r.platform.isEmpty ? p.platform : r.platform,
|
|
|
|
alias: r.alias,
|
|
|
|
tags: p.tags,
|
|
|
|
forceAlwaysRelay: r.forceAlwaysRelay,
|
|
|
|
rdpPort: r.rdpPort,
|
|
|
|
rdpUsername: r.rdpUsername);
|
2022-11-28 18:16:29 +08:00
|
|
|
}
|
|
|
|
|
2023-08-02 22:25:54 +08:00
|
|
|
bool shouldSync(Peer a, Peer b) {
|
|
|
|
return a.hash != b.hash ||
|
|
|
|
a.username != b.username ||
|
|
|
|
a.platform != b.platform ||
|
|
|
|
a.hostname != b.hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Peer>> getRecentPeers() async {
|
|
|
|
try {
|
|
|
|
if (peers.isEmpty) [];
|
|
|
|
List<String> filteredPeerIDs;
|
|
|
|
if (sync_all_from_recent) {
|
|
|
|
sync_all_from_recent = false;
|
|
|
|
filteredPeerIDs = peers.map((e) => e.id).toList();
|
|
|
|
} else {
|
|
|
|
final new_stored_str = await bind.mainGetNewStoredPeers();
|
|
|
|
if (new_stored_str.isEmpty) return [];
|
|
|
|
List<String> new_stores =
|
|
|
|
(jsonDecode(new_stored_str) as List<dynamic>)
|
|
|
|
.map((e) => e.toString())
|
|
|
|
.toList();
|
|
|
|
final abPeerIds = peers.map((e) => e.id).toList();
|
|
|
|
filteredPeerIDs =
|
|
|
|
new_stores.where((e) => abPeerIds.contains(e)).toList();
|
|
|
|
}
|
|
|
|
if (filteredPeerIDs.isEmpty) return [];
|
|
|
|
final loadStr = await bind.mainLoadRecentPeersForAb(
|
|
|
|
filter: jsonEncode(filteredPeerIDs));
|
|
|
|
if (loadStr.isEmpty) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
List<dynamic> mapPeers = jsonDecode(loadStr);
|
|
|
|
List<Peer> recents = List.empty(growable: true);
|
|
|
|
for (var m in mapPeers) {
|
|
|
|
if (m is Map<String, dynamic>) {
|
|
|
|
recents.add(Peer.fromJson(m));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return recents;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint('getRecentPeers:$e');
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!shouldSyncAb()) return;
|
|
|
|
final oldPeers = peers.toList();
|
|
|
|
final recents = await getRecentPeers();
|
|
|
|
if (recents.isEmpty) return;
|
|
|
|
for (var i = 0; i < peers.length; i++) {
|
|
|
|
var p = peers[i];
|
|
|
|
var r = recents.firstWhereOrNull((r) => p.id == r.id);
|
|
|
|
if (r != null) {
|
|
|
|
peers[i] = merge(r, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool changed = false;
|
|
|
|
for (var i = 0; i < peers.length; i++) {
|
|
|
|
final o = oldPeers[i];
|
|
|
|
final p = peers[i];
|
|
|
|
if (shouldSync(o, p)) {
|
|
|
|
changed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Be careful with loop calls
|
|
|
|
if (changed) {
|
|
|
|
pushAb();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint('syncFromRecent:$e');
|
2022-11-28 18:16:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 22:25:54 +08:00
|
|
|
save() {
|
|
|
|
try {
|
|
|
|
final infos = peers
|
|
|
|
.map((e) => (<String, dynamic>{
|
|
|
|
"id": e.id,
|
|
|
|
"hash": e.hash,
|
|
|
|
}))
|
|
|
|
.toList();
|
|
|
|
final m = <String, dynamic>{
|
|
|
|
"access_token": bind.mainGetLocalOption(key: 'access_token'),
|
|
|
|
"peers": infos,
|
|
|
|
};
|
|
|
|
bind.mainSaveAb(json: jsonEncode(m));
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint('ab save:$e');
|
2022-07-29 12:03:24 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-25 16:26:51 +08:00
|
|
|
}
|