client/ftpd/src/se/lu/thep/coreftpd/ftp_server/FTPSetup.java

Code
Comments
Other
Rev Date Author Line
741 10 Oct 06 olle 1 /*
1652 22 May 07 gregory 2  $Id$
741 10 Oct 06 olle 3
1916 31 Aug 07 jari 4  Copyright (C) 2006 Olle Mansson
1916 31 Aug 07 jari 5  Copyright (C) 2007 Gregory Vincic
741 10 Oct 06 olle 6
1652 22 May 07 gregory 7  This file is part of Proteios.
1652 22 May 07 gregory 8  Available at http://www.proteios.org/
741 10 Oct 06 olle 9
1652 22 May 07 gregory 10  Proteios is free software; you can redistribute it and/or modify it
1652 22 May 07 gregory 11  under the terms of the GNU General Public License as published by
1652 22 May 07 gregory 12  the Free Software Foundation; either version 2 of the License, or
1652 22 May 07 gregory 13  (at your option) any later version.
741 10 Oct 06 olle 14
1652 22 May 07 gregory 15  Proteios is distributed in the hope that it will be useful, but
1652 22 May 07 gregory 16  WITHOUT ANY WARRANTY; without even the implied warranty of
1652 22 May 07 gregory 17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1652 22 May 07 gregory 18  General Public License for more details.
741 10 Oct 06 olle 19
1652 22 May 07 gregory 20  You should have received a copy of the GNU General Public License
1652 22 May 07 gregory 21  along with this program; if not, write to the Free Software
1652 22 May 07 gregory 22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1652 22 May 07 gregory 23  02111-1307, USA.
1652 22 May 07 gregory 24  */
741 10 Oct 06 olle 25
741 10 Oct 06 olle 26 //  Xerver Free Web Server
741 10 Oct 06 olle 27 //  Copyright (C) 2002-2005 Omid Rouhani
741 10 Oct 06 olle 28 //
741 10 Oct 06 olle 29 //
741 10 Oct 06 olle 30 //  This program is free software; you can redistribute it and/or
741 10 Oct 06 olle 31 //  modify it under the terms of the GNU General Public License
741 10 Oct 06 olle 32 //  as published by the Free Software Foundation; either version 2
741 10 Oct 06 olle 33 //  of the License, or (at your option) any later version.
741 10 Oct 06 olle 34 //
741 10 Oct 06 olle 35 //  This program is distributed in the hope that it will be useful,
741 10 Oct 06 olle 36 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
741 10 Oct 06 olle 37 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
741 10 Oct 06 olle 38 //  GNU General Public License for more details.
741 10 Oct 06 olle 39 //
741 10 Oct 06 olle 40 //  You should have received a copy of the GNU General Public License
741 10 Oct 06 olle 41 //  along with this program; if not, write to the Free Software
741 10 Oct 06 olle 42 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
741 10 Oct 06 olle 43 //
741 10 Oct 06 olle 44 //
741 10 Oct 06 olle 45 //  #############################################################
741 10 Oct 06 olle 46 //  ##  YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT:          ##
741 10 Oct 06 olle 47 //  ##  HTTP://WWW.JAVASCRIPT.NU/XERVER/                       ##
741 10 Oct 06 olle 48 //  ##                                                         ##
741 10 Oct 06 olle 49 //  ##  IF YOUR SOFTWARE IS NOT RELEASED UNDER THE             ##
741 10 Oct 06 olle 50 //  ##  GNU GENERAL PUBLIC LICENSE (GPL),                      ##
741 10 Oct 06 olle 51 //  ##  PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!!   ##
741 10 Oct 06 olle 52 //  ##                                                         ##
741 10 Oct 06 olle 53 //  ##  FOR FULL LICENSE, PLEASE READ "XERVER LICENSE".        ##
741 10 Oct 06 olle 54 //  #############################################################
741 10 Oct 06 olle 55
1652 22 May 07 gregory 56 package se.lu.thep.coreftpd.ftp_server;
741 10 Oct 06 olle 57
791 23 Oct 06 olle 58 import se.lu.thep.coreftpd.common.HostInfo;
741 10 Oct 06 olle 59 import java.net.BindException;
741 10 Oct 06 olle 60 import java.net.ServerSocket;
741 10 Oct 06 olle 61 import java.net.Socket;
741 10 Oct 06 olle 62
741 10 Oct 06 olle 63 /**
1652 22 May 07 gregory 64  * 
1652 22 May 07 gregory 65  * <B>About this class:</B> <BR>
1652 22 May 07 gregory 66  * Run this file when you want to start Xerver FTP setup. <BR>
741 10 Oct 06 olle 67  * Note that you can pass arguments to this file when you run its main method.
1652 22 May 07 gregory 68  * 
1652 22 May 07 gregory 69  * 
741 10 Oct 06 olle 70  * @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a>
741 10 Oct 06 olle 71  * @version 1.0
741 10 Oct 06 olle 72  */
741 10 Oct 06 olle 73
1652 22 May 07 gregory 74 final public class FTPSetup extends Thread {
1652 22 May 07 gregory 75   private static final boolean b_showErrors = false;
1652 22 May 07 gregory 76
1652 22 May 07 gregory 77   public final static int i_portNr = 32124;
1652 22 May 07 gregory 78
741 10 Oct 06 olle 79   private ServerSocket myServerSocket;
741 10 Oct 06 olle 80
1652 22 May 07 gregory 81   private static boolean FTPSetupHasStarted = false;
741 10 Oct 06 olle 82
1652 22 May 07 gregory 83   private boolean b_portError = false;
741 10 Oct 06 olle 84
1652 22 May 07 gregory 85   private boolean b_portInUse = false;
1652 22 May 07 gregory 86
1652 22 May 07 gregory 87   public static boolean b_printOutput = false;
1652 22 May 07 gregory 88
1652 22 May 07 gregory 89   public static void main(String[] s) {
1652 22 May 07 gregory 90     FTPSetup setup = null;
1652 22 May 07 gregory 91     boolean b_startRemote = false;
1652 22 May 07 gregory 92     boolean b_startServer = true;
1652 22 May 07 gregory 93     int port = i_portNr;
1652 22 May 07 gregory 94     b_printOutput = true;
1652 22 May 07 gregory 95
1652 22 May 07 gregory 96     if (s.length != 0) {
1652 22 May 07 gregory 97       for (int i = 0; i < s.length; i++) {
1652 22 May 07 gregory 98         if (s[i].startsWith("-p")) {
1652 22 May 07 gregory 99           try {
1652 22 May 07 gregory 100             port = Integer.parseInt(s[i].substring(2));
1652 22 May 07 gregory 101           } catch (Exception e) {
1652 22 May 07 gregory 102             b_startServer = false;
741 10 Oct 06 olle 103             showOutput("Invalid port.");
741 10 Oct 06 olle 104             showOutput("The flag given should be \"-pXX\" where XX is a port number.");
741 10 Oct 06 olle 105           }
1652 22 May 07 gregory 106         } else if (s[i].equals("-remote") || s[i].equals("-r")) {
1652 22 May 07 gregory 107           b_startRemote = true;
1652 22 May 07 gregory 108         } else // if (s[i].equals("-help") || s[i].equals("-h"))
741 10 Oct 06 olle 109         {
1652 22 May 07 gregory 110           b_startServer = false;
741 10 Oct 06 olle 111           showOutput("Flags:");
741 10 Oct 06 olle 112           showOutput("-h or -help = Show this help text.");
1652 22 May 07 gregory 113           showOutput("-pXX = Start setup on port XX, not " + i_portNr
1652 22 May 07 gregory 114               + " which is default.");
741 10 Oct 06 olle 115           showOutput("-r or -remote = Allow all IP-addresses to change settings in setup.");
741 10 Oct 06 olle 116         }
741 10 Oct 06 olle 117       }
741 10 Oct 06 olle 118     }
741 10 Oct 06 olle 119
1652 22 May 07 gregory 120     if (b_startServer) {
1652 22 May 07 gregory 121       setup = new FTPSetup(port);
741 10 Oct 06 olle 122
1652 22 May 07 gregory 123       if (!setup.getPortError()) {
1652 22 May 07 gregory 124         String s_IP = HostInfo.getIPorLocalhost();
741 10 Oct 06 olle 125         showOutput("FTPSetup is now running, please visit:");
1652 22 May 07 gregory 126         showOutput("http://" + s_IP + ":" + port + "/");
1652 22 May 07 gregory 127       } else {
741 10 Oct 06 olle 128         if (setup.isPortInUse())
1652 22 May 07 gregory 129           showOutput("Port "
1652 22 May 07 gregory 130               + port
1652 22 May 07 gregory 131               + " is already in use.\nXerver FTP Setup is probably already running.");
741 10 Oct 06 olle 132         else
1652 22 May 07 gregory 133           showOutput("Port "
1652 22 May 07 gregory 134               + port
1652 22 May 07 gregory 135               + " is an invalid port number.\nPlease choose another port number.");
741 10 Oct 06 olle 136       }
741 10 Oct 06 olle 137     }
741 10 Oct 06 olle 138
1652 22 May 07 gregory 139     if (b_startRemote && b_startServer) {
1652 22 May 07 gregory 140       // This inside the other if-statement because here we are 100% sure
1652 22 May 07 gregory 141       // "setup!=null"... (unnecessary?)
1652 22 May 07 gregory 142       if (!setup.getPortError()) {
741 10 Oct 06 olle 143         showOutput("Remote setup has been started.");
741 10 Oct 06 olle 144         setup.startFTPSetup();
741 10 Oct 06 olle 145       }
741 10 Oct 06 olle 146     }
741 10 Oct 06 olle 147
741 10 Oct 06 olle 148     if (b_startServer)
741 10 Oct 06 olle 149       setup.start();
741 10 Oct 06 olle 150   }
741 10 Oct 06 olle 151
1652 22 May 07 gregory 152   public FTPSetup() {
741 10 Oct 06 olle 153     this(i_portNr);
790 23 Oct 06 olle 154     // *** Debug Info
1652 22 May 07 gregory 155     System.out.println("FTPSetup::FTPSetup(): Start - i_portNr = "
1652 22 May 07 gregory 156         + i_portNr);
741 10 Oct 06 olle 157   }
741 10 Oct 06 olle 158
1652 22 May 07 gregory 159   public FTPSetup(int port) {
790 23 Oct 06 olle 160     // *** Debug Info
1652 22 May 07 gregory 161     System.out.println("FTPSetup::FTPSetup(int port): Start - port = "
1652 22 May 07 gregory 162         + port);
741 10 Oct 06 olle 163     listenToPort(port);
741 10 Oct 06 olle 164   }
741 10 Oct 06 olle 165
1652 22 May 07 gregory 166   private void listenToPort(int port) {
1652 22 May 07 gregory 167     try {
1652 22 May 07 gregory 168       try {
741 10 Oct 06 olle 169         myServerSocket = new ServerSocket(port);
1652 22 May 07 gregory 170       } catch (BindException be) // port is in use
741 10 Oct 06 olle 171       {
1652 22 May 07 gregory 172         b_portError = true;
1652 22 May 07 gregory 173         b_portInUse = true;
1652 22 May 07 gregory 174       } catch (IllegalArgumentException iae) // port number is invalid,
1652 22 May 07 gregory 175                           // for example if
1652 22 May 07 gregory 176                           // port=999999
741 10 Oct 06 olle 177       {
1652 22 May 07 gregory 178         b_portError = true;
741 10 Oct 06 olle 179       }
1652 22 May 07 gregory 180     } catch (Exception e) {
741 10 Oct 06 olle 181       showError(e);
741 10 Oct 06 olle 182     }
741 10 Oct 06 olle 183   }
741 10 Oct 06 olle 184
2386 14 Nov 07 gregory 185   @Override
1652 22 May 07 gregory 186   public void run() {
741 10 Oct 06 olle 187     try {
741 10 Oct 06 olle 188       Socket so_userConnection;
1652 22 May 07 gregory 189       if (!b_portInUse && !b_portError) // If [port is not in use] and
1652 22 May 07 gregory 190                         // [port is valid (not like port
1652 22 May 07 gregory 191                         // 999999)]
741 10 Oct 06 olle 192       {
1652 22 May 07 gregory 193         while (true) // IMPORTANT!!! make sure this loop won't go
1652 22 May 07 gregory 194                 // around too many times without doing anything
1652 22 May 07 gregory 195                 // or java will use up to 100% CPU. Instead let
1652 22 May 07 gregory 196                 // the loop be halted by for example an accept
1652 22 May 07 gregory 197                 // call which will wait for a network connection
1652 22 May 07 gregory 198                 // to be established...
741 10 Oct 06 olle 199         {
1652 22 May 07 gregory 200           try {
1652 22 May 07 gregory 201             // System.out.println("V�ntar p� connection...");
1652 22 May 07 gregory 202             try {
1652 22 May 07 gregory 203               so_userConnection = myServerSocket.accept(); // Wait
1652 22 May 07 gregory 204                                       // until
1652 22 May 07 gregory 205                                       // a
1652 22 May 07 gregory 206                                       // request
1652 22 May 07 gregory 207                                       // is
1652 22 May 07 gregory 208                                       // sent
1652 22 May 07 gregory 209                                       // to
1652 22 May 07 gregory 210                                       // the
1652 22 May 07 gregory 211                                       // server
1652 22 May 07 gregory 212                                       // (with
1652 22 May 07 gregory 213                                       // "myServerSocket.setSoTimeout()"
1652 22 May 07 gregory 214                                       // we
1652 22 May 07 gregory 215                                       // can
1652 22 May 07 gregory 216                                       // limit
1652 22 May 07 gregory 217                                       // the
1652 22 May 07 gregory 218                                       // time
1652 22 May 07 gregory 219                                       // we
1652 22 May 07 gregory 220                                       // wait
1652 22 May 07 gregory 221                                       // for
1652 22 May 07 gregory 222                                       // "accept")
741 10 Oct 06 olle 223               so_userConnection.setSoTimeout(5000);
1652 22 May 07 gregory 224             } catch (Exception e) // If the port is already in
1652 22 May 07 gregory 225                         // use... (However, we should
1652 22 May 07 gregory 226                         // never be able to get in here,
1652 22 May 07 gregory 227                         // but just to be safe)
741 10 Oct 06 olle 228             {
741 10 Oct 06 olle 229               stopFTPSetup();
741 10 Oct 06 olle 230               break;
741 10 Oct 06 olle 231             }
741 10 Oct 06 olle 232
1652 22 May 07 gregory 233             if (FTPSetupHasStarted
1652 22 May 07 gregory 234                 || so_userConnection.getLocalAddress().equals(
1652 22 May 07 gregory 235                     so_userConnection.getInetAddress())) {
741 10 Oct 06 olle 236               (new FTPSetupConnection(so_userConnection)).start();
1652 22 May 07 gregory 237             } else {
741 10 Oct 06 olle 238               so_userConnection.close();
741 10 Oct 06 olle 239             }
1652 22 May 07 gregory 240           } catch (Exception e) {
1652 22 May 07 gregory 241             if (b_showErrors)
1652 22 May 07 gregory 242               System.out.println("An error has occured @ run\n"
1652 22 May 07 gregory 243                   + e);
741 10 Oct 06 olle 244           }
741 10 Oct 06 olle 245         }
741 10 Oct 06 olle 246       }
1652 22 May 07 gregory 247     } catch (Exception e) {
1652 22 May 07 gregory 248       showError(e);
1652 22 May 07 gregory 249     }
741 10 Oct 06 olle 250   }
741 10 Oct 06 olle 251
1652 22 May 07 gregory 252   private void showError(Exception e) {
1652 22 May 07 gregory 253     if (b_showErrors) {
741 10 Oct 06 olle 254       System.out.println("Error: " + e);
741 10 Oct 06 olle 255       e.printStackTrace();
741 10 Oct 06 olle 256     }
741 10 Oct 06 olle 257   }
741 10 Oct 06 olle 258
1652 22 May 07 gregory 259   private static void showOutput(String txt) {
741 10 Oct 06 olle 260     if (b_printOutput)
741 10 Oct 06 olle 261       System.out.println(txt);
741 10 Oct 06 olle 262   }
741 10 Oct 06 olle 263
1652 22 May 07 gregory 264   public void startFTPSetup() {
1652 22 May 07 gregory 265     FTPSetupHasStarted = true;
741 10 Oct 06 olle 266   }
741 10 Oct 06 olle 267
1652 22 May 07 gregory 268   public void stopFTPSetup() {
1652 22 May 07 gregory 269     FTPSetupHasStarted = false;
741 10 Oct 06 olle 270   }
741 10 Oct 06 olle 271
1652 22 May 07 gregory 272   public static int getPort() {
741 10 Oct 06 olle 273     return i_portNr;
741 10 Oct 06 olle 274   }
741 10 Oct 06 olle 275
1652 22 May 07 gregory 276   public boolean isPortInUse() {
741 10 Oct 06 olle 277     return b_portInUse;
741 10 Oct 06 olle 278   }
741 10 Oct 06 olle 279
1652 22 May 07 gregory 280   public boolean getPortError() {
741 10 Oct 06 olle 281     return b_portError;
741 10 Oct 06 olle 282   }
741 10 Oct 06 olle 283 }