Skip to content

Commit

Permalink
proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
doghappy committed Jun 1, 2022
1 parent 2c63fdf commit 5dbafea
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 2 deletions.
51 changes: 51 additions & 0 deletions src/SocketIOClient.IntegrationTests/V4WebSocketTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 1,11 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
using System.Net.WebSockets;
using System.Net.Sockets;

namespace SocketIOClient.IntegrationTests
{
Expand All @@ -25,5 29,52 @@ public async Task Should_Be_Work_Even_An_Exception_Thrown_From_Handler()

results.Should().Equal(6);
}

[TestMethod]
public void Should_Throw_WebSocketException_If_Proxy_Server_Not_Start()
{
using var io = CreateSocketIO(new SocketIOOptions
{
Proxy = new WebProxy("localhost", 6138),
Reconnection = false,
ConnectionTimeout = TimeSpan.FromSeconds(1)
});
Action action = () => io.ConnectAsync().Wait();
action.Should().Throw<WebSocketException>();
}

[TestMethod]
public async Task Should_Able_To_Connect_To_Proxy()
{
var msgs = new List<string>();
_ = Task.Run(StartProxyServer);
await Task.Delay(200);
using var io = CreateSocketIO(new SocketIOOptions
{
Proxy = new WebProxy("localhost", 6138),
Reconnection = false,
ConnectionTimeout = TimeSpan.FromSeconds(2)
});
Action action = () => io.ConnectAsync().Wait();
action.Should().Throw<TaskCanceledException>();
msgs.Should().BeEquivalentTo(new[] { $"CONNECT localhost:11400 HTTP/1.1{Environment.NewLine}Host: localhost:11400{Environment.NewLine Environment.NewLine}" });

void StartProxyServer()
{
var proxy = new TcpListener(IPAddress.Parse("127.0.0.1"), 6138);
proxy.Start();
byte[] bytes = new byte[256];
while (true)
{
var client = proxy.AcceptTcpClient();
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
msgs.Add(System.Text.Encoding.UTF8.GetString(bytes, 0, i));
}
}
}
}
}
}
1 change: 1 addition & 0 deletions src/SocketIOClient/SocketIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 207,7 @@ private async Task CreateTransportAsync()
}
_transport.Namespace = _namespace;
SetHeaders();
_transport.SetProxy(Options.Proxy);
}

private void SetHeaders()
Expand Down
2 changes: 2 additions & 0 deletions src/SocketIOClient/SocketIOOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 1,7 @@
using SocketIOClient.Transport;
using System;
using System.Collections.Generic;
using System.Net;

namespace SocketIOClient
{
Expand Down Expand Up @@ -61,5 62,6 @@ public double RandomizationFactor
public bool AutoUpgrade { get; set; }

public object Auth { get; set; }
public IWebProxy Proxy { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/SocketIOClient/Transport/BaseTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 7,7 @@
using SocketIOClient.JsonSerializer;
using SocketIOClient.Messages;
using SocketIOClient.UriConverters;
using System.Net;

namespace SocketIOClient.Transport
{
Expand Down Expand Up @@ -130,6 131,7 @@ private void StartPing(CancellationToken cancellationToken)
public abstract Task DisconnectAsync(CancellationToken cancellationToken);

public abstract void AddHeader(string key, string val);
public abstract void SetProxy(IWebProxy proxy);

public virtual void Dispose()
{
Expand Down
6 changes: 6 additions & 0 deletions src/SocketIOClient/Transport/HttpTransport.cs
Original file line number Diff line number Diff line change
@@ -1,4 1,5 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -94,6 95,11 @@ public override void AddHeader(string key, string val)
_http.DefaultRequestHeaders.Add(key, val);
}

public override void SetProxy(IWebProxy proxy)
{
throw new NotImplementedException();
}

public override void Dispose()
{
base.Dispose();
Expand Down
2 changes: 2 additions & 0 deletions src/SocketIOClient/Transport/IClientWebSocket.cs
Original file line number Diff line number Diff line change
@@ -1,4 1,5 @@
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -12,5 13,6 @@ public interface IClientWebSocket : IDisposable
Task DisconnectAsync(CancellationToken cancellationToken);
Task SendAsync(byte[] bytes, TransportMessageType type, bool endOfMessage, CancellationToken cancellationToken);
void AddHeader(string key, string val);
void SetProxy(IWebProxy proxy);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 1,5 @@
using System;
using System.Net;
using System.Net.WebSockets;
using System.Reactive.Linq;
using System.Reactive.Subjects;
Expand Down Expand Up @@ -36,7 37,7 @@ public SystemNetWebSocketsClientWebSocket(int eio)

private void Listen()
{
Task.Factory.StartNew(async() =>
Task.Factory.StartNew(async () =>
{
while (true)
{
Expand Down Expand Up @@ -133,6 134,8 @@ public void AddHeader(string key, string val)
_ws.Options.SetRequestHeader(key, val);
}

public void SetProxy(IWebProxy proxy) => _ws.Options.Proxy = proxy;

public void Dispose()
{
_textSubject.Dispose();
Expand Down
3 changes: 2 additions & 1 deletion src/SocketIOClient/Transport/WebSocketTransport.cs
Original file line number Diff line number Diff line change
@@ -1,4 1,5 @@
using System;
using System.Net;
using System.Reactive.Linq;
using System.Text;
using System.Threading;
Expand All @@ -19,7 20,6 @@ public WebSocketTransport(IClientWebSocket ws, SocketIOOptions options, IJsonSer
_ws.BytesObservable.Subscribe(this);
}

const int ReceiveChunkSize = 1024 * 8;
const int SendChunkSize = 1024 * 8;

readonly IClientWebSocket _ws;
Expand Down Expand Up @@ -82,6 82,7 @@ public override async Task SendAsync(Payload payload, CancellationToken cancella
}

public override void AddHeader(string key, string val) => _ws.AddHeader(key, val);
public override void SetProxy(IWebProxy proxy) => _ws.SetProxy(proxy);

public override void Dispose()
{
Expand Down

0 comments on commit 5dbafea

Please sign in to comment.