Skip to content

Commit 8efc541

Browse files
authored
Merge pull request #4 from mrzcpoGit/master
支持使用用户名和密码的 代理
2 parents d2e4514 + acd85c6 commit 8efc541

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

ehttp/ehttp.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package ehttp
44
import (
55
"bytes"
66
"crypto/tls"
7+
"encoding/base64"
78
"encoding/json"
89
"errors"
910
"fmt"
@@ -37,6 +38,8 @@ type Ehttp struct {
3738
E代理方式 int
3839
//代理ip
3940
Proxy string
41+
//代理用户名:代理密码
42+
ProxyAuth string
4043
//全局头信息
4144
全局头信息 string
4245
//默认头信息
@@ -431,6 +434,7 @@ func (this *Ehttp) setObj() *Ehttp {
431434
//等待接收服务端的回复的头域的最大时间。零值表示不设置超时。
432435
ResponseHeaderTimeout: time.Duration(this.TimeOut) * time.Second,
433436
Proxy: nil,
437+
ProxyConnectHeader: nil,
434438
//如果DisableKeepAlives为真,会禁止不同HTTP请求之间TCP连接的重用。
435439
DisableKeepAlives: false,
436440
DisableCompression: false,
@@ -443,8 +447,18 @@ func (this *Ehttp) setObj() *Ehttp {
443447
trans.Proxy = func(_ *http.Request) (*url.URL, error) {
444448
return url.Parse(this.Proxy)
445449
}
450+
if len(this.ProxyAuth) > 3 && strings.Index(this.ProxyAuth, ":") > -1{
451+
encodedAuth := base64.StdEncoding.EncodeToString([]byte(this.ProxyAuth))
452+
453+
trans.ProxyConnectHeader = http.Header{
454+
"Proxy-Authorization": []string{"Basic " + encodedAuth},
455+
}
456+
}else{
457+
trans.ProxyConnectHeader = nil
458+
}
446459
}
447460

461+
448462
client := &http.Client{
449463
Transport: trans,
450464
CheckRedirect: func(req *http.Request, via []*http.Request) error {
@@ -459,20 +473,22 @@ func (this *Ehttp) setObj() *Ehttp {
459473
return this
460474
}
461475

462-
// SetProxy 设置代理访问
476+
// SetProxy 设置代理访问, 如果没有 用户名 密码,则留"",带用户名和密码的用法:SetProxy("127.0.0.1:8888","user:pass") 不带的用法:SetProxy("127.0.0.1:8888","")
463477
//
464478
// SetProxy("http://127.0.0.1:8888")
465-
func (this *Ehttp) SetProxy(proxy string) *Ehttp {
479+
func (this *Ehttp) SetProxy(proxy string,proxyAuth string) *Ehttp {
466480
//检查 前面是否带有 http:// 或者 https:// 如果没有则自动添加 socks5:// 则不自动添加
467481
if strings.Index(proxy, "://") == -1 {
468482
proxy = "http://" + proxy
469483
}
470484

471485
this.Proxy = proxy
486+
this.ProxyAuth = proxyAuth
487+
472488
return this
473489
}
474-
func (this *Ehttp) E设置全局HTTP代理(proxy string) *Ehttp {
475-
return this.SetProxy(proxy)
490+
func (this *Ehttp) E设置全局HTTP代理(proxy string,proxyAuth string) *Ehttp {
491+
return this.SetProxy(proxy,proxyAuth)
476492
}
477493
func (this *Ehttp) SetTimeOut(超时时间 int) *Ehttp {
478494
this.TimeOut = 超时时间

0 commit comments

Comments
 (0)