mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 23:34:24 +01:00
#87 new: auto ask add system key (~/.ssh/id_rsa)
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
import 'dart:io';
|
||||
import 'package:toolbox/core/utils/misc.dart' show getHome, pathJoin;
|
||||
import 'package:toolbox/data/model/app/error.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
|
||||
part 'private_key_info.g.dart';
|
||||
@@ -10,37 +7,25 @@ class PrivateKeyInfo {
|
||||
@HiveField(0)
|
||||
late String id;
|
||||
@HiveField(1)
|
||||
late String privateKey;
|
||||
late String key;
|
||||
@Deprecated('Never use this field')
|
||||
@HiveField(2)
|
||||
late String password;
|
||||
|
||||
PrivateKeyInfo(
|
||||
this.id,
|
||||
this.privateKey,
|
||||
this.password,
|
||||
);
|
||||
PrivateKeyInfo({
|
||||
required this.id,
|
||||
required this.key,
|
||||
});
|
||||
|
||||
PrivateKeyInfo.fromJson(Map<String, dynamic> json) {
|
||||
id = json["id"].toString();
|
||||
privateKey = json["private_key"].toString();
|
||||
password = json["password"].toString();
|
||||
key = json["private_key"].toString();
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = <String, dynamic>{};
|
||||
data["id"] = id;
|
||||
data["private_key"] = privateKey;
|
||||
data["password"] = password;
|
||||
data["private_key"] = key;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class SystemPrivateKeyInfo extends PrivateKeyInfo {
|
||||
SystemPrivateKeyInfo() : super("System private key", "", "");
|
||||
|
||||
Future getKey() async {
|
||||
File idRsaFile = File(pathJoin(getHome(), ".ssh/id_rsa"));
|
||||
if (!await idRsaFile.exists()) {
|
||||
this.privateKey="";
|
||||
}
|
||||
this.privateKey= await idRsaFile.readAsString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,8 @@ class PrivateKeyInfoAdapter extends TypeAdapter<PrivateKeyInfo> {
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return PrivateKeyInfo(
|
||||
fields[0] as String,
|
||||
fields[1] as String,
|
||||
fields[2] as String,
|
||||
id: fields[0] as String,
|
||||
key: fields[1] as String,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -30,8 +29,9 @@ class PrivateKeyInfoAdapter extends TypeAdapter<PrivateKeyInfo> {
|
||||
..writeByte(0)
|
||||
..write(obj.id)
|
||||
..writeByte(1)
|
||||
..write(obj.privateKey)
|
||||
..write(obj.key)
|
||||
..writeByte(2)
|
||||
// ignore: deprecated_member_use_from_same_package
|
||||
..write(obj.password);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,29 +4,33 @@ import 'package:toolbox/data/store/private_key.dart';
|
||||
import 'package:toolbox/locator.dart';
|
||||
|
||||
class PrivateKeyProvider extends BusyProvider {
|
||||
List<PrivateKeyInfo> get infos => _infos;
|
||||
List<PrivateKeyInfo> get pkis => _pkis;
|
||||
final _store = locator<PrivateKeyStore>();
|
||||
late List<PrivateKeyInfo> _infos;
|
||||
late List<PrivateKeyInfo> _pkis;
|
||||
|
||||
void loadData() {
|
||||
_infos = _store.fetch();
|
||||
_pkis = _store.fetch();
|
||||
}
|
||||
|
||||
void addInfo(PrivateKeyInfo info) {
|
||||
_infos.add(info);
|
||||
void add(PrivateKeyInfo info) {
|
||||
_pkis.add(info);
|
||||
_store.put(info);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void delInfo(PrivateKeyInfo info) {
|
||||
_infos.removeWhere((e) => e.id == info.id);
|
||||
void delete(PrivateKeyInfo info) {
|
||||
_pkis.removeWhere((e) => e.id == info.id);
|
||||
_store.delete(info);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void updateInfo(PrivateKeyInfo old, PrivateKeyInfo newInfo) {
|
||||
final idx = _infos.indexWhere((e) => e.id == old.id);
|
||||
_infos[idx] = newInfo;
|
||||
void update(PrivateKeyInfo old, PrivateKeyInfo newInfo) {
|
||||
final idx = _pkis.indexWhere((e) => e.id == old.id);
|
||||
if (idx == -1) {
|
||||
_pkis.add(newInfo);
|
||||
} else {
|
||||
_pkis[idx] = newInfo;
|
||||
}
|
||||
_store.put(newInfo);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'package:toolbox/core/persistant_store.dart';
|
||||
import 'package:toolbox/data/model/server/private_key_info.dart';
|
||||
import 'package:toolbox/core/utils/platform.dart';
|
||||
|
||||
class PrivateKeyStore extends PersistentStore {
|
||||
late SystemPrivateKeyInfo systemPrivateKeyInfo;
|
||||
void put(PrivateKeyInfo info) {
|
||||
box.put(info.id, info);
|
||||
}
|
||||
@@ -18,19 +15,10 @@ class PrivateKeyStore extends PersistentStore {
|
||||
ps.add(s);
|
||||
}
|
||||
}
|
||||
if (isLinux || isMacOS) {
|
||||
SystemPrivateKeyInfo sysPk = SystemPrivateKeyInfo();
|
||||
unawaited(sysPk.getKey());
|
||||
systemPrivateKeyInfo = sysPk;
|
||||
ps.add(sysPk);
|
||||
}
|
||||
return ps;
|
||||
}
|
||||
|
||||
PrivateKeyInfo? get(String? id) {
|
||||
if (id == "System private key") {
|
||||
return this.systemPrivateKeyInfo;
|
||||
}
|
||||
if (id == null) return null;
|
||||
return box.get(id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user