Skip to content

Instantly share code, notes, and snippets.

@ionicpanda
Created October 3, 2018 21:16
Show Gist options
  • Save ionicpanda/a73ff31c36444a8b570e3055dfa3cafc to your computer and use it in GitHub Desktop.
Save ionicpanda/a73ff31c36444a8b570e3055dfa3cafc to your computer and use it in GitHub Desktop.
package com.commercebank.serveranalyzer.BackEnd;
//Response Header Imports
import java.net.*;
import java.util.*;
import java.io.IOException;
/////////////////////////////////
import com.commercebank.serveranalyzer.BackEnd.DataPieces.CertChainData;
import com.commercebank.serveranalyzer.BackEnd.DataPieces.CipherSuiteData;
import com.commercebank.serveranalyzer.BackEnd.DataPieces.DataPiece;
import com.commercebank.serveranalyzer.BackEnd.DataPieces.ResponseHeaderData;
//import org.springframework.web.servlet.ModelAndView;
import java.util.Map;
import java.security.cert.X509Certificate;
import java.net.URL;
import javax.net.ssl.*;
import java.security.cert.Certificate;
import java.net.MalformedURLException;
public class ServerAnalyzerBackEnd {
private String url;
private boolean validURL;
private ArrayList<DataPiece> dataToFetch;
public ServerAnalyzerBackEnd(String url) {
this.url = url;
this.validURL = validateURLSyntax();
this.dataToFetch = new ArrayList<>();
}
// Add all data pieces to fetch
public void fetchAll() {
dataToFetch.add(new ResponseHeaderData());
dataToFetch.add(new CertChainData());
dataToFetch.add(new CipherSuiteData());
}
//add a single data piece to be fetched
public void addDataPieceToFetch(DataPiece dataPiece) {
dataToFetch.add(dataPiece);
}
public Map<String, Object> createOutput(){
String host;
Map<String, Object> params = new HashMap<>();
params.put("url", url);
String secureURL = "";
URL testURL;
int port;
String cert_chain = "";
if (!validURL)
{
System.out.println("Invalid URL syntax.");
returnErrorJson();
}
//url is not null, assign to secureURL to create https connection
if(url != null){
secureURL = url;
}
//Initialize sesson and connection
SSLSession session = null;
HttpsURLConnection connection = null;
//create URL object from 'url'
//create https connection based off of url and then call getCertChain method to pull that cert chain
try {
testURL = new URL(secureURL);
port = testURL.getPort();
host = testURL.getHost();
SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
Socket socket = socketFactory.createSocket(host, port);
session = ((SSLSocket) socket).getSession();
connection = (HttpsURLConnection)testURL.openConnection();
}
catch(UnknownHostException e){
System.out.println("This ip address could not be determined");
returnErrorJson();
}
catch(MalformedURLException e){
e.printStackTrace();
returnErrorJson();
}
catch(IOException e){
e.printStackTrace();
}
// fetch data for all data pieces
for (DataPiece dataPiece : dataToFetch) {
dataPiece.fetchData(connection, session);
}
//put all data pieces into hashmap
for (DataPiece dataPiece : dataToFetch) {
params.put(dataPiece.getDataName(), dataPiece.getDataResult());
}
return params;
// return new ModelAndView("showURL", params);
}
public boolean validateURLSyntax()
{
int firstColon;
int secondColon;
boolean URLsyntax = true;
/* If the user types in google.com, corrects to https://google.com*/
if (this.url.length() < 8)
{
this.url = "https://".concat(this.url);
}
else if (!this.url.substring(0,8).equals("https://") && !this.url.substring(0,6).equals("ftp://"))
{
this.url = "https://".concat(this.url);
}
/*Forces URL to start with https://www. or https:// with a "." and some domain name, or is FTP*/
String URLregex = "^(https://www\\..*\\..*|https://.*\\..*|ftp://.*)";
if (!this.url.matches(URLregex))
{
URLsyntax = false;
}
//Removes an ending forward slash if present
if (this.url.charAt(this.url.length() - 1) == '/')
{
this.url = this.url.substring(0, this.url.length() - 1);
}
//Checks if a port number was specified. If not, appends :443 to the URL.
String portRegex = "^.*:.*:.*";
if (!this.url.matches(portRegex))
{
this.url = this.url + ":443";
}
//Something like https://www.google.com: and no port actually specified or junk is after the port.
secondColon = this.url.lastIndexOf(':');
if (this.url.charAt(this.url.length() - 1) == ':' || !Character.isDigit(this.url.charAt(secondColon + 1)))
{
URLsyntax = false;
}
return URLsyntax;
}
public void returnErrorJson(){
System.out.println("Returning error json");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment