~ruther/NosSmooth

3118318ce189879fd59c8c5ad2acf99d5f9d02fe — Rutherther 2 years ago 03c730e
feat(pcap): support multiple clients for same connection
M Pcap/NosSmooth.Pcap/NosSmooth.Pcap.csproj => Pcap/NosSmooth.Pcap/NosSmooth.Pcap.csproj +2 -2
@@ 4,8 4,8 @@
        <TargetFramework>net7.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <VersionPrefix>1.0.1</VersionPrefix>
        <PackageReleaseNotes>Delete only connections that are not seen for some time.</PackageReleaseNotes>
        <VersionPrefix>1.0.2</VersionPrefix>
        <PackageReleaseNotes>Support mutliple clients for same connection.</PackageReleaseNotes>
    </PropertyGroup>

    <ItemGroup>

M Pcap/NosSmooth.Pcap/PcapNostaleClient.cs => Pcap/NosSmooth.Pcap/PcapNostaleClient.cs +4 -4
@@ 113,12 113,12 @@ public class PcapNostaleClient : BaseNostaleClient
                {
                    if (lastConnection is not null)
                    {
                        _pcapManager.UnregisterConnection(lastConnection.Value);
                        _pcapManager.UnregisterConnection(lastConnection.Value, this);
                        _crypto.EncryptionKey = 0;
                    }
                    if (reverseLastConnection is not null)
                    {
                        _pcapManager.UnregisterConnection(reverseLastConnection.Value);
                        _pcapManager.UnregisterConnection(reverseLastConnection.Value, this);
                    }

                    if (connection is not null)


@@ 159,12 159,12 @@ public class PcapNostaleClient : BaseNostaleClient
            _pcapManager.RemoveClient();
            if (lastConnection is not null)
            {
                _pcapManager.UnregisterConnection(lastConnection.Value);
                _pcapManager.UnregisterConnection(lastConnection.Value, this);
            }

            if (reverseLastConnection is not null)
            {
                _pcapManager.UnregisterConnection(reverseLastConnection.Value);
                _pcapManager.UnregisterConnection(reverseLastConnection.Value, this);
            }

            _running = false;

M Pcap/NosSmooth.Pcap/PcapNostaleManager.cs => Pcap/NosSmooth.Pcap/PcapNostaleManager.cs +36 -7
@@ 23,7 23,7 @@ public class PcapNostaleManager
    private readonly ILogger<PcapNostaleManager> _logger;
    private readonly PcapNostaleOptions _options;
    private readonly ConcurrentDictionary<TcpConnection, ConnectionData> _connections;
    private readonly ConcurrentDictionary<TcpConnection, PcapNostaleClient> _clients;
    private readonly ConcurrentDictionary<TcpConnection, List<PcapNostaleClient>> _clients;
    private Task? _deletionTask;
    private CancellationTokenSource? _deletionTaskCancellationSource;
    private int _clientsCount;


@@ 39,7 39,7 @@ public class PcapNostaleManager
        _logger = logger;
        _options = options.Value;
        _connections = new ConcurrentDictionary<TcpConnection, ConnectionData>();
        _clients = new ConcurrentDictionary<TcpConnection, PcapNostaleClient>();
        _clients = new ConcurrentDictionary<TcpConnection, List<PcapNostaleClient>>();
    }

    /// <summary>


@@ 78,7 78,22 @@ public class PcapNostaleManager
    /// <param name="client">The client to associate the connection with.</param>
    internal void RegisterConnection(TcpConnection connection, PcapNostaleClient client)
    {
        _clients.AddOrUpdate(connection, (c) => client, (c1, c2) => client);
        _clients.AddOrUpdate
        (
            connection,
            _ =>
            {
                var clients = new List<PcapNostaleClient>();
                clients.Add(client);
                return clients;
            },
            (_, currentClients) =>
            {
                var clients = new List<PcapNostaleClient>(currentClients);
                clients.Add(client);
                return clients;
            }
        );

        if (_connections.TryGetValue(connection, out var data))
        {


@@ 93,9 108,20 @@ public class PcapNostaleManager
    /// Disassociate the given connection.
    /// </summary>
    /// <param name="connection">The connection to disassociate.</param>
    internal void UnregisterConnection(TcpConnection connection)
    /// <param name="client">The client to unregister.</param>
    internal void UnregisterConnection(TcpConnection connection, PcapNostaleClient client)
    {
        _clients.TryRemove(connection, out _);
        _clients.AddOrUpdate
        (
            connection,
            (c) => new List<PcapNostaleClient>(),
            (c1, c2) =>
            {
                var clients = new List<PcapNostaleClient>(c2);
                clients.Remove(client);
                return clients;
            }
        );
    }

    private void Stop()


@@ 212,9 238,12 @@ public class PcapNostaleManager
            data.SniffedData.Add(tcpPacket.PayloadData);
        }

        if (_clients.TryGetValue(tcpConnection, out var client))
        if (_clients.TryGetValue(tcpConnection, out var clients))
        {
            client.OnPacketArrival((LibPcapLiveDevice)e.Device, tcpConnection, tcpPacket.PayloadData);
            foreach (var client in clients)
            {
                client.OnPacketArrival((LibPcapLiveDevice)e.Device, tcpConnection, tcpPacket.PayloadData);
            }
        }
    }


Do not follow this link