Skip to content

Commit

Permalink
Switch back IEnumerable<IPPackets> to IPPacket[]
Browse files Browse the repository at this point in the history
  • Loading branch information
trudyhood committed Aug 31, 2021
1 parent 9b11034 commit 04729a6
Show file tree
Hide file tree
Showing 21 changed files with 55 additions and 149 deletions.
5 changes: 2 additions & 3 deletions VpnHood.Client.Device.Android/AppVpnService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
Expand Down Expand Up @@ -110,13 +109,13 @@ public void SendPacketToInbound(IPPacket ipPacket)
_outStream?.Write(ipPacket.Bytes);
}

public void SendPacketToInbound(IEnumerable<IPPacket> ipPackets)
public void SendPacketToInbound(IPPacket[] ipPackets)
{
foreach (var ipPacket in ipPackets)
_outStream?.Write(ipPacket.Bytes);
}

public void SendPacketToOutbound(IEnumerable<IPPacket> ipPackets)
public void SendPacketToOutbound(IPPacket[] ipPackets)
{
throw new NotSupportedException();
}
Expand Down
9 changes: 3 additions & 6 deletions VpnHood.Client.Device.WinDivert/WinDivertPacketCapture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
Expand All @@ -16,7 +15,6 @@ namespace VpnHood.Client.Device.WinDivert
public class WinDivertPacketCapture : IPacketCapture
{
protected readonly SharpPcap.WinDivert.WinDivertDevice Device;
private readonly IPPacket[] _receivedPackets = new IPPacket[1];

private bool _disposed;
private IpNetwork[]? _includeNetworks;
Expand Down Expand Up @@ -53,7 +51,7 @@ public virtual void ProtectSocket(Socket socket)
$"{nameof(ProcessPacketReceivedFromInbound)} is not supported by {nameof(WinDivertDevice)}");
}

public void SendPacketToInbound(IEnumerable<IPPacket> ipPackets)
public void SendPacketToInbound(IPPacket[] ipPackets)
{
foreach (var ipPacket in ipPackets)
SendPacket(ipPacket, false);
Expand All @@ -69,7 +67,7 @@ public void SendPacketToOutbound(IPPacket ipPacket)
SendPacket(ipPacket, true);
}

public void SendPacketToOutbound(IEnumerable<IPPacket> ipPackets)
public void SendPacketToOutbound(IPPacket[] ipPackets)
{
foreach (var ipPacket in ipPackets)
SendPacket(ipPacket, true);
Expand Down Expand Up @@ -181,8 +179,7 @@ protected virtual void ProcessPacketReceivedFromInbound(IPPacket ipPacket)
{
try
{
_receivedPackets[0] = ipPacket;
var eventArgs = new PacketReceivedEventArgs(_receivedPackets, this);
var eventArgs = new PacketReceivedEventArgs(new [] {ipPacket}, this);
OnPacketReceivedFromInbound?.Invoke(this, eventArgs);
}
catch (Exception ex)
Expand Down
5 changes: 2 additions & 3 deletions VpnHood.Client.Device/IPacketCapture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using PacketDotNet;
Expand Down Expand Up @@ -40,8 +39,8 @@ public interface IPacketCapture : IDisposable
event EventHandler OnStopped;
void ProtectSocket(Socket socket);
void SendPacketToInbound(IPPacket ipPacket);
void SendPacketToInbound(IEnumerable<IPPacket> packets);
void SendPacketToInbound(IPPacket[] packets);
void SendPacketToOutbound(IPPacket ipPacket);
void SendPacketToOutbound(IEnumerable<IPPacket> ipPackets);
void SendPacketToOutbound(IPPacket[] ipPackets);
}
}
5 changes: 2 additions & 3 deletions VpnHood.Client.Device/PacketReceivedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System;
using System.Collections.Generic;
using PacketDotNet;

namespace VpnHood.Client.Device
{
public class PacketReceivedEventArgs : EventArgs
{
public PacketReceivedEventArgs(IEnumerable<IPPacket> ipPackets, IPacketCapture packetCapture)
public PacketReceivedEventArgs(IPPacket[] ipPackets, IPacketCapture packetCapture)
{
IpPackets = ipPackets ?? throw new ArgumentNullException(nameof(ipPackets));
PacketCapture = packetCapture ?? throw new ArgumentNullException(nameof(packetCapture));
}

public IEnumerable<IPPacket> IpPackets { get; }
public IPPacket[] IpPackets { get; }
public IPacketCapture PacketCapture { get; }
}
}
14 changes: 8 additions & 6 deletions VpnHood.Client/Exceptions/RedirectHostException.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using System;
using System.Net;
using VpnHood.Common.Exceptions;
using VpnHood.Common.Messaging;

