Ajout de la gestion des SSL invalides

This commit is contained in:
Alexis Delhaie
2019-10-19 22:20:45 +02:00
parent 2610781798
commit c6c505e6f2
42 changed files with 233 additions and 97 deletions

View File

@@ -1,11 +1,24 @@
package ovh.alexisdelhaie.curling;
import ovh.alexisdelhaie.curling.windows.MainWindow;
import javax.swing.plaf.synth.SynthLookAndFeel;
import javax.swing.*;
import java.util.logging.Logger;
public class Runtime {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
Logger.getGlobal().warning(e.getMessage());
} catch (IllegalAccessException e) {
Logger.getGlobal().warning(e.getMessage());
} catch (UnsupportedLookAndFeelException e) {
Logger.getGlobal().warning(e.getMessage());
}
MainWindow window = new MainWindow();
window.showFrame();
}

View File

@@ -1,22 +1,32 @@
package ovh.alexisdelhaie.curling.web;
import javax.net.ssl.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
public class Client {
public static long TIMEOUT = 10000;
private String body;
private Map<String, String> headers;
private Method method;
private String url;
private long timeout;
private boolean allowInvalidSSL;
private RequestError error;
private String log;
@@ -58,17 +68,26 @@ public class Client {
}
public Client(String body, Map<String, String> headers, Method method, String url) {
this(body, headers, method, url, false, TIMEOUT);
}
public Client(String body, Map<String, String> headers, Method method, String url, boolean allowInvalidSSL) {
this(body, headers, method, url, allowInvalidSSL, TIMEOUT);
}
public Client(String body, Map<String, String> headers, Method method, String url, boolean allowInvalidSSL, long timeout) {
this.body = body;
this.headers = headers;
this.method = method;
this.url = url;
this.timeout = timeout;
this.allowInvalidSSL = allowInvalidSSL;
this.log = "";
}
public CompletableFuture<String> run() {
HttpClient httpClient = HttpClient.newBuilder().build();
try {
HttpClient httpClient = getClient();
HttpRequest request = requestBuilder();
if (request != null) {
@@ -86,9 +105,11 @@ public class Client {
}
result += String.format("\n%s", (String)r.body());
} catch (IOException e) {
e.printStackTrace();
result += String.format("\n/!\\ %s", e.getMessage());
error = new RequestError(e.getMessage(), e.getLocalizedMessage(), e);
} catch (InterruptedException e) {
e.printStackTrace();
result += String.format("\n/!\\ %s", e.getMessage());
error = new RequestError("Thread interrupted", "The action was stopped (by you maybe)", e);
}
return result;
@@ -100,6 +121,10 @@ public class Client {
} catch (URISyntaxException e) {
error = new RequestError("URL invalide", "Le format de l'URL est invalide", e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return null;
@@ -123,6 +148,31 @@ public class Client {
if(method == Method.POST || method == Method.PUT) {
log += String.format("%s", body);
}
b.timeout(Duration.ofMillis(timeout));
return b.build();
}
private HttpClient getClient() throws KeyManagementException, NoSuchAlgorithmException {
HttpClient.Builder b = HttpClient.newBuilder();
b.followRedirects(HttpClient.Redirect.NEVER);
b.version(HttpClient.Version.HTTP_2);
if(allowInvalidSSL) {
b.sslContext(getInvalidSSLConfig());
}
return b.build();
}
private SSLContext getInvalidSSLConfig() throws NoSuchAlgorithmException, KeyManagementException {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {return null;}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType){}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType){}
}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
return sc;
}
}

View File

@@ -104,14 +104,6 @@
<text value="Content"/>
</properties>
</component>
<component id="328a" class="javax.swing.JTextArea" binding="valueField">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
<component id="3fe5d" class="javax.swing.JComboBox" binding="encodageField">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
@@ -123,6 +115,19 @@
</model>
</properties>
</component>
<scrollpane id="62efa">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="ac535" class="javax.swing.JTextArea" binding="valueField">
<constraints/>
<properties/>
</component>
</children>
</scrollpane>
</children>
</grid>
</form>

View File

@@ -1,24 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ovh.alexisdelhaie.curling.windows.MainWindow">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="6" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="7" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="739" height="412"/>
<xy x="48" y="54" width="1120" height="412"/>
</constraints>
<properties/>
<border type="none" title="cURLing"/>
<children>
<component id="b4b69" class="javax.swing.JComboBox" binding="httpTypeBox">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="HTTP"/>
<item value="HTTPS"/>
</model>
</properties>
</component>
<component id="dc7ca" class="javax.swing.JComboBox" binding="methodBox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
@@ -32,17 +21,9 @@
</model>
</properties>
</component>
<component id="3d00f" class="javax.swing.JTextField" binding="urlField">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="adde4" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Custom headers"/>
@@ -50,48 +31,15 @@
</component>
<component id="1286d" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Body datas"/>
</properties>
</component>
<component id="627e7" class="javax.swing.JButton" binding="xFormEncodedButton" default-binding="true">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="Add X-form-encoded values"/>
<text value="Add X-form-encoded values"/>
</properties>
</component>
<component id="10f69" class="javax.swing.JProgressBar" binding="progressBar1" default-binding="true">
<constraints>
<grid row="5" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="76150" class="javax.swing.JButton" binding="newHeaderButton">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="New header"/>
<text value="New header"/>
</properties>
</component>
<component id="3d3b2" class="javax.swing.JButton" binding="addAuthButton">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="Basic authentication"/>
<text value="Basic authentication"/>
</properties>
</component>
<scrollpane id="4befb">
<constraints>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="4" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@@ -104,7 +52,7 @@
</scrollpane>
<scrollpane id="cdcdf">
<constraints>
<grid row="4" column="0" row-span="2" col-span="3" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="5" column="0" row-span="2" col-span="4" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@@ -115,19 +63,9 @@
</component>
</children>
</scrollpane>
<component id="c866d" class="javax.swing.JButton" binding="runButton">
<constraints>
<grid row="5" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
<label value="Start"/>
<text value="Start"/>
</properties>
</component>
<scrollpane id="1b18">
<constraints>
<grid row="0" column="3" row-span="5" col-span="2" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="0" column="4" row-span="6" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@@ -141,6 +79,115 @@
</component>
</children>
</scrollpane>
<grid id="815c" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="282" height="34"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="b4b69" class="javax.swing.JComboBox" binding="httpTypeBox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="HTTP"/>
<item value="HTTPS"/>
</model>
</properties>
</component>
<component id="3d00f" class="javax.swing.JTextField" binding="urlField">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="254" height="30"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
<grid id="37c3f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="6" column="4" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="c866d" class="javax.swing.JButton" binding="runButton">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
<label value="Start"/>
<text value="Start"/>
</properties>
</component>
<component id="10f69" class="javax.swing.JProgressBar" binding="progressBar1" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
</children>
</grid>
<grid id="a2fab" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="76150" class="javax.swing.JButton" binding="newHeaderButton">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="New header"/>
<text value="New header"/>
</properties>
</component>
<component id="3d3b2" class="javax.swing.JButton" binding="addAuthButton">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="329" height="30"/>
</grid>
</constraints>
<properties>
<label value="Basic authentication"/>
<text value="Basic authentication"/>
</properties>
</component>
</children>
</grid>
<component id="627e7" class="javax.swing.JButton" binding="xFormEncodedButton" default-binding="true">
<constraints>
<grid row="4" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="329" height="30"/>
</grid>
</constraints>
<properties>
<label value="Add X-form-encoded values"/>
<text value="Add X-form-encoded values"/>
</properties>
</component>
<component id="adccd" class="javax.swing.JCheckBox" binding="allowInvalidSSLField">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="329" height="18"/>
</grid>
</constraints>
<properties>
<enabled value="false"/>
<text value="Allow invalid SSL certificate"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@@ -7,6 +7,8 @@ import ovh.alexisdelhaie.curling.web.Method;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.net.http.HttpClient;
import java.util.HashMap;
import java.util.Map;
@@ -33,14 +35,17 @@ public class MainWindow extends JDialog {
private JButton newHeaderButton;
private JButton addAuthButton;
private JButton runButton;
private JCheckBox allowInvalidSSLField;
private DefaultListModel<String> model;
private Map<String, String> headers;
private Map<String, String> params;
private boolean allowInvalidSSL;
public MainWindow() {
super((Dialog) null);
setTitle(WINDOW_TITLE);
allowInvalidSSL = false;
headers = new HashMap<>();
model = new DefaultListModel<>();
list1.setModel(model);
@@ -49,6 +54,14 @@ public class MainWindow extends JDialog {
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
httpTypeBox.addActionListener((event) -> {
allowInvalidSSLField.setEnabled(httpTypeBox.getSelectedItem().equals("HTTPS"));
});
allowInvalidSSLField.addItemListener((event) -> {
allowInvalidSSL = (event.getStateChange() == ItemEvent.SELECTED);
});
newHeaderButton.addActionListener((event) -> {
AddHeader ah = new AddHeader();
HeaderModel h = ah.getValue();
@@ -92,7 +105,7 @@ public class MainWindow extends JDialog {
private void start(ActionEvent ev) {
startLoading(true);
Client c = new Client(bodyArea.getText(), headers, getMethod(), urlBuilder());
Client c = new Client(bodyArea.getText(), headers, getMethod(), urlBuilder(), getAllowInvalidSSL());
CompletableFuture<String> f = c.run();
new Thread(new Runnable() {
@Override
@@ -137,6 +150,15 @@ public class MainWindow extends JDialog {
newHeaderButton.setEnabled(!b);
addAuthButton.setEnabled(!b);
runButton.setEnabled(!b);
allowInvalidSSLField.setEnabled(!b);
}
private boolean getAllowInvalidSSL() {
if(httpTypeBox.getSelectedItem().equals("HTTPS")) {
return allowInvalidSSL;
}
return false;
}
private void startLoading(boolean b) {