Allow users to opt-in to proxies (#888)

* Allow users to opt-in to proxies

* Allow opting in to proxies on the WebSocket
This commit is contained in:
Christopher F
2017-12-07 16:47:01 -05:00
committed by GitHub
parent 39b5d0e74c
commit 678a7238e6
4 changed files with 36 additions and 22 deletions

View File

@@ -22,7 +22,7 @@ namespace Discord.Net.Rest
private CancellationToken _cancelToken; private CancellationToken _cancelToken;
private bool _isDisposed; private bool _isDisposed;
public DefaultRestClient(string baseUrl) public DefaultRestClient(string baseUrl, bool useProxy = false)
{ {
_baseUrl = baseUrl; _baseUrl = baseUrl;
@@ -30,7 +30,7 @@ namespace Discord.Net.Rest
{ {
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
UseCookies = false, UseCookies = false,
UseProxy = false UseProxy = useProxy,
}); });
SetHeader("accept-encoding", "gzip, deflate"); SetHeader("accept-encoding", "gzip, deflate");

View File

@@ -4,11 +4,15 @@ namespace Discord.Net.Rest
{ {
public static class DefaultRestClientProvider public static class DefaultRestClientProvider
{ {
public static readonly RestClientProvider Instance = url => public static readonly RestClientProvider Instance = Create();
public static RestClientProvider Create(bool useProxy = false)
{
return url =>
{ {
try try
{ {
return new DefaultRestClient(url); return new DefaultRestClient(url, useProxy);
} }
catch (PlatformNotSupportedException ex) catch (PlatformNotSupportedException ex)
{ {
@@ -16,4 +20,5 @@ namespace Discord.Net.Rest
} }
}; };
} }
}
} }

View File

@@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.IO; using System.IO;
using System.Net;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
@@ -23,18 +24,20 @@ namespace Discord.Net.WebSockets
private readonly SemaphoreSlim _lock; private readonly SemaphoreSlim _lock;
private readonly Dictionary<string, string> _headers; private readonly Dictionary<string, string> _headers;
private ClientWebSocket _client; private ClientWebSocket _client;
private IWebProxy _proxy;
private Task _task; private Task _task;
private CancellationTokenSource _cancelTokenSource; private CancellationTokenSource _cancelTokenSource;
private CancellationToken _cancelToken, _parentToken; private CancellationToken _cancelToken, _parentToken;
private bool _isDisposed, _isDisconnecting; private bool _isDisposed, _isDisconnecting;
public DefaultWebSocketClient() public DefaultWebSocketClient(IWebProxy proxy = null)
{ {
_lock = new SemaphoreSlim(1, 1); _lock = new SemaphoreSlim(1, 1);
_cancelTokenSource = new CancellationTokenSource(); _cancelTokenSource = new CancellationTokenSource();
_cancelToken = CancellationToken.None; _cancelToken = CancellationToken.None;
_parentToken = CancellationToken.None; _parentToken = CancellationToken.None;
_headers = new Dictionary<string, string>(); _headers = new Dictionary<string, string>();
_proxy = proxy;
} }
private void Dispose(bool disposing) private void Dispose(bool disposing)
{ {
@@ -70,7 +73,7 @@ namespace Discord.Net.WebSockets
_cancelToken = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _cancelTokenSource.Token).Token; _cancelToken = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _cancelTokenSource.Token).Token;
_client = new ClientWebSocket(); _client = new ClientWebSocket();
_client.Options.Proxy = null; _client.Options.Proxy = _proxy;
_client.Options.KeepAliveInterval = TimeSpan.Zero; _client.Options.KeepAliveInterval = TimeSpan.Zero;
foreach (var header in _headers) foreach (var header in _headers)
{ {

View File

@@ -1,21 +1,27 @@
using System; using System;
using System.Net;
namespace Discord.Net.WebSockets namespace Discord.Net.WebSockets
{ {
public static class DefaultWebSocketProvider public static class DefaultWebSocketProvider
{ {
#if DEFAULTWEBSOCKET #if DEFAULTWEBSOCKET
public static readonly WebSocketProvider Instance = () => public static readonly WebSocketProvider Instance = Create();
public static WebSocketProvider Create(IWebProxy proxy = null)
{
return () =>
{ {
try try
{ {
return new DefaultWebSocketClient(); return new DefaultWebSocketClient(proxy);
} }
catch (PlatformNotSupportedException ex) catch (PlatformNotSupportedException ex)
{ {
throw new PlatformNotSupportedException("The default WebSocketProvider is not supported on this platform.", ex); throw new PlatformNotSupportedException("The default WebSocketProvider is not supported on this platform.", ex);
} }
}; };
}
#else #else
public static readonly WebSocketProvider Instance = () => public static readonly WebSocketProvider Instance = () =>
{ {