feat(proxy): 增加代理池演练并修复账号密码提取,发布 0.5.1

This commit is contained in:
yml
2026-11-20 12:08:13 +08:00
parent 98b3dc4b16
commit 635d80e63e
13 changed files with 874 additions and 19 deletions
+27
View File
@@ -44,6 +44,10 @@ func (p *IPZanProvider) RequestURL(apiURL string, count int) (string, error) {
}
query := u.Query()
query.Set("num", strconv.Itoa(count))
if strings.EqualFold(strings.TrimSpace(query.Get("mode")), "auth") {
// txt 只返回 IP:端口,会丢失每个代理各自的账号密码,随后必然触发 407。
query.Set("format", "json")
}
u.RawQuery = query.Encode()
return u.String(), nil
}
@@ -74,6 +78,9 @@ func (p *IPZanProvider) Parse(apiURL string, body []byte) ([]Endpoint, error) {
if len(payload.Data) > 0 {
endpoints := parseEndpointJSON(payload.Data, protocolFromAPI(apiURL))
if len(endpoints) > 0 {
if err := validateIPZanAuthEndpoints(apiURL, endpoints); err != nil {
return nil, err
}
return endpoints, nil
}
}
@@ -87,9 +94,29 @@ func (p *IPZanProvider) Parse(apiURL string, body []byte) ([]Endpoint, error) {
Fatal: false,
}
}
if err := validateIPZanAuthEndpoints(apiURL, endpoints); err != nil {
return nil, err
}
return endpoints, nil
}
func validateIPZanAuthEndpoints(apiURL string, endpoints []Endpoint) error {
u, err := url.Parse(strings.TrimSpace(apiURL))
if err != nil || !strings.EqualFold(strings.TrimSpace(u.Query().Get("mode")), "auth") {
return nil
}
for _, endpoint := range endpoints {
if strings.TrimSpace(endpoint.Username) == "" || strings.TrimSpace(endpoint.Password) == "" {
return &ProviderError{
Provider: "品赞",
Message: "账号密码模式返回缺少账号或密码;请移除 format=txt,让程序使用 JSON 格式提取",
Fatal: true,
}
}
}
return nil
}
func (p *IPZanProvider) EnsureWhitelist(ctx context.Context, httpClient *http.Client, cfg WhitelistConfig) error {
if strings.TrimSpace(cfg.IP) == "" {
return &ProviderError{Provider: "品赞", Message: "自动白名单缺少公网 IP", Fatal: true}
+29
View File
@@ -24,6 +24,35 @@ func TestIPZanRequestURLSetsRequestedCount(t *testing.T) {
}
}
func TestIPZanRequestURLForcesJSONForAuthMode(t *testing.T) {
raw := "https://service.ipzan.com/core-extract?num=1&mode=auth&format=txt&secret=x"
got, err := NewIPZanProvider().RequestURL(raw, 8)
if err != nil {
t.Fatal(err)
}
u, err := url.Parse(got)
if err != nil {
t.Fatal(err)
}
if mode := u.Query().Get("mode"); mode != "auth" {
t.Fatalf("mode=%q, want auth", mode)
}
if format := u.Query().Get("format"); format != "json" {
t.Fatalf("format=%q, want json", format)
}
}
func TestIPZanParseRejectsAuthModeWithoutCredentials(t *testing.T) {
body := []byte(`{"code":0,"status":200,"data":{"list":[{"ip":"1.2.3.4","port":8001}]}}`)
_, err := NewIPZanProvider().Parse(
"https://service.ipzan.com/core-extract?mode=auth&protocol=1",
body,
)
if err == nil || !IsFatal(err) {
t.Fatalf("expected fatal missing-credentials error, got %v", err)
}
}
func TestIPZanParseTextMultiple(t *testing.T) {
body := []byte("1.2.3.4:8001\r\n5.6.7.8:8002\r\n")
got, err := NewIPZanProvider().Parse(