Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 4c5d4cd

Browse files
authored
Merge pull request #712 from justcoding121/master
beta
2 parents 8cbf0f4 + b67ad5d commit 4c5d4cd

File tree

19 files changed

+2764
-109
lines changed

19 files changed

+2764
-109
lines changed

examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ public void StartProxy()
9090
//};
9191

9292
//proxyServer.AddEndPoint(transparentEndPoint);
93-
//proxyServer.UpStreamHttpProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };
94-
//proxyServer.UpStreamHttpsProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };
93+
//proxyServer.UpStreamHttpProxy = new ExternalProxy("localhost", 8888);
94+
//proxyServer.UpStreamHttpsProxy = new ExternalProxy("localhost", 8888);
95+
96+
// SOCKS proxy
97+
//proxyServer.UpStreamHttpProxy = new ExternalProxy("46.63.0.17", 4145) { ProxyType = ExternalProxyType.Socks4 };
98+
//proxyServer.UpStreamHttpsProxy = new ExternalProxy("46.63.0.17", 4145) { ProxyType = ExternalProxyType.Socks4 };
9599

96100
foreach (var endPoint in proxyServer.ProxyEndPoints)
97101
{

src/Titanium.Web.Proxy/Extensions/TcpExtensions.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,18 @@ namespace Titanium.Web.Proxy.Extensions
55
{
66
internal static class TcpExtensions
77
{
8-
internal static void CloseSocket(this TcpClient tcpClient)
9-
{
10-
if (tcpClient == null)
11-
{
12-
return;
13-
}
14-
15-
try
16-
{
17-
tcpClient.Close();
18-
}
19-
catch
20-
{
21-
// ignored
22-
}
23-
}
24-
25-
268
/// <summary>
279
/// Check if a TcpClient is good to be used.
2810
/// This only checks if send is working so local socket is still connected.
2911
/// Receive can only be verified by doing a valid read from server without exceptions.
3012
/// So in our case we should retry with new connection from pool if first read after getting the connection fails.
3113
/// https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected(v=vs.110).aspx
3214
/// </summary>
33-
/// <param name="client"></param>
15+
/// <param name="socket"></param>
3416
/// <returns></returns>
35-
internal static bool IsGoodConnection(this TcpClient client)
17+
internal static bool IsGoodConnection(this Socket socket)
3618
{
37-
var socket = client.Client;
38-
39-
if (!client.Connected || !socket.Connected)
19+
if (!socket.Connected)
4020
{
4121
return false;
4222
}

src/Titanium.Web.Proxy/Http/HttpWebClient.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Net;
33
using System.Threading;
44
using System.Threading.Tasks;
5+
using Titanium.Web.Proxy.Models;
56
using Titanium.Web.Proxy.Network.Tcp;
67

78
namespace Titanium.Web.Proxy.Http
@@ -103,10 +104,14 @@ internal async Task SendRequest(bool enable100ContinueBehaviour, bool isTranspar
103104
{
104105
var upstreamProxy = Connection.UpStreamProxy;
105106

106-
bool useUpstreamProxy = upstreamProxy != null && Connection.IsHttps == false;
107+
bool useUpstreamProxy = upstreamProxy != null && upstreamProxy.ProxyType == ExternalProxyType.Http &&
108+
!Connection.IsHttps;
107109

108110
var serverStream = Connection.Stream;
109111

112+
string? upstreamProxyUserName = null;
113+
string? upstreamProxyPassword = null;
114+
110115
string url;
111116
if (!useUpstreamProxy || isTransparent)
112117
{
@@ -115,19 +120,13 @@ internal async Task SendRequest(bool enable100ContinueBehaviour, bool isTranspar
115120
else
116121
{
117122
url = Request.RequestUri.ToString();
118-
}
119-
120-
string? upstreamProxyUserName = null;
121-
string? upstreamProxyPassword = null;
122123

123-
// Send Authentication to Upstream proxy if needed
124-
if (!isTransparent && upstreamProxy != null
125-
&& Connection.IsHttps == false
126-
&& !string.IsNullOrEmpty(upstreamProxy.UserName)
127-
&& upstreamProxy.Password != null)
128-
{
129-
upstreamProxyUserName = upstreamProxy.UserName;
130-
upstreamProxyPassword = upstreamProxy.Password;
124+
// Send Authentication to Upstream proxy if needed
125+
if (!string.IsNullOrEmpty(upstreamProxy!.UserName) && upstreamProxy.Password != null)
126+
{
127+
upstreamProxyUserName = upstreamProxy.UserName;
128+
upstreamProxyPassword = upstreamProxy.Password;
129+
}
131130
}
132131

133132
// prepare the request & headers

src/Titanium.Web.Proxy/Models/ExternalProxy.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class ExternalProxy : IExternalProxy
2525
/// </summary>
2626
public bool BypassLocalhost { get; set; }
2727

28+
public ExternalProxyType ProxyType { get; set; }
29+
2830
/// <summary>
2931
/// Username.
3032
/// </summary>
@@ -111,4 +113,16 @@ public override string ToString()
111113
return $"{HostName}:{Port}";
112114
}
113115
}
116+
117+
public enum ExternalProxyType
118+
{
119+
/// <summary>A HTTP/HTTPS proxy server.</summary>
120+
Http,
121+
122+
/// <summary>A SOCKS4[A] proxy server.</summary>
123+
Socks4,
124+
125+
/// <summary>A SOCKS5 proxy server.</summary>
126+
Socks5
127+
}
114128
}

src/Titanium.Web.Proxy/Models/IExternalProxy.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public interface IExternalProxy
1212
/// </summary>
1313
bool BypassLocalhost { get; set; }
1414

15+
ExternalProxyType ProxyType { get; set; }
16+
1517
/// <summary>
1618
/// Username.
1719
/// </summary>

src/Titanium.Web.Proxy/Network/Tcp/TcpClientConnection.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ internal class TcpClientConnection : IDisposable
1818
{
1919
public object ClientUserData { get; set; }
2020

21-
internal TcpClientConnection(ProxyServer proxyServer, TcpClient tcpClient)
21+
internal TcpClientConnection(ProxyServer proxyServer, Socket tcpClientSocket)
2222
{
23-
this.tcpClient = tcpClient;
23+
this.tcpClientSocket = tcpClientSocket;
2424
this.proxyServer = proxyServer;
2525
this.proxyServer.UpdateClientConnectionCount(true);
2626
}
@@ -29,21 +29,21 @@ internal TcpClientConnection(ProxyServer proxyServer, TcpClient tcpClient)
2929

3030
public Guid Id { get; } = Guid.NewGuid();
3131

32-
public EndPoint LocalEndPoint => tcpClient.Client.LocalEndPoint;
32+
public EndPoint LocalEndPoint => tcpClientSocket.LocalEndPoint;
3333

34-
public EndPoint RemoteEndPoint => tcpClient.Client.RemoteEndPoint;
34+
public EndPoint RemoteEndPoint => tcpClientSocket.RemoteEndPoint;
3535

3636
internal SslProtocols SslProtocol { get; set; }
3737

3838
internal SslApplicationProtocol NegotiatedApplicationProtocol { get; set; }
3939

40-
private readonly TcpClient tcpClient;
40+
private readonly Socket tcpClientSocket;
4141

4242
private int? processId;
4343

4444
public Stream GetStream()
4545
{
46-
return tcpClient.GetStream();
46+
return new NetworkStream(tcpClientSocket, true);
4747
}
4848

4949
public int GetProcessId(ProxyEndPoint endPoint)
@@ -86,7 +86,15 @@ public void Dispose()
8686
// This way we can push tcp Time_Wait to client side when possible.
8787
await Task.Delay(1000);
8888
proxyServer.UpdateClientConnectionCount(false);
89-
tcpClient.CloseSocket();
89+
90+
try
91+
{
92+
tcpClientSocket.Close();
93+
}
94+
catch
95+
{
96+
// ignore
97+
}
9098
});
9199
}
92100
}

0 commit comments

Comments
 (0)