client/ftpd/src/se/lu/thep/coreftpd/ftp_server/FTPServerController.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.io.File;
741 10 Oct 06 olle 60
741 10 Oct 06 olle 61 /**
1652 22 May 07 gregory 62  * 
1652 22 May 07 gregory 63  * <B>About this class:</B> <BR>
1652 22 May 07 gregory 64  * Want to bound Xerver Free FTP Server to your GNU GPL application? <BR>
1652 22 May 07 gregory 65  * Then this is the only class your program will use directly. <BR>
1652 22 May 07 gregory 66  * You shall not have to learn about the other classes that are used. <BR>
1652 22 May 07 gregory 67  * All methods in this class are static. <BR>
741 10 Oct 06 olle 68  * <BR>
1652 22 May 07 gregory 69  * When Xerver Free FTP Server was written it was designed to be easy to bound
1652 22 May 07 gregory 70  * with other applications. <BR>
1652 22 May 07 gregory 71  * Xerver Free Web Server is for example using Xerver Free FTP Server. <BR>
741 10 Oct 06 olle 72  * <BR>
1652 22 May 07 gregory 73  * <B>To start using Xerver Free FTP Server in your own application follow these
1652 22 May 07 gregory 74  * steps:</B> <BR>
1652 22 May 07 gregory 75  * 1.) Download the source files for Xerver Free Web Server. <BR>
1652 22 May 07 gregory 76  * 2.) Copy the folders called "ftp_server" and "common" to the directory where
1652 22 May 07 gregory 77  * you have stored your Java GNU GPL application. <BR>
1652 22 May 07 gregory 78  * 3.) Add "<CODE>import ftp_server.*;import common.*;</CODE>" to the top of
1652 22 May 07 gregory 79  * all your .java-files. <BR>
1652 22 May 07 gregory 80  * 4.) You're done! Now you can call the methods of this class and start the FTP
1652 22 May 07 gregory 81  * server. <BR>
741 10 Oct 06 olle 82  * <BR>
1652 22 May 07 gregory 83  * <B>Examples:</B> <BR>
1652 22 May 07 gregory 84  * Here is an example to show you how you can easily implement Xerver Free FTP
1652 22 May 07 gregory 85  * Server into your application. <BR>
741 10 Oct 06 olle 86  * <FONT COLOR=gray>
1652 22 May 07 gregory 87  * 
741 10 Oct 06 olle 88  * <PRE>
1652 22 May 07 gregory 89  * 
1652 22 May 07 gregory 90  * <FONT COLOR=red>import ftp_server.*;</FONT> <FONT COLOR=red>import common.*;</FONT>
1652 22 May 07 gregory 91  * 
1652 22 May 07 gregory 92  * class Dummy { public static void main(String [] s) { <FONT
1652 22 May 07 gregory 93  * COLOR=red>FTPServerController.startFTPServer();</FONT> System.out.println(<FONT
1652 22 May 07 gregory 94  * COLOR=blue>"Xerver Free FTP Server "</FONT>+<FONT
1652 22 May 07 gregory 95  * COLOR=red>FTPServerController.getVersionString()</FONT>+<FONT COLOR=blue>"
1652 22 May 07 gregory 96  * is running."</FONT>); System.out.println(<FONT COLOR=blue>"Connect to "</FONT>+<FONT
1652 22 May 07 gregory 97  * COLOR=red>FTPServerController.getIP()</FONT>); System.out.println(<FONT
1652 22 May 07 gregory 98  * COLOR=blue>"at port "</FONT>+<FONT COLOR=red>FTPServerController.getPort()</FONT>+<FONT
1652 22 May 07 gregory 99  * COLOR=blue>"."</FONT>);
1652 22 May 07 gregory 100  * 
1652 22 May 07 gregory 101  * <FONT COLOR=red>FTPServerController.stopFTPServer();</FONT>
1652 22 May 07 gregory 102  * System.out.println(<FONT COLOR=blue>"You can no longer connect to "</FONT>+<FONT
1652 22 May 07 gregory 103  * COLOR=red>FTPServerController.getIP()</FONT>+<FONT COLOR=blue>"."</FONT>);
1652 22 May 07 gregory 104  * 
1652 22 May 07 gregory 105  * <FONT COLOR=red>FTPServerController.startFTPLocalSetup();</FONT>
1652 22 May 07 gregory 106  * System.out.println(<FONT COLOR=blue>"Only you (=your IP) can now visit
1652 22 May 07 gregory 107  * Xerver FTP Setup at:"</FONT>); System.out.println(<FONT
1652 22 May 07 gregory 108  * COLOR=blue>"http://"</FONT>+<FONT COLOR=red>FTPServerController.getIP()</FONT>+<FONT
1652 22 May 07 gregory 109  * COLOR=blue>":"</FONT>+<FONT COLOR=red>FTPServerController.getSetupPort()</FONT>+<FONT
1652 22 May 07 gregory 110  * COLOR=blue>"/"</FONT>);
1652 22 May 07 gregory 111  * 
1652 22 May 07 gregory 112  * <FONT COLOR=red>FTPServerController.startFTPRemoteSetup();</FONT>
1652 22 May 07 gregory 113  * System.out.println(<FONT COLOR=blue>"Anyone (any IP) can now visit Xerver
1652 22 May 07 gregory 114  * FTP Setup at:"</FONT>); System.out.println(<FONT COLOR=blue>"http://"</FONT>+<FONT
1652 22 May 07 gregory 115  * COLOR=red>FTPServerController.getIP()</FONT>+<FONT COLOR=blue>":"</FONT>+<FONT
1652 22 May 07 gregory 116  * COLOR=red>FTPServerController.getSetupPort()</FONT>+<FONT COLOR=blue>"/"</FONT>);
1652 22 May 07 gregory 117  * 
1652 22 May 07 gregory 118  * <FONT COLOR=red>FTPServerController.startFTPLocalSetup();</FONT>
1652 22 May 07 gregory 119  * System.out.println(<FONT COLOR=blue>"Only you (=your IP) can now visit
1652 22 May 07 gregory 120  * Xerver FTP Setup at:"</FONT>); System.out.println(<FONT
1652 22 May 07 gregory 121  * COLOR=blue>"http://"</FONT>+<FONT COLOR=red>FTPServerController.getIP()</FONT>+<FONT
1652 22 May 07 gregory 122  * COLOR=blue>":"</FONT>+<FONT COLOR=red>FTPServerController.getSetupPort()</FONT>+<FONT
1652 22 May 07 gregory 123  * COLOR=blue>"/"</FONT>); System.out.println(<FONT COLOR=blue>"Remote
1652 22 May 07 gregory 124  * computers can no longer reach setup."</FONT>);
1652 22 May 07 gregory 125  * 
1652 22 May 07 gregory 126  * System.out.println(<FONT COLOR=blue>"Can it be easier to implement an FTP
1652 22 May 07 gregory 127  * server or what? :)"</FONT>); System.out.println(<FONT COLOR=blue>"Enjoy
1652 22 May 07 gregory 128  * Xerver Free FTP Server!"</FONT>); }//main }//class
1652 22 May 07 gregory 129  * 
741 10 Oct 06 olle 130  * </PRE>
1652 22 May 07 gregory 131  * 
741 10 Oct 06 olle 132  * </FONT>
1652 22 May 07 gregory 133  * 
741 10 Oct 06 olle 134  * @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a>
741 10 Oct 06 olle 135  * @version 1.0
741 10 Oct 06 olle 136  */
741 10 Oct 06 olle 137
1652 22 May 07 gregory 138 final public class FTPServerController {
1652 22 May 07 gregory 139   private static FTPServer server = null;
741 10 Oct 06 olle 140
1652 22 May 07 gregory 141   private static FTPSetup setup = null;
741 10 Oct 06 olle 142
1652 22 May 07 gregory 143   private static final double d_FTPversion = 1.05;
741 10 Oct 06 olle 144
1652 22 May 07 gregory 145   private static final String s_FTPversion = "1.05";
741 10 Oct 06 olle 146
1652 22 May 07 gregory 147   private static String s_serverIP = null;
1652 22 May 07 gregory 148
1652 22 May 07 gregory 149   // public final static String
1652 22 May 07 gregory 150   // s_initFile="ftp_data"+File.separator+"FTP.cfg";
1652 22 May 07 gregory 151   // public final static String s_dataFolder="ftp_data"+File.separator;
1652 22 May 07 gregory 152   // public final static String s_userFolder="users"+File.separator;
790 23 Oct 06 olle 153   /*
790 23 Oct 06 olle 154    * Proteios settings
790 23 Oct 06 olle 155    */
1652 22 May 07 gregory 156   public final static String s_initFile = "/ftp.properties";
741 10 Oct 06 olle 157
1652 22 May 07 gregory 158   public final static String s_dataFolder = "org" + File.separator
1652 22 May 07 gregory 159       + "proteios" + File.separator + "ftp" + File.separator + "ftp_data"
1652 22 May 07 gregory 160       + File.separator;
1652 22 May 07 gregory 161
1652 22 May 07 gregory 162   public final static String s_userFolder = "org" + File.separator
1652 22 May 07 gregory 163       + "proteios" + File.separator + "ftp" + File.separator + "users"
1652 22 May 07 gregory 164       + File.separator;
1652 22 May 07 gregory 165
1652 22 May 07 gregory 166   public final static String s_userDataExtension = ".dat";
1652 22 May 07 gregory 167
1652 22 May 07 gregory 168   public static void main(String[] s) {
741 10 Oct 06 olle 169     startFTPServer();
741 10 Oct 06 olle 170     startFTPLocalSetup();
741 10 Oct 06 olle 171   }
741 10 Oct 06 olle 172
741 10 Oct 06 olle 173   /**
1652 22 May 07 gregory 174    * Starts the FTP server.
1652 22 May 07 gregory 175    */
1652 22 May 07 gregory 176   public static void startFTPServer() {
1652 22 May 07 gregory 177     if (server == null) // If [server has not started] or [port number has
1652 22 May 07 gregory 178               // changed (then restart server)]...
741 10 Oct 06 olle 179     {
1652 22 May 07 gregory 180       server = new FTPServer();
741 10 Oct 06 olle 181       server.start();
741 10 Oct 06 olle 182     }
741 10 Oct 06 olle 183
741 10 Oct 06 olle 184     server.startFTPServer();
741 10 Oct 06 olle 185   }
741 10 Oct 06 olle 186
741 10 Oct 06 olle 187   /**
1652 22 May 07 gregory 188    * Stops the FTP server.
1652 22 May 07 gregory 189    */
1652 22 May 07 gregory 190   public static void stopFTPServer() {
741 10 Oct 06 olle 191     /*
1652 22 May 07 gregory 192      * if (server==null) { server=new FTPServer(); server.start(); }
1652 22 May 07 gregory 193      */
1652 22 May 07 gregory 194     if (server != null) {
741 10 Oct 06 olle 195       server.stopFTPServer();
741 10 Oct 06 olle 196     }
741 10 Oct 06 olle 197   }
741 10 Oct 06 olle 198
741 10 Oct 06 olle 199   /**
1652 22 May 07 gregory 200    * This will start FTP Setup and it allows everyone to reach the setup and
1652 22 May 07 gregory 201    * change settings to the server.
1652 22 May 07 gregory 202    */
1652 22 May 07 gregory 203   public static void startFTPRemoteSetup() {
1652 22 May 07 gregory 204     if (setup == null) {
1652 22 May 07 gregory 205       setup = new FTPSetup();
741 10 Oct 06 olle 206       setup.start();
741 10 Oct 06 olle 207     }
741 10 Oct 06 olle 208
741 10 Oct 06 olle 209     setup.startFTPSetup();
741 10 Oct 06 olle 210   }
741 10 Oct 06 olle 211
741 10 Oct 06 olle 212   /**
1652 22 May 07 gregory 213    * This will start FTP Setup but only allows your local IP to visit the
1652 22 May 07 gregory 214    * setup and change settings to the server.
1652 22 May 07 gregory 215    */
1652 22 May 07 gregory 216   // Turn off remote setup if already turned on
1652 22 May 07 gregory 217   public static void startFTPLocalSetup() {
1652 22 May 07 gregory 218     if (setup == null) {
1652 22 May 07 gregory 219       setup = new FTPSetup();
741 10 Oct 06 olle 220       setup.start();
741 10 Oct 06 olle 221     }
741 10 Oct 06 olle 222
741 10 Oct 06 olle 223     setup.stopFTPSetup();
741 10 Oct 06 olle 224   }
741 10 Oct 06 olle 225
741 10 Oct 06 olle 226   /**
1652 22 May 07 gregory 227    * This is the same as <CODE>startFTPLocalSetup()</CODE>! <BR>
1652 22 May 07 gregory 228    * Despite the name, this does not turn off the Xerver FTP Setup. <BR>
1652 22 May 07 gregory 229    * Your local IP is always allowed to change settings in setup.
1652 22 May 07 gregory 230    */
1652 22 May 07 gregory 231   public static void stopFTPSetup() {
1652 22 May 07 gregory 232     if (setup == null) {
1652 22 May 07 gregory 233       setup = new FTPSetup();
741 10 Oct 06 olle 234       setup.start();
741 10 Oct 06 olle 235     }
741 10 Oct 06 olle 236
741 10 Oct 06 olle 237     setup.stopFTPSetup();
741 10 Oct 06 olle 238   }
741 10 Oct 06 olle 239
741 10 Oct 06 olle 240   /**
1652 22 May 07 gregory 241    * Get the version of Xerver Free FTP Server.
1652 22 May 07 gregory 242    */
1652 22 May 07 gregory 243   public static String getVersionString() {
741 10 Oct 06 olle 244     return s_FTPversion;
741 10 Oct 06 olle 245   }
741 10 Oct 06 olle 246
741 10 Oct 06 olle 247   /**
1652 22 May 07 gregory 248    * Get the version of Xerver Free FTP Server.
1652 22 May 07 gregory 249    */
1652 22 May 07 gregory 250   public static double getVersion() {
741 10 Oct 06 olle 251     return d_FTPversion;
741 10 Oct 06 olle 252   }
741 10 Oct 06 olle 253
741 10 Oct 06 olle 254   /**
1652 22 May 07 gregory 255    * Get your outer IP (the IP that is shown to computers outside your
1652 22 May 07 gregory 256    * network).
1652 22 May 07 gregory 257    */
1652 22 May 07 gregory 258   public static String getIP() {
1652 22 May 07 gregory 259     if (s_serverIP == null) {
1652 22 May 07 gregory 260       s_serverIP = HostInfo.getIPorLocalhost();
741 10 Oct 06 olle 261     }
741 10 Oct 06 olle 262
741 10 Oct 06 olle 263     return s_serverIP;
741 10 Oct 06 olle 264   }
741 10 Oct 06 olle 265
741 10 Oct 06 olle 266   /**
1652 22 May 07 gregory 267    * Get the port the server is listening for. Clients shall connect to this
1652 22 May 07 gregory 268    * port.
1652 22 May 07 gregory 269    */
1652 22 May 07 gregory 270   public static int getPort() {
1652 22 May 07 gregory 271     if (server == null)
741 10 Oct 06 olle 272       return -1;
741 10 Oct 06 olle 273     else
741 10 Oct 06 olle 274       return server.getPort();
741 10 Oct 06 olle 275   }
741 10 Oct 06 olle 276
741 10 Oct 06 olle 277   /**
1652 22 May 07 gregory 278    * Get the port the server setup is listening to. Your browser shall connect
1652 22 May 07 gregory 279    * to http://localhost:PortNumber/ when you want change settings.
1652 22 May 07 gregory 280    */
1652 22 May 07 gregory 281   public static int getSetupPort() {
741 10 Oct 06 olle 282     return FTPSetup.getPort();
741 10 Oct 06 olle 283   }
741 10 Oct 06 olle 284
741 10 Oct 06 olle 285   /**
1652 22 May 07 gregory 286    * Returns true iff the FTP Server port is already in use.
1652 22 May 07 gregory 287    */
1652 22 May 07 gregory 288   public static boolean isPortInUseServer() {
741 10 Oct 06 olle 289     return server.isPortInUse();
741 10 Oct 06 olle 290   }
741 10 Oct 06 olle 291
741 10 Oct 06 olle 292   /**
1652 22 May 07 gregory 293    * Returns true iff we have a port error for the FTP Server (for example if
1652 22 May 07 gregory 294    * you try to run the server on port 999999).
1652 22 May 07 gregory 295    */
1652 22 May 07 gregory 296   public static boolean getPortErrorServer() {
741 10 Oct 06 olle 297     return server.getPortError();
741 10 Oct 06 olle 298   }
741 10 Oct 06 olle 299
741 10 Oct 06 olle 300   /**
1652 22 May 07 gregory 301    * Returns true iff the FTP Setup Server port is already in use.
1652 22 May 07 gregory 302    */
1652 22 May 07 gregory 303   public static boolean isPortInUseSetup() {
741 10 Oct 06 olle 304     return setup.isPortInUse();
741 10 Oct 06 olle 305   }
741 10 Oct 06 olle 306
741 10 Oct 06 olle 307   /**
1652 22 May 07 gregory 308    * Returns true iff we have a port error for the FTP Setup Server (for
1652 22 May 07 gregory 309    * example if you try to run the setup server on port 999999).
1652 22 May 07 gregory 310    */
1652 22 May 07 gregory 311   public static boolean getPortErrorSetup() {
741 10 Oct 06 olle 312     return setup.getPortError();
741 10 Oct 06 olle 313   }
741 10 Oct 06 olle 314 }