Skip to content

Commit 17eaca2

Browse files
committed
feat(SSL): Added option to disable SSL verification
1 parent 328eb9b commit 17eaca2

File tree

16 files changed

+578
-218
lines changed

16 files changed

+578
-218
lines changed

Scripts/Connection/RESTConnector.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ public Request()
258258
/// The http method for use with UnityWebRequest.
259259
/// </summary>
260260
public string HttpMethod { get; set; }
261+
private bool disableSslVerification = false;
262+
/// <summary>
263+
/// Gets and sets the option to disable ssl verification
264+
/// </summary>
265+
public bool DisableSslVerification
266+
{
267+
get { return disableSslVerification; }
268+
set { disableSslVerification = value; }
269+
}
261270
#endregion
262271
}
263272
#endregion
@@ -315,6 +324,12 @@ public bool Send(Request request)
315324
if (request == null)
316325
throw new ArgumentNullException("request");
317326

327+
#if !NETFX_CORE
328+
if (request.DisableSslVerification)
329+
{
330+
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
331+
}
332+
#endif
318333
_requests.Enqueue(request);
319334

320335
// if we are not already running a co-routine to send the Requests
@@ -696,6 +711,15 @@ private class WatsonRequest
696711
/// The response headers.
697712
/// </summary>
698713
public Dictionary<string, string> ResponseHeaders { get; set; }
714+
private bool disableSslVerification = false;
715+
/// <summary>
716+
/// Gets and sets the option to disable ssl verification
717+
/// </summary>
718+
public bool DisableSslVerification
719+
{
720+
get { return disableSslVerification; }
721+
set { disableSslVerification = value; }
722+
}
699723

700724
public IEnumerator Send(string url, Dictionary<string, string> headers)
701725
{
@@ -708,8 +732,10 @@ public IEnumerator Send(string url, Dictionary<string, string> headers)
708732
}
709733

710734
#if !NETFX_CORE
711-
// This fixes the exception thrown by self-signed certificates.
712-
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
735+
if (DisableSslVerification)
736+
{
737+
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
738+
}
713739
#endif
714740

715741
// Create web request

Scripts/Connection/WSConnector.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ public TextMessage(string text, Dictionary<string, string> headers = null)
156156
/// Headers to pass when making the socket.
157157
/// </summary>
158158
private Dictionary<string, string> _headers;
159-
public Dictionary<string, string> Headers {
159+
public Dictionary<string, string> Headers
160+
{
160161
get
161162
{
162163
if (_headers == null)
@@ -176,6 +177,16 @@ public Dictionary<string, string> Headers {
176177
/// The current state of this connector.
177178
/// </summary>
178179
public ConnectionState State { get { return _connectionState; } set { _connectionState = value; } }
180+
181+
private bool disableSslVerification = false;
182+
/// <summary>
183+
/// Gets and sets the option to disable ssl verification
184+
/// </summary>
185+
public bool DisableSslVerification
186+
{
187+
get { return disableSslVerification; }
188+
set { disableSslVerification = value; }
189+
}
179190
#endregion
180191

181192
#region Private Data
@@ -304,7 +315,7 @@ public static WSConnector CreateConnector(Credentials credentials, string functi
304315
return connector;
305316
}
306317

307-
#region Public Functions
318+
#region Public Functions
308319
/// <summary>
309320
/// This function sends the given message object.
310321
/// </summary>
@@ -356,9 +367,9 @@ public void Close()
356367
// setting the state to closed will make the SendThread automatically exit.
357368
_connectionState = ConnectionState.CLOSED;
358369
}
359-
#endregion
370+
#endregion
360371

361-
#region Private Functions
372+
#region Private Functions
362373
private IEnumerator ProcessReceiveQueue()
363374
{
364375
while (_connectionState == ConnectionState.CONNECTED
@@ -389,9 +400,9 @@ private IEnumerator ProcessReceiveQueue()
389400
if (OnClose != null)
390401
OnClose(this);
391402
}
392-
#endregion
403+
#endregion
393404

394-
#region Threaded Functions
405+
#region Threaded Functions
395406
// NOTE: All functions in this region are operating in a background thread, do NOT call any Unity functions!
396407
#if !NETFX_CORE
397408
private void SendMessages()
@@ -409,6 +420,14 @@ private void SendMessages()
409420
ws.OnClose += OnWSClose;
410421
ws.OnError += OnWSError;
411422
ws.OnMessage += OnWSMessage;
423+
424+
if (DisableSslVerification)
425+
{
426+
ws.SslConfiguration.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
427+
{
428+
return true;
429+
};
430+
}
412431
#if NET_4_6
413432
// Enable TLS 1.1 and TLS 1.2 if we are on .NET 4.x
414433
ws.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
@@ -590,6 +609,6 @@ private void WebSocket_MessageReceived(MessageWebSocket sender, MessageWebSocket
590609
}
591610
}
592611
#endif
593-
#endregion
612+
#endregion
594613
}
595614
}

0 commit comments

Comments
 (0)