A simple .NET library for working with Wintun virtual network adapters, allowing you to create adapters, send and receive packets, and integrate with your applications using dependency injection.
- Create and manage Wintun adapters.
- Send and receive raw network packets.
- Supports async packet handling via events.
- Easy integration with .NET Dependency Injection.
Use the built-in Dependency Injection support to register the Wintun service:
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddWinTun();
var serviceProvider = services.BuildServiceProvider();
var winTunService = serviceProvider.GetRequiredService<IWinTunService>();var adapter = winTunService.CreateAdapter("MyAdapter");adapter.StartSession(capacity: 1024); // Optional capacity parameterSubscribe to the PacketReceived event to handle incoming packets:
adapter.PacketReceived += (sender, packet) =>
{
Console.WriteLine($"Received {packet.Length} bytes");
// Process packet here
};byte[] data = new byte[] { 0x01, 0x02, 0x03 };
adapter.SendPacket(data);adapter.EndSession();
adapter.Dispose();
winTunService.Dispose();public interface IWinTunService : IDisposable
{
IWinTunAdapter CreateAdapter(string adapterName);
}public interface IWinTunAdapter : IDisposable
{
void StartSession(uint? capacity = null);
void EndSession();
event EventHandler<byte[]>? PacketReceived;
void SendPacket(byte[] data);
}public static class DependencyInjection
{
public static IServiceCollection AddWinTun(this IServiceCollection services)
{
services.AddSingleton<IWinTunService, WinTunService>();
return services;
}
}This project is licensed under the MIT License. See LICENSE for details.
- Running as administrator may be required for certain network operations.
- Written and tested in .NET 9.