SocksProxy 1.0.5

dotnet add package SocksProxy --version 1.0.5                
NuGet\Install-Package SocksProxy -Version 1.0.5                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SocksProxy" Version="1.0.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SocksProxy --version 1.0.5                
#r "nuget: SocksProxy, 1.0.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SocksProxy as a Cake Addin
#addin nuget:?package=SocksProxy&version=1.0.5

// Install SocksProxy as a Cake Tool
#tool nuget:?package=SocksProxy&version=1.0.5                

GeekTools.SocksProxy

一个高性能、易用的 .NET SOCKS 代理实现,支持 SOCKS4/4a/5 协议。

NuGet

✨ 特性

  • 支持 SOCKS4、SOCKS4a 和 SOCKS5 协议
  • 支持用户名/密码认证 (SOCKS5)
  • 支持用户标识 (SOCKS4/4a)
  • 智能的资源管理
    • 自动复用相同配置的代理实例
    • 默认跟随 HttpClientHandler 生命周期
    • 可选的自动清理模式
    • 线程安全的实现

📦 安装

dotnet add package GeekTools.SocksProxy

🚀 快速开始

基本用法(扩展方法)

// 1. 默认跟随handler生命周期
var handler = new HttpClientHandler()
    .UseSocksProxy("127.0.0.1", 1080);

// 2. 启用自动清理(30分钟后清理)
var handler2 = new HttpClientHandler()
    .UseSocksProxy(
        "127.0.0.1", 
        1080,
        enableAutoCleanup: true
    );

// 3. 启用自动清理(自定义清理时间)
var handler3 = new HttpClientHandler()
    .UseSocksProxy(
        "127.0.0.1", 
        1080,
        enableAutoCleanup: true,
        idleTimeout: TimeSpan.FromMinutes(10)
    );

// 4. 带认证的SOCKS5代理
var handler4 = new HttpClientHandler()
    .UseSocksProxy(
        "127.0.0.1",
        1080,
        username: "user",
        password: "pass"
    );

// 使用配置好的handler创建HttpClient
using var client = new HttpClient(handler);

原始工厂方法

// 1. 默认跟随handler生命周期
var handler = new HttpClientHandler();
handler.Proxy = SocksWebProxy.GetProxy(
    "127.0.0.1", 
    1080,
    handler: handler
);

// 2. 启用自动清理
var options = new SocksWebProxyOptions 
{
    EnableAutoCleanup = true,
    IdleTimeout = TimeSpan.FromMinutes(30)
};

var handler2 = new HttpClientHandler
{
    Proxy = SocksWebProxy.GetProxy("127.0.0.1", 1080, options: options)
};

🔧 资源管理

生命周期跟随模式(默认)

  • 代理实例跟随 HttpClientHandler 的生命周期
  • 当 handler 被回收时,对应的代理实例也会被清理
  • 适合大多数使用场景
  • 无需额外配置

自动清理模式(可选)

  • 需要显式启用
  • 代理实例在闲置超时后自动清理
  • 适合长期运行且需要定期清理的应用
  • 可自定义清理间隔和闲置超时时间

📝 注意事项

  1. SOCKS4 只支持 IPv4
  2. SOCKS4a 添加了域名解析支持
  3. SOCKS5 支持 IPv4、IPv6 和域名
  4. 相同配置的代理实例会被自动复用
  5. 建议根据使用场景选择合适的资源管理模式:
    • 一般情况下使用默认的生命周期跟随模式即可
    • 对于需要定期清理资源的长期运行应用,可以启用自动清理模式

🔍 性能考虑

  • 实例复用
    • 相同配置的代理实例自动复用
    • 避免重复创建代理实例
  • 资源管理
    • 默认跟随 handler 生命周期,资源管理更可控
    • 可选的自动清理模式满足特殊需求
  • 线程安全
    • 使用 ConcurrentDictionary 确保线程安全
    • 原子操作和锁优化

📋 支持的平台

  • .NET Standard 2.0+
  • .NET Core 3.1+
  • .NET 5.0+
  • .NET 6.0+
  • .NET 7.0+
  • .NET 8.0+

📄 许可证

MIT License

📚 更新日志

1.0.5 (2024-11-21)

  • 增加生命周期跟随HttpClientHandler选项

1.0.4 (2024-11-21)

  • 代码重构
  • 简化逻辑,去除依赖注入、扩展方法,统一工厂创建实例
  • 自动复用和清理代理实例

1.0.2 (2024-11-20)

  • 可选依赖包安装

1.0.1 (2024-11-20)

  • 优化资源清理机制
  • 添加异步清理支持
  • 改进日志输出
  • 增强性能监控
  • 完善文档

1.0.0 (2024-11-19)

  • 初始发布
  • 支持 SOCKS4/4a/5 协议
  • 自动清理代理资源
  • 可配置的缓存机制
  • 添加依赖注入支持
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.5 50 11/21/2024
1.0.4 46 11/21/2024
1.0.3 58 11/20/2024
1.0.2 50 11/20/2024
1.0.1 50 11/20/2024
1.0.0 50 11/19/2024