Skip to content

Commit acd85c6

Browse files
authored
Update ehttp.go
支持使用用户名和密码的 代理
1 parent f4c7e30 commit acd85c6

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

ehttp/ehttp.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ type Ehttp struct {
3838
E代理方式 int
3939
//代理ip
4040
Proxy string
41-
ProxyUser string
42-
ProxyPass string
41+
//代理用户名:代理密码
42+
ProxyAuth string
4343
//全局头信息
4444
全局头信息 string
4545
//默认头信息
@@ -447,9 +447,8 @@ func (this *Ehttp) setObj() *Ehttp {
447447
trans.Proxy = func(_ *http.Request) (*url.URL, error) {
448448
return url.Parse(this.Proxy)
449449
}
450-
if len(this.ProxyUser)>0 && len(this.ProxyPass)>0{
451-
auth := this.ProxyUser + ":" + this.ProxyPass
452-
encodedAuth := base64.StdEncoding.EncodeToString([]byte(auth))
450+
if len(this.ProxyAuth) > 3 && strings.Index(this.ProxyAuth, ":") > -1{
451+
encodedAuth := base64.StdEncoding.EncodeToString([]byte(this.ProxyAuth))
453452

454453
trans.ProxyConnectHeader = http.Header{
455454
"Proxy-Authorization": []string{"Basic " + encodedAuth},
@@ -474,23 +473,22 @@ func (this *Ehttp) setObj() *Ehttp {
474473
return this
475474
}
476475

477-
// SetProxy 设置代理访问, 如果没有 用户名 密码,则留""
476+
// SetProxy 设置代理访问, 如果没有 用户名 密码,则留"",带用户名和密码的用法:SetProxy("127.0.0.1:8888","user:pass") 不带的用法:SetProxy("127.0.0.1:8888","")
478477
//
479478
// SetProxy("http://127.0.0.1:8888")
480-
func (this *Ehttp) SetProxy(proxy string,proxyUser string,proxyPass string) *Ehttp {
479+
func (this *Ehttp) SetProxy(proxy string,proxyAuth string) *Ehttp {
481480
//检查 前面是否带有 http:// 或者 https:// 如果没有则自动添加 socks5:// 则不自动添加
482481
if strings.Index(proxy, "://") == -1 {
483482
proxy = "http://" + proxy
484483
}
485484

486485
this.Proxy = proxy
487-
this.ProxyUser = proxyUser
488-
this.ProxyPass = proxyPass
486+
this.ProxyAuth = proxyAuth
489487

490488
return this
491489
}
492-
func (this *Ehttp) E设置全局HTTP代理(proxy string,proxyUser string,proxyPass string) *Ehttp {
493-
return this.SetProxy(proxy,proxyUser,proxyPass)
490+
func (this *Ehttp) E设置全局HTTP代理(proxy string,proxyAuth string) *Ehttp {
491+
return this.SetProxy(proxy,proxyAuth)
494492
}
495493
func (this *Ehttp) SetTimeOut(超时时间 int) *Ehttp {
496494
this.TimeOut = 超时时间

0 commit comments

Comments
 (0)