Files

291 lines
7.6 KiB
Dart
Raw Permalink Normal View History

2022-08-02 13:10:09 +08:00
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import 'platform_model.dart';
2023-08-16 08:59:50 +08:00
// ignore: depend_on_referenced_packages
import 'package:collection/collection.dart';
class Peer {
final String id;
2024-03-20 15:05:54 +08:00
String hash; // personal ab hash password
String password; // shared ab password
String username; // pc username
2023-08-17 21:17:31 +08:00
String hostname;
String platform;
2022-10-08 16:53:03 +09:00
String alias;
List<dynamic> tags;
2022-11-28 18:16:29 +08:00
bool forceAlwaysRelay = false;
String rdpPort;
String rdpUsername;
bool online = false;
String loginName; //login username
2025-02-15 12:13:11 +08:00
String device_group_name;
2025-10-12 14:59:42 +08:00
String note;
2024-03-20 15:05:54 +08:00
bool? sameServer;
String getId() {
if (alias != '') {
return alias;
}
return id;
}
2022-09-21 21:20:19 +08:00
Peer.fromJson(Map<String, dynamic> json)
: id = json['id'] ?? '',
2023-08-02 22:25:54 +08:00
hash = json['hash'] ?? '',
2024-03-20 15:05:54 +08:00
password = json['password'] ?? '',
2022-09-21 21:20:19 +08:00
username = json['username'] ?? '',
hostname = json['hostname'] ?? '',
platform = json['platform'] ?? '',
2022-09-21 21:20:19 +08:00
alias = json['alias'] ?? '',
2022-11-28 18:16:29 +08:00
tags = json['tags'] ?? [],
forceAlwaysRelay = json['forceAlwaysRelay'] == 'true',
rdpPort = json['rdpPort'] ?? '',
rdpUsername = json['rdpUsername'] ?? '',
2024-03-20 15:05:54 +08:00
loginName = json['loginName'] ?? '',
2025-02-15 12:13:11 +08:00
device_group_name = json['device_group_name'] ?? '',
2025-10-12 14:59:42 +08:00
note = json['note'] is String ? json['note'] : '',
2024-03-20 15:05:54 +08:00
sameServer = json['same_server'];
2022-10-08 16:53:03 +09:00
Map<String, dynamic> toJson() {
return <String, dynamic>{
"id": id,
2023-08-02 22:25:54 +08:00
"hash": hash,
2024-03-20 15:05:54 +08:00
"password": password,
2022-10-08 16:53:03 +09:00
"username": username,
"hostname": hostname,
"platform": platform,
"alias": alias,
"tags": tags,
2022-11-28 18:16:29 +08:00
"forceAlwaysRelay": forceAlwaysRelay.toString(),
"rdpPort": rdpPort,
"rdpUsername": rdpUsername,
'loginName': loginName,
2025-02-15 12:13:11 +08:00
'device_group_name': device_group_name,
2025-10-12 14:59:42 +08:00
'note': note,
2024-03-20 15:05:54 +08:00
'same_server': sameServer,
2022-10-08 16:53:03 +09:00
};
}
2024-04-02 22:08:47 +08:00
Map<String, dynamic> toCustomJson({required bool includingHash}) {
2024-03-20 15:05:54 +08:00
var res = <String, dynamic>{
"id": id,
"username": username,
"hostname": hostname,
"platform": platform,
"alias": alias,
"tags": tags,
};
if (includingHash) {
res['hash'] = hash;
}
return res;
}
Map<String, dynamic> toGroupCacheJson() {
return <String, dynamic>{
"id": id,
"username": username,
"hostname": hostname,
"platform": platform,
"login_name": loginName,
2025-02-15 12:13:11 +08:00
"device_group_name": device_group_name,
};
}
Peer({
required this.id,
2023-08-02 22:25:54 +08:00
required this.hash,
2024-03-20 15:05:54 +08:00
required this.password,
required this.username,
required this.hostname,
required this.platform,
2022-09-21 21:20:19 +08:00
required this.alias,
required this.tags,
2022-11-28 18:16:29 +08:00
required this.forceAlwaysRelay,
required this.rdpPort,
required this.rdpUsername,
required this.loginName,
2025-02-15 12:13:11 +08:00
required this.device_group_name,
2025-10-12 14:59:42 +08:00
required this.note,
2024-03-20 15:05:54 +08:00
this.sameServer,
});
Peer.loading()
: this(
2022-11-28 18:16:29 +08:00
id: '...',
2023-08-02 22:25:54 +08:00
hash: '',
2024-03-20 15:05:54 +08:00
password: '',
2022-11-28 18:16:29 +08:00
username: '...',
hostname: '...',
platform: '...',
alias: '',
tags: [],
forceAlwaysRelay: false,
rdpPort: '',
rdpUsername: '',
loginName: '',
2025-02-15 12:13:11 +08:00
device_group_name: '',
2025-10-12 14:59:42 +08:00
note: '',
2022-11-28 18:16:29 +08:00
);
2023-08-16 08:59:50 +08:00
bool equal(Peer other) {
return id == other.id &&
hash == other.hash &&
2024-03-20 15:05:54 +08:00
password == other.password &&
2023-08-16 08:59:50 +08:00
username == other.username &&
hostname == other.hostname &&
platform == other.platform &&
alias == other.alias &&
tags.equals(other.tags) &&
forceAlwaysRelay == other.forceAlwaysRelay &&
rdpPort == other.rdpPort &&
rdpUsername == other.rdpUsername &&
2025-02-15 12:13:11 +08:00
device_group_name == other.device_group_name &&
2025-10-12 14:59:42 +08:00
loginName == other.loginName &&
note == other.note;
2023-08-16 08:59:50 +08:00
}
2023-08-16 22:43:17 +08:00
2026-06-17 22:04:34 +08:00
factory Peer.copy(Peer other) {
final peer = Peer(
id: other.id,
hash: other.hash,
password: other.password,
username: other.username,
hostname: other.hostname,
platform: other.platform,
alias: other.alias,
tags: other.tags.toList(),
forceAlwaysRelay: other.forceAlwaysRelay,
rdpPort: other.rdpPort,
rdpUsername: other.rdpUsername,
loginName: other.loginName,
device_group_name: other.device_group_name,
note: other.note,
sameServer: other.sameServer);
peer.online = other.online;
return peer;
}
}
2023-06-20 23:34:05 +08:00
enum UpdateEvent { online, load }
2024-03-20 15:05:54 +08:00
typedef GetInitPeers = RxList<Peer> Function();
class Peers extends ChangeNotifier {
2022-09-01 06:18:29 -07:00
final String name;
final String loadEvent;
List<Peer> peers = List.empty(growable: true);
2025-02-20 18:31:12 +08:00
// Part of the peers that are not in the rest peers list.
// When there're too many peers, we may want to load the front 100 peers first,
// so we can see peers in UI quickly. `restPeerIds` is the rest peers' ids.
// And then load all peers later.
List<String> restPeerIds = List.empty(growable: true);
2024-03-20 15:05:54 +08:00
final GetInitPeers? getInitPeers;
2023-06-20 23:34:05 +08:00
UpdateEvent event = UpdateEvent.load;
2022-08-02 13:10:09 +08:00
static const _cbQueryOnlines = 'callback_query_onlines';
Peers(
2024-03-20 15:05:54 +08:00
{required this.name,
required this.getInitPeers,
required this.loadEvent}) {
peers = getInitPeers?.call() ?? [];
2022-09-11 19:52:38 -07:00
platformFFI.registerEventHandler(_cbQueryOnlines, name, (evt) async {
_updateOnlineState(evt);
});
2022-09-11 19:52:38 -07:00
platformFFI.registerEventHandler(loadEvent, name, (evt) async {
2022-08-02 13:10:09 +08:00
_updatePeers(evt);
});
}
@override
void dispose() {
2022-09-01 06:18:29 -07:00
platformFFI.unregisterEventHandler(_cbQueryOnlines, name);
platformFFI.unregisterEventHandler(loadEvent, name);
super.dispose();
}
Peer getByIndex(int index) {
2022-09-01 06:18:29 -07:00
if (index < peers.length) {
return peers[index];
} else {
return Peer.loading();
}
}
int getPeersCount() {
2022-09-01 06:18:29 -07:00
return peers.length;
}
void _updateOnlineState(Map<String, dynamic> evt) {
int changedCount = 0;
evt['onlines'].split(',').forEach((online) {
2022-09-01 06:18:29 -07:00
for (var i = 0; i < peers.length; i++) {
if (peers[i].id == online) {
if (!peers[i].online) {
changedCount += 1;
peers[i].online = true;
}
}
}
});
evt['offlines'].split(',').forEach((offline) {
2022-09-01 06:18:29 -07:00
for (var i = 0; i < peers.length; i++) {
if (peers[i].id == offline) {
if (peers[i].online) {
changedCount += 1;
peers[i].online = false;
}
}
}
});
if (changedCount > 0) {
event = UpdateEvent.online;
notifyListeners();
}
}
2022-08-02 13:10:09 +08:00
void _updatePeers(Map<String, dynamic> evt) {
final onlineStates = _getOnlineStates();
2024-03-20 15:05:54 +08:00
if (getInitPeers != null) {
peers = getInitPeers?.call() ?? [];
} else {
peers = _decodePeers(evt['peers']);
}
2025-02-20 18:31:12 +08:00
restPeerIds = [];
if (evt['ids'] != null) {
restPeerIds = (evt['ids'] as String).split(',');
}
2022-09-01 06:18:29 -07:00
for (var peer in peers) {
2022-08-02 13:10:09 +08:00
final state = onlineStates[peer.id];
peer.online = state != null && state != false;
2022-09-01 06:18:29 -07:00
}
2023-06-20 23:34:05 +08:00
event = UpdateEvent.load;
2022-08-02 13:10:09 +08:00
notifyListeners();
}
Map<String, bool> _getOnlineStates() {
2022-09-01 06:18:29 -07:00
var onlineStates = <String, bool>{};
for (var peer in peers) {
2022-08-02 13:10:09 +08:00
onlineStates[peer.id] = peer.online;
2022-09-01 06:18:29 -07:00
}
2022-08-02 13:10:09 +08:00
return onlineStates;
}
List<Peer> _decodePeers(String peersStr) {
try {
if (peersStr == "") return [];
List<dynamic> peers = json.decode(peersStr);
2022-09-21 21:20:19 +08:00
return peers.map((peer) {
return Peer.fromJson(peer as Map<String, dynamic>);
}).toList();
2022-08-02 13:10:09 +08:00
} catch (e) {
2022-09-01 06:18:29 -07:00
debugPrint('peers(): $e');
2022-08-02 13:10:09 +08:00
}
return [];
}
}