741 |
10 Oct 06 |
olle |
1 |
/* |
1652 |
22 May 07 |
gregory |
$Id$ |
741 |
10 Oct 06 |
olle |
3 |
|
1916 |
31 Aug 07 |
jari |
Copyright (C) 2006 Olle Mansson |
1916 |
31 Aug 07 |
jari |
Copyright (C) 2007 Gregory Vincic |
741 |
10 Oct 06 |
olle |
6 |
|
1652 |
22 May 07 |
gregory |
This file is part of Proteios. |
1652 |
22 May 07 |
gregory |
Available at http://www.proteios.org/ |
741 |
10 Oct 06 |
olle |
9 |
|
1652 |
22 May 07 |
gregory |
Proteios is free software; you can redistribute it and/or modify it |
1652 |
22 May 07 |
gregory |
under the terms of the GNU General Public License as published by |
1652 |
22 May 07 |
gregory |
the Free Software Foundation; either version 2 of the License, or |
1652 |
22 May 07 |
gregory |
(at your option) any later version. |
741 |
10 Oct 06 |
olle |
14 |
|
1652 |
22 May 07 |
gregory |
Proteios is distributed in the hope that it will be useful, but |
1652 |
22 May 07 |
gregory |
WITHOUT ANY WARRANTY; without even the implied warranty of |
1652 |
22 May 07 |
gregory |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1652 |
22 May 07 |
gregory |
General Public License for more details. |
741 |
10 Oct 06 |
olle |
19 |
|
1652 |
22 May 07 |
gregory |
You should have received a copy of the GNU General Public License |
1652 |
22 May 07 |
gregory |
along with this program; if not, write to the Free Software |
1652 |
22 May 07 |
gregory |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
1652 |
22 May 07 |
gregory |
02111-1307, USA. |
1652 |
22 May 07 |
gregory |
24 |
*/ |
741 |
10 Oct 06 |
olle |
// Xerver Free Web Server |
741 |
10 Oct 06 |
olle |
// Copyright (C) 2002-2005 Omid Rouhani |
741 |
10 Oct 06 |
olle |
27 |
// |
741 |
10 Oct 06 |
olle |
28 |
// |
741 |
10 Oct 06 |
olle |
// This program is free software; you can redistribute it and/or |
741 |
10 Oct 06 |
olle |
// modify it under the terms of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// as published by the Free Software Foundation; either version 2 |
741 |
10 Oct 06 |
olle |
// of the License, or (at your option) any later version. |
741 |
10 Oct 06 |
olle |
33 |
// |
741 |
10 Oct 06 |
olle |
// This program is distributed in the hope that it will be useful, |
741 |
10 Oct 06 |
olle |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
741 |
10 Oct 06 |
olle |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
741 |
10 Oct 06 |
olle |
// GNU General Public License for more details. |
741 |
10 Oct 06 |
olle |
38 |
// |
741 |
10 Oct 06 |
olle |
// You should have received a copy of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// along with this program; if not, write to the Free Software |
741 |
10 Oct 06 |
olle |
// 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 |
// ## YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT: ## |
741 |
10 Oct 06 |
olle |
// ## HTTP://WWW.JAVASCRIPT.NU/XERVER/ ## |
741 |
10 Oct 06 |
olle |
47 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## IF YOUR SOFTWARE IS NOT RELEASED UNDER THE ## |
741 |
10 Oct 06 |
olle |
// ## GNU GENERAL PUBLIC LICENSE (GPL), ## |
741 |
10 Oct 06 |
olle |
// ## PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!! ## |
741 |
10 Oct 06 |
olle |
51 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## 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 |
* <B>About this class:</B> <BR> |
1652 |
22 May 07 |
gregory |
* Classes you can use to get a computers outer IP. <BR> |
741 |
10 Oct 06 |
olle |
* 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 |
* @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a> |
741 |
10 Oct 06 |
olle |
* @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 |
* Returns computers outer IP. If no internet connection is found or if |
1652 |
22 May 07 |
gregory |
* www.javascript.nu is down it returns <CODE>InetAddress.getLocalHost().getHostAddress()</CODE>. |
1652 |
22 May 07 |
gregory |
* <BR> |
1652 |
22 May 07 |
gregory |
* Exception are only thrown from |
1652 |
22 May 07 |
gregory |
* "InetAddress.getLocalHost().getHostAddress()", which should not happen |
1652 |
22 May 07 |
gregory |
* 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 |
* Returns local computers host name. <BR> |
1652 |
22 May 07 |
gregory |
* If <CODE>InetAddress.getLocalHost()</CODE> throws an Exception |
1652 |
22 May 07 |
gregory |
* "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 |
* Returns computers outer IP. If no internet connection is found or if |
1652 |
22 May 07 |
gregory |
* www.javascript.nu is down it returns <CODE>InetAddress.getLocalHost().getHostAddress()</CODE>. |
1652 |
22 May 07 |
gregory |
* If <CODE>InetAddress.getLocalHost().getHostAddress()</CODE> throws an |
1652 |
22 May 07 |
gregory |
* error, returns the string "localhost". <BR> |
1652 |
22 May 07 |
gregory |
* 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 |
* Returns computers local IP. If no internet connection is found or if |
1652 |
22 May 07 |
gregory |
* www.javascript.nu is down it returns the string "localhost". <BR> |
1652 |
22 May 07 |
gregory |
* 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 |
* Returns computers outer IP. Returns null if no internet connection is |
1652 |
22 May 07 |
gregory |
* 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 |
} |