feat: Wake On LAN

This commit is contained in:
lollipopkit
2024-05-09 23:27:05 +08:00
parent b876981243
commit e2ddd48a79
23 changed files with 290 additions and 94 deletions

View File

@@ -1,6 +1,7 @@
import 'package:hive_flutter/hive_flutter.dart';
import 'package:toolbox/data/model/server/custom.dart';
import 'package:toolbox/data/model/server/server.dart';
import 'package:toolbox/data/model/server/wol_cfg.dart';
import 'package:toolbox/data/res/provider.dart';
import '../app/error.dart';
@@ -38,6 +39,9 @@ class ServerPrivateInfo {
@HiveField(10)
final ServerCustom? custom;
@HiveField(11)
final WakeOnLanCfg? wolCfg;
final String id;
const ServerPrivateInfo({
@@ -52,6 +56,7 @@ class ServerPrivateInfo {
this.autoConnect,
this.jumpId,
this.custom,
this.wolCfg,
}) : id = '$user@$ip:$port';
static ServerPrivateInfo fromJson(Map<String, dynamic> json) {
@@ -68,6 +73,9 @@ class ServerPrivateInfo {
final custom = json["customCmd"] == null
? null
: ServerCustom.fromJson(json["custom"].cast<String, dynamic>());
final wolCfg = json["wolCfg"] == null
? null
: WakeOnLanCfg.fromJson(json["wolCfg"].cast<String, dynamic>());
return ServerPrivateInfo(
name: name,
@@ -81,6 +89,7 @@ class ServerPrivateInfo {
autoConnect: autoConnect,
jumpId: jumpId,
custom: custom,
wolCfg: wolCfg,
);
}
@@ -111,6 +120,9 @@ class ServerPrivateInfo {
if (custom != null) {
data["custom"] = custom?.toJson();
}
if (wolCfg != null) {
data["wolCfg"] = wolCfg?.toJson();
}
return data;
}

View File

@@ -28,13 +28,14 @@ class ServerPrivateInfoAdapter extends TypeAdapter<ServerPrivateInfo> {
autoConnect: fields[8] as bool?,
jumpId: fields[9] as String?,
custom: fields[10] as ServerCustom?,
wolCfg: fields[11] as WakeOnLanCfg?,
);
}
@override
void write(BinaryWriter writer, ServerPrivateInfo obj) {
writer
..writeByte(11)
..writeByte(12)
..writeByte(0)
..write(obj.name)
..writeByte(1)
@@ -56,7 +57,9 @@ class ServerPrivateInfoAdapter extends TypeAdapter<ServerPrivateInfo> {
..writeByte(9)
..write(obj.jumpId)
..writeByte(10)
..write(obj.custom);
..write(obj.custom)
..writeByte(11)
..write(obj.wolCfg);
}
@override

View File

@@ -0,0 +1,71 @@
import 'dart:io';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:wake_on_lan/wake_on_lan.dart';
@HiveType(typeId: 8)
final class WakeOnLanCfg {
@HiveField(0)
final String mac;
@HiveField(1)
final String ip;
@HiveField(2)
final String? pwd;
const WakeOnLanCfg({
required this.mac,
required this.ip,
this.pwd,
});
(Object?, bool) validate() {
final macValidation = MACAddress.validate(mac);
final ipValidation = IPAddress.validate(
ip,
type: ip.contains(':')
? InternetAddressType.IPv6
: InternetAddressType.IPv4,
);
final pwdValidation = SecureONPassword.validate(pwd);
final valid =
macValidation.state && ipValidation.state && pwdValidation.state;
final err =
macValidation.error ?? ipValidation.error ?? pwdValidation.error;
return (err, valid);
}
Future<void> wake() async {
if (!validate().$2) {
throw Exception('Invalid WakeOnLanCfg');
}
final ip_ = IPAddress(ip);
final mac_ = MACAddress(mac);
final pwd_ = pwd != null ? SecureONPassword(pwd!) : null;
final obj = WakeOnLAN(ip_, mac_, password: pwd_);
await obj.wake(
repeat: 3,
repeatDelay: const Duration(milliseconds: 500),
);
}
static WakeOnLanCfg fromJson(Map<String, dynamic> json) {
return WakeOnLanCfg(
mac: json['mac'] as String,
ip: json['ip'] as String,
pwd: json['pwd'] as String?,
);
}
Map<String, dynamic> toJson() {
final map = <String, dynamic>{
'mac': mac,
'ip': ip,
};
if (pwd != null) {
map['pwd'] = pwd;
}
return map;
}
}