namespace VpnHood.Client.Exceptions
{
public class RedirectHostException : Exception
public class RedirectHostException : SessionException
{
public RedirectHostException(IPEndPoint redirectHostEndPoint, string? message)
: base(message)
public RedirectHostException(ResponseBase responseBase)
: base(responseBase)
{
RedirectHostEndPoint =
redirectHostEndPoint ?? throw new ArgumentNullException(nameof(redirectHostEndPoint));
if (responseBase.RedirectHostEndPoint == null)
throw new ArgumentNullException(nameof(responseBase.RedirectHostEndPoint));
}

public IPEndPoint RedirectHostEndPoint { get; }
public IPEndPoint RedirectHostEndPoint => SessionResponse.RedirectHostEndPoint!;
}
}
5 changes: 3 additions & 2 deletions VpnHood.Client/TcpProxyHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ public async Task StartListening()
}

// this method should not be called in multi-thread, the return buffer is shared and will be modified on next call
public IEnumerable<IPPacket> ProcessOutgoingPacket(IEnumerable<IPPacket> ipPackets)
public IPPacket[] ProcessOutgoingPacket(IPPacket[] ipPackets)
{
if (_localEndpoint == null)
throw new InvalidOperationException(
$"{nameof(_localEndpoint)} has not been initialized! Did you call {nameof(StartListening)}!");

_ipPackets.Clear(); // prevent reallocation in this intensive method
var ret = _ipPackets;

Expand Down Expand Up @@ -149,7 +150,7 @@ public IEnumerable<IPPacket> ProcessOutgoingPacket(IEnumerable<IPPacket> ipPacke
}
}

return ret;
return ret.ToArray();
}

private async Task ProcessClient(TcpClient tcpOrgClient, CancellationToken cancellationToken)
Expand Down
15 changes: 8 additions & 7 deletions VpnHood.Client/VpnHoodClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using VpnHood.Client.Device;
using VpnHood.Client.Exceptions;
using VpnHood.Common;
using VpnHood.Common.Exceptions;
using VpnHood.Common.Messaging;
using VpnHood.Common.Logging;
using VpnHood.Tunneling;
Expand Down Expand Up @@ -389,11 +390,11 @@ private void PacketCapture_OnPacketReceivedFromInbound(object sender, PacketRece
}

