Skip to content

Commit

Permalink
Add UDP port range support to mosh config
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Dec 19, 2019
1 parent d2b667b commit cb6e88c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 34 deletions.
34 changes: 20 additions & 14 deletions Sessions/MoshSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,24 @@
static const char *usage_format =
"Usage: mosh [options] [user@]host|IP [--] [command]"
"\r\n"
" --server=PATH mosh server on remote machine\r\n"
" (default: mosh-server)\r\n"
" --predict=adaptive local echo for slower links [default]\r\n"
"-a --predict=always use local echo even on fast links\r\n"
"-n --predict=never never use local echo\r\n"
" --server=PATH mosh server on remote machine\r\n"
" (default: mosh-server)\r\n"
" --predict=adaptive local echo for slower links [default]\r\n"
"-a --predict=always use local echo even on fast links\r\n"
"-n --predict=never never use local echo\r\n"
"\r\n"
"-k --key=<MOSH_KEY> MOSH_KEY to connect without ssh\r\n"
"-p NUM --port=NUM server-side UDP port\r\n"
"-P NUM ssh connection port\r\n"
"-T do not allocate a pseudo tty on ssh connection\r\n"
"-2 use ssh2 command\r\n"
"-I id ssh authentication identity name\r\n"
"-k --key=<MOSH_KEY> MOSH_KEY to connect without ssh\r\n"
"-p PORT[:PORT2] --port=PORT[:PORT2] server-side UDP port (range)\r\n"
"-P NUM ssh connection port\r\n"
"-T do not allocate a pseudo tty on ssh connection\r\n"
"-2 use ssh2 command\r\n"
"-I id ssh authentication identity name\r\n"
// " --ssh=COMMAND ssh command to run when setting up session\r\n"
// " (example: \"ssh -p 2222\")\r\n"
// " (default: \"ssh\")\r\n"
"\r\n"
" --verbose verbose mode\r\n"
" --help this message\r\n"
" --verbose verbose mode\r\n"
" --help this message\r\n"
"\r\n";


Expand Down Expand Up @@ -301,7 +301,13 @@ - (void)processMoshSettings:(BKHosts *)host

self.sessionParams.serverPath = self.sessionParams.serverPath ?: server;

self.sessionParams.port = self.sessionParams.port ?: [host.moshPort stringValue];
if (!self.sessionParams.port && host.moshPort) {
self.sessionParams.port = [host.moshPort stringValue];
if (host.moshPortEnd) {
self.sessionParams.port = [NSString stringWithFormat:@"%@:%@", host.moshPort, host.moshPortEnd];
}
}
// self.sessionParams.port = self.sessionParams.port ?: [host.moshPort stringValue];

