Skip to content

Commit

Permalink
Merge branch 'beta' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoding121 committed Jul 19, 2017
2 parents 14bdb65 + 99e3b57 commit 11632fe
Show file tree
Hide file tree
Showing 17 changed files with 959 additions and 944 deletions.
6 changes: 4 additions & 2 deletions Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ public bool ReRequest
/// <summary>
/// Constructor to initialize the proxy
/// </summary>
internal SessionEventArgs(int bufferSize, ProxyEndPoint endPoint, Func<SessionEventArgs, Task> httpResponseHandler)
internal SessionEventArgs(int bufferSize,
ProxyEndPoint endPoint,
Func<SessionEventArgs, Task> httpResponseHandler)
{
this.bufferSize = bufferSize;
this.httpResponseHandler = httpResponseHandler;

ProxyClient = new ProxyClient();
WebSession = new HttpWebClient();
WebSession = new HttpWebClient(bufferSize);

WebSession.ProcessId = new Lazy<int>(() =>
{
Expand Down
6 changes: 4 additions & 2 deletions Titanium.Web.Proxy/EventArguments/TunnelConnectEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using Titanium.Web.Proxy.Models;
using System.Net;
using Titanium.Web.Proxy.Models;

namespace Titanium.Web.Proxy.EventArguments
{
public class TunnelConnectSessionEventArgs : SessionEventArgs
{
public bool IsHttpsConnect { get; set; }

public TunnelConnectSessionEventArgs(ProxyEndPoint endPoint) : base(0, endPoint, null)
public TunnelConnectSessionEventArgs(int bufferSize, ProxyEndPoint endPoint)
: base(bufferSize, endPoint, null)
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Titanium.Web.Proxy.Extensions
{
internal static class DotNetStandardExtensions
internal static class DotNet45Extensions
{
#if NET45
/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Titanium.Web.Proxy/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ internal static class StreamExtensions
/// <param name="input"></param>
/// <param name="output"></param>
/// <param name="onCopy"></param>
internal static async Task CopyToAsync(this Stream input, Stream output, Action<byte[], int, int> onCopy)
internal static async Task CopyToAsync(this Stream input, Stream output, Action<byte[], int, int> onCopy, int bufferSize)
{
byte[] buffer = new byte[81920];
byte[] buffer = new byte[bufferSize];
while (true)
{
int num = await input.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
Expand Down
28 changes: 0 additions & 28 deletions Titanium.Web.Proxy/Extensions/TcpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,6 @@ namespace Titanium.Web.Proxy.Extensions
{
internal static class TcpExtensions
{
/// <summary>
/// verifies if the underlying socket is connected before using a TcpClient connection
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
internal static bool IsConnected(this Socket client)
{
// This is how you can determine whether a socket is still connected.
bool blockingState = client.Blocking;

try
{
var tmp = new byte[1];

client.Blocking = false;
client.Send(tmp, 0, 0);
return true;
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
return e.SocketErrorCode == SocketError.WouldBlock;
}
finally
{
client.Blocking = blockingState;
}
}

#if NET45
/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion Titanium.Web.Proxy/Helpers/HttpRequestWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ namespace Titanium.Web.Proxy.Helpers
{
sealed class HttpRequestWriter : HttpWriter
{
public HttpRequestWriter(Stream stream) : base(stream, true)
public HttpRequestWriter(Stream stream, int bufferSize)
: base(stream, bufferSize, true)
{
}
}
Expand Down
3 changes: 2 additions & 1 deletion Titanium.Web.Proxy/Helpers/HttpResponseWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace Titanium.Web.Proxy.Helpers
{
sealed class HttpResponseWriter : HttpWriter
{
public HttpResponseWriter(Stream stream) : base(stream, true)
public HttpResponseWriter(Stream stream, int bufferSize)
: base(stream, bufferSize, true)
{
}

Expand Down
3 changes: 2 additions & 1 deletion Titanium.Web.Proxy/Helpers/HttpWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Titanium.Web.Proxy.Helpers
{
abstract class HttpWriter : StreamWriter
{
protected HttpWriter(Stream stream, bool leaveOpen) : base(stream, Encoding.ASCII, 1024, leaveOpen)
protected HttpWriter(Stream stream, int bufferSize, bool leaveOpen)
: base(stream, Encoding.ASCII, bufferSize, leaveOpen)
{
NewLine = ProxyConstants.NewLine;
}
Expand Down
7 changes: 4 additions & 3 deletions Titanium.Web.Proxy/Helpers/Tcp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ internal static TcpRow GetTcpRowByLocalPort(IpVersion ipVersion, int localPort)
/// <param name="onDataSend"></param>
/// <param name="onDataReceive"></param>
/// <returns></returns>
internal static async Task SendRaw(Stream clientStream, Stream serverStream, Action<byte[], int, int> onDataSend, Action<byte[], int, int> onDataReceive)
internal static async Task SendRaw(Stream clientStream, Stream serverStream, int bufferSize,
Action<byte[], int, int> onDataSend, Action<byte[], int, int> onDataReceive)
{
//Now async relay all server=>client & client=>server data
var sendRelay = clientStream.CopyToAsync(serverStream, onDataSend);
var sendRelay = clientStream.CopyToAsync(serverStream, onDataSend, bufferSize);

var receiveRelay = serverStream.CopyToAsync(clientStream, onDataReceive);
var receiveRelay = serverStream.CopyToAsync(clientStream, onDataReceive, bufferSize);

await Task.WhenAll(sendRelay, receiveRelay);
}
Expand Down
13 changes: 11 additions & 2 deletions Titanium.Web.Proxy/Http/HttpWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Titanium.Web.Proxy.Http
/// </summary>
public class HttpWebClient : IDisposable
{
private int bufferSize;

/// <summary>
/// Connection to server
/// </summary>
Expand All @@ -23,6 +25,11 @@ public class HttpWebClient : IDisposable
/// </summary>
public Guid RequestId { get; }

/// <summary>
/// Override UpStreamEndPoint for this request; Local NIC via request is made
/// </summary>
public IPEndPoint UpStreamEndPoint { get; set; }

/// <summary>
/// Headers passed with Connect.
/// </summary>
Expand All @@ -49,8 +56,10 @@ public class HttpWebClient : IDisposable
/// </summary>
public bool IsHttps => Request.IsHttps;

internal HttpWebClient()
internal HttpWebClient(int bufferSize)
{
this.bufferSize = bufferSize;

RequestId = Guid.NewGuid();
Request = new Request();
Response = new Response();
Expand All @@ -77,7 +86,7 @@ internal async Task SendRequest(bool enable100ContinueBehaviour)

byte[] requestBytes;
using (var ms = new MemoryStream())
using (var writer = new HttpRequestWriter(ms))
using (var writer = new HttpRequestWriter(ms, bufferSize))
{
var upstreamProxy = ServerConnection.UpStreamHttpProxy;

Expand Down
6 changes: 6 additions & 0 deletions Titanium.Web.Proxy/Network/Tcp/TcpConnection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using StreamExtended.Network;
using System;
using System.Net;
using System.Net.Sockets;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Models;
Expand All @@ -25,6 +26,11 @@ internal class TcpConnection : IDisposable

internal bool UseUpstreamProxy { get; set; }

/// <summary>
/// Local NIC via connection is made
/// </summary>
internal IPEndPoint UpStreamEndPoint { get; set; }

/// <summary>
/// Http version
/// </summary>
Expand Down
18 changes: 11 additions & 7 deletions Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using StreamExtended.Network;
using System;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;
Expand All @@ -17,19 +18,21 @@ namespace Titanium.Web.Proxy.Network.Tcp
internal class TcpConnectionFactory
{
/// <summary>
/// Creates a TCP connection to server
/// Creates a TCP connection to server
/// </summary>
/// <param name="server"></param>
/// <param name="remoteHostName"></param>
/// <param name="remotePort"></param>
/// <param name="httpVersion"></param>
/// <param name="isConnect"></param>
/// <param name="isHttps"></param>
/// <param name="isConnect"></param>
/// <param name="upStreamEndPoint"></param>
/// <param name="externalHttpProxy"></param>
/// <param name="externalHttpsProxy"></param>
/// <returns></returns>
internal async Task<TcpConnection> CreateClient(ProxyServer server, string remoteHostName, int remotePort, Version httpVersion, bool isHttps,
bool isConnect, ExternalProxy externalHttpProxy, ExternalProxy externalHttpsProxy)
internal async Task<TcpConnection> CreateClient(ProxyServer server,
string remoteHostName, int remotePort, Version httpVersion, bool isHttps,
bool isConnect, IPEndPoint upStreamEndPoint, ExternalProxy externalHttpProxy, ExternalProxy externalHttpsProxy)
{
bool useUpstreamProxy = false;
var externalProxy = isHttps ? externalHttpsProxy : externalHttpProxy;
Expand All @@ -52,10 +55,10 @@ internal async Task<TcpConnection> CreateClient(ProxyServer server, string remot
try
{
#if NET45
client = new TcpClient(server.UpStreamEndPoint);
client = new TcpClient(upStreamEndPoint);
#else
client = new TcpClient();
client.Client.Bind(server.UpStreamEndPoint);
client.Client.Bind(upStreamEndPoint);
#endif

//If this proxy uses another external proxy then create a tunnel request for HTTP/HTTPS connections
Expand All @@ -72,7 +75,7 @@ internal async Task<TcpConnection> CreateClient(ProxyServer server, string remot

if (useUpstreamProxy && (isConnect || isHttps))
{
using (var writer = new HttpRequestWriter(stream))
using (var writer = new HttpRequestWriter(stream, server.BufferSize))
{
await writer.WriteLineAsync($"CONNECT {remoteHostName}:{remotePort} HTTP/{httpVersion}");
await writer.WriteLineAsync($"Host: {remoteHostName}:{remotePort}");
Expand Down Expand Up @@ -128,6 +131,7 @@ await writer.WriteLineAsync("Proxy-Authorization" + ": Basic " +
{
UpStreamHttpProxy = externalHttpProxy,
UpStreamHttpsProxy = externalHttpsProxy,
UpStreamEndPoint = upStreamEndPoint,
HostName = remoteHostName,
Port = remotePort,
IsHttps = isHttps,
Expand Down
4 changes: 2 additions & 2 deletions Titanium.Web.Proxy/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.

[assembly: AssemblyTitle("Titanium.Web.Proxy.Properties")]
[assembly: AssemblyTitle("Titanium.Web.Proxy")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Titanium.Web.Proxy.Properties")]
[assembly: AssemblyProduct("Titanium.Web.Proxy")]
[assembly: AssemblyCopyright("Copyright © Titanium 2015-2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
Expand Down
Loading

0 comments on commit 11632fe

Please sign in to comment.