client/ftpd/src/se/lu/thep/coreftpd/common/HostInfo.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 //  Xerver Free Web Server
741 10 Oct 06 olle 26 //  Copyright (C) 2002-2005 Omid Rouhani
741 10 Oct 06 olle 27 //
741 10 Oct 06 olle 28 //
741 10 Oct 06 olle 29 //  This program is free software; you can redistribute it and/or
741 10 Oct 06 olle 30 //  modify it under the terms of the GNU General Public License
741 10 Oct 06 olle 31 //  as published by the Free Software Foundation; either version 2
741 10 Oct 06 olle 32 //  of the License, or (at your option) any later version.
741 10 Oct 06 olle 33 //
741 10 Oct 06 olle 34 //  This program is distributed in the hope that it will be useful,
741 10 Oct 06 olle 35 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
741 10 Oct 06 olle 36 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
741 10 Oct 06 olle 37 //  GNU General Public License for more details.
741 10 Oct 06 olle 38 //
741 10 Oct 06 olle 39 //  You should have received a copy of the GNU General Public License
741 10 Oct 06 olle 40 //  along with this program; if not, write to the Free Software
741 10 Oct 06 olle 41 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
741 10 Oct 06 olle 42 //
741 10 Oct 06 olle 43 //
741 10 Oct 06 olle 44 //  #############################################################
741 10 Oct 06 olle 45 //  ##  YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT:          ##
741 10 Oct 06 olle 46 //  ##  HTTP://WWW.JAVASCRIPT.NU/XERVER/                       ##
741 10 Oct 06 olle 47 //  ##                                                         ##
741 10 Oct 06 olle 48 //  ##  IF YOUR SOFTWARE IS NOT RELEASED UNDER THE             ##
741 10 Oct 06 olle 49 //  ##  GNU GENERAL PUBLIC LICENSE (GPL),                      ##
741 10 Oct 06 olle 50 //  ##  PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!!   ##
741 10 Oct 06 olle 51 //  ##                                                         ##
741 10 Oct 06 olle 52 //  ##  FOR FULL LICENSE, PLEASE READ "XERVER LICENSE".        ##
741 10 Oct 06 olle 53 //  #############################################################
1652 22 May 07 gregory 54 package se.lu.thep.coreftpd.common;
741 10 Oct 06 olle 55
741 10 Oct 06 olle 56 import java.io.BufferedReader;
741 10 Oct 06 olle 57 import java.io.InputStreamReader;
741 10 Oct 06 olle 58 import java.net.InetAddress;
741 10 Oct 06 olle 59 import java.net.URL;
741 10 Oct 06 olle 60
741 10 Oct 06 olle 61 /**
1652 22 May 07 gregory 62  * <B>About this class:</B> <BR>
1652 22 May 07 gregory 63  * Classes you can use to get a computers outer IP. <BR>
741 10 Oct 06 olle 64  * Note that this class connects to www.javascript.nu to get the outer IP.
1652 22 May 07 gregory 65  * 
741 10 Oct 06 olle 66  * @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a>
741 10 Oct 06 olle 67  * @version 1.0
741 10 Oct 06 olle 68  */
2418 22 Nov 07 gregory 69 final public class HostInfo
2418 22 Nov 07 gregory 70 {
1652 22 May 07 gregory 71   private static String s_IPstored = null;
741 10 Oct 06 olle 72
2418 22 Nov 07 gregory 73
741 10 Oct 06 olle 74   /**
1652 22 May 07 gregory 75    * Returns computers outer IP. If no internet connection is found or if
1652 22 May 07 gregory 76    * www.javascript.nu is down it returns <CODE>InetAddress.getLocalHost().getHostAddress()</CODE>.
1652 22 May 07 gregory 77    * <BR>
1652 22 May 07 gregory 78    * Exception are only thrown from
1652 22 May 07 gregory 79    * "InetAddress.getLocalHost().getHostAddress()", which should not happen
1652 22 May 07 gregory 80    * under normal circumstances(??).
1652 22 May 07 gregory 81    */
2418 22 Nov 07 gregory 82   static public String getIP()
2418 22 Nov 07 gregory 83       throws Exception
2418 22 Nov 07 gregory 84   {
1652 22 May 07 gregory 85     String s_IP = getIPFromInternet();
1652 22 May 07 gregory 86     if (s_IP == null)
1652 22 May 07 gregory 87       s_IP = InetAddress.getLocalHost().getHostAddress();
741 10 Oct 06 olle 88     return s_IP;
741 10 Oct 06 olle 89   }
741 10 Oct 06 olle 90
2418 22 Nov 07 gregory 91
741 10 Oct 06 olle 92   /**
1652 22 May 07 gregory 93    * Returns local computers host name. <BR>
1652 22 May 07 gregory 94    * If <CODE>InetAddress.getLocalHost()</CODE> throws an Exception
1652 22 May 07 gregory 95    * "localhost" is returned.
1652 22 May 07 gregory 96    */
2418 22 Nov 07 gregory 97   static public String getLocalHostNameOrLocalhost()
2418 22 Nov 07 gregory 98   {
2418 22 Nov 07 gregory 99     try
2418 22 Nov 07 gregory 100     {
741 10 Oct 06 olle 101       return InetAddress.getLocalHost().getHostName();
2418 22 Nov 07 gregory 102     }
2418 22 Nov 07 gregory 103     catch (Exception e)
2418 22 Nov 07 gregory 104     {
741 10 Oct 06 olle 105       return "localhost";
741 10 Oct 06 olle 106     }
741 10 Oct 06 olle 107   }
741 10 Oct 06 olle 108
2418 22 Nov 07 gregory 109
741 10 Oct 06 olle 110   /**
1652 22 May 07 gregory 111    * Returns computers outer IP. If no internet connection is found or if
1652 22 May 07 gregory 112    * www.javascript.nu is down it returns <CODE>InetAddress.getLocalHost().getHostAddress()</CODE>.
1652 22 May 07 gregory 113    * If <CODE>InetAddress.getLocalHost().getHostAddress()</CODE> throws an
1652 22 May 07 gregory 114    * error, returns the string "localhost". <BR>
1652 22 May 07 gregory 115    * This will never return null.
1652 22 May 07 gregory 116    */
2418 22 Nov 07 gregory 117   static public String getIPorLocalhost()
2418 22 Nov 07 gregory 118   {
2418 22 Nov 07 gregory 119     try
2418 22 Nov 07 gregory 120     {
1652 22 May 07 gregory 121       String s_IP = getIPFromInternet();
1652 22 May 07 gregory 122       if (s_IP == null)
1652 22 May 07 gregory 123         s_IP = InetAddress.getLocalHost().getHostAddress();
1652 22 May 07 gregory 124       if (s_IP != null) // Not necessary?
741 10 Oct 06 olle 125         return s_IP;
741 10 Oct 06 olle 126       return "localhost";
741 10 Oct 06 olle 127     }
2418 22 Nov 07 gregory 128     catch (Exception e)
2418 22 Nov 07 gregory 129     {
2418 22 Nov 07 gregory 130       return "localhost";
2418 22 Nov 07 gregory 131     }
741 10 Oct 06 olle 132   }
741 10 Oct 06 olle 133
2418 22 Nov 07 gregory 134
741 10 Oct 06 olle 135   /**
1652 22 May 07 gregory 136    * Returns computers local IP. If no internet connection is found or if
1652 22 May 07 gregory 137    * www.javascript.nu is down it returns the string "localhost". <BR>
1652 22 May 07 gregory 138    * This will never return null.
1652 22 May 07 gregory 139    */
2418 22 Nov 07 gregory 140   static public String getLocalIPorLocalhost()
2418 22 Nov 07 gregory 141   {
2418 22 Nov 07 gregory 142     try
2418 22 Nov 07 gregory 143     {
741 10 Oct 06 olle 144       String s_IP;
1652 22 May 07 gregory 145       s_IP = InetAddress.getLocalHost().getHostAddress();
1652 22 May 07 gregory 146       if (s_IP != null) // Not necessary?
741 10 Oct 06 olle 147         return s_IP;
741 10 Oct 06 olle 148       return "localhost";
741 10 Oct 06 olle 149     }
2418 22 Nov 07 gregory 150     catch (Exception e)
2418 22 Nov 07 gregory 151     {
2418 22 Nov 07 gregory 152       return "localhost";
2418 22 Nov 07 gregory 153     }
741 10 Oct 06 olle 154   }
741 10 Oct 06 olle 155
2418 22 Nov 07 gregory 156
741 10 Oct 06 olle 157   /**
1652 22 May 07 gregory 158    * Returns computers outer IP. Returns null if no internet connection is
1652 22 May 07 gregory 159    * found or if website is down.
1652 22 May 07 gregory 160    */
741 10 Oct 06 olle 161   //
2418 22 Nov 07 gregory 162   static public String getIPFromInternet()
2418 22 Nov 07 gregory 163   {
2418 22 Nov 07 gregory 164     if (s_IPstored != null)
2418 22 Nov 07 gregory 165     {
741 10 Oct 06 olle 166       return s_IPstored;
741 10 Oct 06 olle 167     }
2418 22 Nov 07 gregory 168     try
2418 22 Nov 07 gregory 169     {
1652 22 May 07 gregory 170       URL updateFile = new URL(
2418 22 Nov 07 gregory 171         "http://www.javascript.nu/xerver/scripts/getip.pl");
1652 22 May 07 gregory 172       BufferedReader inStr = new BufferedReader(new InputStreamReader(
2418 22 Nov 07 gregory 173         updateFile.openStream()));
741 10 Oct 06 olle 174       String tmpStr;
2418 22 Nov 07 gregory 175       while ((tmpStr = inStr.readLine()) != null)
2418 22 Nov 07 gregory 176       {
2418 22 Nov 07 gregory 177         if (tmpStr.startsWith("outerIP"))
2418 22 Nov 07 gregory 178         {
1652 22 May 07 gregory 179           s_IPstored = tmpStr.substring(tmpStr.indexOf("\"") + 1,
2418 22 Nov 07 gregory 180             tmpStr.lastIndexOf("\""));
741 10 Oct 06 olle 181         }
741 10 Oct 06 olle 182       }
741 10 Oct 06 olle 183       return s_IPstored;
2418 22 Nov 07 gregory 184     }
2418 22 Nov 07 gregory 185     catch (Exception e)
2418 22 Nov 07 gregory 186     {
741 10 Oct 06 olle 187       return null;
741 10 Oct 06 olle 188     }
741 10 Oct 06 olle 189   }
741 10 Oct 06 olle 190 }