KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > appsrvapi > PortDetector


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.sun.appsrvapi;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.net.ConnectException JavaDoc;
28 import java.net.InetSocketAddress JavaDoc;
29 import java.net.Socket JavaDoc;
30 import java.net.SocketException JavaDoc;
31 import java.net.SocketTimeoutException JavaDoc;
32
33
34 /* new algo to test of an app server(8.1 and 9.0) is secured or not
35  * @author ludo champenois, and Jean Francois Arcand
36  *
37  **/

38
39 public class PortDetector {
40     
41     /**
42      * This method accepts a hostname and port #. It uses this information
43      * to attempt to connect to the port, send a test query, analyze the
44      * result to determine if the port is secure or unsecure (currently only
45      * http / https is supported).
46      * it might emit a warning in the server log for GlassFish cases
47      * No Harm, just an annoying warning, so we need to use this call only when really needed
48      */

49     public static boolean isSecurePort(String JavaDoc hostname, int port) throws IOException JavaDoc, ConnectException JavaDoc, SocketTimeoutException JavaDoc {
50         // Open the socket w/ a 4 second timeout
51
Socket JavaDoc socket = new Socket JavaDoc();
52         try{
53             socket.connect(new InetSocketAddress JavaDoc(hostname, port),4000);
54         } catch (SocketException JavaDoc ex){// this could be bug 70020 due ot SOCKs proxy not having locahost
55
String JavaDoc socksNonProxyHosts=System.getProperty("socksNonProxyHosts");
56             if ((socksNonProxyHosts!=null) && (socksNonProxyHosts.indexOf("localhost")<0)) {
57                 String JavaDoc localhost;
58                 if (socksNonProxyHosts.length()>0) localhost="|localhost"; else localhost="localhost";
59                 System.setProperty("socksNonProxyHosts", socksNonProxyHosts+localhost);
60                 ConnectException JavaDoc ce = new ConnectException JavaDoc();
61                 ce.initCause(ex);
62                 throw ce; //status unknow at this point
63
//next call, we'll be ok and it will really detect if we are secure or not
64
}
65             
66             
67         }
68         
69         // Send an https query (w/ trailing http query)
70
java.io.OutputStream JavaDoc ostream = socket.getOutputStream();
71         ostream.write(TEST_QUERY);
72         
73         // Get the result
74
java.io.InputStream JavaDoc istream = socket.getInputStream();
75         int count=0;
76         int byteRead = 0;
77         byte[] input = new byte[8192];
78         istream.read(input);
79         
80         // Close the socket
81
socket.close();
82         
83         // Determine protocol from result
84
// Can't read https response w/ OpenSSL (or equiv), so use as
85
// default & try to detect an http response.
86
String JavaDoc response = new String JavaDoc(input).toLowerCase();
87         boolean isSecure = true;
88         if (response.length() == 0) {
89             //isSecure = false;
90
throw new ConnectException JavaDoc();
91         } else if (response.startsWith("http/1.")) {
92             isSecure = false;
93         } else if (response.indexOf("<html") != -1) {
94             isSecure = false;
95         } else if (response.indexOf("connection: ") != -1) {
96             isSecure = false;
97         }
98         return isSecure;
99     }
100     
101     
102     public static void main(String JavaDoc[] args) throws IOException JavaDoc{
103         String JavaDoc host = args[0];
104         int port = Integer.parseInt(args[1]);
105         System.out.println("host: " + " port: " + port);
106         System.out.println("isSecure: " + isSecurePort(host,port));
107     }
108     
109     
110     /**
111      * This is the test query used to ping the server in an attempt to
112      * determine if it is secure or not.
113      */

114     public static byte [] TEST_QUERY = new byte [] {
115         // The following SSL query is from nmap (http://www.insecure.org)
116
// This HTTPS request should work for most (all?) https servers
117
(byte)0x16, (byte)0x03, (byte)0x00, (byte)0x00, (byte) 'S', (byte)0x01,
118         (byte)0x00, (byte)0x00, (byte) 'O', (byte)0x03, (byte)0x00, (byte) '?',
119         (byte) 'G', (byte)0xd7, (byte)0xf7, (byte)0xba, (byte) ',', (byte)0xee,
120         (byte)0xea, (byte)0xb2, (byte) '`', (byte) '~', (byte)0xf3, (byte)0x00,
121         (byte)0xfd, (byte)0x82, (byte) '{', (byte)0xb9, (byte)0xd5, (byte)0x96,
122         (byte)0xc8, (byte) 'w', (byte)0x9b, (byte)0xe6, (byte)0xc4, (byte)0xdb,
123         (byte) '<', (byte) '=', (byte)0xdb, (byte) 'o', (byte)0xef, (byte)0x10,
124         (byte) 'n', (byte)0x00, (byte)0x00, (byte) '(', (byte)0x00, (byte)0x16,
125         (byte)0x00, (byte)0x13, (byte)0x00, (byte)0x0a, (byte)0x00, (byte) 'f',
126         (byte)0x00, (byte)0x05, (byte)0x00, (byte)0x04, (byte)0x00, (byte) 'e',
127         (byte)0x00, (byte) 'd', (byte)0x00, (byte) 'c', (byte)0x00, (byte) 'b',
128         (byte)0x00, (byte) 'a', (byte)0x00, (byte) '`', (byte)0x00, (byte)0x15,
129         (byte)0x00, (byte)0x12, (byte)0x00, (byte)0x09, (byte)0x00, (byte)0x14,
130         (byte)0x00, (byte)0x11, (byte)0x00, (byte)0x08, (byte)0x00, (byte)0x06,
131         (byte)0x00, (byte)0x03, (byte)0x01, (byte)0x00,
132         // The following is a HTTP request, some HTTP servers won't
133
// respond unless the following is also sent
134
(byte) 'G', (byte) 'E', (byte) 'T', (byte) ' ', (byte) '/', (byte) ' ',
135         (byte) 'H', (byte) 'T', (byte) 'T', (byte) 'P', (byte) '/', (byte) '1',
136         (byte) '.', (byte) '0', (byte)'\n', (byte)'\n'
137     };
138 }
139
140
Popular Tags