Initial Commit

This commit is contained in:
Isaac Parenteau
2019-07-20 12:39:03 -05:00
commit 79529ecc40
66 changed files with 6549 additions and 0 deletions

View File

@@ -0,0 +1,95 @@
package net.locusworks.common.net;
import java.security.SecureRandom;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import net.locusworks.common.net.certmanagers.TrustAllCertsManager;
import net.locusworks.common.net.hostverifiers.AllHostValidVerifyer;
public class HttpClientHelper {
public enum HttpSchema {
HTTP,
HTTPS;
public static HttpSchema findEnum(String value) {
for (HttpSchema schema : values()) {
if (value.equalsIgnoreCase(schema.toString())) {
return schema;
}
}
return null;
}
}
private static final String[] TLS = new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"};
private HttpClient client;
private String baseUrl;
/**
* Constructor to handle http connection
* @param protocol protocol to use (http or https)
* @param host the host url
* @param port the host port
* @throws Exception exception
*/
public HttpClientHelper(String protocol, String host, String port) throws Exception {
HttpSchema schema = HttpSchema.findEnum(protocol);
if (schema == null) {
throw new Exception("Unable to find http schema of " + protocol);
}
this.baseUrl = String.format("%s://%s:%s", schema.toString().toLowerCase(), host, port);
this.client = createClient(schema);
}
private HttpClient createClient(HttpSchema schema) throws Exception {
HttpClientBuilder builder = HttpClientBuilder.create();
if (schema == HttpSchema.HTTP) {
return builder.build();
}
TrustManager[] trustAllCerts = new TrustManager[] { new TrustAllCertsManager() };
//Setup the ssl instance using tls
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new SecureRandom());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, TLS, null, new AllHostValidVerifyer());
builder = builder.setSSLSocketFactory(sslsf);
return builder.build();
}
/**
* Get the http GET response code
* @param endpoint endpoint to get the response from
* @return responseCode
* @throws Exception general exception
*/
public Integer getGetResponseCode(String endpoint) throws Exception {
String url = this.baseUrl + endpoint;
HttpResponse response = this.client.execute(new HttpGet(url));
HttpEntity entity = response.getEntity();
Integer responseCode = response.getStatusLine().getStatusCode();
EntityUtils.consume(entity);
return responseCode;
};
}

View File

@@ -0,0 +1,24 @@
package net.locusworks.common.net.certmanagers;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class TrustAllCertsManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { }
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { }
@Override
public X509Certificate[] getAcceptedIssuers() { return null; }
public static TrustManager[] trustAllCerts() {
return new TrustManager[] { new TrustAllCertsManager() };
}
}

View File

@@ -0,0 +1,11 @@
package net.locusworks.common.net.hostverifiers;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
public class AllHostValidVerifyer implements HostnameVerifier {
@Override
public boolean verify(String arg0, SSLSession arg1) { return true; }
}

View File

@@ -0,0 +1,30 @@
package net.locusworks.common.net.ssl;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import net.locusworks.common.net.certmanagers.TrustAllCertsManager;
import net.locusworks.common.net.hostverifiers.AllHostValidVerifyer;
public class SSLManager {
public static final String[] TLS = new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"};
public static HttpClient getTrustAllTLSClient() throws NoSuchAlgorithmException, KeyManagementException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new TrustManager[] { new TrustAllCertsManager() }, new SecureRandom());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(context, TLS, null, new AllHostValidVerifyer());
return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
}
}