Skip to content

Commit

Permalink
Merge pull request #306 from atsign-foundation/config-reader-gui
Browse files Browse the repository at this point in the history
feat: add config file reader for sshnp
  • Loading branch information
gkc authored Aug 9, 2023
2 parents b36de89 + dbc0edc commit da72bcc
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/sshnoports/lib/common/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ String getDefaultSshDirectory(String homeDirectory) {
return '$homeDirectory/.ssh/'.replaceAll('/', Platform.pathSeparator);
}

String getDefaultSshnpDirectory(String homeDirectory) {
return '$homeDirectory/.sshnp/'.replaceAll('/', Platform.pathSeparator);
}

String getDefaultSshnpConfigDirectory(String homeDirectory) {
return '$homeDirectory/.sshnp/config'.replaceAll('/', Platform.pathSeparator);
}

/// Checks if the provided atSign's atServer has been properly activated with a public RSA key.
/// `atClient` must be authenticated
/// `atSign` is the atSign to check
Expand Down
72 changes: 72 additions & 0 deletions packages/sshnoports/lib/sshnp/sshnp_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,78 @@ class SSHNPParams {
listDevices: partial.listDevices,
);
}

factory SSHNPParams.fromConfigFile(String fileName) {
return SSHNPParams.fromPartial(SSHNPPartialParams.fromConfig(fileName));
}

static Future<Iterable<SSHNPParams>> getConfigFilesFromDirectory(
[String? directory]) async {
var params = <SSHNPParams>[];

var homeDirectory = getHomeDirectory(throwIfNull: true)!;
directory ??= getDefaultSshnpConfigDirectory(homeDirectory);
var files = Directory(directory).list();

await files.forEach((file) {
if (file is! File) return;
try {
var p = SSHNPParams.fromConfigFile(file.path);
params.add(p);
} catch (e) {
print('Error reading config file: ${file.path}');
print(e);
}
});

return params;
}

Future<File> toFile(String fileName, {bool overwrite = false}) async {
var file = File(fileName);
var exists = await file.exists();

if (exists && !overwrite) {
print('Failed to write config file: $fileName already exists');
return file;
}

// FileMode.write will create the file if it does not exist
// and overwrite existing files if it does exist
return file.writeAsString(toConfig(), mode: FileMode.write);
}

Map<String, dynamic> toArgs() {
return {
'from': clientAtSign,
'to': sshnpdAtSign,
'host': host,
'device': device,
'port': port,
'local-port': localPort,
'key-file': atKeysFilePath,
'ssh-public-key': sendSshPublicKey,
'local-ssh-options': localSshOptions,
'rsa': rsa,
'remote-user-name': remoteUsername,
'verbose': verbose,
'root-domain': rootDomain,
'list-devices': listDevices,
};
}

String toConfig() {
var lines = <String>[];
for (var entry in toArgs().entries) {
var key = SSHNPArg.fromName(entry.key).bashName;
var value = entry.value;
if (value is List) {
value = value.join(';');
}
lines.add('$key=$value');
}
return lines.join('\n');
}
}

/// A class which contains a subset of the SSHNPParams
Expand Down

0 comments on commit da72bcc

Please sign in to comment.