001: // natLib.java
002: // -------------------------------------
003: // (C) by Michael Peter Christen; mc@anomic.de
004: // first published on http://www.anomic.de
005: // Frankfurt, Germany, 2004
006: // last major change: 04.05.2004
007: //
008: // $LastChangedDate: 2008-01-22 19:10:03 +0000 (Di, 22 Jan 2008) $
009: // $LastChangedRevision: 4358 $
010: // $LastChangedBy: orbiter $
011: //
012: // This program is free software; you can redistribute it and/or modify
013: // it under the terms of the GNU General Public License as published by
014: // the Free Software Foundation; either version 2 of the License, or
015: // (at your option) any later version.
016: //
017: // This program is distributed in the hope that it will be useful,
018: // but WITHOUT ANY WARRANTY; without even the implied warranty of
019: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: // GNU General Public License for more details.
021: //
022: // You should have received a copy of the GNU General Public License
023: // along with this program; if not, write to the Free Software
024: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025: //
026: // Using this software in any meaning (reading, learning, copying, compiling,
027: // running) means that you agree that the Author(s) is (are) not responsible
028: // for cost, loss of data or any harm that may be caused directly or indirectly
029: // by usage of this softare or this documentation. The usage of this software
030: // is on your own risk. The installation and usage (starting/running) of this
031: // software may allow other people or application to access your computer and
032: // any attached devices and is highly dependent on the configuration of the
033: // software which must be done by the user of the software; the author(s) is
034: // (are) also not responsible for proper configuration and usage of the
035: // software, even if provoked by documentation provided together with
036: // the software.
037: //
038: // Any changes to this file according to the GPL as documented in the file
039: // gpl.txt aside this file in the shipment you received can be done to the
040: // lines that follows this copyright notice here, but changes must not be
041: // done inside the copyright notive above. A re-distribution must contain
042: // the intact and unchanged copyright notice.
043: // Contributions and changes to the program code must be marked as such.
044:
045: package de.anomic.net;
046:
047: import java.net.InetAddress;
048: import java.net.UnknownHostException;
049: import java.util.ArrayList;
050:
051: import de.anomic.http.httpc;
052: import de.anomic.plasma.plasmaSwitchboard;
053: import de.anomic.server.serverDomains;
054: import de.anomic.tools.disorderHeap;
055: import de.anomic.tools.nxTools;
056: import de.anomic.yacy.yacyURL;
057:
058: public class natLib {
059:
060: public static String getDI604(String password) {
061: // this pulls off the ip number from the DI-604 router/nat
062: /*
063: wget --quiet --ignore-length http://admin:<pw>@192.168.0.1:80/status.htm > /dev/null
064: grep -A 1 "IP Address" status.htm | tail -1 | awk '{print $1}' | awk 'BEGIN{FS=">"} {print $2}'
065: rm status.htm
066: */
067: try {
068: ArrayList<String> x = nxTools.strings(httpc.wget(
069: new yacyURL("http://192.168.0.1:80/status.htm",
070: null), "192.168.0.1", 5000, "admin",
071: password, null, null, null));
072: x = nxTools.grep(x, 1, "IP Address");
073: if ((x == null) || (x.size() == 0))
074: return null;
075: String line = nxTools.tail1(x);
076: return nxTools.awk(nxTools.awk(line, " ", 1), ">", 2);
077: } catch (Exception e) {
078: return null;
079: }
080: }
081:
082: private static String getWhatIsMyIP() {
083: try {
084: ArrayList<String> x = nxTools.strings(httpc.wget(
085: new yacyURL("http://www.whatismyip.com/", null),
086: "www.whatsmyip.com", 5000, null, null, null, null,
087: null));
088: x = nxTools.grep(x, 0, "Your IP is");
089: String line = nxTools.tail1(x);
090: return nxTools.awk(line, " ", 4);
091: } catch (Exception e) {
092: return null;
093: }
094: }
095:
096: private static String getStanford() {
097: try {
098: ArrayList<String> x = nxTools
099: .strings(httpc
100: .wget(
101: new yacyURL(
102: "http://www.slac.stanford.edu/cgi-bin/nph-traceroute.pl",
103: null),
104: "www.slac.stanford.edu", 5000,
105: null, null, null, null, null));
106: x = nxTools.grep(x, 0, "firewall protecting your browser");
107: String line = nxTools.tail1(x);
108: return nxTools.awk(line, " ", 7);
109: } catch (Exception e) {
110: return null;
111: }
112: }
113:
114: private static String getIPID() {
115: try {
116: ArrayList<String> x = nxTools.strings(httpc
117: .wget(new yacyURL("http://ipid.shat.net/", null),
118: "ipid.shat.net", 5000, null, null, null,
119: null, null), "UTF-8");
120: x = nxTools.grep(x, 2, "Your IP address");
121: String line = nxTools.tail1(x);
122: return nxTools.awk(nxTools.awk(nxTools.awk(line, " ", 5),
123: ">", 2), "<", 1);
124: } catch (Exception e) {
125: return null;
126: }
127: }
128:
129: private static boolean isNotLocal(String ip) {
130: if ((ip.equals("localhost")) || (ip.startsWith("127"))
131: || (ip.startsWith("192.168"))
132: || (ip.startsWith("172.16")) || (ip.startsWith("10.")))
133: return false;
134: return true;
135: }
136:
137: private static boolean isIP(String ip) {
138: if (ip == null)
139: return false;
140: try {
141: /*InetAddress dummy =*/InetAddress.getByName(ip);
142: return true;
143: } catch (Exception e) {
144: return false;
145: }
146: }
147:
148: //TODO: This is not IPv6 compatible
149: public static boolean isProper(String ip) {
150: plasmaSwitchboard sb = plasmaSwitchboard.getSwitchboard();
151: if (sb != null) {
152: if (sb.isRobinsonMode())
153: return true;
154: String yacyDebugMode = sb.getConfig("yacyDebugMode",
155: "false");
156: if (yacyDebugMode.equals("true")) {
157: return true;
158: }
159: // support for staticIP
160: if (sb.getConfig("staticIP", "").equals(ip)) {
161: return true;
162: }
163: }
164: if (ip == null)
165: return false;
166: if (ip.indexOf(":") >= 0)
167: return false; // ipv6...
168: return (isNotLocal(ip)) && (isIP(ip));
169: }
170:
171: public static final InetAddress getInetAddress(String ip) {
172: if (ip == null)
173: return null;
174: if (ip.length() < 8)
175: return null;
176: String[] ips = ip.split("\\.");
177: if (ips.length != 4)
178: return null;
179: byte[] ipb = new byte[4];
180: try {
181: ipb[0] = (byte) Integer.parseInt(ips[0]);
182: ipb[1] = (byte) Integer.parseInt(ips[1]);
183: ipb[2] = (byte) Integer.parseInt(ips[2]);
184: ipb[3] = (byte) Integer.parseInt(ips[3]);
185: } catch (NumberFormatException e) {
186: return null;
187: }
188: try {
189: return InetAddress.getByAddress(ipb);
190: } catch (UnknownHostException e) {
191: return null;
192: }
193: }
194:
195: private static int retrieveOptions() {
196: return 3;
197: }
198:
199: private static String retrieveFrom(int option) {
200: if ((option < 0) || (option >= retrieveOptions()))
201: return null;
202: if (option == 0)
203: return getWhatIsMyIP();
204: if (option == 1)
205: return getStanford();
206: if (option == 2)
207: return getIPID();
208: return null;
209: }
210:
211: public static String retrieveIP(boolean DI604, String password) {
212: String ip;
213: if (DI604) {
214: // first try the simple way...
215: ip = getDI604(password);
216: if (isProper(ip)) {
217: //System.out.print("{DI604}");
218: return ip;
219: }
220: }
221:
222: // maybe this is a dial-up connection (or LAN and DebugMode) and we can get it from java variables
223: /*InetAddress ia = serverCore.publicIP();
224: if (ia != null) {
225: ip = ia.getHostAddress();
226: if (isProper(ip)) return ip;
227: }*/
228: ip = serverDomains.myPublicIP();
229: if (isProper(ip))
230: return ip;
231:
232: // now go the uneasy way and ask some web responder
233: disorderHeap random = new disorderHeap(retrieveOptions());
234: for (int i = 0; i < retrieveOptions(); i++) {
235: ip = retrieveFrom(random.number());
236: if (isProper(ip))
237: return ip;
238: }
239: return null;
240: }
241:
242: // rDNS services:
243: // http://www.xdr2.net/reverse_DNS_lookup.asp
244: // http://remote.12dt.com/rns/
245: // http://bl.reynolds.net.au/search/
246: // http://www.declude.com/Articles.asp?ID=97
247: // http://www.dnsstuff.com/
248:
249: // listlist: http://www.aspnetimap.com/help/welcome/dnsbl.html
250:
251: public static void main(String[] args) {
252: //System.out.println("PROBE DI604 : " + getDI604(""));
253: //System.out.println("PROBE whatismyip: " + getWhatIsMyIP());
254: //System.out.println("PROBE stanford : " + getStanford());
255: //System.out.println("PROBE ipid : " + getIPID());
256: //System.out.println("retrieveIP-NAT : " + retrieveIP(true,""));
257: //System.out.println("retrieveIP : " + retrieveIP(false,"12345"));
258:
259: System.out.println(isProper(args[0]) ? "yes" : "no");
260: }
261:
262: }
|