251 lines
6.3 KiB
Go
251 lines
6.3 KiB
Go
package proxy
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
proxyTextRegex = regexp.MustCompile(`(?i)(?:(https?|socks5)://)?(?:[^@\s:/]+:[^@\s/]+@)?((?:\d{1,3}\.){3}\d{1,3}):(\d{2,5})`)
|
|
)
|
|
|
|
// GenericProvider 兼容常见 JSON 与纯文本 IP:端口 返回。
|
|
type GenericProvider struct{}
|
|
|
|
func NewGenericProvider() *GenericProvider {
|
|
return &GenericProvider{}
|
|
}
|
|
|
|
func (p *GenericProvider) ID() string {
|
|
return "generic"
|
|
}
|
|
|
|
func (p *GenericProvider) Match(u *url.URL) bool {
|
|
return u != nil
|
|
}
|
|
|
|
func (p *GenericProvider) RequestURL(apiURL string, count int) (string, error) {
|
|
u, err := url.Parse(strings.TrimSpace(apiURL))
|
|
if err != nil {
|
|
return "", fmt.Errorf("代理 API 地址无效: %w", err)
|
|
}
|
|
query := u.Query()
|
|
changed := false
|
|
for _, key := range []string{"num", "count", "quantity"} {
|
|
if query.Has(key) {
|
|
query.Set(key, strconv.Itoa(count))
|
|
changed = true
|
|
}
|
|
}
|
|
if changed {
|
|
u.RawQuery = query.Encode()
|
|
}
|
|
return u.String(), nil
|
|
}
|
|
|
|
func (p *GenericProvider) Parse(apiURL string, body []byte) ([]Endpoint, error) {
|
|
text := strings.TrimSpace(string(body))
|
|
if text == "" {
|
|
return nil, errors.New("代理 API 返回内容为空")
|
|
}
|
|
protocol := protocolFromAPI(apiURL)
|
|
|
|
var payload struct {
|
|
Code *int `json:"code"`
|
|
Status int `json:"status"`
|
|
Info string `json:"info"`
|
|
Msg string `json:"msg"`
|
|
Message string `json:"message"`
|
|
Data json.RawMessage `json:"data"`
|
|
}
|
|
if json.Unmarshal(body, &payload) == nil {
|
|
message := firstNonEmpty(payload.Info, payload.Msg, payload.Message)
|
|
hasError := (payload.Code != nil && *payload.Code != 0) ||
|
|
(payload.Code == nil && payload.Status != 0 && payload.Status != 100 && payload.Status != 200)
|
|
if hasError {
|
|
if message == "" {
|
|
message = "代理平台返回错误"
|
|
}
|
|
return nil, &ProviderError{
|
|
Provider: "通用代理",
|
|
Message: message,
|
|
Fatal: isGenericFatalMessage(message),
|
|
}
|
|
}
|
|
if len(payload.Data) > 0 {
|
|
endpoints := parseEndpointJSON(payload.Data, protocol)
|
|
if len(endpoints) > 0 {
|
|
return endpoints, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
endpoints := parseEndpointJSON(body, protocol)
|
|
if len(endpoints) > 0 {
|
|
return endpoints, nil
|
|
}
|
|
endpoints = parseEndpointText(text, protocol)
|
|
if len(endpoints) > 0 {
|
|
return endpoints, nil
|
|
}
|
|
return nil, fmt.Errorf("未能从代理 API 返回中识别出有效 IP:端口: %s", truncateText(text, 120))
|
|
}
|
|
|
|
func isGenericFatalMessage(message string) bool {
|
|
if strings.Contains(message, "提取频率过快") || strings.Contains(message, "请求过于频繁") {
|
|
return false
|
|
}
|
|
for _, hint := range []string{
|
|
"白名单", "认证", "鉴权", "密钥", "密匙", "套餐", "过期", "禁用", "余额", "未授权",
|
|
} {
|
|
if strings.Contains(strings.ToLower(message), strings.ToLower(hint)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func parseEndpointJSON(raw []byte, protocol string) []Endpoint {
|
|
var value any
|
|
if json.Unmarshal(raw, &value) != nil {
|
|
return nil
|
|
}
|
|
return collectJSONEndpoints(value, protocol)
|
|
}
|
|
|
|
func collectJSONEndpoints(value any, protocol string) []Endpoint {
|
|
switch typed := value.(type) {
|
|
case string:
|
|
return parseEndpointText(typed, protocol)
|
|
case []any:
|
|
var out []Endpoint
|
|
for _, item := range typed {
|
|
out = append(out, collectJSONEndpoints(item, protocol)...)
|
|
}
|
|
return out
|
|
case map[string]any:
|
|
if endpoints := endpointsFromMap(typed, protocol); len(endpoints) > 0 {
|
|
return endpoints
|
|
}
|
|
var out []Endpoint
|
|
for _, key := range []string{"data", "list", "items", "result", "results", "proxies"} {
|
|
if child, ok := typed[key]; ok {
|
|
out = append(out, collectJSONEndpoints(child, protocol)...)
|
|
}
|
|
}
|
|
return out
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func endpointsFromMap(item map[string]any, protocol string) []Endpoint {
|
|
ip := stringValue(item["ip"], item["host"], item["server"])
|
|
port := stringValue(item["port"], item["proxy_port"])
|
|
if ip == "" || port == "" {
|
|
return nil
|
|
}
|
|
itemProtocol := stringValue(item["protocol"], item["scheme"])
|
|
if itemProtocol == "" {
|
|
itemProtocol = protocol
|
|
}
|
|
username := stringValue(item["username"], item["user"], item["account"])
|
|
password := stringValue(item["password"], item["pass"])
|
|
expiresAt := timeValue(item["expired"], item["expire"], item["expiresAt"], item["expireAt"])
|
|
region := strings.TrimSpace(strings.Join([]string{
|
|
stringValue(item["prov"], item["province"]),
|
|
stringValue(item["city"]),
|
|
stringValue(item["region"]),
|
|
}, " "))
|
|
return []Endpoint{{
|
|
URL: buildProxyURL(itemProtocol, ip, port, username, password),
|
|
IP: ip,
|
|
Port: port,
|
|
Region: region,
|
|
Protocol: itemProtocol,
|
|
Username: username,
|
|
Password: password,
|
|
ExpiresAt: expiresAt,
|
|
}}
|
|
}
|
|
|
|
func parseEndpointText(text, protocol string) []Endpoint {
|
|
matches := proxyTextRegex.FindAllStringSubmatch(text, -1)
|
|
out := make([]Endpoint, 0, len(matches))
|
|
for _, match := range matches {
|
|
if len(match) != 4 {
|
|
continue
|
|
}
|
|
matchProtocol := match[1]
|
|
if matchProtocol == "" {
|
|
matchProtocol = protocol
|
|
}
|
|
ip, port := match[2], match[3]
|
|
out = append(out, Endpoint{
|
|
URL: buildProxyURL(matchProtocol, ip, port, "", ""),
|
|
IP: ip,
|
|
Port: port,
|
|
Protocol: matchProtocol,
|
|
})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func stringValue(values ...any) string {
|
|
for _, value := range values {
|
|
switch typed := value.(type) {
|
|
case string:
|
|
if strings.TrimSpace(typed) != "" {
|
|
return strings.TrimSpace(typed)
|
|
}
|
|
case float64:
|
|
return fmt.Sprintf("%.0f", typed)
|
|
case json.Number:
|
|
return typed.String()
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func timeValue(values ...any) time.Time {
|
|
for _, value := range values {
|
|
switch typed := value.(type) {
|
|
case float64:
|
|
return unixTimestamp(typed)
|
|
case json.Number:
|
|
number, err := typed.Float64()
|
|
if err == nil {
|
|
return unixTimestamp(number)
|
|
}
|
|
case string:
|
|
typed = strings.TrimSpace(typed)
|
|
if typed == "" {
|
|
continue
|
|
}
|
|
if number, err := strconv.ParseFloat(typed, 64); err == nil {
|
|
return unixTimestamp(number)
|
|
}
|
|
if parsed, err := time.Parse(time.RFC3339, typed); err == nil {
|
|
return parsed
|
|
}
|
|
}
|
|
}
|
|
return time.Time{}
|
|
}
|
|
|
|
func unixTimestamp(value float64) time.Time {
|
|
if value <= 0 {
|
|
return time.Time{}
|
|
}
|
|
if value >= 1e12 {
|
|
return time.UnixMilli(int64(value))
|
|
}
|
|
return time.Unix(int64(value), 0)
|
|
}
|