mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2026-02-18 14:14:51 +01:00
#87 new: auto ask add system key (~/.ssh/id_rsa)
This commit is contained in:
@@ -74,7 +74,7 @@ class _PrivateKeyEditPageState extends State<PrivateKeyEditPage>
|
||||
IconButton(
|
||||
tooltip: _s.delete,
|
||||
onPressed: () {
|
||||
_provider.delInfo(widget.info!);
|
||||
_provider.delete(widget.info!);
|
||||
context.pop();
|
||||
},
|
||||
icon: const Icon(Icons.delete))
|
||||
@@ -100,9 +100,9 @@ class _PrivateKeyEditPageState extends State<PrivateKeyEditPage>
|
||||
setState(() {
|
||||
_loading = centerSizedLoading;
|
||||
});
|
||||
final info = PrivateKeyInfo(name, key, '');
|
||||
final info = PrivateKeyInfo(id: name, key: key);
|
||||
try {
|
||||
info.privateKey = await compute(decyptPem, [key, pwd]);
|
||||
info.key = await compute(decyptPem, [key, pwd]);
|
||||
} catch (e) {
|
||||
showSnackBar(context, Text(e.toString()));
|
||||
rethrow;
|
||||
@@ -112,9 +112,9 @@ class _PrivateKeyEditPageState extends State<PrivateKeyEditPage>
|
||||
});
|
||||
}
|
||||
if (widget.info != null) {
|
||||
_provider.updateInfo(widget.info!, info);
|
||||
_provider.update(widget.info!, info);
|
||||
} else {
|
||||
_provider.addInfo(info);
|
||||
_provider.add(info);
|
||||
}
|
||||
context.pop();
|
||||
},
|
||||
@@ -194,8 +194,7 @@ class _PrivateKeyEditPageState extends State<PrivateKeyEditPage>
|
||||
Future<void> afterFirstLayout(BuildContext context) async {
|
||||
if (widget.info != null) {
|
||||
_nameController.text = widget.info!.id;
|
||||
_keyController.text = widget.info!.privateKey;
|
||||
_pwdController.text = widget.info!.password;
|
||||
_keyController.text = widget.info!.key;
|
||||
} else {
|
||||
final clipdata = ((await Clipboard.getData(_format))?.text ?? '').trim();
|
||||
if (clipdata.startsWith('-----BEGIN') && clipdata.endsWith('-----')) {
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:toolbox/core/extension/navigator.dart';
|
||||
import 'package:toolbox/core/utils/ui.dart';
|
||||
import 'package:toolbox/data/store/private_key.dart';
|
||||
import 'package:toolbox/locator.dart';
|
||||
|
||||
import '../../../core/route.dart';
|
||||
import '../../../core/utils/misc.dart';
|
||||
import '../../../core/utils/platform.dart';
|
||||
import '../../../data/model/server/private_key_info.dart';
|
||||
import '../../../data/provider/private_key.dart';
|
||||
import '../../../data/res/ui.dart';
|
||||
import 'edit.dart';
|
||||
@@ -24,6 +33,13 @@ class _PrivateKeyListState extends State<PrivateKeysListPage> {
|
||||
_s = S.of(context)!;
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
autoAddSystemPriavteKey();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
@@ -44,21 +60,21 @@ class _PrivateKeyListState extends State<PrivateKeysListPage> {
|
||||
Widget _buildBody() {
|
||||
return Consumer<PrivateKeyProvider>(
|
||||
builder: (_, key, __) {
|
||||
if (key.infos.isEmpty) {
|
||||
if (key.pkis.isEmpty) {
|
||||
return Center(
|
||||
child: Text(_s.noSavedPrivateKey),
|
||||
);
|
||||
}
|
||||
return ListView.builder(
|
||||
padding: const EdgeInsets.all(13),
|
||||
itemCount: key.infos.length,
|
||||
itemCount: key.pkis.length,
|
||||
itemBuilder: (context, idx) {
|
||||
return RoundRectCard(
|
||||
ListTile(
|
||||
title: Text(key.infos[idx].id),
|
||||
title: Text(key.pkis[idx].id),
|
||||
trailing: TextButton(
|
||||
onPressed: () => AppRoute(
|
||||
PrivateKeyEditPage(info: key.infos[idx]),
|
||||
PrivateKeyEditPage(info: key.pkis[idx]),
|
||||
'private key edit page',
|
||||
).go(context),
|
||||
child: Text(_s.edit),
|
||||
@@ -70,4 +86,40 @@ class _PrivateKeyListState extends State<PrivateKeysListPage> {
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void autoAddSystemPriavteKey() {
|
||||
final store = locator<PrivateKeyStore>();
|
||||
// Only trigger on desktop platform and no private key saved
|
||||
if (isDesktop && store.box.keys.isEmpty) {
|
||||
final home = getHomeDir();
|
||||
if (home == null) return;
|
||||
final idRsaFile = File(pathJoin(home, '.ssh/id_rsa'));
|
||||
if (!idRsaFile.existsSync()) return;
|
||||
final sysPk = PrivateKeyInfo(
|
||||
id: 'system',
|
||||
key: idRsaFile.readAsStringSync(),
|
||||
);
|
||||
showRoundDialog(
|
||||
context: context,
|
||||
title: Text(_s.attention),
|
||||
child: Text(_s.addSystemPrivateKeyTip),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
context.pop();
|
||||
AppRoute(
|
||||
PrivateKeyEditPage(info: sysPk),
|
||||
'private key edit page',
|
||||
).go(context);
|
||||
},
|
||||
child: Text(_s.ok),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: Text(_s.cancel),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,17 +200,17 @@ class _ServerEditPageState extends State<ServerEditPage> with AfterLayoutMixin {
|
||||
Widget _buildKeyAuth() {
|
||||
return Consumer<PrivateKeyProvider>(
|
||||
builder: (_, key, __) {
|
||||
for (var item in key.infos) {
|
||||
for (var item in key.pkis) {
|
||||
if (item.id == widget.spi?.pubKeyId) {
|
||||
_pubKeyIndex ??= key.infos.indexOf(item);
|
||||
_pubKeyIndex ??= key.pkis.indexOf(item);
|
||||
}
|
||||
}
|
||||
final tiles = key.infos
|
||||
final tiles = key.pkis
|
||||
.map(
|
||||
(e) => ListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: Text(e.id, textAlign: TextAlign.start),
|
||||
trailing: _buildRadio(key.infos.indexOf(e), e),
|
||||
trailing: _buildRadio(key.pkis.indexOf(e), e),
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
|
||||
Reference in New Issue
Block a user