Home » openjdk-7 » net.bonzoun » cocodonkey » [javadoc | source]

    1   //
    2   //  SshTunnel.java
    3   //  CocoDonkey
    4   //  $Id: SshTunnel.java,v 1.2 2002/07/05 21:34:26 uid85676 Exp $
    5   //
    6   //  Created by Fred Bonzoun on Thu May 23 2002.
    7   //  Copyright (c) 2002 Bonzoun. All rights reserved.
    8   //
    9   //  This library is free software; you can redistribute it and/or modify
   10   //  it under the terms of the GNU Lesser General Public License as published
   11   //  by the Free Software Foundation; either version 2.1 of the License, or
   12   //  (at your option) any later version.
   13   //
   14   //  This library is distributed in the hope that it will be useful,
   15   //  but WITHOUT ANY WARRANTY; without even the implied warranty of
   16   //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17   //  GNU Lesser General Public License for more details.
   18   //
   19   //  You should have received a copy of the GNU Lesser General Public License
   20   //  along with this program; if not, write to the Free Software
   21   //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   22   //
   23   
   24   package net.bonzoun.cocodonkey;
   25   
   26   import java.io;
   27   
   28   public class SshTunnel {
   29   
   30       private static int nextPort = 4001;
   31       
   32       private Process tunnel;
   33       private String hostname;
   34       private String ackAppPath;
   35       private int portIn;
   36       private int portOut;
   37   
   38       public SshTunnel(String hostname) throws IOException {
   39           this(hostname, nextPort++, 4000, null);
   40       }
   41   
   42       public SshTunnel(String hostname, String ackAppPath) throws IOException {
   43           this(hostname, nextPort++, 4000, ackAppPath);
   44       }
   45       
   46       public SshTunnel(String hostname, int portIn, int portOut, String ackAppPath) throws IOException {
   47           this.hostname = hostname;
   48           this.portIn = portIn;
   49           this.portOut = portOut;
   50           this.ackAppPath = ackAppPath;
   51           start();
   52       }
   53       
   54       private void start() throws IOException {
   55           String cmd = "/usr/bin/ssh -N -C -L " + portIn + ":127.0.0.1:" + portOut + " " + hostname;
   56           //String cmd = "/usr/bin/ssh";
   57           //String[] args = new String[]{"-N", "-C", "-L", "" + portIn + ":127.0.0.1:" + portOut, hostname};
   58           String[] env = null;
   59           //if (ackAppPath!=null)
   60           //    env = new String[]{"DISPLAY=localhost", "SSH_ACKPASS="+ackAppPath};
   61           //run(cmd, args, env);
   62           tunnel = Runtime.getRuntime().exec(cmd, env);
   63           try {
   64               Thread.sleep(5000);
   65               StringWriter res = new StringWriter();
   66               //while(tunnel.getInputStream().available()>0)
   67               //    System.out.print((char)tunnel.getInputStream().read());
   68               while(tunnel.getErrorStream().available()>0)
   69                   res.write((char)tunnel.getErrorStream().read());
   70               tunnel.exitValue();
   71               String msg = res.toString();
   72               if (msg.indexOf("Host key verification failed")>=0)
   73                   msg = "You must connect to this host with the terminal at least once to validate its key";
   74               else if (msg.indexOf("Permission denied")>=0)
   75                   msg = "Password requested, you must install your ssh public identity file on the remote host";
   76               throw new IOException(msg);
   77           }
   78           catch(InterruptedException e) {
   79           }
   80           catch(IllegalThreadStateException e) {
   81               // good !
   82           }
   83               
   84       }
   85   
   86       public int port() {
   87           return portIn;
   88       }
   89       
   90       public void close() {
   91           tunnel.destroy();
   92       }
   93   
   94       public void restart() throws IOException {
   95           close();
   96           start();
   97       }
   98   }
   99   
  100   // $Log: SshTunnel.java,v $
  101   // Revision 1.2  2002/07/05 21:34:26  uid85676
  102   // No more native launch and kill
  103   //
  104   // Revision 1.1  2002/05/25 23:41:40  fortun
  105   // *** empty log message ***
  106   //

Home » openjdk-7 » net.bonzoun » cocodonkey » [javadoc | source]