1 19 20 package org.netbeans.modules.j2ee.sun.appsrvapi; 21 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.io.OutputStream ; 27 import java.net.ConnectException ; 28 import java.net.InetSocketAddress ; 29 import java.net.Socket ; 30 import java.net.SocketException ; 31 import java.net.SocketTimeoutException ; 32 33 34 38 39 public class PortDetector { 40 41 49 public static boolean isSecurePort(String hostname, int port) throws IOException , ConnectException , SocketTimeoutException { 50 Socket socket = new Socket (); 52 try{ 53 socket.connect(new InetSocketAddress (hostname, port),4000); 54 } catch (SocketException ex){ String socksNonProxyHosts=System.getProperty("socksNonProxyHosts"); 56 if ((socksNonProxyHosts!=null) && (socksNonProxyHosts.indexOf("localhost")<0)) { 57 String localhost; 58 if (socksNonProxyHosts.length()>0) localhost="|localhost"; else localhost="localhost"; 59 System.setProperty("socksNonProxyHosts", socksNonProxyHosts+localhost); 60 ConnectException ce = new ConnectException (); 61 ce.initCause(ex); 62 throw ce; } 65 66 67 } 68 69 java.io.OutputStream ostream = socket.getOutputStream(); 71 ostream.write(TEST_QUERY); 72 73 java.io.InputStream istream = socket.getInputStream(); 75 int count=0; 76 int byteRead = 0; 77 byte[] input = new byte[8192]; 78 istream.read(input); 79 80 socket.close(); 82 83 String response = new String (input).toLowerCase(); 87 boolean isSecure = true; 88 if (response.length() == 0) { 89 throw new ConnectException (); 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 [] args) throws IOException { 103 String 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 114 public static byte [] TEST_QUERY = new byte [] { 115 (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 (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 |