From f4c7e30999d2f7b850999acf937be53eaefcdc57 Mon Sep 17 00:00:00 2001 From: mrzcpoGit <53836837+mrzcpoGit@users.noreply.github.com> Date: Mon, 29 Apr 2024 15:50:32 +0800 Subject: [PATCH 1/2] Update ehttp.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 代理可以使用用户名和密码了 --- ehttp/ehttp.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/ehttp/ehttp.go b/ehttp/ehttp.go index c4c2624..86d63b9 100644 --- a/ehttp/ehttp.go +++ b/ehttp/ehttp.go @@ -4,6 +4,7 @@ package ehttp import ( "bytes" "crypto/tls" + "encoding/base64" "encoding/json" "errors" "fmt" @@ -37,6 +38,8 @@ type Ehttp struct { E代理方式 int //代理ip Proxy string + ProxyUser string + ProxyPass string //全局头信息 全局头信息 string //默认头信息 @@ -431,6 +434,7 @@ func (this *Ehttp) setObj() *Ehttp { //等待接收服务端的回复的头域的最大时间。零值表示不设置超时。 ResponseHeaderTimeout: time.Duration(this.TimeOut) * time.Second, Proxy: nil, + ProxyConnectHeader: nil, //如果DisableKeepAlives为真,会禁止不同HTTP请求之间TCP连接的重用。 DisableKeepAlives: false, DisableCompression: false, @@ -443,8 +447,19 @@ func (this *Ehttp) setObj() *Ehttp { trans.Proxy = func(_ *http.Request) (*url.URL, error) { return url.Parse(this.Proxy) } + if len(this.ProxyUser)>0 && len(this.ProxyPass)>0{ + auth := this.ProxyUser + ":" + this.ProxyPass + encodedAuth := base64.StdEncoding.EncodeToString([]byte(auth)) + + trans.ProxyConnectHeader = http.Header{ + "Proxy-Authorization": []string{"Basic " + encodedAuth}, + } + }else{ + trans.ProxyConnectHeader = nil + } } + client := &http.Client{ Transport: trans, CheckRedirect: func(req *http.Request, via []*http.Request) error { @@ -459,20 +474,23 @@ func (this *Ehttp) setObj() *Ehttp { return this } -// SetProxy 设置代理访问 +// SetProxy 设置代理访问, 如果没有 用户名 密码,则留"" // // SetProxy("http://127.0.0.1:8888") -func (this *Ehttp) SetProxy(proxy string) *Ehttp { +func (this *Ehttp) SetProxy(proxy string,proxyUser string,proxyPass string) *Ehttp { //检查 前面是否带有 http:// 或者 https:// 如果没有则自动添加 socks5:// 则不自动添加 if strings.Index(proxy, "://") == -1 { proxy = "http://" + proxy } this.Proxy = proxy + this.ProxyUser = proxyUser + this.ProxyPass = proxyPass + return this } -func (this *Ehttp) E设置全局HTTP代理(proxy string) *Ehttp { - return this.SetProxy(proxy) +func (this *Ehttp) E设置全局HTTP代理(proxy string,proxyUser string,proxyPass string) *Ehttp { + return this.SetProxy(proxy,proxyUser,proxyPass) } func (this *Ehttp) SetTimeOut(超时时间 int) *Ehttp { this.TimeOut = 超时时间 From acd85c672923e16679069b5b2b1402b74cf7e7f4 Mon Sep 17 00:00:00 2001 From: mrzcpoGit <53836837+mrzcpoGit@users.noreply.github.com> Date: Mon, 29 Apr 2024 16:03:16 +0800 Subject: [PATCH 2/2] Update ehttp.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 支持使用用户名和密码的 代理 --- ehttp/ehttp.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/ehttp/ehttp.go b/ehttp/ehttp.go index 86d63b9..5642ede 100644 --- a/ehttp/ehttp.go +++ b/ehttp/ehttp.go @@ -38,8 +38,8 @@ type Ehttp struct { E代理方式 int //代理ip Proxy string - ProxyUser string - ProxyPass string + //代理用户名:代理密码 + ProxyAuth string //全局头信息 全局头信息 string //默认头信息 @@ -447,9 +447,8 @@ func (this *Ehttp) setObj() *Ehttp { trans.Proxy = func(_ *http.Request) (*url.URL, error) { return url.Parse(this.Proxy) } - if len(this.ProxyUser)>0 && len(this.ProxyPass)>0{ - auth := this.ProxyUser + ":" + this.ProxyPass - encodedAuth := base64.StdEncoding.EncodeToString([]byte(auth)) + if len(this.ProxyAuth) > 3 && strings.Index(this.ProxyAuth, ":") > -1{ + encodedAuth := base64.StdEncoding.EncodeToString([]byte(this.ProxyAuth)) trans.ProxyConnectHeader = http.Header{ "Proxy-Authorization": []string{"Basic " + encodedAuth}, @@ -474,23 +473,22 @@ func (this *Ehttp) setObj() *Ehttp { return this } -// SetProxy 设置代理访问, 如果没有 用户名 密码,则留"" +// SetProxy 设置代理访问, 如果没有 用户名 密码,则留"",带用户名和密码的用法:SetProxy("127.0.0.1:8888","user:pass") 不带的用法:SetProxy("127.0.0.1:8888","") // // SetProxy("http://127.0.0.1:8888") -func (this *Ehttp) SetProxy(proxy string,proxyUser string,proxyPass string) *Ehttp { +func (this *Ehttp) SetProxy(proxy string,proxyAuth string) *Ehttp { //检查 前面是否带有 http:// 或者 https:// 如果没有则自动添加 socks5:// 则不自动添加 if strings.Index(proxy, "://") == -1 { proxy = "http://" + proxy } this.Proxy = proxy - this.ProxyUser = proxyUser - this.ProxyPass = proxyPass + this.ProxyAuth = proxyAuth return this } -func (this *Ehttp) E设置全局HTTP代理(proxy string,proxyUser string,proxyPass string) *Ehttp { - return this.SetProxy(proxy,proxyUser,proxyPass) +func (this *Ehttp) E设置全局HTTP代理(proxy string,proxyAuth string) *Ehttp { + return this.SetProxy(proxy,proxyAuth) } func (this *Ehttp) SetTimeOut(超时时间 int) *Ehttp { this.TimeOut = 超时时间