Changeset 94
- Timestamp:
- 12/28/05 16:46:03 (3 years ago)
- Files:
-
- trunk/Libs/SSHTunnel.m (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Libs/SSHTunnel.m
r77 r94 50 50 - (BOOL)setTunnelHost:(NSString *)host withPort:(int)port andUser:(NSString *)user 51 51 { 52 if(open) 53 { 54 return NO; 55 } 56 57 [tunnelHost autorelease]; 58 tunnelHost = [[NSString stringWithString:host] retain]; 52 if (open) 53 return NO; 54 55 NSString *oldHost = tunnelHost; 56 tunnelHost = [host copy]; 57 [oldHost release]; 59 58 tunnelPort = port; 60 [tunnelUser autorelease]; 61 tunnelUser = [[NSString stringWithString:user] retain]; 59 60 NSString *oldUser = tunnelUser; 61 tunnelUser = [user copy]; 62 [oldUser release]; 62 63 63 64 return YES; … … 67 68 - (BOOL)setCompression:(BOOL)theBool 68 69 { 69 if(open) 70 { 71 return NO; 72 } 70 if (open) 71 return NO; 73 72 74 73 compression = theBool; 75 76 74 return YES; 77 75 } … … 80 78 - (BOOL)setRemoteAccess:(BOOL)theBool 81 79 { 82 if(open) 83 { 84 return NO; 85 } 80 if (open) 81 return NO; 86 82 87 83 remoteAccess = theBool; 88 89 84 return YES; 90 85 } … … 93 88 - (BOOL)addLocalPortForwardWithPort:(int)lport remoteHost:(NSString *)rhost remotePort:(int)rport 94 89 { 95 if((open) || (lport < 1) || (lport > 65535) || (rport < 1) || (rport > 65535)) 96 { 97 return NO; 98 } 90 if (open || lport < 1 || lport > 65535 || rport < 1 || rport > 65535) 91 return NO; 99 92 100 93 [localPortForwards addObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:lport], rhost, [NSNumber numberWithInt:rport], nil]]; 101 102 94 return YES; 103 95 } … … 106 98 - (BOOL)addRemotePortForwardWithPort:(int)rport localHost:(NSString *)lhost localPort:(int)lport 107 99 { 108 if((open) || (lport < 1) || (lport > 65535) || (rport < 1) || (rport > 65535)) 109 { 110 return NO; 111 } 100 if (open || lport < 1 || lport > 65535 || rport < 1 || rport > 65535) 101 return NO; 112 102 113 103 [remotePortForwards addObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:rport], lhost, [NSNumber numberWithInt:lport], nil]]; 114 115 104 return YES; 116 105 } … … 119 108 - (BOOL)addDynamicPortForwardWithPort:(int)lport 120 109 { 121 if((open) || (lport < 1) || (lport > 65535)) 122 { 123 return NO; 124 } 110 if (open || lport < 1 || lport > 65535) 111 return NO; 125 112 126 113 [dynamicPortForwards addObject:[NSNumber numberWithInt:lport]]; 127 128 114 return YES; 129 115 } … … 132 118 - (NSString *)getOutput 133 119 { 134 return [[[NSString alloc] initWithData:[[thePipe fileHandleForReading] readDataToEndOfFile] encoding:NS ASCIIStringEncoding] autorelease];120 return [[[NSString alloc] initWithData:[[thePipe fileHandleForReading] readDataToEndOfFile] encoding:NSUTF8StringEncoding] autorelease]; 135 121 } 136 122 … … 139 125 { 140 126 141 if((!theSelector) || (!theObject)) 142 { 127 if (!theSelector || !theObject) 143 128 return; 144 }145 129 146 130 closeSelector = theSelector; 147 131 closeObject = theObject; 148 132 149 if(theInfo) 150 { 133 if (theInfo) 151 134 closeInfo = theInfo; 152 }153 154 135 else 155 {156 136 closeInfo = nil; 157 }158 137 } 159 138 … … 166 145 - (BOOL)openTunnel 167 146 { 168 int i; 169 NSMutableArray *arguments; 170 171 /* If the PID is > 1, the tunnel should be open. */ 172 if([self isOpen]) 173 { 174 return NO; 175 } 176 177 if((!tunnelHost) || ([tunnelHost isEqualToString:@""])) 178 { 179 return NO; 180 } 147 148 if ([self isOpen]) 149 return NO; 150 151 if (!tunnelHost || [tunnelHost isEqualToString:@""]) 152 return NO; 181 153 182 154 open = YES; … … 195 167 NSString *toolPath; 196 168 NSString *sshPathString = [[NSUserDefaults standardUserDefaults] stringForKey:SSHToolsPathString]; 197 if (floor(NSAppKitVersionNumber) != NSAppKitVersionNumber10_3) {169 if (floor(NSAppKitVersionNumber) != NSAppKitVersionNumber10_3) 198 170 // It's not Panther (i.e. either Jaguar or Tiger) 199 171 toolPath = [sshPathString stringByAppendingPathComponent:@"ssh"]; 200 } else {201 if([dynamicPortForwards count] > 0 && [[sshPathString stringByStandardizingPath] isEqualToString:@"/usr/bin"]) {202 /* Time to use our internal build */203 toolPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ssh"];204 } else {205 toolPath = [sshPathString stringByAppendingPathComponent:@"ssh"];206 } 207 }208 arguments = [NSMutableArray arrayWithObject:toolPath];209 210 for(i=0; i < [localPortForwards count]; i++)211 { 212 [arguments addObject:[NSString stringWithFormat:@"-L%d:%@:%d", [[ [localPortForwards objectAtIndex:i]objectAtIndex:0] intValue],213 [[localPortForwards objectAtIndex:i]objectAtIndex:1],214 [[[localPortForwards objectAtIndex:i]objectAtIndex:2] intValue]]172 else if([dynamicPortForwards count] > 0 && [[sshPathString stringByStandardizingPath] isEqualToString:@"/usr/bin"]) 173 174 /* Time to use our internal build */ 175 toolPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ssh"]; 176 else 177 toolPath = [sshPathString stringByAppendingPathComponent:@"ssh"]; 178 179 NSMutableArray *arguments = [NSMutableArray arrayWithObject:toolPath]; 180 NSEnumerator *e = [localPortForwards objectEnumerator]; 181 NSArray *portForward; 182 while (portForward = [e nextObject]) 183 { 184 [arguments addObject:[NSString stringWithFormat:@"-L%d:%@:%d", [[portForward objectAtIndex:0] intValue], 185 [portForward objectAtIndex:1], 186 [[portForward objectAtIndex:2] intValue]] 215 187 ]; 216 188 } 217 189 218 for(i=0; i < [remotePortForwards count]; i++) 219 { 220 [arguments addObject:[NSString stringWithFormat:@"-R%d:%@:%d", [[[remotePortForwards objectAtIndex:i] objectAtIndex:0] intValue], 221 [[remotePortForwards objectAtIndex:i] objectAtIndex:1], 222 [[[remotePortForwards objectAtIndex:i] objectAtIndex:2] intValue]] 190 e = [remotePortForwards objectEnumerator]; 191 while (portForward = [e nextObject]) 192 { 193 [arguments addObject:[NSString stringWithFormat:@"-R%d:%@:%d", [[portForward objectAtIndex:0] intValue], 194 [portForward objectAtIndex:1], 195 [[portForward objectAtIndex:2] intValue]] 223 196 ]; 224 197 } 225 198 226 199 // No dynamic ports under Jaguar 227 if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_2) { 228 for(i=0; i < [dynamicPortForwards count]; i++) 200 if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_2) 201 { 202 e = [dynamicPortForwards objectEnumerator]; 203 NSNumber *dynamicPortForward; 204 while (dynamicPortForward = [e nextObject]) 229 205 { 230 206 [arguments addObject:@"-D"]; 231 207 232 208 /* A bit awkward, but it makes sure a number is given. */ 233 [arguments addObject:[NSString stringWithFormat:@"%d", [ [dynamicPortForwards objectAtIndex:i]intValue]]];209 [arguments addObject:[NSString stringWithFormat:@"%d", [dynamicPortForward intValue]]]; 234 210 } 235 211 } 236 212 237 if ((tunnelPort > 0) && (tunnelPort < 65535))238 { 239 [arguments addObject:@"-p"];240 [arguments addObject:[NSString stringWithFormat:@"%d", tunnelPort]];213 if (tunnelPort > 0 && tunnelPort < 65535) 214 { 215 [arguments addObject:@"-p"]; 216 [arguments addObject:[NSString stringWithFormat:@"%d", tunnelPort]]; 241 217 } 242 218 … … 245 221 [arguments addObject:@"-x"]; 246 222 247 if(compression) 248 { 223 if (compression) 249 224 [arguments addObject:@"-C"]; 250 } 251 252 if(remoteAccess) 253 { 225 226 if (remoteAccess) 254 227 [arguments addObject:@"-g"]; 255 }256 228 257 229 [arguments addObject:@"-o"]; 258 230 [arguments addObject:@"PreferredAuthentications=hostbased,publickey,password,keyboard-interactive"]; 259 231 260 if((tunnelUser) && (![tunnelUser isEqualToString:@""])) 261 { 262 [arguments addObject:[NSString stringWithFormat:@"%@@%@", tunnelUser, tunnelHost]]; 263 } 232 if (tunnelUser && ![tunnelUser isEqualToString:@""]) 233 [arguments addObject:[NSString stringWithFormat:@"%@@%@", tunnelUser, tunnelHost]]; 264 234 265 235 else 266 { 267 [arguments addObject:[NSString stringWithFormat:@"%@", tunnelHost]]; 268 } 236 [arguments addObject:[NSString stringWithFormat:@"%@", tunnelHost]]; 269 237 270 238 [tunnel setArguments:arguments]; … … 279 247 withValue:[[NSUserDefaults standardUserDefaults] stringForKey:SocketPathString]]; 280 248 281 if ((closeSelector) && (closeObject)) {249 if (closeSelector && closeObject) 282 250 [tunnel handleTerminateWithSelector:closeSelector toObject:closeObject withInfo:closeInfo]; 283 }284 251 285 252 thePipe = [[NSPipe alloc] init]; … … 288 255 289 256 /* Launch ssh. */ 290 if([tunnel launch] == NO) { 291 return NO; 292 } 257 if (![tunnel launch]) 258 return NO; 293 259 294 260 open = YES; 295 296 261 return YES; 297 262 } … … 299 264 - (void)closeTunnel 300 265 { 301 if ((open) && (tunnel)) {266 if (open && tunnel) 302 267 [tunnel terminate]; 303 }304 268 305 269 [tunnel release];
