Skip to content

Commit 74e1193

Browse files
committed
refactor: update proxy model
Signed-off-by: jiuxia211 <2064166368@qq.com>
1 parent 7a72e40 commit 74e1193

File tree

4 files changed

+104
-39
lines changed

4 files changed

+104
-39
lines changed

config.go

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ import (
3131
func LoadConfigFromEnv() *Config {
3232
config := &Config{
3333
Proxy: ProxyConfig{
34-
TunnelURL: constants.QingGuoTunnelURL,
35-
Enabled: false,
34+
Enabled: false,
3635
},
3736
}
3837

@@ -50,52 +49,93 @@ func LoadConfigFromEnv() *Config {
5049
return config
5150
}
5251

53-
// GetTunnelAddress 获取青果网络隧道地址
54-
func (c *Config) GetTunnelAddress() (string, error) {
52+
// GetProxyAddress 获取青果网络独享代理地址
53+
func (c *Config) GetProxyAddress() (string, error) {
5554
if !c.Proxy.Enabled || c.Proxy.AuthKey == "" || c.Proxy.AuthPwd == "" {
5655
return "", fmt.Errorf("代理未启用或认证信息不完整")
5756
}
5857

5958
client := &http.Client{}
6059

61-
// 构建请求参数
60+
// 首先尝试查询现有的代理
61+
server, err := c.queryExistingProxy(client)
62+
if err == nil && server != "" {
63+
c.Proxy.ProxyServer = server
64+
return server, nil
65+
}
66+
67+
// 如果没有现有代理或查询失败,尝试获取新的代理
68+
return c.getNewProxy(client)
69+
}
70+
71+
// queryExistingProxy 查询现有的代理
72+
func (c *Config) queryExistingProxy(client *http.Client) (string, error) {
73+
params := url.Values{}
74+
params.Set("key", c.Proxy.AuthKey)
75+
params.Set("pwd", c.Proxy.AuthPwd)
76+
77+
resp, err := client.Get(constants.QingGuoExclusiveQueryURL + "?" + params.Encode())
78+
if err != nil {
79+
return "", err
80+
}
81+
defer resp.Body.Close()
82+
83+
var queryResp ExclusiveProxyQueryResponse
84+
if err := json.NewDecoder(resp.Body).Decode(&queryResp); err != nil {
85+
return "", err
86+
}
87+
88+
if queryResp.Code != "SUCCESS" || queryResp.Data == nil || len(queryResp.Data.Tasks) == 0 {
89+
return "", fmt.Errorf("没有现有的代理")
90+
}
91+
92+
// 使用第一个任务的第一个IP
93+
task := queryResp.Data.Tasks[0]
94+
if len(task.IPs) == 0 {
95+
return "", fmt.Errorf("任务中没有可用的IP")
96+
}
97+
98+
return task.IPs[0].Server, nil
99+
}
100+
101+
// getNewProxy 获取新的代理
102+
func (c *Config) getNewProxy(client *http.Client) (string, error) {
62103
params := url.Values{}
63104
params.Set("key", c.Proxy.AuthKey)
64105
params.Set("pwd", c.Proxy.AuthPwd)
65106

66-
// 发送GET请求
67-
resp, err := client.Get(c.Proxy.TunnelURL + "?" + params.Encode())
107+
resp, err := client.Get(constants.QingGuoExclusiveGetURL + "?" + params.Encode())
68108
if err != nil {
69-
return "", errno.HTTPQueryError.WithMessage("获取隧道地址失败").WithErr(err)
109+
return "", errno.HTTPQueryError.WithMessage("获取代理地址失败").WithErr(err)
70110
}
71111
defer resp.Body.Close()
72112

73-
var tunnelResp TunnelResponse
74-
if err := json.NewDecoder(resp.Body).Decode(&tunnelResp); err != nil {
75-
return "", errno.HTTPQueryError.WithMessage("解析隧道地址响应失败").WithErr(err)
113+
var getResp ExclusiveProxyGetResponse
114+
if err := json.NewDecoder(resp.Body).Decode(&getResp); err != nil {
115+
return "", errno.HTTPQueryError.WithMessage("解析代理地址响应失败").WithErr(err)
76116
}
77117

78-
if tunnelResp.Code != "SUCCESS" {
79-
return "", fmt.Errorf("获取隧道地址失败,响应码: %s", tunnelResp.Code)
118+
if getResp.Code != "SUCCESS" {
119+
return "", fmt.Errorf("获取代理地址失败,响应码: %s, 消息: %s", getResp.Code, getResp.Message)
80120
}
81121

82-
// 检查是否有可用的隧道数据
83-
if len(tunnelResp.Data) == 0 {
84-
return "", fmt.Errorf("没有可用的隧道地址")
122+
// 检查是否有可用的数据
123+
if getResp.Data == nil || len(getResp.Data.IPs) == 0 {
124+
return "", fmt.Errorf("没有可用的代理地址")
85125
}
86126

87-
// 使用第一个可用的隧道地址
88-
tunnelServer := tunnelResp.Data[0].Server
89-
if tunnelServer == "" {
90-
return "", fmt.Errorf("隧道地址为空")
127+
// 使用第一个可用的代理地址
128+
proxyServer := getResp.Data.IPs[0].Server
129+
if proxyServer == "" {
130+
return "", fmt.Errorf("代理地址为空")
91131
}
92132

93133
// 更新配置中的代理服务器地址
94-
c.Proxy.ProxyServer = tunnelServer
95-
return tunnelServer, nil
134+
c.Proxy.ProxyServer = proxyServer
135+
return proxyServer, nil
96136
}
97137

98-
// GetProxyURL 根据青果网络文档生成代理URL
138+
// GetProxyURL 根据青果网络独享代理文档生成代理URL
99139
func (c *Config) GetProxyURL() (*url.URL, error) {
100140
if !c.Proxy.Enabled {
101141
return nil, fmt.Errorf("代理未启用")
@@ -105,7 +145,7 @@ func (c *Config) GetProxyURL() (*url.URL, error) {
105145
return nil, fmt.Errorf("代理配置信息不完整")
106146
}
107147

108-
// 普通模式:每次请求都自动切换IP
148+
// 独享代理模式:使用key作为用户名,pwd作为密码
109149
link := fmt.Sprintf("http://%s:%s@%s", c.Proxy.AuthKey, c.Proxy.AuthPwd, c.Proxy.ProxyServer)
110150
return url.Parse(link)
111151
}

constants/constants.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const (
4242
AutoCaptchaVerifyURL = "https://statistics.fzuhelper.w2fzu.com/api/login/validateCode?validateCode"
4343

4444
// 青果网络代理相关常量
45-
QingGuoTunnelURL = "https://share.proxy.qg.net/pool" // 青果网络隧道地址获取接口
45+
QingGuoExclusiveGetURL = "https://exclusive.proxy.qg.net/get" // 青果网络独享代理提取接口
46+
QingGuoExclusiveQueryURL = "https://exclusive.proxy.qg.net/query" // 青果网络独享代理查询接口
4647
)
4748

4849
var BuildingArray = []string{"公共教学楼东1", "公共教学楼东2", "公共教学楼东3", "公共教学楼文科楼", "公共教学楼西1", "公共教学楼西2", "公共教学楼西3", "公共教学楼中楼"}

jwch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ func NewStudent() *Student {
4242
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
4343
}
4444

45-
// 如果启用了代理,先获取隧道地址再设置代理
45+
// 如果启用了代理,先获取代理地址再设置代理
4646
if config.Proxy.Enabled {
47-
_, err := config.GetTunnelAddress()
47+
_, err := config.GetProxyAddress()
4848
if err == nil && config.Proxy.ProxyServer != "" {
4949
proxyURL, err := config.GetProxyURL()
5050
if err == nil {

model.go

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,7 @@ type LocateDate struct {
221221
type ProxyConfig struct {
222222
AuthKey string `json:"auth_key"` // 青果网络认证密钥
223223
AuthPwd string `json:"auth_pwd"` // 青果网络认证密码
224-
TunnelURL string `json:"tunnel_url"` // 隧道地址获取接口
225-
ProxyServer string `json:"proxy_server"` // 代理服务器地址 (从隧道接口获取)
224+
ProxyServer string `json:"proxy_server"` // 代理服务器地址 (从代理接口获取)
226225
Enabled bool `json:"enabled"` // 是否启用代理
227226
}
228227

@@ -231,16 +230,41 @@ type Config struct {
231230
Proxy ProxyConfig `json:"proxy"` // 代理配置
232231
}
233232

234-
// TunnelData 隧道数据
235-
type TunnelData struct {
236-
Server string `json:"server"` // 代理服务器地址,格式: host:port
237-
Area string `json:"area"` // 区域代码
238-
Distinct bool `json:"distinct"` // 是否独享
233+
// ExclusiveProxyIP 独享代理IP信息
234+
type ExclusiveProxyIP struct {
235+
ProxyIP string `json:"proxy_ip"` // 代理IP地址
236+
Server string `json:"server"` // 代理服务器地址,格式: host:port
237+
AreaCode int `json:"area_code"` // 区域代码
238+
Area string `json:"area"` // 区域名称
239+
ISP string `json:"isp"` // 运营商
240+
Deadline string `json:"deadline"` // 过期时间
239241
}
240242

241-
// TunnelResponse 隧道地址响应
242-
type TunnelResponse struct {
243-
Code string `json:"code"` // 响应码,"SUCCESS"表示成功
244-
Data []TunnelData `json:"data"` // 隧道数据数组
245-
RequestId string `json:"request_id"` // 请求ID
243+
// ExclusiveProxyTask 独享代理任务
244+
type ExclusiveProxyTask struct {
245+
TaskID string `json:"task_id"` // 任务ID
246+
IPs []ExclusiveProxyIP `json:"ips"` // IP列表
247+
Num int `json:"num"` // IP数量
248+
}
249+
250+
// ExclusiveProxyGetResponse 独享代理提取响应
251+
type ExclusiveProxyGetResponse struct {
252+
Code string `json:"code"` // 响应码,"SUCCESS"表示成功
253+
Data *struct {
254+
TaskID string `json:"task_id"` // 任务ID
255+
IPs []ExclusiveProxyIP `json:"ips"` // IP列表
256+
Num int `json:"num"` // IP数量
257+
} `json:"data"` // 数据
258+
Message string `json:"message"` // 错误消息(如果有)
259+
RequestId string `json:"request_id"` // 请求ID
260+
}
261+
262+
// ExclusiveProxyQueryResponse 独享代理查询响应
263+
type ExclusiveProxyQueryResponse struct {
264+
Code string `json:"code"` // 响应码,"SUCCESS"表示成功
265+
Data *struct {
266+
Num int `json:"num"` // 任务数量
267+
Tasks []ExclusiveProxyTask `json:"tasks"` // 任务列表
268+
} `json:"data"` // 数据
269+
RequestId string `json:"request_id"` // 请求ID
246270
}

0 commit comments

Comments
 (0)