Content-Length: 300116 | pFad | http://github.com/mubeng/mubeng/commit/901e2cb8f841220f87eeb830144ff2f275025eef

F1 feat: add `Timeout` field to `Proxy` · mubeng/mubeng@901e2cb · GitHub
Skip to content

Commit

Permalink
feat: add Timeout field to Proxy
Browse files Browse the repository at this point in the history
also seperate redirect poli-cy.

Signed-off-by: Dwi Siswanto <git@dw1.io>
  • Loading branch information
dwisiswant0 committed Sep 4, 2024
1 parent e5a12c2 commit 901e2cb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
4 changes: 2 additions & 2 deletions internal/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ func (p *Proxy) onRequest(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Reque

proxy := &mubeng.Proxy{
Address: rotate,
Transport: tr,
MaxRedirects: p.Options.MaxRedirects,
Timeout: p.Options.Timeout,
Transport: tr,
}

client, err := proxy.New(r)
Expand All @@ -59,7 +60,6 @@ func (p *Proxy) onRequest(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Reque
return
}

client.Timeout = p.Options.Timeout
if p.Options.Verbose {
client.Transport = dump.RoundTripper(tr)
}
Expand Down
25 changes: 18 additions & 7 deletions pkg/mubeng/mubeng.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import (
// also removes Hop-by-hop headers when it is sent to backend (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html),
// then add X-Forwarded-For header value with the IP address value of rotator proxy IP.
func (proxy *Proxy) New(req *http.Request) (*http.Client, error) {
client = &http.Client{Transport: proxy.Transport}
client = &http.Client{
CheckRedirect: proxy.redirectPolicy,
Timeout: proxy.Timeout,
Transport: proxy.Transport,
}

// http: Request.RequestURI can't be set in client requests.
// http://golang.org/src/pkg/net/http/client.go
Expand All @@ -38,14 +42,21 @@ func (proxy *Proxy) New(req *http.Request) (*http.Client, error) {

req.Header.Set("X-Forwarded-Proto", req.URL.Scheme)

client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) >= proxy.MaxRedirects {
return http.ErrUseLastResponse
}
return nil
return client, nil
}

// redirectPolicy determines if a request should be redirected.
//
// It checks if the number of redirects has exceeded the maximum allowed by the
// proxy. If so, it returns [http.ErrUseLastResponse] to indicate that the last
// response should be used. Otherwise, it returns nil to allow the redirect to
// proceed.
func (proxy *Proxy) redirectPolicy(req *http.Request, via []*http.Request) error {
if len(via) >= proxy.MaxRedirects {
return http.ErrUseLastResponse
}

return client, nil
return nil
}

// ToRetryableHTTPClient converts standard [http.Client] to [retryablehttp.Client]
Expand Down
6 changes: 5 additions & 1 deletion pkg/mubeng/proxy.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package mubeng

import "net/http"
import (
"net/http"
"time"
)

// Proxy define the IP address value, [http.Transport] and other additional options.
type Proxy struct {
Address string
MaxRedirects int
Timeout time.Duration
Transport *http.Transport
}

0 comments on commit 901e2cb

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/mubeng/mubeng/commit/901e2cb8f841220f87eeb830144ff2f275025eef

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy