Skip to content

Commit

Permalink
Merge pull request #4 from mrzcpoGit/master
Browse files Browse the repository at this point in the history
支持使用用户名和密码的 代理
  • Loading branch information
duolabmeng6 authored May 5, 2024
2 parents d2e4514 + acd85c6 commit 8efc541
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions ehttp/ehttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package ehttp
import (
"bytes"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -37,6 +38,8 @@ type Ehttp struct {
E代理方式 int
//代理ip
Proxy string
//代理用户名:代理密码
ProxyAuth string
//全局头信息
全局头信息 string
//默认头信息
Expand Down Expand Up @@ -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,
Expand All @@ -443,8 +447,18 @@ func (this *Ehttp) setObj() *Ehttp {
trans.Proxy = func(_ *http.Request) (*url.URL, error) {
return url.Parse(this.Proxy)
}
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},
}
}else{
trans.ProxyConnectHeader = nil
}
}


client := &http.Client{
Transport: trans,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
Expand All @@ -459,20 +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) *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.ProxyAuth = proxyAuth

return this
}
func (this *Ehttp) E设置全局HTTP代理(proxy string) *Ehttp {
return this.SetProxy(proxy)
func (this *Ehttp) E设置全局HTTP代理(proxy string,proxyAuth string) *Ehttp {
return this.SetProxy(proxy,proxyAuth)
}
func (this *Ehttp) SetTimeOut(超时时间 int) *Ehttp {
this.TimeOut = 超时时间
Expand Down

0 comments on commit 8efc541

Please sign in to comment.