Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion lib/pages/remotes/add_remote.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ class _AddRemotePageState extends State<AddRemotePage> {
final _portController = TextEditingController();
final _usernameController = TextEditingController();
final _passwordController = TextEditingController();
final _privateKeyController = TextEditingController();
final _socketPathController = TextEditingController();

late final String _id;
bool _keepPassword = false;
bool _keepPrivateKey = false;

static const _defaultPort = "22";
static const _defaultSocketPath = "/tmp/mpvsocket";
Expand All @@ -51,12 +53,17 @@ class _AddRemotePageState extends State<AddRemotePage> {
_portController.dispose();
_usernameController.dispose();
_passwordController.dispose();
_privateKeyController.dispose();
_socketPathController.dispose();

if (!_keepPassword) {
_deletePassword();
}

if (!_keepPrivateKey) {
_deletePrivateKey();
}

super.dispose();
}

Expand Down Expand Up @@ -124,6 +131,11 @@ class _AddRemotePageState extends State<AddRemotePage> {
),
),
_Field(_passwordController, "Password", password: true),
_Field(
_privateKeyController,
"Private Key",
privateKey: true,
),
_Field(
_socketPathController,
"Socket path",
Expand Down Expand Up @@ -191,10 +203,19 @@ class _AddRemotePageState extends State<AddRemotePage> {
SecureStorage.savePassword(_id, password);
}

Future<void> _savePrivateKey() async {
final privateKey = _privateKeyController.text;
SecureStorage.savePrivateKey(_id, privateKey);
}

Future<void> _deletePassword() async {
SecureStorage.deletePassword(_id);
}

Future<void> _deletePrivateKey() async {
SecureStorage.deletePrivateKey(_id);
}

Future<bool> _testConnection() async {
final conn = _readForm();
if (conn == null) {
Expand All @@ -208,6 +229,7 @@ class _AddRemotePageState extends State<AddRemotePage> {
});

await _savePassword();
await _savePrivateKey();

final sc = StreamController<String>();
sc.stream.listen((line) {
Expand Down Expand Up @@ -268,8 +290,10 @@ class _AddRemotePageState extends State<AddRemotePage> {
Preferences.remoteConnections.setValue(remotes);

await _savePassword();
await _savePrivateKey();

_keepPassword = true;
_keepPrivateKey = true;
Navigator.pop(context);
}
}
Expand All @@ -284,6 +308,7 @@ class _Field extends StatefulWidget {
this.urlKeyboard = false,
this.portNumber = false,
this.password = false,
this.privateKey = false,
Key? key,
}) : super(key: key);

Expand All @@ -294,6 +319,7 @@ class _Field extends StatefulWidget {
final bool urlKeyboard;
final bool portNumber;
final bool password;
final bool privateKey;

@override
State<_Field> createState() => _FieldState();
Expand All @@ -305,6 +331,7 @@ class _FieldState extends State<_Field> {
@override
Widget build(BuildContext context) {
final field = TextFormField(
maxLines: widget.privateKey ? null : 1,
controller: widget.controller,
textInputAction: TextInputAction.next,
obscureText: widget.password && !showPassword,
Expand All @@ -314,7 +341,9 @@ class _FieldState extends State<_Field> {
? TextInputType.url
: (widget.password && showPassword)
? TextInputType.visiblePassword
: TextInputType.text,
: widget.privateKey
? TextInputType.multiline
: TextInputType.text,
decoration: InputDecoration(
alignLabelWithHint: true,
floatingLabelBehavior: (widget.defaultValue != null)
Expand Down
4 changes: 4 additions & 0 deletions lib/remote_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ class RemoteConnection {

Future<bool> testConnection(Sink<String> printOut) async {
final socket = await SSHSocket.connect(host, port);
final privateKey = await SecureStorage.getPrivateKeyById(id);
final client = SSHClient(
socket,
username: username,
onPasswordRequest: _getPassword,
identities: privateKey == null ? null : SSHKeyPair.fromPem(privateKey),
printDebug: (s) => printOut.add(s ?? ""),
);

Expand Down Expand Up @@ -93,10 +95,12 @@ class RemoteConnection {

Future<MpvSocket> connect() async {
final socket = await SSHSocket.connect(host, port);
final privateKey = await SecureStorage.getPrivateKeyById(id);
final client = SSHClient(
socket,
username: username,
onPasswordRequest: _getPassword,
identities: privateKey == null ? null : SSHKeyPair.fromPem(privateKey),
);

await client.authenticated;
Expand Down
12 changes: 12 additions & 0 deletions lib/secure_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ class SecureStorage {
static Future<void> deletePassword(String id) async {
await _storage.delete(key: "password_$id");
}

static Future<String?> getPrivateKeyById(String id) async {
return _storage.read(key: "privateKey_$id");
}

static Future<void> savePrivateKey(String id, String privateKey) async {
await _storage.write(key: "privateKey_$id", value: privateKey);
}

static Future<void> deletePrivateKey(String id) async {
await _storage.delete(key: "privateKey_$id");
}
}