Skip to content

Commit

Permalink
Our default IP discover method is local. Refs blinksh#1127
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Nov 27, 2020
1 parent 08407be commit 6e6bd18
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 52 deletions.
99 changes: 47 additions & 52 deletions Sessions/MoshSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,11 @@ - (int)initParamaters:(int)argc argv:(char **)argv
return [self dieMsg:@(usage_format)];
}

// Our default is local
if (!self.sessionParams.experimentalRemoteIp) {
self.sessionParams.experimentalRemoteIp = @"remote";
self.sessionParams.experimentalRemoteIp = @"local";
}

if ([@"remote" isEqual:self.sessionParams.experimentalRemoteIp]) {
} else if ([@"local" isEqual:self.sessionParams.experimentalRemoteIp]) {
} else {
if (![@[@"remote", @"local"] containsObject:self.sessionParams.experimentalRemoteIp]) {
return [self dieMsg:@(usage_format)];
}

Expand Down Expand Up @@ -357,11 +355,16 @@ - (void)setConnParamsWithSsh:(NSString *)ssh userHost:(NSString *)userHost port:
{
ssh = ssh ? ssh : @"ssh";

NSString *echoSshConnectionCommand = @"";
if ([@"remote" isEqual:self.sessionParams.experimentalRemoteIp]) {
echoSshConnectionCommand = @"echo \"\" && echo \"MOSH SSH_CONNECTION $SSH_CONNECTION\" &&";
NSMutableArray * sshArgs;

BOOL useIPFromSSHConnectionEnv = [@"remote" isEqual:self.sessionParams.experimentalRemoteIp];
if (useIPFromSSHConnectionEnv) {
NSString * echoSshConnectionCommand = @"echo \"\" && echo \"MOSH SSH_CONNECTION $SSH_CONNECTION\" &&";
sshArgs = [@[ssh, @"-o compression=no", sshTTY ? @"-t" : @"-T", userHost, echoSshConnectionCommand, command] mutableCopy];
} else {
sshArgs = [@[ssh, @"-o _printaddress=yes", @"-o compression=no", sshTTY ? @"-t" : @"-T", userHost, command] mutableCopy];
}
NSMutableArray*sshArgs = [@[ssh, @"-o _printaddress=yes", @"-o compression=no", sshTTY ? @"-t" : @"-T", userHost, echoSshConnectionCommand, command] mutableCopy];

if (port) {
[sshArgs insertObject:[NSString stringWithFormat:@"-p %@", port] atIndex:1];
}
Expand All @@ -372,67 +375,59 @@ - (void)setConnParamsWithSsh:(NSString *)ssh userHost:(NSString *)userHost port:
[sshArgs insertObject:@"-v" atIndex:1];
}

NSString *sshCmd = [sshArgs componentsJoinedByString:@" "];
NSString * sshCmd = [sshArgs componentsJoinedByString:@" "];
[self debugMsg:sshCmd];

FILE *term_r = ios_popen(sshCmd.UTF8String, "r");

// Capture ssh output and process parameters for Mosh connection
char *buf = NULL;
size_t buf_sz = 0;
NSString *line;

NSString *ipPattern = @"Connected to (\\S*)$";
NSRegularExpression *ipFormat = [NSRegularExpression regularExpressionWithPattern:ipPattern options:0 error:nil];
NSString * line;

NSString *connPattern = @"MOSH CONNECT (\\d+) (\\S*)$";
NSRegularExpression *connFormat = [NSRegularExpression regularExpressionWithPattern:connPattern options:0 error:nil];

NSString *sshConnectionPattern = @"MOSH SSH_CONNECTION (\\S*) (\\d*) (\\S*) (\\d*)$";
NSRegularExpression *sshConnectionFormat = [NSRegularExpression regularExpressionWithPattern:sshConnectionPattern options:0 error:nil];

NSTextCheckingResult *match;
NSString* ipPattern;
int ipMatchIdx;

if (useIPFromSSHConnectionEnv) {
ipPattern = @"MOSH SSH_CONNECTION (\\S*) (\\d*) (\\S*) (\\d*)$";
ipMatchIdx = 3;
} else {
ipPattern = @"Connected to (\\S*)$";
ipMatchIdx = 1;
}
NSRegularExpression *ipFormat = [NSRegularExpression regularExpressionWithPattern:ipPattern options:0 error:nil];

NSString * connPattern = @"MOSH CONNECT (\\d+) (\\S*)$";
NSRegularExpression * connFormat = [NSRegularExpression regularExpressionWithPattern:connPattern options:0 error:nil];
NSTextCheckingResult * match;
ssize_t n = 0;

NSString *ipFromIpFormat = @"";
NSString *ipFromSshConnection = @"";

while ((n = getline(&buf, &buf_sz, term_r)) >= 0) {
line = [NSString stringWithFormat:@"%.*s", (int)n, buf];
if ((match = [ipFormat firstMatchInString:line options:0 range:NSMakeRange(0, line.length)])) {
NSRange matchRange = [match rangeAtIndex:1];
ipFromIpFormat = [line substringWithRange:matchRange];
} else if ((match = [connFormat firstMatchInString:line options:0 range:NSMakeRange(0, line.length)])) {
NSRange matchRange = [match rangeAtIndex:1];
self.sessionParams.port = [line substringWithRange:matchRange];
matchRange = [match rangeAtIndex:2];
self.sessionParams.key = [line substringWithRange:matchRange];
} else if ((match = [sshConnectionFormat firstMatchInString:line options:0 range:NSMakeRange(0, line.length)])) {
NSRange matchRange = [match rangeAtIndex:3];
ipFromSshConnection = [line substringWithRange:matchRange];
} else {
fwrite(buf, 1, n, _stream.out);
NSRange lineRange = NSMakeRange(0, line.length);

match = [ipFormat firstMatchInString:line options:0 range:lineRange];
if (match) {
self.sessionParams.ip = [line substringWithRange:[match rangeAtIndex:ipMatchIdx]];
continue;
}

match = [connFormat firstMatchInString:line options:0 range:lineRange];
if (match) {
self.sessionParams.port = [line substringWithRange:[match rangeAtIndex:1]];
self.sessionParams.key = [line substringWithRange:[match rangeAtIndex:2]];
break;
}

fwrite(buf, 1, n, _stream.out);
}

fclose(term_r);

if ([@"remote" isEqual:self.sessionParams.experimentalRemoteIp]) {
if (ipFromSshConnection) {
self.sessionParams.ip = ipFromSshConnection;
} else {
*error = [NSError errorWithDomain:@"blink.mosh.ssh" code:0 userInfo:@{ NSLocalizedDescriptionKey : @"Did not find remote IP address" }];
return;
}
} else {
if (ipFromIpFormat) {
self.sessionParams.ip = ipFromIpFormat;
} else {
*error = [NSError errorWithDomain:@"blink.mosh.ssh" code:0 userInfo:@{ NSLocalizedDescriptionKey : @"Did not find remote IP address" }];
return;
}
}
if (!self.sessionParams.ip) {
*error = [NSError errorWithDomain:@"blink.mosh.ssh" code:0 userInfo:@{ NSLocalizedDescriptionKey : @"Did not find remote IP address" }];
return;
}

if (self.sessionParams.key == nil || self.sessionParams.port == nil) {
*error = [NSError errorWithDomain:@"blink.mosh.ssh" code:0 userInfo:@{ NSLocalizedDescriptionKey : @"Did not find remote IP address" }];
Expand Down
3 changes: 3 additions & 0 deletions Sessions/SessionParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import UIKit
case predictionMode
case startupCmd
case serverPath
case experimentalRemoteIp
}

override func encode(with coder: NSCoder) {
Expand All @@ -94,6 +95,7 @@ import UIKit
coder.encode(predictionMode, for: Key.predictionMode)
coder.encode(startupCmd, for: Key.startupCmd)
coder.encode(serverPath, for: Key.serverPath)
coder.encode(experimentalRemoteIp, for: Key.experimentalRemoteIp)
}

required init?(coder: NSCoder) {
Expand All @@ -105,6 +107,7 @@ import UIKit
self.predictionMode = coder.decode(for: Key.predictionMode)
self.startupCmd = coder.decode(for: Key.startupCmd)
self.serverPath = coder.decode(for: Key.serverPath)
self.experimentalRemoteIp = coder.decode(for: Key.experimentalRemoteIp)
}


Expand Down

0 comments on commit 6e6bd18

Please sign in to comment.