SocksProxy 1.0.0
.NET 5.0
This package targets .NET 5.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package SocksProxy --version 1.0.0
NuGet\Install-Package SocksProxy -Version 1.0.0
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.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SocksProxy --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SocksProxy, 1.0.0"
#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.0 // Install SocksProxy as a Cake Tool #tool nuget:?package=SocksProxy&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SocksWebProxy
一个高性能、易用的 .NET SOCKS 代理实现,支持 SOCKS4/4a/5 协议。
✨ 特性
- 支持 SOCKS4、SOCKS4a 和 SOCKS5 协议
- 支持用户名/密码认证 (SOCKS5)
- 支持用户标识 (SOCKS4/4a)
- 自动清理代理资源
- 线程安全
- 内存友好的缓存机制
- 灵活的配置选项
- 支持依赖注入
- 多种使用方式
📦 安装
# 基本包
dotnet add package GeekTools.SocksProxy
# 如果需要依赖注入支持
dotnet add package Microsoft.Extensions.Http
🚀 快速开始
1. 基本用法
// 直接使用
var handler = new HttpClientHandler
{
Proxy = new SocksWebProxy("127.0.0.1", 1080)
};
var client = new HttpClient(handler);
// 使用扩展方法
var handler = new HttpClientHandler()
.UseSocksProxy("127.0.0.1", 1080);
var client = new HttpClient(handler);
// 使用工厂方法
var proxy = SocksWebProxy.GetProxy("127.0.0.1", 1080);
2. 依赖注入
// 基本注册
services.AddHttpClientWithSocksProxy(
"MyClient",
"127.0.0.1",
1080);
// 带配置的注册
services.AddHttpClientWithSocksProxy(
"MyClient",
"127.0.0.1",
1080,
client =>
{
client.BaseAddress = new Uri("https://api.example.com");
client.Timeout = TimeSpan.FromSeconds(30);
});
// 类型化客户端注册
services.AddHttpClientWithSocksProxy<MyApiClient>(
"127.0.0.1",
1080);
// 在服务中使用
public class MyService
{
private readonly HttpClient _client;
public MyService(IHttpClientFactory clientFactory)
{
_client = clientFactory.CreateClient("MyClient");
}
}
// 使用类型化客户端
public class MyApiClient
{
private readonly HttpClient _client;
public MyApiClient(HttpClient client)
{
_client = client;
}
}
3. SOCKS5 认证
// 直接使用
var handler = new HttpClientHandler()
.UseSocksProxy("127.0.0.1", 1080,
SocksWebProxy.SocksVersion.Socks5,
username: "user",
password: "pass");
// 依赖注入
services.AddHttpClientWithSocksProxy(
"MyClient",
"127.0.0.1",
1080,
version: SocksWebProxy.SocksVersion.Socks5,
username: "user",
password: "pass");
4. 自定义配置
var options = new SocksWebProxyOptions
{
EnableAutoCleanup = true, // 启用自动清理
EnableCaching = true, // 启用缓存
CleanupInterval = TimeSpan.FromMinutes(10), // 清理间隔
CacheExpiration = TimeSpan.FromHours(1) // 缓存过期时间
};
var handler = new HttpClientHandler()
.UseSocksProxy("127.0.0.1", 1080, options: options);
🔧 配置选项
SocksWebProxyOptions
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
EnableAutoCleanup | bool | true | 是否启用自动清理未使用的代理资源 |
EnableCaching | bool | true | 是否启用代理实例缓存 |
CleanupInterval | TimeSpan | 5分钟 | 清理未使用代理实例的时间间隔 |
CacheExpiration | TimeSpan | 30分钟 | 缓存的代理实例的过期时间 |
详细配置说明:
EnableAutoCleanup
- 作用:自动清理不再使用的代理资源,防止资源泄露
- 建议:一般情况下保持启用,需要手动管理时可禁用
EnableCaching
- 作用:复用相同配置的代理实例,优化内存使用
- 建议:频繁创建代理时建议启用
CleanupInterval
- 作用:定期清理未使用或过期的代理实例
- 建议:根据应用场景调整清理间隔
CacheExpiration
- 作用:控制缓存代理实例的生命周期
- 建议:根据代理服务器的稳定性调整
配置场景示例
// 高性能场景
var options = new SocksWebProxyOptions
{
EnableAutoCleanup = true,
EnableCaching = true,
CleanupInterval = TimeSpan.FromHours(1),
CacheExpiration = TimeSpan.FromHours(24)
};
// 内存敏感场景
var options = new SocksWebProxyOptions
{
EnableAutoCleanup = true,
EnableCaching = true,
CleanupInterval = TimeSpan.FromMinutes(1),
CacheExpiration = TimeSpan.FromMinutes(5)
};
// 手动管理场景
var options = new SocksWebProxyOptions
{
EnableAutoCleanup = false,
EnableCaching = false
};
📝 注意事项
- SOCKS4 只支持 IPv4
- SOCKS4a 添加了域名解析支持
- SOCKS5 支持 IPv4、IPv6 和域名
- 代理实例会被缓存和复用
- 自动清理过期的缓存实例
- 依赖注入需要安装 Microsoft.Extensions.Http 包
🔍 性能考虑
- 使用 ConcurrentDictionary 确保线程安全
- 自动管理代理实例生命周期
- 定期清理未使用的资源
- 可配置的缓存策略
- 依赖注入支持单例模式
📋 支持的平台
- .NET Standard 2+
- .NET 5.0+
- .NET 6.0+
- .NET 7.0+
- .NET 8.0+
📄 许可证
MIT License
📚 更新日志
1.0.0 (2024-01-19)
- 初始发布
- 支持 SOCKS4/4a/5 协议
- 自动清理代理资源
- 可配置的缓存机制
- 添加依赖注入支持
Product | Versions 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
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Http (>= 9.0.0)
-
.NETStandard 2.1
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Http (>= 9.0.0)
-
net5.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Http (>= 9.0.0)
-
net6.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Http (>= 9.0.0)
-
net7.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Http (>= 9.0.0)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Http (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.