SocksProxy 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package SocksProxy --version 1.0.4                
NuGet\Install-Package SocksProxy -Version 1.0.4                
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.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SocksProxy --version 1.0.4                
#r "nuget: SocksProxy, 1.0.4"                
#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.4

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

SocksWebProxy

GeekTools.SocksProxy

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

NuGet

✨ 特性

  • 支持 SOCKS4、SOCKS4a 和 SOCKS5 协议
  • 支持用户名/密码认证 (SOCKS5)
  • 支持用户标识 (SOCKS4/4a)
  • 智能的资源管理
    • 自动复用相同配置的代理实例
    • 自动清理长时间未使用的代理
    • 线程安全的实现

📦 安装

dotnet add package GeekTools.SocksProxy

🚀 快速开始

基本用法

// 创建代理实例
var handler = new HttpClientHandler
{
    Proxy = SocksWebProxy.GetProxy("127.0.0.1", 1080)
};
var client = new HttpClient(handler);

SOCKS5 认证

var handler = new HttpClientHandler
{
    Proxy = SocksWebProxy.GetProxy(
        "127.0.0.1", 
        1080,
        SocksWebProxy.SocksVersion.Socks5,
        username: "user",
        password: "pass"
    )
};
var client = new HttpClient(handler);

自定义配置

var options = new SocksWebProxyOptions
{
    IdleTimeout = TimeSpan.FromHours(1),      // 代理实例闲置多久后清理
    CleanupInterval = TimeSpan.FromMinutes(10) // 检查清理间隔
};

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

🔧 配置选项

SocksWebProxyOptions

参数 类型 默认值 说明
IdleTimeout TimeSpan 30分钟 代理实例在多久未使用后清理
CleanupInterval TimeSpan 5分钟 检查清理间隔

📝 注意事项

  1. SOCKS4 只支持 IPv4
  2. SOCKS4a 添加了域名解析支持
  3. SOCKS5 支持 IPv4、IPv6 和域名
  4. 相同配置的代理实例会被自动复用
  5. 长时间未使用的代理实例会被自动清理

🔍 性能考虑

  • 实例复用
    • 相同配置的代理实例自动复用
    • 避免重复创建代理实例
  • 资源管理
    • 自动清理长时间未使用的代理实例
    • 基于最后使用时间的清理机制
  • 线程安全
    • 使用 ConcurrentDictionary 确保线程安全
    • 原子操作和锁优化

🔍 为什么需要自动清理?

代理实例的资源特性

代理实例会维护一个本地的 HTTP 代理服务器(监听本地端口),即使代理实例本身被 GC 回收,这些底层资源也可能无法及时释放,主要涉及:

  • 占用的本地端口
  • 活跃的网络连接
  • 相关的线程资源

GC 的局限性

  1. GC 只能回收托管内存,无法直接处理底层网络资源
  2. 即使实现了 finalizer,也无法保证及时执行
  3. 依赖 GC 可能导致资源长时间占用

自动清理的优势

  1. 主动释放资源

    • 及时释放端口占用
    • 立即关闭网络连接
    • 清理相关线程资源
  2. 可预测的资源管理

    • 基于实际使用时间清理
    • 避免资源耗尽风险
    • 更好的资源使用效率
  3. 性能优化

    • 减少资源竞争
    • 降低系统负载
    • 优化内存使用

📋 支持的平台

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

📄 许可证

MIT License

📚 更新日志

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 20 11/21/2024
1.0.4 18 11/21/2024
1.0.3 36 11/20/2024
1.0.2 29 11/20/2024
1.0.1 32 11/20/2024
1.0.0 33 11/19/2024