17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.utils;
|
|
|
15 |
|
41 |
ilm |
16 |
import java.io.ByteArrayOutputStream;
|
|
|
17 |
import java.io.IOException;
|
|
|
18 |
import java.io.InputStream;
|
|
|
19 |
import java.io.OutputStream;
|
17 |
ilm |
20 |
import java.net.InetAddress;
|
|
|
21 |
import java.net.NetworkInterface;
|
67 |
ilm |
22 |
import java.net.ServerSocket;
|
17 |
ilm |
23 |
import java.net.SocketException;
|
41 |
ilm |
24 |
import java.net.URL;
|
174 |
ilm |
25 |
import java.security.KeyManagementException;
|
|
|
26 |
import java.security.NoSuchAlgorithmException;
|
|
|
27 |
import java.security.cert.X509Certificate;
|
17 |
ilm |
28 |
import java.util.Enumeration;
|
|
|
29 |
|
41 |
ilm |
30 |
import javax.net.ssl.HostnameVerifier;
|
|
|
31 |
import javax.net.ssl.HttpsURLConnection;
|
174 |
ilm |
32 |
import javax.net.ssl.SSLContext;
|
41 |
ilm |
33 |
import javax.net.ssl.SSLSession;
|
174 |
ilm |
34 |
import javax.net.ssl.TrustManager;
|
|
|
35 |
import javax.net.ssl.X509TrustManager;
|
41 |
ilm |
36 |
|
17 |
ilm |
37 |
public class NetUtils {
|
|
|
38 |
|
67 |
ilm |
39 |
public static int findFreePort(final int preferred) {
|
|
|
40 |
return findFreePort(null, preferred);
|
|
|
41 |
}
|
|
|
42 |
|
17 |
ilm |
43 |
/**
|
67 |
ilm |
44 |
* Returns a free port number on localhost.
|
|
|
45 |
*
|
|
|
46 |
* @param addr the bind address, <code>null</code> meaning an address of the loopback interface.
|
|
|
47 |
* @param preferred if this port is free, then it's returned.
|
|
|
48 |
* @return preferred if free otherwise any free port.
|
|
|
49 |
*/
|
|
|
50 |
public static int findFreePort(final String addr, final int preferred) {
|
|
|
51 |
if (isPortFree(addr, preferred))
|
|
|
52 |
return preferred;
|
|
|
53 |
else
|
|
|
54 |
return findFreePort(addr);
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Returns a free port number on localhost.
|
|
|
59 |
*
|
|
|
60 |
* @return a free port number on localhost, or -1 if unable to find a free port
|
|
|
61 |
*/
|
|
|
62 |
public static int findFreePort() {
|
|
|
63 |
return findFreePort(null);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
public static int findFreePort(final String addr) {
|
|
|
67 |
return checkPort(addr, 0);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
public static boolean isPortFree(final String addr, final int port) {
|
|
|
71 |
if (port <= 0)
|
|
|
72 |
throw new IllegalArgumentException(port + " is negative");
|
|
|
73 |
return checkPort(addr, port) == port;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
// with code from from org/eclipse/jdt/launching/SocketUtil.java
|
|
|
77 |
private static int checkPort(final String addr, final int port) {
|
|
|
78 |
ServerSocket socket = null;
|
|
|
79 |
try {
|
|
|
80 |
// ATTN InetAddress.getByName(null) means an address of the loopback interface, while
|
|
|
81 |
// passing null means any/all local addresses. The problem is that the constructor will
|
|
|
82 |
// succeed for a given port even if it is already bound with the other address.
|
|
|
83 |
socket = new ServerSocket(port, 0, InetAddress.getByName(addr));
|
|
|
84 |
return socket.getLocalPort();
|
|
|
85 |
} catch (IOException e) {
|
|
|
86 |
} finally {
|
|
|
87 |
if (socket != null) {
|
|
|
88 |
try {
|
|
|
89 |
socket.close();
|
|
|
90 |
} catch (IOException e) {
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
return -1;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
17 |
ilm |
98 |
* Whether the passed address refers to this computer.
|
|
|
99 |
*
|
|
|
100 |
* @param addr an ip or dns address, eg "192.168.28.52".
|
|
|
101 |
* @return <code>true</code> if <code>addr</code> is bound to an interface of this computer.
|
|
|
102 |
*/
|
|
|
103 |
static public final boolean isSelfAddr(String addr) {
|
|
|
104 |
if (addr == null)
|
|
|
105 |
return false;
|
|
|
106 |
if (addr.startsWith("127.") || addr.startsWith("localhost"))
|
|
|
107 |
return true;
|
|
|
108 |
|
|
|
109 |
try {
|
|
|
110 |
final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
|
|
|
111 |
while (en.hasMoreElements()) {
|
|
|
112 |
final NetworkInterface ni = en.nextElement();
|
|
|
113 |
final Enumeration<InetAddress> addresses = ni.getInetAddresses();
|
|
|
114 |
while (addresses.hasMoreElements()) {
|
|
|
115 |
final InetAddress inetAddress = addresses.nextElement();
|
|
|
116 |
if (addr.startsWith(inetAddress.getHostAddress()))
|
|
|
117 |
return true;
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
return false;
|
|
|
121 |
} catch (SocketException e) {
|
|
|
122 |
e.printStackTrace();
|
|
|
123 |
return false;
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
|
57 |
ilm |
127 |
public static final HostnameVerifier HostnameNonVerifier = new HostnameVerifier() {
|
|
|
128 |
@Override
|
41 |
ilm |
129 |
public boolean verify(String hostname, SSLSession session) {
|
|
|
130 |
return true;
|
|
|
131 |
}
|
57 |
ilm |
132 |
};
|
41 |
ilm |
133 |
|
174 |
ilm |
134 |
public static final String getHTTPContent(String address) {
|
|
|
135 |
return getHTTPContent(address, false);
|
|
|
136 |
}
|
|
|
137 |
|
57 |
ilm |
138 |
public static final String getHTTPContent(String address, final boolean dontVerify) {
|
41 |
ilm |
139 |
String content = "";
|
|
|
140 |
OutputStream out = null;
|
|
|
141 |
HttpsURLConnection conn = null;
|
|
|
142 |
InputStream in = null;
|
|
|
143 |
try {
|
|
|
144 |
URL url = new URL(address);
|
|
|
145 |
out = new ByteArrayOutputStream();
|
|
|
146 |
conn = (HttpsURLConnection) url.openConnection();
|
|
|
147 |
// Sur la connexion
|
57 |
ilm |
148 |
if (dontVerify) {
|
|
|
149 |
conn.setHostnameVerifier(HostnameNonVerifier);
|
41 |
ilm |
150 |
// ou globalement
|
|
|
151 |
// HttpsURLConnection.setDefaultHostnameVerifier(new CustomizedHostnameVerifier());
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
in = conn.getInputStream();
|
|
|
155 |
final byte[] buffer = new byte[1024];
|
|
|
156 |
int numRead;
|
|
|
157 |
|
|
|
158 |
while ((numRead = in.read(buffer)) != -1) {
|
|
|
159 |
out.write(buffer, 0, numRead);
|
|
|
160 |
|
|
|
161 |
}
|
|
|
162 |
content = out.toString();
|
|
|
163 |
|
|
|
164 |
} catch (Exception exception) {
|
|
|
165 |
exception.printStackTrace();
|
|
|
166 |
} finally {
|
|
|
167 |
try {
|
|
|
168 |
if (in != null) {
|
|
|
169 |
in.close();
|
|
|
170 |
}
|
|
|
171 |
if (out != null) {
|
|
|
172 |
out.close();
|
|
|
173 |
}
|
|
|
174 |
} catch (IOException ioe) {
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
return content;
|
|
|
179 |
}
|
174 |
ilm |
180 |
|
|
|
181 |
// Create a trust manager that does not validate certificate chains
|
|
|
182 |
static private final TrustManager[] TRUSTALL_MANAGERS = new TrustManager[] { new X509TrustManager() {
|
|
|
183 |
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
|
|
184 |
return null;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
public void checkClientTrusted(X509Certificate[] certs, String authType) {
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
public void checkServerTrusted(X509Certificate[] certs, String authType) {
|
|
|
191 |
}
|
|
|
192 |
} };
|
|
|
193 |
|
|
|
194 |
static public final SSLContext createTrustAllContext() throws KeyManagementException, NoSuchAlgorithmException {
|
|
|
195 |
final SSLContext sc = SSLContext.getInstance("TLS");
|
|
|
196 |
sc.init(null, TRUSTALL_MANAGERS, new java.security.SecureRandom());
|
|
|
197 |
return sc;
|
|
|
198 |
}
|
17 |
ilm |
199 |
}
|