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:
@@ -22,7 +22,7 @@ namespace Discord.Net.Rest
|
||||
private CancellationToken _cancelToken;
|
||||
private bool _isDisposed;
|
||||
|
||||
public DefaultRestClient(string baseUrl)
|
||||
public DefaultRestClient(string baseUrl, bool useProxy = false)
|
||||
{
|
||||
_baseUrl = baseUrl;
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace Discord.Net.Rest
|
||||
{
|
||||
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
|
||||
UseCookies = false,
|
||||
UseProxy = false
|
||||
UseProxy = useProxy,
|
||||
});
|
||||
SetHeader("accept-encoding", "gzip, deflate");
|
||||
|
||||
|
||||
@@ -4,11 +4,15 @@ namespace Discord.Net.Rest
|
||||
{
|
||||
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
|
||||
{
|
||||
return new DefaultRestClient(url);
|
||||
return new DefaultRestClient(url, useProxy);
|
||||
}
|
||||
catch (PlatformNotSupportedException ex)
|
||||
{
|
||||
@@ -16,4 +20,5 @@ namespace Discord.Net.Rest
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
@@ -23,18 +24,20 @@ namespace Discord.Net.WebSockets
|
||||
private readonly SemaphoreSlim _lock;
|
||||
private readonly Dictionary<string, string> _headers;
|
||||
private ClientWebSocket _client;
|
||||
private IWebProxy _proxy;
|
||||
private Task _task;
|
||||
private CancellationTokenSource _cancelTokenSource;
|
||||
private CancellationToken _cancelToken, _parentToken;
|
||||
private bool _isDisposed, _isDisconnecting;
|
||||
|
||||
public DefaultWebSocketClient()
|
||||
public DefaultWebSocketClient(IWebProxy proxy = null)
|
||||
{
|
||||
_lock = new SemaphoreSlim(1, 1);
|
||||
_cancelTokenSource = new CancellationTokenSource();
|
||||
_cancelToken = CancellationToken.None;
|
||||
_parentToken = CancellationToken.None;
|
||||
_headers = new Dictionary<string, string>();
|
||||
_proxy = proxy;
|
||||
}
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
@@ -70,7 +73,7 @@ namespace Discord.Net.WebSockets
|
||||
_cancelToken = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _cancelTokenSource.Token).Token;
|
||||
|
||||
_client = new ClientWebSocket();
|
||||
_client.Options.Proxy = null;
|
||||
_client.Options.Proxy = _proxy;
|
||||
_client.Options.KeepAliveInterval = TimeSpan.Zero;
|
||||
foreach (var header in _headers)
|
||||
{
|
||||
|
||||
@@ -1,21 +1,27 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
|
||||
namespace Discord.Net.WebSockets
|
||||
{
|
||||
public static class DefaultWebSocketProvider
|
||||
{
|
||||
#if DEFAULTWEBSOCKET
|
||||
public static readonly WebSocketProvider Instance = () =>
|
||||
public static readonly WebSocketProvider Instance = Create();
|
||||
|
||||
public static WebSocketProvider Create(IWebProxy proxy = null)
|
||||
{
|
||||
return () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
return new DefaultWebSocketClient();
|
||||
return new DefaultWebSocketClient(proxy);
|
||||
}
|
||||
catch (PlatformNotSupportedException ex)
|
||||
{
|
||||
throw new PlatformNotSupportedException("The default WebSocketProvider is not supported on this platform.", ex);
|
||||
}
|
||||
};
|
||||
}
|
||||
#else
|
||||
public static readonly WebSocketProvider Instance = () =>
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user