Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add Dialer.ProxyConnectHeader#605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
AllenX2018 wants to merge2 commits intogorilla:main
base:main
Choose a base branch
Loading
fromAllenX2018:fix-issue-479
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletionclient.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -110,6 +110,9 @@ type Dialer struct {
// If Jar is nil, cookies are not sent in requests and ignored
// in responses.
Jar http.CookieJar

// Custom proxy connect header
ProxyConnectHeader http.Header
}

// Dial creates a new client connection by calling DialContext with a background context.
Expand DownExpand Up@@ -304,7 +307,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
return nil, nil, err
}
if proxyURL != nil {
dialer, err := proxy_FromURL(proxyURL,netDialerFunc(netDial))
dialer, err := proxy_FromURL(proxyURL,&netDialer{d.ProxyConnectHeader,netDial})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Should check if the header is set before using it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Custom header information, can be empty

if err != nil {
return nil, nil, err
}
Expand Down
7 changes: 7 additions & 0 deletionsclient_server_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -157,6 +157,9 @@ func TestProxyDial(t *testing.T) {

cstDialer := cstDialer // make local copy for modification on next line.
cstDialer.Proxy = http.ProxyURL(surl)
cstDialer.ProxyConnectHeader = map[string][]string{
"User-Agents": {"xxx"},
}

connect := false
origHandler := s.Server.Config.Handler
Expand All@@ -167,6 +170,10 @@ func TestProxyDial(t *testing.T) {
if r.Method == http.MethodConnect {
connect = true
w.WriteHeader(http.StatusOK)
if r.Header.Get("User-Agents") != "xxx" {
t.Log("xxx not found in the request header")
http.Error(w, "header xxx not found", http.StatusMethodNotAllowed)
}
return
}

Expand Down
20 changes: 16 additions & 4 deletionsproxy.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,21 +14,29 @@ import (
"strings"
)

type netDialerFunc func(network, addr string) (net.Conn, error)
type netDialer struct {
proxyHeader http.Header
f func(network, addr string) (net.Conn, error)
}

func (fn netDialerFunc) Dial(network, addr string) (net.Conn, error) {
returnfn(network, addr)
func (n netDialer) Dial(network, addr string) (net.Conn, error) {
returnn.f(network, addr)
}

func init() {
proxy_RegisterDialerType("http", func(proxyURL *url.URL, forwardDialer proxy_Dialer) (proxy_Dialer, error) {
return &httpProxyDialer{proxyURL: proxyURL, forwardDial: forwardDialer.Dial}, nil
p, ok := forwardDialer.(*netDialer)
if !ok {
return nil, errors.New("type assertion failed when ini proxy info")
}
return &httpProxyDialer{proxyURL: proxyURL, forwardDial: forwardDialer.Dial, proxyHeader: p.proxyHeader}, nil
})
}

type httpProxyDialer struct {
proxyURL *url.URL
forwardDial func(network, addr string) (net.Conn, error)
proxyHeader http.Header
}

func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error) {
Expand All@@ -47,6 +55,10 @@ func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error)
}
}

for k, v := range hpd.proxyHeader {
connectHeader[k] = v
}

connectReq := &http.Request{
Method: http.MethodConnect,
URL: &url.URL{Opaque: addr},
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp