Sync README files from source repository [skip ci]

This commit is contained in:
Gitea Actions
2026-01-26 06:18:20 +00:00
parent f558a9c713
commit 4e7b46e058
4 changed files with 335 additions and 155 deletions
+217 -126
View File
@@ -1,10 +1,21 @@
# Service quản trị Resource trong hệ thống,
# Service quản trị Resource trong hệ thống
- Hỗ trợ 2 giao thức: http(31249), grpc(31287)
- Quản trị 2 resource: proxy pool, token pool
> **📖 Migration Guide**: Nếu bạn đang sử dụng ProxyPool và muốn migrate sang SmartPool, xem [Migration Guide](./MIGRATION_GUIDE_PROXYPOOL_TO_SMARTPOOL.md)
---
# ResourcePool Usage Guide
This guide provides instructions on how to set up and use the **TokenPool** **ProxyPool** in your project.
This guide provides instructions on how to set up and use **TokenPool** and **ProxyPool** in your project.
> **Note**: This repository contains two systems:
> - **ResourcePool** (Legacy): TokenPool và ProxyPool với gRPC/HTTP API
> - **SmartPool** (New): Intelligent proxy management với SDK client
>
> Xem [SmartPool documentation](./src/Icomm.SmartPool.Proxy/README.md) cho hệ thống mới.
---
@@ -12,21 +23,21 @@ This guide provides instructions on how to set up and use the **TokenPool** **P
1. Add the following NuGet package references to your project:
```xml
Icomm.ResourcePool.Abstractions
```
```xml
Icomm.TokenPool
```
```xml
Icomm.ProxyPool
<PackageReference Include="Icomm.ResourcePool.Abstractions" Version="x.x.x" />
<PackageReference Include="Icomm.TokenPool" Version="x.x.x" />
```
---
## Service Registration
Register the necessary services in the `Startup.cs` or equivalent service configuration file:
### TokenPool Registration
Register TokenPool service in `Startup.cs` or `Program.cs`:
```csharp
using Icomm.TokenPool.Extensions;
services
.AddConfigManager(hostContext.Configuration)
.AddTokenPool(
@@ -39,157 +50,175 @@ services
opt.Port = 31287;
#endif
}
)
.AddProxyPool(
hostContext.Configuration,
opt =>
{
#if DEBUG
opt.Host = "10.9.3.70";
opt.protocol = ResourcePool.Core.Protocol.Grpc;
opt.Port = 31287;
#endif
}
)
;
);
```
**Configuration Options:**
- `Host`: Server host address
- `Port`: Server port (default: 31287 for gRPC, 31249 for HTTP)
- `protocol`: `Protocol.Grpc` or `Protocol.Http`
- `TimeOut`: Request timeout in milliseconds
---
## Usage TokenPool
Once the services are registered, you can inject and use the `ITokenPoolService` in your application.
Once the service is registered, you can inject and use `ITokenPoolService` in your application.
### Basic Usage
### Example:
```csharp
private readonly ITokenPoolService _tokenPoolService;
using Icomm.TokenPool.Contracts;
using Icomm.ResourcePool.Abstractions.Requests;
using Icomm.ResourcePool.Abstractions.Responses;
public MyService(ITokenPoolService tokenPoolService)
public class MyService
{
_tokenPoolService = tokenPoolService;
}
// example with EstimateRequest
public async Task UseTokenPool()
{
// Example usage of the _tokenPoolService
var token = await _tokenPoolService.RequestToken(
new TokenRequest()
{
EstimateRequest = 1,
Action = action,
Platform = "1",
}
);
Console.WriteLine($"Retrieved Token: {token}");
}
// example withouy EstimateRequest
public async Task UseTokenPool()
{
// Example usage of the _tokenPoolService
var token = await _tokenPoolService.RequestToken(
new TokenRequest()
{
Action = action,
Platform = "1",
}
);
await _tokenPoolService.UsedToken(token);
Console.WriteLine($"Retrieved Token: {token}");
private readonly ITokenPoolService _tokenPoolService;
public MyService(ITokenPoolService tokenPoolService)
{
_tokenPoolService = tokenPoolService;
}
// Example with EstimateRequest
public async Task UseTokenPool()
{
var token = await _tokenPoolService.RequestToken(
new TokenRequest()
{
AccessToken = "your-access-token",
EstimateRequest = 1,
Action = "action-name",
Platform = "1",
}
);
Console.WriteLine($"Retrieved Token: {token.Token}");
}
// Example without EstimateRequest (manual release)
public async Task UseTokenPoolManual()
{
var token = await _tokenPoolService.RequestToken(
new TokenRequest()
{
AccessToken = "your-access-token",
Action = "action-name",
Platform = "1",
}
);
// Use token...
// Release token when done
await _tokenPoolService.UsedToken(token);
Console.WriteLine($"Token released: {token.Token}");
}
}
```
- The `ITokenPoolService` provides methods to interact with the Token Pool.
- Ensure that the service is properly registered and injected where needed.
### Available Methods
- `RequestToken(TokenRequest request)`: Request a token from the pool
- `UsedToken(TokenApp token)`: Release a token back to the pool
- `ExpireByToken(ExpireByTokenRequest request)`: Expire a token by token value
- `ExpireById(ExpireByTokenIdRequest request)`: Expire a token by ID
- `UpdateStatus(UpdateResourceStatusRequest request)`: Update token status
- `ResourceExpired(TokenApp resource, TimeSpan timeExpire, string accessToken)`: Mark token as expired for a duration
---
## Usage ProxyPool
Once the services are registered, you can inject and use the `IProxyPoolService` in your application.
ProxyPool is accessed via **gRPC** or **HTTP REST API** directly. There is no client SDK like TokenPool.
### gRPC Usage
### Example:
```csharp
private readonly IProxyPoolService _proxyPool;;
using Icomm.ResourcePool.Abstractions;
using Icomm.ResourcePool.Abstractions.Requests;
using MagicOnion.Client;
public MyService(IProxyPoolService proxyPool)
{
_proxyPool = proxyPool;
}
// example with EstimateRequest
public async Task UseProxyPoolWithRestSharp()
{
// Example usage of the _tokenPoolService
var proxy = await _proxyPool.GetProxyServerAsync(
cancellationToken: cancellationToken
);
var clientOptions = new RestClientOptions(url)
{
Proxy = proxy
};
Console.WriteLine($"Retrieved Token: {token}");
}
// Create gRPC channel
var channel = GrpcChannel.ForAddress("http://10.9.3.70:31287");
var client = MagicOnionClient.Create<IProxyService>(channel);
// example with EstimateRequest
public async Task UseRawProxyPoolWithRestSharp()
// Get proxy
var request = new ProxyRequest
{
// Example usage of the _tokenPoolService
var proxy = await _proxyPool.GetRawProxyServerAsync(
cancellationToken: cancellationToken
);
var clientOptions = new RestClientOptions(url)
AccessToken = "your-access-token"
};
var response = await client.GetProxy(request);
var proxy = response.Proxy;
// Use proxy with HttpClient
var handler = new HttpClientHandler
{
Proxy = new WebProxy($"{proxy.Host}:{proxy.Port}")
{
Proxy = new WebProxy(proxy.Host, proxy.Port)
};
Console.WriteLine($"Retrieved Token: {token}");
}
Credentials = new NetworkCredential(proxy.AuthUsername, proxy.AuthPassword)
}
};
var httpClient = new HttpClient(handler);
```
- The `ITokenPoolService` `IProxyPoolService` provides methods to interact with the Token Pool.
- Ensure that the service is properly registered and injected where needed.
### HTTP REST API Usage
---
#### Get Proxy (Simple)
## Notes
**Endpoint**: `POST /api/resource-pool/v1/ProxyPool/get`
- The `opt.Host`, `opt.Protocol`, and `opt.Port` configuration should match your environment settings.
- In production, replace the debug configurations with the appropriate production values.
```csharp
using RestSharp;
---
var client = new RestClient("http://10.9.3.70:31249");
var request = new RestRequest("/api/resource-pool/v1/ProxyPool/get", Method.Post);
request.AddJsonBody(new ProxyRequest
{
AccessToken = "your-access-token"
});
## API Documentation
var response = await client.ExecuteAsync<ProxyResponse>(request);
var proxy = response.Data.Proxy;
### Proxy Pool API - GetProxyByFeature
// Use with RestSharp
var restClient = new RestClient("https://target-site.com")
{
Proxy = new WebProxy($"{proxy.Host}:{proxy.Port}")
{
Credentials = new NetworkCredential(proxy.AuthUsername, proxy.AuthPassword)
}
};
```
Endpoint: `POST /api/resource-pool/v1/ProxyPool/get-v2`
#### Get Proxy By Feature (Advanced)
**Endpoint**: `POST /api/resource-pool/v1/ProxyPool/get-v2`
Lấy proxy server theo features và chiến lược lọc cụ thể.
#### 📋 Request Parameters
**Request Parameters:**
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `access_token` | string | **Required** | Access token for authentication |
| `target_domain` | string | Optional | Domain đích cần truy cập (vd: facebook.com, x.com) |
| `type` | string | **Required** | Loại proxy ("http", "httpv6", "socks") |
| `country` | string | Optional | Mã quốc gia proxy ("VN", "US", "JP") |
| `strategy` | string | **Required** | Chiến lược chọn proxy |
| `referer_proxy` | object | Optional | Proxy tham chiếu để tìm proxy thay thế tốt nhất |
#### ⚠️ Logic Ưu Tiên Tham Số
**⚠️ Logic Ưu Tiên Tham Số**
**Nếu `referer_proxy.id` khác null**: Hệ thống sẽ ưu tiên lấy thông tin từ proxy referer làm tham số lọc chính, sau đó mới sử dụng các tham số `type`, `country`, `target_domain` để lọc bổ sung.
Nếu `referer_proxy.id` khác null: Hệ thống sẽ ưu tiên lấy thông tin từ proxy referer làm tham số lọc chính, sau đó mới sử dụng các tham số `type`, `country`, `target_domain` để lọc bổ sung.
**Cách hoạt động**:
1. **Bước 1**: Hệ thống lấy thông tin của proxy có ID = `referer_proxy.id`
2. **Bước 2**: Sử dụng thông tin proxy đó (type, country, location) làm tiêu chí lọc chính
3. **Bước 3**: Áp dụng các tham số `type`, `country`, `target_domain` như điều kiện lọc bổ sung
4. **Bước 4**: Thực hiện strategy được chọn trên pool đã được lọc
**Cách hoạt động:**
1. Hệ thống lấy thông tin của proxy có ID = `referer_proxy.id`
2. Sử dụng thông tin proxy đó (type, country, location) làm tiêu chí lọc chính
3. Áp dụng các tham số `type`, `country`, `target_domain` như điều kiện lọc bổ sung
4. Thực hiện strategy được chọn trên pool đã được lọc
**Lợi ích**:
- Tìm proxy thay thế có đặc tính tương tự với proxy đang sử dụng
- Đảm bảo tính nhất quán trong quality và performance
- Hỗ trợ thuật toán fallback khi proxy hiện tại gặp vấn đề
#### 🎯 Available Strategies
**🎯 Available Strategies**
##### 1. 🎲 Random Strategy (`strategy: "random"`)
- **Tham số bắt buộc**: `type`
@@ -198,6 +227,7 @@ Lấy proxy server theo features và chiến lược lọc cụ thể.
- **Ví dụ**:
```json
{
"access_token": "your-token",
"type": "httpv6",
"strategy": "random",
"country": "VN"
@@ -211,6 +241,7 @@ Lấy proxy server theo features và chiến lược lọc cụ thể.
- **Ví dụ**:
```json
{
"access_token": "your-token",
"type": "http",
"strategy": "round_robin",
"country": "US"
@@ -224,6 +255,7 @@ Lấy proxy server theo features và chiến lược lọc cụ thể.
- **Ví dụ**:
```json
{
"access_token": "your-token",
"strategy": "least_used",
"referer_proxy": {
"id": 123
@@ -240,6 +272,7 @@ Lấy proxy server theo features và chiến lược lọc cụ thể.
- **Ví dụ**:
```json
{
"access_token": "your-token",
"target_domain": "facebook.com",
"type": "httpv6",
"strategy": "least_delay",
@@ -254,6 +287,7 @@ Lấy proxy server theo features và chiến lược lọc cụ thể.
- **Ví dụ**:
```json
{
"access_token": "your-token",
"target_domain": "instagram.com",
"type": "http",
"strategy": "adaptive_ranking",
@@ -261,7 +295,7 @@ Lấy proxy server theo features và chiến lược lọc cụ thể.
}
```
#### 📤 Response Format
**📤 Response Format**
```json
{
@@ -282,36 +316,55 @@ Lấy proxy server theo features và chiến lược lọc cụ thể.
}
```
#### 🔧 Example Usage
**🔧 Example Usage**
**Sử dụng với HttpClient:**
```csharp
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
var request = new ProxyRequestByFeature
{
AccessToken = "your-token",
Type = "httpv6",
Strategy = "adaptive_ranking",
TargetDomain = "facebook.com",
Country = "VN"
};
var httpClient = new HttpClient();
var response = await httpClient.PostAsJsonAsync(
"/api/resource-pool/v1/ProxyPool/get-v2",
"http://10.9.3.70:31249/api/resource-pool/v1/ProxyPool/get-v2",
request
);
var proxyData = await response.Content.ReadFromJsonAsync<ProxyResponse>();
// Sử dụng proxy với HttpClient
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Proxy",
$"http://{proxyData.Proxy.AuthUsername}:{proxyData.Proxy.AuthPassword}@{proxyData.Proxy.Host}:{proxyData.Proxy.Port}");
var handler = new HttpClientHandler
{
Proxy = new WebProxy($"{proxyData.Proxy.Host}:{proxyData.Proxy.Port}")
{
Credentials = new NetworkCredential(
proxyData.Proxy.AuthUsername,
proxyData.Proxy.AuthPassword
)
}
};
var proxiedClient = new HttpClient(handler);
```
**Sử dụng với RestSharp:**
```csharp
using RestSharp;
using System.Net;
var client = new RestClient("http://10.9.3.70:31249");
var request = new RestRequest("/api/resource-pool/v1/ProxyPool/get-v2", Method.Post);
request.AddJsonBody(new ProxyRequestByFeature
{
AccessToken = "your-token",
Type = "http",
Strategy = "least_delay",
TargetDomain = "instagram.com",
@@ -319,21 +372,59 @@ request.AddJsonBody(new ProxyRequestByFeature
});
var response = await client.ExecuteAsync<ProxyResponse>(request);
var proxy = new WebProxy($"{response.Data.Proxy.Host}:{response.Data.Proxy.Port}")
var proxy = response.Data.Proxy;
var proxyObj = new WebProxy($"{proxy.Host}:{proxy.Port}")
{
Credentials = new NetworkCredential(response.Data.Proxy.AuthUsername, response.Data.Proxy.AuthPassword)
Credentials = new NetworkCredential(proxy.AuthUsername, proxy.AuthPassword)
};
var restClient = new RestClient("https://api.example.com")
{
Proxy = proxy
Proxy = proxyObj
};
```
**Log Proxy Usage:**
**Endpoint**: `POST /api/resource-pool/v1/ProxyPool/used`
```csharp
var logRequest = new ProxyLogRequest
{
AccessToken = "your-token",
ProxyId = proxy.Id,
StatusCode = 200,
ResponseTimeMs = 450
};
var logResponse = await client.PostAsJsonAsync(
"http://10.9.3.70:31249/api/resource-pool/v1/ProxyPool/used",
logRequest
);
```
---
## Notes
## Proxy Pool Diagram
![Proxy Pool Diagram](Diagram-Proxy.png)
## Token Pool Diagram
![Token Pool Diagram](Diagram-Token.png)
- The `opt.Host`, `opt.Protocol`, and `opt.Port` configuration should match your environment settings.
- In production, replace the debug configurations with the appropriate production values.
- TokenPool supports both gRPC and HTTP protocols via `AddTokenPool` extension.
- ProxyPool is accessed directly via gRPC (`IProxyService`) or HTTP REST API endpoints.
- For SOCKS5 proxy support, see [HttpToSocks5Proxy](./src/HttpToSocks5Proxy/README.md).
---
## SmartPool (New System)
For the new intelligent proxy management system with SDK client, see:
- **[SmartPool SDK Documentation](./src/Icomm.SmartPool.Proxy/README.md)**
- **[SmartPool API Documentation](./src/Icomm.API.SmartPool/README.md)**
---
## Diagrams
- **Proxy Pool Diagram**: ![Proxy Pool Diagram](Diagram-Proxy.png)
- **Token Pool Diagram**: ![Token Pool Diagram](Diagram-Token.png)