NSString *startupCmd = host.moshStartup.length ? host.moshStartup : nil;
self.sessionParams.startupCmd = self.sessionParams.startupCmd ?: startupCmd;
Expand Down
6 changes: 3 additions & 3 deletions Settings/Base.lproj/Settings.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -789,10 +789,10 @@
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="UDP port" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="5He-rQ-CdM">
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="UDP PORT[:PORT2]" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="5He-rQ-CdM">
<rect key="frame" x="118" y="12" width="637" height="20"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" spellCheckingType="no" keyboardType="numberPad"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" spellCheckingType="no" keyboardType="numbersAndPunctuation"/>
<connections>
<action selector="textFieldDidChange:" destination="B0L-YN-Jg0" eventType="editingChanged" id="GeO-ls-ejT"/>
<outlet property="delegate" destination="B0L-YN-Jg0" id="dQF-Sn-Ie5"/>
Expand Down Expand Up @@ -2206,7 +2206,7 @@
<image name="user" width="22" height="22"/>
</resources>
<inferredMetricsTieBreakers>
<segue reference="y9I-rB-ZKT"/>
<segue reference="9Nb-rb-KdA"/>
<segue reference="dMC-e1-GtW"/>
</inferredMetricsTieBreakers>
</document>
3 changes: 2 additions & 1 deletion Settings/Model/BKHosts.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ enum BKMoshPrediction {
@property (nonatomic, strong) NSString *key;
@property (nonatomic, strong) NSString *moshServer;
@property (nonatomic, strong) NSNumber *moshPort;
@property (nonatomic, strong) NSNumber *moshPortEnd;
@property (nonatomic, strong) NSString *moshStartup;
@property (nonatomic, strong) NSNumber *prediction;
@property (nonatomic, strong) NSString *proxyCmd;
Expand All @@ -61,7 +62,7 @@ enum BKMoshPrediction {
+ (instancetype)withHost:(NSString *)ID;
+ (void)loadHosts;
+ (BOOL)saveHosts;
+ (instancetype)saveHost:(NSString *)host withNewHost:(NSString *)newHost hostName:(NSString *)hostName sshPort:(NSString *)sshPort user:(NSString *)user password:(NSString *)password hostKey:(NSString *)hostKey moshServer:(NSString *)moshServer moshPort:(NSString *)moshPort startUpCmd:(NSString *)startUpCmd prediction:(enum BKMoshPrediction)prediction proxyCmd:(NSString *)proxyCmd;
+ (instancetype)saveHost:(NSString *)host withNewHost:(NSString *)newHost hostName:(NSString *)hostName sshPort:(NSString *)sshPort user:(NSString *)user password:(NSString *)password hostKey:(NSString *)hostKey moshServer:(NSString *)moshServer moshPortRange:(NSString *)moshPortRange startUpCmd:(NSString *)startUpCmd prediction:(enum BKMoshPrediction)prediction proxyCmd:(NSString *)proxyCmd;
+ (void)updateHost:(NSString *)host withiCloudId:(CKRecordID *)iCloudId andLastModifiedTime:(NSDate *)lastModifiedTime;
+ (void)markHost:(NSString *)host forRecord:(CKRecord *)record withConflict:(BOOL)hasConflict;
+ (NSMutableArray<BKHosts *> *)all;
Expand Down
40 changes: 30 additions & 10 deletions Settings/Model/BKHosts.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ - (id)initWithCoder:(NSCoder *)coder
_key = [coder decodeObjectForKey:@"key"];
_moshServer = [coder decodeObjectForKey:@"moshServer"];
_moshPort = [coder decodeObjectForKey:@"moshPort"];
_moshPortEnd = [coder decodeObjectForKey:@"moshPortEnd"];
_moshStartup = [coder decodeObjectForKey:@"moshStartup"];
_prediction = [coder decodeObjectForKey:@"prediction"];
_lastModifiedTime = [coder decodeObjectForKey:@"lastModifiedTime"];
Expand All @@ -71,6 +72,7 @@ - (void)encodeWithCoder:(NSCoder *)encoder
[encoder encodeObject:_key forKey:@"key"];
[encoder encodeObject:_moshServer forKey:@"moshServer"];
[encoder encodeObject:_moshPort forKey:@"moshPort"];
[encoder encodeObject:_moshPortEnd forKey:@"moshPortEnd"];
[encoder encodeObject:_moshStartup forKey:@"moshStartup"];
[encoder encodeObject:_prediction forKey:@"prediction"];
[encoder encodeObject:_lastModifiedTime forKey:@"lastModifiedTime"];
Expand All @@ -79,7 +81,7 @@ - (void)encodeWithCoder:(NSCoder *)encoder
[encoder encodeObject:_proxyCmd forKey:@"proxyCmd"];
}

- (id)initWithHost:(NSString *)host hostName:(NSString *)hostName sshPort:(NSString *)sshPort user:(NSString *)user passwordRef:(NSString *)passwordRef hostKey:(NSString *)hostKey moshServer:(NSString *)moshServer moshPort:(NSString *)moshPort startUpCmd:(NSString *)startUpCmd prediction:(enum BKMoshPrediction)prediction proxyCmd:(NSString *)proxyCmd
- (id)initWithHost:(NSString *)host hostName:(NSString *)hostName sshPort:(NSString *)sshPort user:(NSString *)user passwordRef:(NSString *)passwordRef hostKey:(NSString *)hostKey moshServer:(NSString *)moshServer moshPortRange:(NSString *)moshPortRange startUpCmd:(NSString *)startUpCmd prediction:(enum BKMoshPrediction)prediction proxyCmd:(NSString *)proxyCmd
{
self = [super init];
if (self) {
Expand All @@ -94,8 +96,12 @@ - (id)initWithHost:(NSString *)host hostName:(NSString *)hostName sshPort:(NSStr
if (![moshServer isEqualToString:@""]) {
_moshServer = moshServer;
}
if (![moshPort isEqualToString:@""]) {
_moshPort = [NSNumber numberWithInt:moshPort.intValue];
if (![moshPortRange isEqualToString:@""]) {
NSArray<NSString *> *parts = [moshPortRange componentsSeparatedByString:@":"];
_moshPort = [NSNumber numberWithInt:parts[0].intValue];
if (parts.count > 1) {
_moshPortEnd = [NSNumber numberWithInt:parts[1].intValue];
}
}
_moshStartup = startUpCmd;
_prediction = [NSNumber numberWithInt:prediction];
Expand Down Expand Up @@ -149,7 +155,7 @@ + (BOOL)saveHosts
return [NSKeyedArchiver archiveRootObject:Hosts toFile:[BlinkPaths blinkHostsFile]];
}

+ (instancetype)saveHost:(NSString *)host withNewHost:(NSString *)newHost hostName:(NSString *)hostName sshPort:(NSString *)sshPort user:(NSString *)user password:(NSString *)password hostKey:(NSString *)hostKey moshServer:(NSString *)moshServer moshPort:(NSString *)moshPort startUpCmd:(NSString *)startUpCmd prediction:(enum BKMoshPrediction)prediction proxyCmd:(NSString *)proxyCmd
+ (instancetype)saveHost:(NSString *)host withNewHost:(NSString *)newHost hostName:(NSString *)hostName sshPort:(NSString *)sshPort user:(NSString *)user password:(NSString *)password hostKey:(NSString *)hostKey moshServer:(NSString *)moshServer moshPortRange:(NSString *)moshPortRange startUpCmd:(NSString *)startUpCmd prediction:(enum BKMoshPrediction)prediction proxyCmd:(NSString *)proxyCmd
{
NSString *pwdRef = @"";
if (password) {
Expand All @@ -160,7 +166,7 @@ + (instancetype)saveHost:(NSString *)host withNewHost:(NSString *)newHost hostNa
BKHosts *bkHost = [BKHosts withHost:host];
// Save password to keychain if it changed
if (!bkHost) {
bkHost = [[BKHosts alloc] initWithHost:newHost hostName:hostName sshPort:sshPort user:user passwordRef:pwdRef hostKey:hostKey moshServer:moshServer moshPort:moshPort startUpCmd:startUpCmd prediction:prediction proxyCmd:proxyCmd];
bkHost = [[BKHosts alloc] initWithHost:newHost hostName:hostName sshPort:sshPort user:user passwordRef:pwdRef hostKey:hostKey moshServer:moshServer moshPortRange:moshPortRange startUpCmd:startUpCmd prediction:prediction proxyCmd:proxyCmd];
[Hosts addObject:bkHost];
} else {
bkHost.host = newHost;
Expand All @@ -174,10 +180,14 @@ + (instancetype)saveHost:(NSString *)host withNewHost:(NSString *)newHost hostNa
bkHost.passwordRef = pwdRef;
bkHost.key = hostKey;
bkHost.moshServer = moshServer;
if (![moshPort isEqualToString:@""]) {
bkHost.moshPort = [NSNumber numberWithInt:moshPort.intValue];
}else{
bkHost.moshPort = nil;
bkHost.moshPort = nil;
bkHost.moshPortEnd = nil;
if (![moshPortRange isEqualToString:@""]) {
NSArray<NSString *> *parts = [moshPortRange componentsSeparatedByString:@":"];
bkHost.moshPort = [NSNumber numberWithInt:parts[0].intValue];
if (parts.count > 1) {
bkHost.moshPortEnd = [NSNumber numberWithInt:parts[1].intValue];
}
}
bkHost.moshStartup = startUpCmd;
bkHost.prediction = [NSNumber numberWithInt:prediction];
Expand Down Expand Up @@ -283,6 +293,7 @@ + (CKRecord *)recordFromHost:(BKHosts *)host
[hostRecord setValue:host.key forKey:@"key"];

[hostRecord setValue:host.moshPort forKey:@"moshPort"];
[hostRecord setValue:host.moshPortEnd forKey:@"moshPortEnd"];
[hostRecord setValue:host.moshServer forKey:@"moshServer"];
[hostRecord setValue:host.moshStartup forKey:@"moshStartup"];
[hostRecord setValue:host.password forKey:@"password"];
Expand All @@ -296,14 +307,23 @@ + (CKRecord *)recordFromHost:(BKHosts *)host

+ (BKHosts *)hostFromRecord:(CKRecord *)hostRecord
{
NSNumber *moshPort = [hostRecord valueForKey:@"moshPort"];
NSNumber *moshPortEnd = [hostRecord valueForKey:@"moshPortEnd"];

NSString *moshPortRange = moshPort ? moshPort.stringValue : @"";
if (moshPort && moshPortEnd) {
moshPortRange = [NSString stringWithFormat:@"%@:%@", moshPortRange, moshPortEnd.stringValue];
}


BKHosts *host = [[BKHosts alloc] initWithHost:[hostRecord valueForKey:@"host"]
hostName:[hostRecord valueForKey:@"hostName"]
sshPort:[hostRecord valueForKey:@"port"] ? [[hostRecord valueForKey:@"port"] stringValue] : @""
user:[hostRecord valueForKey:@"user"]
passwordRef:[hostRecord valueForKey:@"passwordRef"]
hostKey:[hostRecord valueForKey:@"key"]
moshServer:[hostRecord valueForKey:@"moshServer"]
moshPort:[hostRecord valueForKey:@"moshPort"] ? [[hostRecord valueForKey:@"moshPort"] stringValue] : @""
moshPortRange:moshPortRange
startUpCmd:[hostRecord valueForKey:@"moshStartup"]
prediction:[[hostRecord valueForKey:@"prediction"] intValue]
proxyCmd:[hostRecord valueForKey:@"proxyCmd"]];
Expand Down
12 changes: 10 additions & 2 deletions Settings/Network/BKiCloudSyncHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ - (void)saveHostRecord:(CKRecord *)hostRecord withHost:(NSString *)host
if (![updatedHost.host isEqualToString:oldHost.host]) {
[[BKHosts all] removeObject:oldHost];
}
NSNumber *moshPort = updatedHost.moshPort;
NSNumber *moshPortEnd = updatedHost.moshPortEnd;

NSString *moshPortRange = moshPort ? moshPort.stringValue : @"";
if (moshPort && moshPortEnd) {
moshPortRange = [NSString stringWithFormat:@"%@:%@", moshPortRange, moshPortEnd.stringValue];
}

[BKHosts saveHost:host
withNewHost:updatedHost.host
hostName:updatedHost.hostName
Expand All @@ -299,9 +307,9 @@ - (void)saveHostRecord:(CKRecord *)hostRecord withHost:(NSString *)host
password:updatedHost.password
hostKey:updatedHost.key
moshServer:updatedHost.moshServer
moshPort:updatedHost.moshPort ? updatedHost.moshPort.stringValue : @""
moshPortRange:moshPortRange
startUpCmd:updatedHost.moshStartup prediction:updatedHost.prediction.intValue
proxyCmd: updatedHost.proxyCmd
proxyCmd:updatedHost.proxyCmd
];
[BKHosts updateHost:updatedHost.host withiCloudId:hostRecord.recordID andLastModifiedTime:hostRecord.modificationDate];
}
Expand Down
25 changes: 21 additions & 4 deletions Settings/ViewControllers/BKHosts/BKHostsDetailViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ - (void)viewDidLoad
_predictionDetail.text = [BKHosts predictionStringForRawValue:_bkHost.prediction.intValue];
_moshServerField.text = _bkHost.moshServer;
if (_bkHost.moshPort != nil) {
_moshPortField.text = [NSString stringWithFormat:@"%@", _bkHost.moshPort];
if (_bkHost.moshPortEnd != nil) {
_moshPortField.text = [NSString stringWithFormat:@"%@:%@", _bkHost.moshPort, _bkHost.moshPortEnd];
} else {
_moshPortField.text = [NSString stringWithFormat:@"%@", _bkHost.moshPort];
}
}
_startUpCmdField.text = _bkHost.moshStartup;
_proxyCmdField.text = _bkHost.proxyCmd;
Expand Down Expand Up @@ -169,7 +173,7 @@ - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sende
password:_passwordField.text
hostKey:_hostKeyDetail.text
moshServer:_moshServerField.text
moshPort:_moshPortField.text
moshPortRange:_moshPortField.text
startUpCmd:_startUpCmdField.text
prediction:[BKHosts predictionValueForString:_predictionDetail.text]
proxyCmd:_proxyCmdField.text
Expand Down Expand Up @@ -212,9 +216,14 @@ - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == _sshPortField || textField == _moshPortField) {
if (textField == _sshPortField) {
NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0) || [string isEqualToString:@""];
} else if (textField == _moshPortField) {
NSMutableCharacterSet * charSet = [[NSCharacterSet decimalDigitCharacterSet] mutableCopy];
[charSet addCharactersInString:@":"];
NSCharacterSet *nonColumNonNumberSet = [charSet invertedSet];
return ([string stringByTrimmingCharactersInSet:nonColumNonNumberSet].length > 0) || [string isEqualToString:@""];
} else if (textField == _hostField || textField == _hostNameField || textField == _userField) {
if ([string isEqualToString:@" "]) {
return NO;
Expand Down Expand Up @@ -255,6 +264,14 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
if (_bkHost.iCloudRecordId) {
[[BKiCloudSyncHandler sharedHandler] deleteRecord:_bkHost.iCloudRecordId ofType:BKiCloudRecordTypeHosts];
}
NSNumber *moshPort = _bkHost.moshPort;
NSNumber *moshPortEnd = _bkHost.moshPortEnd;

NSString *moshPortRange = moshPort ? moshPort.stringValue : @"";
if (moshPort && moshPortEnd) {
moshPortRange = [NSString stringWithFormat:@"%@:%@", moshPortRange, moshPortEnd.stringValue];
}

[BKHosts saveHost:_bkHost.host
withNewHost:_bkHost.iCloudConflictCopy.host
hostName:_bkHost.iCloudConflictCopy.hostName
Expand All @@ -263,7 +280,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
password:_bkHost.iCloudConflictCopy.password
hostKey:_bkHost.iCloudConflictCopy.key
moshServer:_bkHost.iCloudConflictCopy.moshServer
moshPort:_bkHost.iCloudConflictCopy.moshPort ? _bkHost.iCloudConflictCopy.moshPort.stringValue : @""
moshPortRange: moshPortRange
startUpCmd:_bkHost.iCloudConflictCopy.moshStartup
prediction:_bkHost.iCloudConflictCopy.prediction.intValue
proxyCmd:_bkHost.iCloudConflictCopy.proxyCmd
Expand Down

0 comments on commit cb6e88c

Please sign in to comment.