Skip to content

Commit

Permalink
remove IsHandled from OnPacketArrivalFromInbound
Browse files Browse the repository at this point in the history
  • Loading branch information
trudyhood committed Jul 12, 2021
1 parent 016995c commit b768abf
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 50 deletions.
4 changes: 2 additions & 2 deletions VpnHood.Client.App/VpnHoodApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ public async Task Connect(Guid clientProfileId, bool diagnose = false, string us
packetCapture.Mtu = TunnelUtil.MtuWithoutFragmentation;

// App filters
if (packetCapture.IsExcludeAppsSupported && UserSettings.AppFiltersMode == FilterMode.Exclude) packetCapture.ExcludeApps = UserSettings.AppFilters;
if (packetCapture.IsIncludeAppsSupported && UserSettings.AppFiltersMode == FilterMode.Include) packetCapture.IncludeApps = UserSettings.AppFilters;
if (packetCapture.CanExcludeApps && UserSettings.AppFiltersMode == FilterMode.Exclude) packetCapture.ExcludeApps = UserSettings.AppFilters;
if (packetCapture.CanIncludeApps && UserSettings.AppFiltersMode == FilterMode.Include) packetCapture.IncludeApps = UserSettings.AppFilters;

// connect
await ConnectInternal(packetCapture, userAgent);
Expand Down
6 changes: 3 additions & 3 deletions VpnHood.Client.Device.Android/AppVpnService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class AppVpnService : VpnService, IPacketCapture
public bool CanSendPacketToOutbound => false;

#region Application Filter
public bool IsExcludeAppsSupported => true;
public bool IsIncludeAppsSupported => true;
public bool CanExcludeApps => true;
public bool CanIncludeApps => true;
public string[] ExcludeApps { get; set; } = Array.Empty<string>();
public string[] IncludeApps { get; set; } = Array.Empty<string>();
#endregion
Expand Down Expand Up @@ -187,7 +187,7 @@ public void SendPacketToInbound(IEnumerable<IPPacket> ipPackets)
public void SendPacketToOutbound(IEnumerable<IPPacket> ipPackets)
=> throw new NotSupportedException();

public bool IsProtectSocketSuported => true;
public bool CanProtectSocket => true;

public void ProtectSocket(System.Net.Sockets.Socket socket)
{
Expand Down
8 changes: 3 additions & 5 deletions VpnHood.Client.Device.WinDivert/WinDivertPacketCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ protected virtual void ProcessPacket(IPPacket ipPacket)
{
var eventArgs = new PacketCaptureArrivalEventArgs(new[] { ipPacket }, this); //todo: share buffer
OnPacketArrivalFromInbound?.Invoke(this, eventArgs);
foreach (var item in eventArgs.ArivalPackets.Where(x => x.Passthru))
SendPacket(item.IpPacket, true);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -128,8 +126,8 @@ public IpNetwork[] IncludeNetworks
}

#region Applications Filter
public bool IsExcludeAppsSupported => false;
public bool IsIncludeAppsSupported => false;
public bool CanExcludeApps => false;
public bool CanIncludeApps => false;
public string[] ExcludeApps { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
public string[] IncludeApps { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }

Expand Down Expand Up @@ -183,7 +181,7 @@ public void StopCapture()

public IPAddress[] DnsServers { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }

public bool IsProtectSocketSuported => false;
public bool CanProtectSocket => false;
public void ProtectSocket(System.Net.Sockets.Socket socket) => throw new NotSupportedException($"{nameof(ProcessPacket)} is not supported by {nameof(WinDivertDevice)}");

}
Expand Down
6 changes: 3 additions & 3 deletions VpnHood.Client.Device/IPacketCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public interface IPacketCapture : IDisposable
bool IsDnsServersSupported { get; }
IPAddress[] DnsServers { get; set; }

bool IsExcludeAppsSupported { get; }
bool IsIncludeAppsSupported { get; }
bool CanExcludeApps { get; }
bool CanIncludeApps { get; }

/// <summary>
/// Unique id of excluded applications
Expand All @@ -32,7 +32,7 @@ public interface IPacketCapture : IDisposable
bool IsMtuSupported { get;}
int Mtu { get; set; }

bool IsProtectSocketSuported { get; }
bool CanProtectSocket { get; }
void ProtectSocket(System.Net.Sockets.Socket socket);
void SendPacketToInbound(IEnumerable<IPPacket> packets);
event EventHandler<PacketCaptureArrivalEventArgs> OnPacketArrivalFromInbound;
Expand Down
33 changes: 3 additions & 30 deletions VpnHood.Client.Device/PacketCaptureArrivalEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,17 @@
using PacketDotNet;
using System;
using System.Collections.Generic;
using System.Linq;

namespace VpnHood.Client.Device
{
public class PacketCaptureArrivalEventArgs : EventArgs
{
public class ArivalPacket
{
private bool _passthru;

public ArivalPacket(IPPacket ipPacket, bool isPassthruSupported)
{
IpPacket = ipPacket;
IsPassthruSupported = isPassthruSupported;
}

public IPPacket IpPacket { get; }
public bool IsPassthruSupported { get; }
public bool Passthru
{
get => _passthru;
set
{
if (!IsPassthruSupported) throw new NotSupportedException($"{nameof(Passthru)} is not supported by this PacketCapture!");
_passthru = value;
}
}
public bool IsHandled { get; set; }
}

public ArivalPacket[] ArivalPackets { get; }
public IEnumerable<IPPacket> IpPackets { get; }
public IPacketCapture PacketCapture { get; }

public PacketCaptureArrivalEventArgs(IEnumerable<IPPacket> ipPackets, IPacketCapture packetCapture)
{
if (ipPackets is null) throw new ArgumentNullException(nameof(ipPackets));
ArivalPackets = ipPackets.Select(x => new ArivalPacket(x, packetCapture.CanSendPacketToOutbound)).ToArray();
PacketCapture = packetCapture;
IpPackets = ipPackets ?? throw new ArgumentNullException(nameof(ipPackets));
PacketCapture = packetCapture ?? throw new ArgumentNullException(nameof(packetCapture));
}
}
}
13 changes: 6 additions & 7 deletions VpnHood.Client/VpnHoodClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected override Ping CreatePing() //PacketCapture can not protect Ping so Pin
protected override UdpClient CreateUdpClientListener()
{
UdpClient udpClient = new(0);
if (_client._packetCapture.IsProtectSocketSuported)
if (_client._packetCapture.CanProtectSocket)
_client._packetCapture.ProtectSocket(udpClient.Client);
return udpClient;
}
Expand Down Expand Up @@ -242,7 +242,7 @@ private void ConfigPacketFilter()
excludeNetworks.AddRange(IpNetwork.LocalNetworks);

// exclude server if ProtectSocket is not supported to prevent loop back
if (!_packetCapture.IsProtectSocketSuported)
if (!_packetCapture.CanProtectSocket)
excludeNetworks.Add(new IpNetwork(ServerTcpEndPoint.Address));

// clear include networks
Expand Down Expand Up @@ -298,11 +298,10 @@ private void PacketCapture_OnPacketArrivalFromInbound(object sender, PacketCaptu
var tcpHostPackets = _sendingPacket.TcpHostPackets;
var passthruPackets = _sendingPacket.PassthruPackets;
var proxyPackets = _sendingPacket.ProxyPackets;
foreach (var arivalPacket in e.ArivalPackets)
foreach (var ipPacket in e.IpPackets)
{
var ipPacket = arivalPacket.IpPacket;
if (_cancellationTokenSource.IsCancellationRequested) return;
if (arivalPacket.IsHandled || ipPacket.Version != IPVersion.IPv4)
if (ipPacket.Version != IPVersion.IPv4)
continue;

var isInRange = IsInIncludeIpRange(ipPacket.DestinationAddress);
Expand Down Expand Up @@ -502,7 +501,7 @@ private void AddUdpChannel(int udpPort, byte[] udpKey)
VhLogger.Instance.LogInformation(GeneralEventId.DatagramChannel, $"Creating {VhLogger.FormatTypeName<UdpChannel>()}... ServerEp: {udpEndPoint}");

var udpClient = new UdpClient();
if (_packetCapture.IsProtectSocketSuported)
if (_packetCapture.CanProtectSocket)
_packetCapture.ProtectSocket(udpClient.Client);
udpClient.Connect(udpEndPoint);
var udpChannel = new UdpChannel(true, udpClient, SessionId, udpKey);
Expand All @@ -515,7 +514,7 @@ internal async Task<TcpClientStream> GetSslConnectionToServer(EventId eventId, C
try
{
// create tcpConnection
if (_packetCapture.IsProtectSocketSuported)
if (_packetCapture.CanProtectSocket)
_packetCapture.ProtectSocket(tcpClient.Client);

// Client.Timeout does not affect in ConnectAsync
Expand Down

0 comments on commit b768abf

Please sign in to comment.