205 lines
5.1 KiB
Dart
205 lines
5.1 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:dartssh2/dartssh2.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
// for ed2219 key generation
|
|
import 'package:cryptography/cryptography.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
// secret storage
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
void main() {
|
|
runApp(const MyApp());
|
|
}
|
|
|
|
Future<OpenSSHEd25519KeyPair> generateKeyPair() async {
|
|
final algorithm = Ed25519();
|
|
|
|
// Generate a key pair
|
|
final cryptographyKeyPair = await algorithm.newKeyPair();
|
|
|
|
final privateKey = await cryptographyKeyPair.extractPrivateKeyBytes();
|
|
final publicKey = (await cryptographyKeyPair.extractPublicKey()).bytes;
|
|
|
|
final dartsshKeyPair = OpenSSHEd25519KeyPair(
|
|
Uint8List.fromList(publicKey),
|
|
Uint8List.fromList(privateKey + publicKey),
|
|
"",
|
|
);
|
|
|
|
return dartsshKeyPair;
|
|
}
|
|
|
|
String encodePublicKey(OpenSSHEd25519KeyPair keyPair, {String comment = ''}) {
|
|
final publicKeyBytes = keyPair.toPublicKey().encode();
|
|
return "ssh-ed25519 ${base64.encode(publicKeyBytes)} $comment";
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'Flutter Demo',
|
|
theme: ThemeData(
|
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
|
),
|
|
home: const MyHomePage(title: 'LeineLab e.V. Key App'),
|
|
);
|
|
}
|
|
}
|
|
|
|
class MyHomePage extends StatefulWidget {
|
|
const MyHomePage({super.key, required this.title});
|
|
|
|
final String title;
|
|
|
|
@override
|
|
State<MyHomePage> createState() => _MyHomePageState();
|
|
}
|
|
|
|
class _MyHomePageState extends State<MyHomePage> {
|
|
int navIndex = 0;
|
|
OpenSSHEd25519KeyPair? keyPair;
|
|
String _output = '';
|
|
String key = '';
|
|
|
|
Future<void> doSSH() async {
|
|
SSHSocket? socket;
|
|
|
|
try {
|
|
socket = await SSHSocket.connect(
|
|
'192.168.2.123',
|
|
22,
|
|
timeout: const Duration(seconds: 0, milliseconds: 500),
|
|
);
|
|
|
|
final client = SSHClient(
|
|
socket,
|
|
username: 'test',
|
|
identities: List.of(keyPair != null ? [keyPair!] : []),
|
|
);
|
|
|
|
final uptime = await client.run('uptime');
|
|
setState(() {
|
|
_output = utf8.decode(uptime);
|
|
});
|
|
} catch (e) {
|
|
setState(() {
|
|
_output = "Error: $e";
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
|
|
Future<void> generateKey() async {
|
|
// Generate a key pair
|
|
|
|
final generatedKeyPair =
|
|
await generateKeyPair(); // local variable avoids downcast to ? ptr
|
|
keyPair = generatedKeyPair;
|
|
|
|
setState(() {
|
|
key = encodePublicKey(generatedKeyPair, comment: "leinelab-app-key");
|
|
});
|
|
|
|
await SystemChannels.platform.invokeMethod<void>(
|
|
'Clipboard.setData',
|
|
<String, dynamic>{'text': key},
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final String outputText = _output.isNotEmpty
|
|
? 'Output: $_output'
|
|
: 'Press the button to run a command.';
|
|
|
|
final bodyComponentMain = Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
Text('Current output:'),
|
|
Text(outputText, style: Theme.of(context).textTheme.headlineMedium),
|
|
],
|
|
),
|
|
);
|
|
|
|
final bodyComponentInfo = Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[Center(child: Text('Info\n\n$key'))],
|
|
),
|
|
);
|
|
|
|
final actionButtonMain = FloatingActionButton(
|
|
onPressed: doSSH,
|
|
tooltip: 'Do SSH',
|
|
child: const Icon(Icons.computer),
|
|
);
|
|
|
|
final actionButtonInfo = FloatingActionButton(
|
|
onPressed: generateKey,
|
|
tooltip: 'Regenerate Key',
|
|
child: const Icon(Icons.refresh),
|
|
);
|
|
|
|
var bodyComponent = bodyComponentMain;
|
|
var actionButton = actionButtonMain;
|
|
|
|
if (navIndex == 0) {
|
|
// Main page
|
|
bodyComponent = bodyComponentMain;
|
|
actionButton = actionButtonMain;
|
|
} else if (navIndex == 1) {
|
|
// Info page
|
|
bodyComponent = bodyComponentInfo;
|
|
actionButton = actionButtonInfo;
|
|
} else {
|
|
throw Exception('Unknown navIndex: $navIndex');
|
|
}
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(backgroundColor: Colors.amber, title: Text(widget.title)),
|
|
body: bodyComponent,
|
|
floatingActionButton: actionButton,
|
|
drawer: Drawer(
|
|
child: ListView(
|
|
padding: EdgeInsets.zero,
|
|
children: <Widget>[
|
|
DrawerHeader(
|
|
decoration: BoxDecoration(color: Colors.amber),
|
|
child: Text(
|
|
widget.title,
|
|
style: TextStyle(color: Colors.black, fontSize: 24),
|
|
),
|
|
),
|
|
ListTile(
|
|
title: const Text('Main Page'),
|
|
onTap: () {
|
|
setState(() {
|
|
navIndex = 0;
|
|
});
|
|
Navigator.pop(context);
|
|
},
|
|
),
|
|
ListTile(
|
|
title: const Text('Info Page'),
|
|
onTap: () {
|
|
setState(() {
|
|
navIndex = 1;
|
|
});
|
|
Navigator.pop(context);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|