hbbs.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_hbb/common.dart';
  4. import 'package:flutter_hbb/consts.dart';
  5. import 'package:flutter_hbb/models/peer_model.dart';
  6. import '../../models/platform_model.dart';
  7. class HttpType {
  8. static const kAuthReqTypeAccount = "account";
  9. static const kAuthReqTypeMobile = "mobile";
  10. static const kAuthReqTypeSMSCode = "sms_code";
  11. static const kAuthReqTypeEmailCode = "email_code";
  12. static const kAuthReqTypeTfaCode = "tfa_code";
  13. static const kAuthResTypeToken = "access_token";
  14. static const kAuthResTypeEmailCheck = "email_check";
  15. static const kAuthResTypeTfaCheck = "tfa_check";
  16. }
  17. enum UserStatus { kDisabled, kNormal, kUnverified }
  18. // to-do: The UserPayload does not contain all the fields of the user.
  19. // Is all the fields of the user needed?
  20. class UserPayload {
  21. String name = '';
  22. String email = '';
  23. String note = '';
  24. String? verifier;
  25. UserStatus status;
  26. bool isAdmin = false;
  27. UserPayload.fromJson(Map<String, dynamic> json)
  28. : name = json['name'] ?? '',
  29. email = json['email'] ?? '',
  30. note = json['note'] ?? '',
  31. verifier = json['verifier'],
  32. status = json['status'] == 0
  33. ? UserStatus.kDisabled
  34. : json['status'] == -1
  35. ? UserStatus.kUnverified
  36. : UserStatus.kNormal,
  37. isAdmin = json['is_admin'] == true;
  38. Map<String, dynamic> toJson() {
  39. final Map<String, dynamic> map = {
  40. 'name': name,
  41. 'status': status == UserStatus.kDisabled
  42. ? 0
  43. : status == UserStatus.kUnverified
  44. ? -1
  45. : 1,
  46. };
  47. return map;
  48. }
  49. Map<String, dynamic> toGroupCacheJson() {
  50. final Map<String, dynamic> map = {
  51. 'name': name,
  52. };
  53. return map;
  54. }
  55. }
  56. class PeerPayload {
  57. String id = '';
  58. Map<String, dynamic> info = {};
  59. int? status;
  60. String user = '';
  61. String user_name = '';
  62. String? device_group_name;
  63. String note = '';
  64. PeerPayload.fromJson(Map<String, dynamic> json)
  65. : id = json['id'] ?? '',
  66. info = (json['info'] is Map<String, dynamic>) ? json['info'] : {},
  67. status = json['status'],
  68. user = json['user'] ?? '',
  69. user_name = json['user_name'] ?? '',
  70. device_group_name = json['device_group_name'] ?? '',
  71. note = json['note'] ?? '';
  72. static Peer toPeer(PeerPayload p) {
  73. return Peer.fromJson({
  74. "id": p.id,
  75. 'loginName': p.user_name,
  76. "username": p.info['username'] ?? '',
  77. "platform": _platform(p.info['os']),
  78. "hostname": p.info['device_name'],
  79. "device_group_name": p.device_group_name,
  80. });
  81. }
  82. static String? _platform(dynamic field) {
  83. if (field == null) {
  84. return null;
  85. }
  86. final fieldStr = field.toString();
  87. List<String> list = fieldStr.split(' / ');
  88. if (list.isEmpty) return null;
  89. final os = list[0];
  90. switch (os.toLowerCase()) {
  91. case 'windows':
  92. return kPeerPlatformWindows;
  93. case 'linux':
  94. return kPeerPlatformLinux;
  95. case 'macos':
  96. return kPeerPlatformMacOS;
  97. case 'android':
  98. return kPeerPlatformAndroid;
  99. default:
  100. if (fieldStr.toLowerCase().contains('linux')) {
  101. return kPeerPlatformLinux;
  102. }
  103. return null;
  104. }
  105. }
  106. }
  107. class LoginRequest {
  108. String? username;
  109. String? password;
  110. String? id;
  111. String? uuid;
  112. bool? autoLogin;
  113. String? type;
  114. String? verificationCode;
  115. String? tfaCode;
  116. String? secret;
  117. LoginRequest(
  118. {this.username,
  119. this.password,
  120. this.id,
  121. this.uuid,
  122. this.autoLogin,
  123. this.type,
  124. this.verificationCode,
  125. this.tfaCode,
  126. this.secret});
  127. Map<String, dynamic> toJson() {
  128. final Map<String, dynamic> data = <String, dynamic>{};
  129. if (username != null) data['username'] = username;
  130. if (password != null) data['password'] = password;
  131. if (id != null) data['id'] = id;
  132. if (uuid != null) data['uuid'] = uuid;
  133. if (autoLogin != null) data['autoLogin'] = autoLogin;
  134. if (type != null) data['type'] = type;
  135. if (verificationCode != null) {
  136. data['verificationCode'] = verificationCode;
  137. }
  138. if (tfaCode != null) data['tfaCode'] = tfaCode;
  139. if (secret != null) data['secret'] = secret;
  140. Map<String, dynamic> deviceInfo = {};
  141. try {
  142. deviceInfo = jsonDecode(bind.mainGetLoginDeviceInfo());
  143. } catch (e) {
  144. debugPrint('Failed to decode get device info: $e');
  145. }
  146. data['deviceInfo'] = deviceInfo;
  147. return data;
  148. }
  149. }
  150. class LoginResponse {
  151. String? access_token;
  152. String? type;
  153. String? tfa_type;
  154. String? secret;
  155. UserPayload? user;
  156. LoginResponse(
  157. {this.access_token, this.type, this.tfa_type, this.secret, this.user});
  158. LoginResponse.fromJson(Map<String, dynamic> json) {
  159. access_token = json['access_token'];
  160. type = json['type'];
  161. tfa_type = json['tfa_type'];
  162. secret = json['secret'];
  163. user = json['user'] != null ? UserPayload.fromJson(json['user']) : null;
  164. }
  165. }
  166. class RequestException implements Exception {
  167. int statusCode;
  168. String cause;
  169. RequestException(this.statusCode, this.cause);
  170. @override
  171. String toString() {
  172. return "RequestException, statusCode: $statusCode, error: $cause";
  173. }
  174. }
  175. enum ShareRule {
  176. read(1),
  177. readWrite(2),
  178. fullControl(3);
  179. const ShareRule(this.value);
  180. final int value;
  181. static String desc(int v) {
  182. if (v == ShareRule.read.value) {
  183. return translate('Read-only');
  184. }
  185. if (v == ShareRule.readWrite.value) {
  186. return translate('Read/Write');
  187. }
  188. if (v == ShareRule.fullControl.value) {
  189. return translate('Full Control');
  190. }
  191. return v.toString();
  192. }
  193. static String shortDesc(int v) {
  194. if (v == ShareRule.read.value) {
  195. return 'R';
  196. }
  197. if (v == ShareRule.readWrite.value) {
  198. return 'RW';
  199. }
  200. if (v == ShareRule.fullControl.value) {
  201. return 'F';
  202. }
  203. return v.toString();
  204. }
  205. static ShareRule? fromValue(int v) {
  206. if (v == ShareRule.read.value) {
  207. return ShareRule.read;
  208. }
  209. if (v == ShareRule.readWrite.value) {
  210. return ShareRule.readWrite;
  211. }
  212. if (v == ShareRule.fullControl.value) {
  213. return ShareRule.fullControl;
  214. }
  215. return null;
  216. }
  217. }
  218. class AbProfile {
  219. String guid;
  220. String name;
  221. String owner;
  222. String? note;
  223. int rule;
  224. AbProfile(this.guid, this.name, this.owner, this.note, this.rule);
  225. AbProfile.fromJson(Map<String, dynamic> json)
  226. : guid = json['guid'] ?? '',
  227. name = json['name'] ?? '',
  228. owner = json['owner'] ?? '',
  229. note = json['note'] ?? '',
  230. rule = json['rule'] ?? 0;
  231. }
  232. class AbTag {
  233. String name;
  234. int color;
  235. AbTag(this.name, this.color);
  236. AbTag.fromJson(Map<String, dynamic> json)
  237. : name = json['name'] ?? '',
  238. color = json['color'] ?? '';
  239. }
  240. class DeviceGroupPayload {
  241. String name;
  242. DeviceGroupPayload(this.name);
  243. DeviceGroupPayload.fromJson(Map<String, dynamic> json)
  244. : name = json['name'] ?? '';
  245. Map<String, dynamic> toGroupCacheJson() {
  246. final Map<String, dynamic> map = {
  247. 'name': name,
  248. };
  249. return map;
  250. }
  251. }