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: implemented #202 #208

Merged
merged 7 commits into from
Jun 15, 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
49 changes: 31 additions & 18 deletions lib/sshnp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ class SSHNP {
/// Defaults to false
final bool rsa;



// ====================================================================
// Volatile instance variables, injected via constructor
// but possibly modified later on
Expand Down Expand Up @@ -85,9 +83,10 @@ class SSHNP {
@visibleForTesting
late final String clientAtSign;

/// The username to use on the remote host in the ssh session. Is fetched
/// from the sshnpd by [fetchRemoteUserName] during [init]
late final String remoteUsername;
/// The username to use on the remote host in the ssh session. Either passed
/// through class constructor or fetched from the sshnpd
/// by [fetchRemoteUserName] during [init]
String? remoteUsername;
purnimavenkatasubbu marked this conversation as resolved.
Show resolved Hide resolved

/// Set by [generateSshKeys] during [init].
/// sshnp generates a new keypair for each ssh session, using ed25519 by
Expand Down Expand Up @@ -148,7 +147,8 @@ class SSHNP {
// volatile fields
required this.host,
required this.port,
required this.localPort
required this.localPort,
this.remoteUsername,
}) {
nameSpace = '$device.sshnp';
clientAtSign = atClient.getCurrentAtSign()!;
Expand Down Expand Up @@ -185,12 +185,14 @@ class SSHNP {

await generateSshKeys();

await fetchRemoteUserName();
if (remoteUsername == null) {
await fetchRemoteUserName();
}

// find a spare local port
if (localPort == '0') {
ServerSocket serverSocket =
await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
localPort = serverSocket.port.toString();
await serverSocket.close();
}
Expand Down Expand Up @@ -226,7 +228,9 @@ class SSHNP {
..namespace = nameSpace
..sharedBy = clientAtSign
..sharedWith = sshnpdAtSign
..metadata = (Metadata()..ttr=-1..ttl=10000);
..metadata = (Metadata()
..ttr = -1
..ttl = 10000);

try {
await atClient.notificationService
Expand Down Expand Up @@ -285,8 +289,8 @@ class SSHNP {
String notificationKey = notification.key
.replaceAll('${notification.to}:', '')
.replaceAll('.$device.sshnp${notification.from}', '')
// convert to lower case as the latest AtClient converts notification
// keys to lower case when received
// convert to lower case as the latest AtClient converts notification
// keys to lower case when received
.toLowerCase();
logger.info('Received $notificationKey notification');
if (notification.value == 'connected') {
Expand All @@ -299,11 +303,11 @@ class SSHNP {
}
}


/// Look up the user name ... we expect a key to have been shared with us by
/// sshnpd. Let's say we are @human running sshnp, and @daemon is running
/// sshnpd, then we expect a key to have been shared whose ID is
/// @human:username.device.sshnp@daemon
/// Is not called if remoteUserName was set via constructor
Future<void> fetchRemoteUserName() async {
purnimavenkatasubbu marked this conversation as resolved.
Show resolved Hide resolved
AtKey userNameRecordID = AtKey.fromString('$clientAtSign:username.$nameSpace$sshnpdAtSign');
try {
Expand Down Expand Up @@ -385,8 +389,8 @@ class SSHNP {
..sharedBy = clientAtSign // shared by us
..sharedWith = host // shared with the sshrvd host
..metadata = (Metadata()
// as we are sending a notification to the sshrvd namespace,
// we don't want to append our namespace
// as we are sending a notification to the sshrvd namespace,
// we don't want to append our namespace
..namespaceAware = false
..ttr = -1
..ttl = 10000);
Expand Down Expand Up @@ -518,6 +522,8 @@ class SSHNP {
p.rsa = r['rsa'];
p.verbose = r['verbose'];

p.remoteUsername = r['remote-user-name'];

return p;
}

Expand Down Expand Up @@ -549,7 +555,8 @@ class SSHNP {
localPort: p.localPort,
localSshOptions: p.localSshOptions,
rsa: p.rsa,
sendSshPublicKey: p.sendSshPublicKey
sendSshPublicKey: p.sendSshPublicKey,
remoteUsername: p.remoteUsername,
);
if (p.verbose) {
sshnp.logger.logger.level = Level.INFO;
Expand Down Expand Up @@ -584,8 +591,9 @@ class SSHNP {
..atKeysFilePath = atKeysFilePath
..atProtocolEmitted = Version(2, 0, 0);

AtOnboardingService onboardingService =
AtOnboardingServiceImpl(clientAtSign, atOnboardingConfig, atServiceFactory: ServiceFactoryWithNoOpSyncService());
AtOnboardingService onboardingService = AtOnboardingServiceImpl(
clientAtSign, atOnboardingConfig,
atServiceFactory: ServiceFactoryWithNoOpSyncService());

await onboardingService.authenticate();

Expand Down Expand Up @@ -637,6 +645,10 @@ class SSHNP {
abbr: 'r',
defaultsTo: false,
help: 'Use RSA 4096 keys rather than the default ED25519 keys');
parser.addOption('remote-user-name',
abbr: 'u',
mandatory: false,
help: 'user name to use in the ssh session on the remote host');
return parser;
}

Expand All @@ -649,7 +661,7 @@ class SSHNP {
late String sshnpDir;
if (Platform.executable.endsWith('${Platform.pathSeparator}sshnp')) {
List<String> pathList =
Platform.resolvedExecutable.split(Platform.pathSeparator);
Platform.resolvedExecutable.split(Platform.pathSeparator);
pathList.removeLast();
sshnpDir = pathList.join(Platform.pathSeparator) + Platform.pathSeparator;

Expand All @@ -675,4 +687,5 @@ class SSHNPParams {
late final List<String> localSshOptions;
late final bool rsa;
late final bool verbose;
late final String? remoteUsername;
}
1 change: 1 addition & 0 deletions test/sshnp_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void main() {
expect(p.localSshOptions, []);
expect(p.rsa, false);
expect(p.verbose, false);
expect(p.remoteUsername, null);
});
});
}