001: package com.sun.portal.search.admin;
002:
003: import java.io.*;
004: import java.net.*;
005: import java.util.*;
006: import com.sun.portal.search.admin.resources.SearchResource;
007: import com.sun.portal.util.Platform;
008:
009: public class FilterSimulator {
010: static String h_errmsg[] = { "simulator.successdns",
011: "simulator.namenotfound", "simulator.dnsserverfail",
012: "simulator.norecovery", "simulator.dns.noaddress",
013: "simulator.unknownerror" };
014: Locale locale = Locale.getDefault();
015:
016: class HostResult {
017:
018: String h_cname;
019: int h_type;
020: int h_errno;
021: int h_length;
022: String[] h_ips;
023: String[] h_aliases;
024: String org_url;
025:
026: public HostResult(String url, int code, String host,
027: int length, int type, String[] ps, String[] aliases) {
028: org_url = url;
029: h_cname = host;
030: h_length = length;
031: h_type = type;
032: h_ips = ps;
033: h_aliases = aliases;
034: h_errno = code;
035: }
036:
037: public String toString() {
038: String result = "";
039:
040: result += "GetHostByname() "
041: + getI18n("simulator.resultfor") + " '" + org_url
042: + "'\n";
043: result += "h_error: " + h_errno + " - ";
044: switch (h_errno) {
045: case 0:
046: result += getI18n("simulator.successdns");
047: break;
048: case 1:
049: result += getI18n("simulator.namenotfound");
050: break;
051: case 2:
052: result += getI18n("simulator.dnsserverfail");
053: break;
054: case 3:
055: result += getI18n("simulator.norecovery");
056: break;
057: case 4:
058: result += getI18n("simulator.samename");
059: break;
060: default:
061: result += getI18n("simulator.unknownerror");
062: break;
063: }
064:
065: // NT guess
066:
067: result += "\nName: " + (h_cname != null ? h_cname : "");
068: if (h_aliases != null) {
069: for (int i = 0; i < h_aliases.length
070: && h_aliases[i] != null; i++)
071: result += "\nAliase: " + h_aliases[i];
072: }
073: result += "\naddrtype: " + h_type;
074: result += "\nlength: " + h_length;
075: if (h_ips != null) {
076: for (int i = 0; i < h_ips.length && h_ips[i] != null; i++)
077: result += "\nip: " + h_ips[i];
078: }
079: result += "\n";
080: return result;
081: }
082: }
083:
084: class RedirectResult {
085: String redirect_url;
086: String server_type;
087: int content_length;
088: int return_code;
089:
090: public RedirectResult(int code, String host, String type,
091: int length) {
092: redirect_url = host;
093: server_type = type;
094: content_length = length;
095: return_code = code;
096: }
097: }
098:
099: public FilterSimulator(Locale locale) {
100: this .locale = locale;
101: }
102:
103: public FilterSimulator() {
104: }
105:
106: String getI18n(String input) {
107: return SearchResource.geti18nString(input, locale);
108: }
109:
110: String accepted() {
111: return font("green", getI18n("simulator.accepted"));
112: }
113:
114: String rejected() {
115: return font("red", getI18n("simulator.reject"));
116: }
117:
118: String warning() {
119: return font("brown", getI18n("simulator.warning"));
120: }
121:
122: String href(String host) {
123: return "<a target=_blank href = \"" + host + "\">" + host
124: + "</a>";
125: }
126:
127: String font(String color, String text) {
128: return "<font color=" + color + ">" + text + "</font>";
129: }
130:
131: String trim_fr(String s) {
132: int i;
133: s = s.trim();
134: if (s.startsWith("<URL:")) {
135: String host = s.substring(5, s.length() - 1); // the closiing >
136: s = href(host);
137: } else if ((i = s.indexOf(" -- ")) > 0) {
138: s = s.substring(0, i);
139: }
140: return s;
141: }
142:
143: String format(String head, String body) {
144: return "<tr><td>" + head + "</td>\n<td>" + body
145: + "</td></tr>\n";
146: }
147:
148: int getInt(BufferedReader in) throws Exception {
149: String s = in.readLine();
150: try {
151: return Integer.parseInt(s);
152: } catch (Exception e) {
153: return -1;
154: }
155: }
156:
157: String[] increaseArray(String[] src) {
158: if (src == null)
159: return new String[10];
160: String[] tmp = new String[src.length + 10];
161: System.arraycopy(src, 0, tmp, 0, src.length);
162: return tmp;
163: }
164:
165: boolean sameHost(String url1, String host) {
166: if (url1.charAt(0) == '/') // no protocal, uri only
167: return true;
168: if (url1.indexOf("://" + host) > 0) // same host
169: return true;
170: return false;
171: }
172:
173: public void getDomainByName(String root, String host)
174: throws Exception {
175: HostResult r = doGetHostByName(root, host);
176: switch (r.h_errno) {
177: case 0:
178: throw new Exception(getI18n("simulator.sameashostname"));
179: case 4: /* Valid name, no data record of requested type */
180: break;
181: case 1:
182: case 2:
183: case 3:
184: throw new Exception(getI18n("simulator.namenotfound"));
185: default:
186: throw new Exception(getI18n("simulator.unknownerror"));
187: }
188: return;
189: }
190:
191: public String getHostByName(String root, String host)
192: throws Exception {
193: HostResult r = doGetHostByName(root, host);
194: if (r.h_errno != 0) {
195: if (r.h_errno > 0 && r.h_errno < h_errmsg.length)
196: throw new Exception(getI18n(h_errmsg[r.h_errno]));
197: else
198: throw new Exception(getI18n("simulator.unknownerror"));
199: }
200: return r.h_cname;
201: }
202:
203: public HostResult doGetHostByName(String root, String host)
204: throws Exception {
205: String runCsCli = null;
206: //runtime.exec() need to be explicitly be told that it is a bat file
207: runCsCli = Platform.getCommand("run-cs-cli");
208: String cmd = root + File.separator + runCsCli + " hostinfo "
209: + host;
210: Process proc = Runtime.getRuntime().exec(cmd);
211: BufferedReader in = new BufferedReader(new InputStreamReader(
212: proc.getInputStream()));
213:
214: String cname = null;
215: String[] aliases = null;
216: String[] ps = null;
217: int h_type = 0, h_len = 0;
218: int code = -1;
219: String s;
220: boolean is_ok = false;
221:
222: // always return a code
223: code = getInt(in);
224: if (code != 0) {
225: if (code == 4)
226: cname = host;
227: while ((s = in.readLine()) != null) {
228: }
229: in.close();
230: return new HostResult(host, code, cname, 0, 0, null, null);
231: }
232:
233: // then cname, type, len
234: cname = in.readLine();
235: h_type = getInt(in);
236: h_len = getInt(in);
237:
238: // now the ps if any
239: int count = 0;
240: int len = 0;
241: while ((s = in.readLine()) != null) {
242: if (s.equalsIgnoreCase("ALIASES"))
243: break;
244:
245: if (len < ++count) { // need more space
246: ps = increaseArray(ps);
247: len = ps.length;
248: }
249: ps[count - 1] = s;
250: }
251: if (s == null)
252: return new HostResult(host, code, cname, h_type, h_len, ps,
253: null);
254:
255: count = 0;
256: len = 0;
257: while ((s = in.readLine()) != null) {
258: if (len < ++count) { // need more space
259: aliases = increaseArray(aliases);
260: len = aliases.length;
261: }
262: aliases[count - 1] = s;
263: }
264: return new HostResult(host, code, cname, h_type, h_len, ps,
265: aliases);
266: }
267:
268: public RedirectResult doRedirect(String root,
269: boolean is_visual_server, String url) throws Exception {
270: String runCsCli = null;
271: runCsCli = Platform.getCommand("run-cs-cli");
272: String cmd = root + File.separator + runCsCli + " ufetch ";
273: if (!is_visual_server) // plus header
274: cmd += "-d ";
275: cmd += url;
276:
277: Process proc = Runtime.getRuntime().exec(cmd);
278: BufferedReader in = new BufferedReader(new InputStreamReader(
279: proc.getInputStream()));
280: String s;
281: String host = null, type = null;
282: int comp_len = 0, len = 0;
283: boolean is_ok = false;
284: boolean isredirect = false;
285:
286: while ((s = in.readLine()) != null) {
287: is_ok = true;
288:
289: if (!is_visual_server) {
290: // ufetch return head last
291: if (s.length() > 7
292: && s.substring(0, 7)
293: .equalsIgnoreCase("Server:")) {
294: type = s.substring(7).trim();
295: }
296: if (s.length() > 15
297: && s.substring(0, 15).equalsIgnoreCase(
298: "Content-length:")) {
299: try {
300: len = Integer.parseInt(s.substring(7).trim());
301: } catch (Exception e) {
302: }
303: }
304: if (s.length() > 9
305: && s.substring(0, 9).equalsIgnoreCase(
306: "Location:")) {
307: host = s.substring(9).trim();
308: if (!(host.equals(url + "/") || (url.equals(host
309: + "/"))))
310: isredirect = true;
311: }
312: }
313: comp_len += s.length();
314: }
315: in.close();
316: if (len == 0)
317: len = comp_len;
318: if (!is_ok)
319: return new RedirectResult(-1, "", "", len);
320: else if (isredirect)
321: return new RedirectResult(1, host, type, len);
322: else
323: return new RedirectResult(0, null, type, len);
324: }
325:
326: public String runProb(boolean isSmartHostHeuristics, String root,
327: boolean showDNS, String url) throws Exception {
328: String result = "";
329: URL urlo;
330: String host = "", protocol = "", uri = "";
331: int port = 0;
332:
333: try {
334: urlo = new URL(url);
335: host = urlo.getHost();
336: protocol = urlo.getProtocol();
337: uri = urlo.getFile();
338: port = urlo.getPort();
339: } catch (Exception e) {
340: result = warning() + " " + getI18n("simulator.malformed");
341: return result;
342: }
343: if (port < 1)
344: port = 80;
345:
346: result += getI18n("simulator.checkingurl");
347: result += "\n<br>";
348:
349: HostResult hr = doGetHostByName(root, host);
350: if (showDNS) {
351: result += "<pre>\n";
352: result += hr.toString();
353: result += "</pre>\n";
354: }
355: result += getI18n("simulator.result") + " ";
356: if (hr.h_errno != 0) {
357: String tmp;
358: if (hr.h_errno == 1)
359: tmp = "<b>" + host + "</b>: "
360: + getI18n("simulator.hostnotfound");
361: else if (hr.h_errno == 2)
362: tmp = "<b>" + host + "</b>: "
363: + getI18n("simulator.dnsfailure");
364: else if (hr.h_errno == 3)
365: tmp = "<b>" + host + "</b>: "
366: + getI18n("simulator.dnsfailnorecovery");
367: else if (hr.h_errno == 4)
368: tmp = "<b>" + host + "</b> "
369: + getI18n("simulator.isdomain");
370: else
371: // != 0
372: tmp = "<b>" + host + "</b>: "
373: + getI18n("simulator.unknownerror");
374: result += tmp;
375: return result;
376: } // h_errno != 0
377:
378: result += "<b>" + host + "</b> "
379: + getI18n("simulator.validname");
380: result += "<br>\n";
381:
382: boolean is_alias = false;
383: if (hr.h_aliases != null) {
384: for (int j = 0; j < hr.h_aliases.length; j++) {
385: if (hr.h_aliases[j].indexOf(host) > -1) {
386: is_alias = true;
387: break;
388: }
389: }
390: }
391: if (is_alias) {
392: result += getI18n("simulator.note");
393: result += " <b>" + host + "</b> "
394: + getI18n("simulator.aliasnamefor") + " <b>"
395: + hr.h_cname + "</b>";
396: result += "<br>\n";
397: } else {
398: result += getI18n("simulator.note");
399: result += " <b>" + host + "</b> "
400: + getI18n("simulator.cnamefor");
401: result += "<br>\n";
402: result += getI18n("simulator.note");
403: result += getI18n("simulator.nodnstrans");
404: result += "<br>\n";
405: if (!host.equalsIgnoreCase(hr.h_cname)) {
406: result += getI18n("simulator.refername");
407: result += " <b>" + hr.h_cname + "</b>";
408: result += "<br>\n";
409: }
410: }
411:
412: boolean is_cname_shh = false;
413: if (is_alias && host.startsWith("www.")
414: && hr.h_cname.startsWith("www")) {
415: int k;
416: for (k = 3; k < hr.h_cname.length()
417: && Character.isDigit(hr.h_cname.charAt(k)); k++)
418: ;
419: if (k < hr.h_cname.length() && hr.h_cname.charAt(k) == '.')
420: is_cname_shh = true;
421: }
422:
423: boolean is_host_shh = false;
424: String host_main = "";
425: if (is_alias && host.startsWith("www")) {
426: int k;
427: for (k = 3; k < host.length()
428: && Character.isDigit(host.charAt(k)); k++)
429: ;
430: if (k < host.length() && host.charAt(k) == '.') {
431: is_host_shh = true;
432: host_main = "www" + host.substring(k);
433: }
434: }
435:
436: if (is_cname_shh) {
437: if (isSmartHostHeuristics) {
438: result += getI18n("simulator.note");
439: result += getI18n("simulator.smarthoston");
440: result += " <b>" + host + "</b> ";
441: result += "<br>\n";
442: } else {
443: result += getI18n("simulator.note");
444: result += getI18n("simulator.ifsmarthoston");
445: result += " <b>" + hr.h_cname + "</b> ";
446: result += getI18n("simulator.automapto");
447: result += " <b>" + host + "</b> ";
448: result += "<br>\n";
449: }
450: result += getI18n("simulator.note");
451: result += getI18n("simulator.ifhostlike");
452: result += " <b>" + hr.h_cname + "</b> ";
453: result += getI18n("simulator.hasunique");
454: result += "<br>\n";
455: } else if (is_host_shh) {
456: if (isSmartHostHeuristics) {
457: result += getI18n("simulator.actionsmarthostison");
458: result += " '<b>" + host_main + "</b>'.";
459: } else {
460: result += getI18n("simulator.note");
461: result += " <b>" + host + "</b> ";
462: result += getI18n("simulator.dnsmirror");
463: result += "<br>\n";
464: result += getI18n("simulator.heavilyload");
465: result += " <b>" + host_main + "</b>. ";
466: result += getI18n("simulator.enablesmarthost");
467: result += " <b>" + host + "</b> ";
468: result += "<br>\n";
469: }
470: result += getI18n("simulator.note");
471: result += getI18n("simulator.ifhostlike");
472: result += " <b>" + hr.h_cname + "</b> ";
473: result += getI18n("simulator.hasunique");
474: result += "<br>\n";
475: }
476:
477: if (!protocol.startsWith("http"))
478: return result;
479:
480: result += "\n<hr>\n";
481: result += getI18n("simulator.checkredirect");
482: result += "<br>\n";
483:
484: RedirectResult r = doRedirect(root, false, url);
485: if (r.return_code < 0) {
486: result += getI18n("simulator.failconnect");
487: result += " " + href(url) + "<br>";
488: result += getI18n("simulator.noservice");
489: return result;
490: } else if (r.return_code == 0) {
491: result += getI18n("simulator.noredirectat");
492: result += " " + href(url) + "<br>";
493: } else {
494: result += href(url) + " ";
495: result += getI18n("simulator.isredirect") + " ";
496: if (r.redirect_url.indexOf("://") > 0)
497: result += href(r.redirect_url);
498: else
499: result += href(protocol + "://" + host + ":" + port
500: + (r.redirect_url.charAt(0) == '/' ? "/" : "")
501: + r.redirect_url);
502: result += "<br>\n";
503:
504: if (sameHost(r.redirect_url, host)) {
505: result += getI18n("simulator.redirect2same");
506: } else {
507: result += getI18n("simulator.needsite4redirect");
508: result += " <b>" + href(r.redirect_url) + "</b>";
509: }
510: result += "<br>\n";
511: }
512:
513: result += "<hr>\n";
514: result += getI18n("simulator.servertype")
515: + " "
516: + (r.server_type == null ? getI18n("simulator.unknowntype")
517: : r.server_type) + "<br>\n";
518:
519: if (r.return_code >= 0) {
520: result += "<hr>\n";
521: result += getI18n("simulator.checkvirtual");
522: result += "<br>\n";
523:
524: RedirectResult r1 = doRedirect(root, true, url);
525: String url2 = protocol + "://" + hr.h_cname + ":" + port
526: + uri;
527: RedirectResult r2 = doRedirect(root, true, url2);
528: if (r1.return_code < 0) {
529: result += getI18n("simulator.failconnect");
530: result += " " + href(url) + "<br>";
531: result += getI18n("simulator.noservice");
532: }
533: if (r2.return_code < 0) {
534: result += getI18n("simulator.failconnect");
535: result += " " + href(url2) + "<br>";
536: result += getI18n("simulator.noservice");
537: }
538: if (r1.content_length < 0) {
539: result += getI18n("simulator.failconnect");
540: result += " " + href(url2) + "<br>";
541: result += getI18n("simulator.noservice");
542: }
543: if (r2.return_code < 0 || r1.return_code < 0)
544: return result;
545: if (r2.content_length == 0) {
546: result += getI18n("simulator.failcontentlength");
547: result += " " + href(url2) + "<br>";
548: result += getI18n("simulator.nocontentlength");
549: }
550: if (r1.content_length == 0) {
551: result += getI18n("simulator.failcontentlength");
552: result += " " + href(url) + "<br>";
553: result += getI18n("simulator.nocontentlength");
554: }
555: if (r1.content_length == 0 || r2.content_length == 0)
556: return result;
557: if (r1.content_length == r2.content_length) {
558: result += getI18n("simulator.novirtual");
559: result += " " + href(url);
560: } else {
561: result += href(url2) + " ";
562: result += getI18n("simulator.isvirtual");
563: result += " " + href(url2);
564: }
565: result += "<br>\n";
566: }
567: return result;
568: }
569:
570: public String runSimulator(boolean isSmartHostHeuristics,
571: String root, String url_list[], boolean do_dns,
572: boolean do_redirect) throws Exception {
573: String runCsCli = null;
574: runCsCli = Platform.getCommand("run-cs-cli");
575: String cmd = root + File.separator + runCsCli + " frsim -l "
576: + root + File.separator + "config" + File.separator
577: + "filterrules.conf";
578:
579: Process proc;
580: BufferedReader in;
581: PrintWriter out;
582: String s;
583: String result = "";
584: boolean is_ok;
585:
586: for (int i = 0; i < url_list.length; i++) {
587: is_ok = false;
588:
589: proc = Runtime.getRuntime().exec(cmd);
590: in = new BufferedReader(new InputStreamReader(proc
591: .getInputStream()));
592: out = new PrintWriter(proc.getOutputStream());
593: out.println(url_list[i]); // input ur
594: out.flush();
595: out.close();
596:
597: while ((s = in.readLine()) != null) {
598: if (s.substring(0, 9).equalsIgnoreCase("Accepted:")) {
599: is_ok = true;
600: result += format(accepted(),
601: trim_fr(s.substring(9)));
602: } else if (s.substring(0, 9).equalsIgnoreCase(
603: "Rejected:")) {
604: result += format(rejected(),
605: trim_fr(s.substring(9)));
606: } else if (s.substring(0, 8).equalsIgnoreCase(
607: "Skipping")) {
608: result += format(warning(), trim_fr(s));
609: } else {
610: result += format(warning(), s);
611: }
612: }
613: in.close();
614:
615: URL url;
616: String host = "";
617: try {
618: url = new URL(url_list[i]);
619: host = url.getHost();
620: } catch (Exception e) {
621: result += format(warning(),
622: getI18n("simulator.malformed"));
623: is_ok = false;
624: }
625:
626: if (is_ok && do_dns) {
627: HostResult hr = doGetHostByName(root, host);
628: if (hr.h_errno != 0) {
629: String tmp;
630: if (hr.h_errno == 1)
631: tmp = getI18n("simulator.invalidhost") + host;
632: else if (hr.h_errno == 2 || hr.h_errno == 3)
633: tmp = getI18n("simulator.dnsreterror");
634: else if (hr.h_errno == 4)
635: tmp = getI18n("simulator.donmainnothost");
636: else
637: // != 0
638: tmp = getI18n("simulator.unknownerror");
639: result += format(rejected(), tmp);
640: } else if (hr.h_aliases != null) {
641: boolean found = false;
642: boolean ok = false;
643: for (int j = 0; i < hr.h_aliases.length; j++) {
644: if (hr.h_aliases[j].indexOf(host) > -1) {
645: found = true;
646: break;
647: }
648: }
649: if (found && isSmartHostHeuristics
650: && hr.h_cname.length() > 4) {
651: char c = hr.h_cname.charAt(4);
652:
653: if (hr.h_cname.startsWith("www")
654: && (c == '.' || Character.isDigit(c)))
655: ok = true;
656: }
657: if (ok) {
658: result += format(
659: accepted(),
660: getI18n("simulator.alias")
661: + " "
662: + hr.h_cname
663: + " "
664: + getI18n("simulator.aliasallow"));
665: } else {
666: result += format(rejected(),
667: getI18n("simulator.alias4") + " "
668: + hr.h_cname);
669: }
670: } else {
671: result += format(accepted(),
672: getI18n("simulator.validhostname"));
673:
674: }
675: }
676:
677: if (is_ok && do_redirect) {
678: RedirectResult r = doRedirect(root, false, url_list[i]);
679: if (r.return_code < 0) {
680: result += format(warning(),
681: getI18n("simulator.failconnect"));
682: }
683:
684: if (r.return_code == 0) {
685: result += format(accepted(),
686: getI18n("simulator.noredirect"));
687: } else if (sameHost(r.redirect_url, host)) {
688: result += format(warning(),
689: getI18n("simulator.rejectbyrule") + host);
690: } else {
691: result += format(rejected(),
692: getI18n("simulator.redirect2")
693: + href(r.redirect_url));
694: }
695: }
696: }
697: return result;
698: }
699:
700: static void main(String args[]) throws Exception {
701: FilterSimulator frsim = new FilterSimulator();
702: System.out.println("<html><body>\n");
703: String result = frsim.runSimulator(true, args[0], args, true,
704: true);
705: System.out.println("\n<hr>Simulator Result is:<hr><table>\n ");
706: System.out.println(result);
707: System.out.println("</table>\n");
708: result = frsim.runProb(true, args[0], true, args[1]);
709: System.out.println("\n<hr>Site Prob Result is:<hr><table>\n ");
710: System.out.println(result);
711: System.out.println("</table>\n");
712: System.out.println("</body></html>\n");
713: }
714: }
|