~ubuntu-branches/ubuntu/natty/smuxi/natty

« back to all changes in this revision

Viewing changes to src/Frontend/SshTunnelManager.cs

  • Committer: Bazaar Package Importer
  • Author(s): Mirco Bauer
  • Date: 2010-01-11 22:47:12 UTC
  • mto: (4.2.5 sid) (1.1.4 upstream) (15.1.1 sid)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20100111224712-cdvx3vondz2g08vg
ImportĀ upstreamĀ versionĀ 0.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
131
131
        
132
132
        public void Setup()
133
133
        {
 
134
            Trace.Call();
 
135
 
134
136
            if (String.IsNullOrEmpty(f_Program)) {
135
137
                // use plink by default if it's there
136
138
                if (File.Exists("plink.exe")) {
144
146
                throw new ApplicationException(_("SSH client application was not found: " + f_Program));
145
147
            }
146
148
            if (f_Program.ToLower().EndsWith("putty.exe")) {
147
 
                throw new ApplicationException(_("SSH client must be either OpenSSH (ssh) or Plink (plink.exe, _not_ putty.exe)"));
 
149
                throw new ApplicationException(_("SSH client must be either OpenSSH (ssh) or Plink (plink.exe, not putty.exe)"));
148
150
            }
149
151
            
150
152
            bool isPutty = false;
171
173
        
172
174
        public void Connect()
173
175
        {
 
176
            Trace.Call();
 
177
 
 
178
#if LOG4NET
 
179
            f_Logger.Debug("Connect(): checking if local forward port is free...");
 
180
#endif
 
181
            using (TcpClient tcpClient = new TcpClient()) {
 
182
                try {
 
183
                    tcpClient.Connect(f_ForwardBindAddress, f_ForwardBindPort);
 
184
                    // the connect worked, panic!
 
185
                    var msg = String.Format(
 
186
                        _("The local SSH forwarding port {0} is already in " +
 
187
                          "use. Is there an old SSH tunnel still active?"),
 
188
                        f_ForwardBindPort
 
189
                    );
 
190
                    throw new ApplicationException(msg);
 
191
                } catch (SocketException ex) {
 
192
                }
 
193
            }
 
194
 
174
195
#if LOG4NET
175
196
            f_Logger.Debug("Connect(): setting up ssh tunnel using command: " +
176
197
                           f_ProcessStartInfo.FileName + " " +
186
207
                    string output = f_Process.StandardOutput.ReadToEnd();
187
208
                    string error = f_Process.StandardError.ReadToEnd();
188
209
                    string msg = String.Format(
189
 
                        _("SSH tunnel setup failed with (exit code: {0})\n\n" +
 
210
                        _("SSH tunnel setup failed (exit code: {0})\n\n" +
190
211
                          "SSH program: {1}\n" +
191
212
                          "SSH parameters: {2}\n\n" +
192
213
                          "Program Error:\n" +
193
214
                          "{3}\n" +
194
 
                          "Prgram Output:\n" +
 
215
                          "Program Output:\n" +
195
216
                          "{4}\n"),
196
217
                        f_Process.ExitCode,
197
218
                        f_ProcessStartInfo.FileName,
251
272
        
252
273
        public void Disconnect()
253
274
        {
 
275
            Trace.Call();
 
276
 
254
277
            if (f_Process != null && !f_Process.HasExited) {
 
278
#if LOG4NET
 
279
                f_Logger.Debug("Disconnect(): killing ssh tunnel...");
 
280
#endif
255
281
                f_Process.Kill();
 
282
                f_Process.WaitForExit();
 
283
#if LOG4NET
 
284
                f_Logger.Debug("Disconnect(): ssh tunnel exited");
 
285
#endif
256
286
            }
257
287
        }
258
288
        
260
290
        {
261
291
            string sshArguments = String.Empty;
262
292
 
 
293
            Version sshVersion = GetOpenSshVersion();
263
294
            // starting with OpenSSH version 4.4p1 we can use the
264
295
            // ExitOnForwardFailure option for detecting tunnel issues better
265
296
            // as the process will quit nicely, for more details see:
266
297
            // http://projects.qnetp.net/issues/show/145
267
298
            // NOTE: the patch level is mapped to the micro component
268
 
            if (GetOpenSshVersion() >= new Version("4.4.1")) {
 
299
            if (sshVersion >= new Version("4.4.1")) {
269
300
                // exit if the tunnel setup didn't work somehow
270
301
                sshArguments += " -o ExitOnForwardFailure=yes";
271
302
            }
272
 
            
 
303
 
 
304
            // with OpenSSH 3.8 we can use the keep-alive feature of SSH that
 
305
            // will check the remote peer in defined intervals and kills the
 
306
            // tunnel if it reached the max value
 
307
            if (sshVersion >= new Version("3.8")) {
 
308
                // exit if the peer can't be reached for more than 90 seconds
 
309
                sshArguments += " -o ServerAliveInterval=30 -o ServerAliveCountMax=3";
 
310
            }
 
311
 
273
312
            // run in the background (detach)
274
313
            // plink doesn't support this and we can't control the process this way!
275
314
            //sshArguments += " -f";
363
402
            }
364
403
 
365
404
            string msg = String.Format(
366
 
                _("Couldn't get OpenSSH version (exit code: {0})\n\n" +
 
405
                _("OpenSSH version number not found (exit code: {0})\n\n" +
367
406
                  "SSH program: {1}\n\n" +
368
407
                  "Program Error:\n" +
369
408
                  "{2}\n" +
370
 
                  "Prgram Output:\n" +
 
409
                  "Program Output:\n" +
371
410
                  "{3}\n"),
372
411
                f_Process.ExitCode,
373
412
                f_Program,