// send packets
if (passthruPackets.Count > 0) _packetCapture.SendPacketToOutbound(passthruPackets);
if (passthruPackets.Count > 0) _packetCapture.SendPacketToOutbound(passthruPackets.ToArray());
if (proxyPackets.Count > 0) _clientProxyManager.SendPacket(proxyPackets);
if (tunnelPackets.Count > 0) Tunnel.SendPacket(tunnelPackets);
if (tunnelPackets.Count > 0) Tunnel.SendPacket(tunnelPackets.ToArray());
if (tcpHostPackets.Count > 0)
_packetCapture.SendPacketToInbound(_tcpProxyHost.ProcessOutgoingPacket(tcpHostPackets));
_packetCapture.SendPacketToInbound(_tcpProxyHost.ProcessOutgoingPacket(tcpHostPackets.ToArray()));
}
}
catch (Exception ex)
Expand Down Expand Up @@ -768,16 +769,16 @@ internal async Task<T> SendRequest<T>(Stream stream, RequestCode requestCode, ob
SessionStatus.ErrorMessage = response.ErrorMessage;
SessionStatus.SuppressedBy = response.SuppressedBy;
Dispose();
throw new Exception(response.ErrorMessage);
throw new SessionException(response);

case SessionErrorCode.RedirectHost:
throw new RedirectHostException(response.RedirectHostEndPoint!, response.ErrorMessage);
throw new RedirectHostException(response);

case SessionErrorCode.GeneralError:
throw new Exception(response.ErrorMessage);
throw new SessionException(response);

default:
throw new Exception(response.ErrorMessage);
throw new SessionException(response);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using VpnHood.Common.Messaging;

namespace VpnHood.Tunneling.Messaging
namespace VpnHood.Common.Exceptions
{
public class SessionException : Exception
{
Expand Down
1 change: 0 additions & 1 deletion VpnHood.Common/VpnHood.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Exceptions\" />
<Folder Include="Resources\" />
</ItemGroup>

Expand Down
20 changes: 0 additions & 20 deletions VpnHood.Server/SessionException.cs

This file was deleted.

1 change: 1 addition & 0 deletions VpnHood.Server/SessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using VpnHood.Common.Exceptions;
using VpnHood.Common.Messaging;
using VpnHood.Common.Trackers;
using VpnHood.Common.Logging;
Expand Down
1 change: 1 addition & 0 deletions VpnHood.Server/TcpHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using VpnHood.Common;
using VpnHood.Common.Exceptions;
using VpnHood.Common.Messaging;
using VpnHood.Common.Logging;
using VpnHood.Tunneling;
Expand Down
9 changes: 4 additions & 5 deletions VpnHood.Tunneling/ChannelPacketReceivedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System;
using System.Collections.Generic;
using PacketDotNet;

namespace VpnHood.Tunneling
{
public class ChannelPacketReceivedEventArgs : EventArgs
{
public ChannelPacketReceivedEventArgs(IEnumerable<IPPacket> ipPackets, IChannel channel)
public IPPacket[] IpPackets { get; }
public IChannel Channel { get; }

public ChannelPacketReceivedEventArgs(IPPacket[] ipPackets, IChannel channel)
{
IpPackets = ipPackets;
Channel = channel;
}

public IEnumerable<IPPacket> IpPackets { get; }
public IChannel Channel { get; }
}
}
2 changes: 2 additions & 0 deletions VpnHood.Tunneling/IChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace VpnHood.Tunneling
public interface IChannel : IDisposable
{
bool Connected { get; }

// ReSharper disable once UnusedMemberInSuper.Global
DateTime LastActivityTime { get; }
long SentByteCount { get; }
long ReceivedByteCount { get; }
Expand Down
3 changes: 1 addition & 2 deletions VpnHood.Tunneling/IDatagramChannel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using PacketDotNet;

Expand All @@ -8,6 +7,6 @@ namespace VpnHood.Tunneling
public interface IDatagramChannel : IChannel
{
event EventHandler<ChannelPacketReceivedEventArgs> OnPacketReceived;
Task SendPacketAsync(IEnumerable<IPPacket> packets);
Task SendPacketAsync(IPPacket[] packets);
}
}
2 changes: 1 addition & 1 deletion VpnHood.Tunneling/Messaging/RequestCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public enum RequestCode : byte
Hello = 1, // data: [4B_jsonLength][json_HelloRequest]
TcpDatagramChannel = 2, // data: [4B_jsonLength][json_TcpDatagramChannelRequest]
TcpProxyChannel = 3, // data: [4B_jsonLength][json_TcpProxyChannelRequest]
SessionStatus = 4, // data: [4B_jsonLength][json_BaseRequest]
// SessionStatus = 4, // data: [4B_jsonLength][json_BaseRequest]
UdpChannel = 5 // data: [4B_jsonLength][json_BaseRequest]
}
}
9 changes: 3 additions & 6 deletions VpnHood.Tunneling/StreamPacketReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ public StreamPacketReader(Stream stream)
}


/// <summary>
/// The return reference will be changed on next call! Consider to call ToArray in async usage
/// </summary>
/// <returns>null if nothing read</returns>
public async Task<IEnumerable<IPPacket>?> ReadAsync()
/// <returns>null if read nothing</returns>
public async Task<IPPacket[]?> ReadAsync()
{
_ipPackets.Clear();

Expand Down Expand Up @@ -68,7 +65,7 @@ public StreamPacketReader(Stream stream)
moreData = false;
}

return _ipPackets.Count != 0 ? _ipPackets : null;
return _ipPackets.Count != 0 ? _ipPackets.ToArray() : null;
}
}
}
6 changes: 3 additions & 3 deletions VpnHood.Tunneling/TcpDatagramChannel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -41,7 +40,7 @@ public void Start()
_ = ReadTask();
}

public async Task SendPacketAsync(IEnumerable<IPPacket> ipPackets)
public async Task SendPacketAsync(IPPacket[] ipPackets)
{
if (_disposed)
throw new ObjectDisposedException(nameof(TcpDatagramChannel));
Expand Down Expand Up @@ -99,14 +98,15 @@ private async Task ReadTask()
}
catch
{
// ignored
}
finally
{
Dispose();
}
}

private void FireReceivedPackets(IEnumerable<IPPacket> ipPackets)
private void FireReceivedPackets(IPPacket[] ipPackets)
{
if (_disposed)
return;
Expand Down
Loading

0 comments on commit 04729a6

Please sign in to comment.