Skip to content

Commit

Permalink
remove shell dependancies
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin Constable committed Apr 24, 2022
1 parent fd6e2c2 commit cf416c9
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions lib/cleanup_sshnp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import 'dart:io';
// local packages
import 'package:sshnoports/home_directory.dart';
// @platform packages
import 'package:at_utils/at_logger.dart';




Future<void> cleanUp(sessionId,_logger) async {
Future<void> cleanUp(String sessionId, AtSignLogger _logger) async {
String? homeDirectory = getHomeDirectory();
if (homeDirectory == null) {
return;
Expand All @@ -16,9 +15,41 @@ Future<void> cleanUp(sessionId,_logger) async {
sshHomeDirectory = homeDirectory + '\\.ssh\\';
}
// Wait a few seconds for the remote ssh session to connect back here
// do we need to wait ?
_logger.info('Tidying up files');
// do we need to wait ? Yes as it takes time for the reverse ssh to connect
// 2 seconds seems like a reasonable time...
// could make it an option if folks have trouble.
_logger.info('Tidying up files');

sleep(Duration(seconds: 2));
await Process.run('rm', ['${sessionId}_rsa', '${sessionId}_rsa.pub'], workingDirectory: sshHomeDirectory);
await Process.run('sed', ['-i', '/$sessionId/d', 'authorized_keys'], workingDirectory: sshHomeDirectory);
}
// Delete the generated RSA keys and remove the entry from ~/.ssh/authorized_keys
// await deleteFile('$sshHomeDirectory${sessionId}_rsa', _logger);
// await deleteFile('$sshHomeDirectory${sessionId}_rsa.pub', _logger);
//await removeSession(sshHomeDirectory, sessionId, _logger);

}

Future<int> deleteFile(String fileName, AtSignLogger _logger) async {
try {
final file = File(fileName);

await file.delete();
} catch (e) {
_logger.severe("Error deleting file : $fileName");
}
return 0;
}

Future<void> removeSession(String sshHomeDirectory, String sessionId, AtSignLogger _logger) async {
try {
final File file = File('${sshHomeDirectory}authorized_keys');
// read into List of strings
final List<String> lines = await file.readAsLines();
// find the line we want to remove
lines.removeWhere((element) => element.contains(sessionId));
// Write back the file and add a \n
await file.writeAsString(lines.join('\n'));
await file.writeAsString('\n', mode: FileMode.writeOnlyAppend);
} catch (e) {
_logger.severe('Unable to tidy up ${sshHomeDirectory}authorized_keys');
}
}

0 comments on commit cf416c9

Please sign in to comment.