-
Notifications
You must be signed in to change notification settings - Fork 15
/
profile_terminal_action.dart
178 lines (159 loc) · 6.57 KB
/
profile_terminal_action.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import 'dart:developer';
import 'package:at_client_mobile/at_client_mobile.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:noports_core/sshnp.dart';
import 'package:sshnp_flutter/src/controllers/navigation_controller.dart';
import 'package:sshnp_flutter/src/controllers/navigation_rail_controller.dart';
import 'package:sshnp_flutter/src/controllers/terminal_session_controller.dart';
import 'package:sshnp_flutter/src/presentation/widgets/profile_screen_widgets/profile_actions/profile_action_button.dart';
import 'package:sshnp_flutter/src/repository/private_key_manager_repository.dart';
import 'package:sshnp_flutter/src/utility/sizes.dart';
import '../../../../repository/profile_private_key_manager_repository.dart';
import '../../utility/custom_snack_bar.dart';
class ProfileTerminalAction extends ConsumerStatefulWidget {
final SshnpParams params;
const ProfileTerminalAction(this.params, {Key? key}) : super(key: key);
@override
ConsumerState<ProfileTerminalAction> createState() => _ProfileTerminalActionState();
}
class _ProfileTerminalActionState extends ConsumerState<ProfileTerminalAction> {
Future<void> showProgress(String status) {
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const CircularProgressIndicator(),
gapH16,
Text(status),
],
)),
);
}
Future<void> onPressed() async {
if (mounted) {
showProgress('Starting Shell Session...');
}
/// Issue a new session id
final sessionId = ref.watch(terminalSessionController.notifier).createSession();
/// Create the session controller for the new session id
final sessionController = ref.watch(terminalSessionFamilyController(sessionId).notifier);
// TODO: add try
try {
// TODO ensure that this keyPair gets uploaded to the app first
// final privateKeyManager = ref.watch(privateKeyManagerFamilyController(privateKeyNickname));
// final params = privateKeyManager.when(data: (value) {
// log('content: ${value.content}, passPhrase: ${value.passPhrase}');
// return SshnpParams.merge(
// widget.params,
// SshnpPartialParams(identityFile: value.content, identityPassphrase: value.passPhrase),
// );
// }, error: (error, stackTrace) {
// log(error.toString());
// }, loading: () {
// log('loading');
// });
AtClient atClient = AtClientManager.getInstance().atClient;
// TODO: Delete the below line
// DartSshKeyUtil keyUtil = DartSshKeyUtil();
// widget.params was originally used
// AtSshKeyPair keyPair = await keyUtil.getKeyPair(
// identifier: widget.params.identityFile ?? 'id_${atClient.getCurrentAtSign()!.replaceAll('@', '')}',
// );
// TODO: Get values from biometric storage (PrivateKeyManagerController)
final profilePrivateKey =
await ProfilePrivateKeyManagerRepository.readProfilePrivateKeyManager(widget.params.profileName ?? '');
final privateKeyManager =
await PrivateKeyManagerRepository.readPrivateKeyManager(profilePrivateKey.privateKeyNickname);
// log('private key is: ${privateKeyManager!.privateKeyFileName}');
// log('private key manager passphrase is: ${privateKeyManager.passPhrase}');
// AtSshKeyPair keyPair = AtSshKeyPair.fromPem(
// privateKeyManager.content,
// identifier: privateKeyManager.privateKeyFileName,
// passphrase: privateKeyManager.passPhrase,
// // passphrase: privateKeyManager.passPhrase,
// );
final keyPair = privateKeyManager.toAtSshKeyPair();
final sshnp = Sshnp.dartPure(
// params: sshnpParams,
params: SshnpParams.merge(
widget.params,
SshnpPartialParams(
verbose: kDebugMode,
idleTimeout: 30,
),
),
atClient: atClient,
identityKeyPair: keyPair,
);
final result = await sshnp.run();
if (result is SshnpError) {
throw result;
}
if (result is SshnpCommand) {
if (sshnp.canRunShell) {
if (mounted) {
context.pop();
showProgress('running shell session...');
}
log('running shell session...');
SshnpRemoteProcess shell = await sshnp.runShell();
if (mounted) {
context.pop();
showProgress('starting terminal session...');
}
log('starting terminal session');
sessionController.startSession(
shell,
terminalTitle: '${widget.params.sshnpdAtSign}-${widget.params.device}',
);
}
sessionController.issueDisplayName(widget.params.profileName!);
ref.read(navigationRailController.notifier).setRoute(AppRoute.terminal);
if (mounted) {
context.pushReplacementNamed(AppRoute.terminal.name);
}
}
//TODO: Add catch
} catch (e) {
sessionController.dispose();
if (mounted) {
log('error: ${e.toString()}');
context.pop();
CustomSnackBar.error(content: e.toString());
}
}
}
@override
Widget build(BuildContext context) {
//TODO: Add a terminal icon that calls on pressed. Reuse old code
return ProfileActionButton(onPressed: onPressed, icon: const Icon(Icons.terminal));
// final profilePrivateKeys = ref.watch(profilePrivateKeyManagerListController);
// return profilePrivateKeys.when(
// data: (data) {
// return PopupMenuButton(
// icon: const Icon(Icons.terminal),
// tooltip: 'select a private key to ssh with',
// itemBuilder: (itemBuilderContext) => data
// .map((e) => PopupMenuItem(
// onTap: (() async => await ref.read(profilePrivateKeyManagerListController.notifier).remove(e)),
// child: Row(
// children: [
// const Icon(Icons.vpn_key),
// gapW12,
// Text(e),
// ],
// ),
// ))
// .toList(),
// );
// },
// error: (error, stack) => Center(child: Text(error.toString())),
// loading: () => const Center(child: CircularProgressIndicator()));
}
}