Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add config file reader for sshnp #306

Merged
merged 2 commits into from
Aug 9, 2023
Merged
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
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