Skip to content

Commit

Permalink
use SocketFactory in client
Browse files Browse the repository at this point in the history
  • Loading branch information
trudyhood committed Jul 12, 2021
1 parent 9ba6145 commit d2a6860
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
12 changes: 5 additions & 7 deletions VpnHood.Client/TcpProxyHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using VpnHood.Tunneling.Messages;
using VpnHood.Client.Device;
using System.Collections.Generic;
using System.IO;
using VpnHood.Common;

namespace VpnHood.Client
Expand All @@ -19,20 +18,18 @@ class TcpProxyHost : IDisposable
{
private readonly IPAddress _loopbackAddress;
private readonly TcpListener _tcpListener;
private readonly IPacketCapture _packetCapture;
private readonly CancellationTokenSource _cancellationTokenSource = new();
private readonly List<IPPacket> _ipPackets = new();
private IPEndPoint _localEndpoint;
private bool _disposed;
private VpnHoodClient Client { get; }

public TcpProxyHost(VpnHoodClient client, IPacketCapture packetCapture, IPAddress loopbackAddress)
public TcpProxyHost(VpnHoodClient client, IPAddress loopbackAddress)
{
if (!client.Connected)
throw new Exception($"{typeof(TcpProxyHost).Name}: is not connected!");

Client = client ?? throw new ArgumentNullException(nameof(client));
_packetCapture = packetCapture ?? throw new ArgumentNullException(nameof(packetCapture));
_loopbackAddress = loopbackAddress ?? throw new ArgumentNullException(nameof(loopbackAddress));
_tcpListener = new TcpListener(IPAddress.Any, 0);
}
Expand Down Expand Up @@ -159,12 +156,13 @@ private async Task ProcessClient(TcpClient tcpOrgClient, CancellationToken cance

// check invalid income
if (!Equals(orgRemoteEndPoint.Address, _loopbackAddress))
throw new Exception($"TcpProxy rejected the outband connection!");
throw new Exception($"TcpProxy rejected an outband connection!");

// Check IpFilter
if (!Client.IsInIncludeIpRange(natItem.DestinationAddress))
if (!Client.IsInIpRange(natItem.DestinationAddress))
{
var tcpClient = new TcpClient() { NoDelay = true };
var tcpClient = Client.SocketFactory.CreateTcpClient();
tcpClient.NoDelay = true;
await Util.TcpClient_ConnectAsync(tcpClient, natItem.DestinationAddress, natItem.DestinationPort, tcpOrgClient.ReceiveTimeout, cancellationToken);
var bypassChannel = new TcpProxyChannel(new TcpClientStream(tcpOrgClient, tcpOrgClient.GetStream()), new TcpClientStream(tcpClient, tcpClient.GetStream()));
return;
Expand Down
12 changes: 8 additions & 4 deletions VpnHood.Client/VpnHoodClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using VpnHood.Client.Device;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using VpnHood.Tunneling.Factory;

namespace VpnHood.Client
{
Expand All @@ -30,7 +31,7 @@ protected override Ping CreatePing() //PacketCapture can not protect Ping so Pin
=> throw new NotSupportedException($"{nameof(CreatePing)} is not supported by {nameof(ClientProxyManager)}!");
protected override UdpClient CreateUdpClient()
{
UdpClient udpClient = new(0);
var udpClient = _client.SocketFactory.CreateUdpClient();
if (_client._packetCapture.CanProtectSocket)
_client._packetCapture.ProtectSocket(udpClient.Client);
return udpClient;
Expand Down Expand Up @@ -68,6 +69,8 @@ public void Clear()

internal Nat Nat { get; }
internal Tunnel Tunnel { get; private set; }
internal SocketFactory SocketFactory { get; }

public int Timeout { get; set; }
public Token Token { get; }
public Guid ClientId { get; }
Expand Down Expand Up @@ -104,6 +107,7 @@ public VpnHoodClient(IPacketCapture packetCapture, Guid clientId, Token token, C
Version = options.Version;
ExcludeLocalNetwork = options.ExcludeLocalNetwork;
UseUdpChannel = options.UseUdpChannel;
SocketFactory = options.SocketFactory;
IncludeIpRanges = options.IncludeIpRanges != null ? IpRange.Sort(options.IncludeIpRanges).ToArray() : null;
ExcludeIpRanges = options.ExcludeIpRanges != null ? IpRange.Sort(options.ExcludeIpRanges).ToArray() : null;
Nat = new Nat(true);
Expand Down Expand Up @@ -210,7 +214,7 @@ public async Task Connect()

// create Tcp Proxy Host
VhLogger.Instance.LogTrace($"Creating {VhLogger.FormatTypeName<TcpProxyHost>()}...");
_tcpProxyHost = new TcpProxyHost(this, _packetCapture, TcpProxyLoopbackAddress);
_tcpProxyHost = new TcpProxyHost(this, TcpProxyLoopbackAddress);
_ = _tcpProxyHost.StartListening();

// Preparing device
Expand Down Expand Up @@ -304,7 +308,7 @@ private void PacketCapture_OnPacketReceivedFromInbound(object sender, Device.Pac
if (ipPacket.Version != IPVersion.IPv4)
continue;

var isInRange = IsInIncludeIpRange(ipPacket.DestinationAddress);
var isInRange = IsInIpRange(ipPacket.DestinationAddress);

// DNS packet must go through tunnel
if (!_packetCapture.IsDnsServersSupported && UpdateDnsRequest(ipPacket, true))
Expand Down Expand Up @@ -353,7 +357,7 @@ private void PacketCapture_OnPacketReceivedFromInbound(object sender, Device.Pac
}
}

public bool IsInIncludeIpRange(IPAddress ipAddress)
public bool IsInIpRange(IPAddress ipAddress)
{
// all IPs are included if there is no filter
if (IncludeIpRanges == null && ExcludeIpRanges == null)
Expand Down

0 comments on commit d2a6860

Please sign in to comment.