From fb62910b17636789c886d51c84ff0c1ea4aeffe2 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sat, 21 Nov 2020 01:26:02 +0000 Subject: [PATCH] HTTP proxy 24 hours timeout, fix #303 --- cmd/gluetun/main.go | 2 +- internal/httpproxy/handler.go | 23 ++++++++++------------- internal/httpproxy/http.go | 5 +---- internal/httpproxy/https.go | 2 +- internal/httpproxy/loop.go | 8 ++------ internal/httpproxy/server.go | 5 ++--- 6 files changed, 17 insertions(+), 28 deletions(-) diff --git a/cmd/gluetun/main.go b/cmd/gluetun/main.go index c68e8d90..d0b18240 100644 --- a/cmd/gluetun/main.go +++ b/cmd/gluetun/main.go @@ -243,7 +243,7 @@ func _main(background context.Context, args []string) int { //nolint:gocognit,go go publicIPLooper.RunRestartTicker(ctx, wg) publicIPLooper.SetPeriod(allSettings.PublicIPPeriod) // call after RunRestartTicker - httpProxyLooper := httpproxy.NewLooper(httpClient, logger, allSettings.HTTPProxy) + httpProxyLooper := httpproxy.NewLooper(logger, allSettings.HTTPProxy) wg.Add(1) go httpProxyLooper.Run(ctx, wg) diff --git a/internal/httpproxy/handler.go b/internal/httpproxy/handler.go index 5c206739..8ee3cdb9 100644 --- a/internal/httpproxy/handler.go +++ b/internal/httpproxy/handler.go @@ -9,20 +9,18 @@ import ( "github.com/qdm12/golibs/logging" ) -func newHandler(ctx context.Context, wg *sync.WaitGroup, - client *http.Client, logger logging.Logger, +func newHandler(ctx context.Context, wg *sync.WaitGroup, logger logging.Logger, stealth, verbose bool, username, password string) http.Handler { - const relayTimeout = 10 * time.Second + const httpTimeout = 24 * time.Hour return &handler{ - ctx: ctx, - wg: wg, - client: client, - logger: logger, - relayTimeout: relayTimeout, - verbose: verbose, - stealth: stealth, - username: username, - password: password, + ctx: ctx, + wg: wg, + client: &http.Client{Timeout: httpTimeout}, + logger: logger, + verbose: verbose, + stealth: stealth, + username: username, + password: password, } } @@ -31,7 +29,6 @@ type handler struct { wg *sync.WaitGroup client *http.Client logger logging.Logger - relayTimeout time.Duration verbose, stealth bool username, password string } diff --git a/internal/httpproxy/http.go b/internal/httpproxy/http.go index 438872dc..fd0bd7c9 100644 --- a/internal/httpproxy/http.go +++ b/internal/httpproxy/http.go @@ -1,7 +1,6 @@ package httpproxy import ( - "context" "fmt" "io" "net" @@ -18,9 +17,7 @@ func (h *handler) handleHTTP(responseWriter http.ResponseWriter, request *http.R return } - ctx, cancel := context.WithTimeout(h.ctx, h.relayTimeout) - defer cancel() - request = request.WithContext(ctx) + request = request.WithContext(h.ctx) request.RequestURI = "" diff --git a/internal/httpproxy/https.go b/internal/httpproxy/https.go index fa00a34e..32bafdc4 100644 --- a/internal/httpproxy/https.go +++ b/internal/httpproxy/https.go @@ -9,7 +9,7 @@ import ( ) func (h *handler) handleHTTPS(responseWriter http.ResponseWriter, request *http.Request) { - dialer := net.Dialer{Timeout: h.relayTimeout} + dialer := net.Dialer{} destinationConn, err := dialer.DialContext(h.ctx, "tcp", request.Host) if err != nil { http.Error(responseWriter, err.Error(), http.StatusServiceUnavailable) diff --git a/internal/httpproxy/loop.go b/internal/httpproxy/loop.go index e36ad109..4789f4bf 100644 --- a/internal/httpproxy/loop.go +++ b/internal/httpproxy/loop.go @@ -3,7 +3,6 @@ package httpproxy import ( "context" "fmt" - "net/http" "sync" "github.com/qdm12/gluetun/internal/settings" @@ -20,7 +19,6 @@ type Looper interface { } type looper struct { - client *http.Client settings settings.HTTPProxy settingsMutex sync.RWMutex logger logging.Logger @@ -29,10 +27,8 @@ type looper struct { stop chan struct{} } -func NewLooper(client *http.Client, logger logging.Logger, - settings settings.HTTPProxy) Looper { +func NewLooper(logger logging.Logger, settings settings.HTTPProxy) Looper { return &looper{ - client: client, settings: settings, logger: logger.WithPrefix("http proxy: "), restart: make(chan struct{}), @@ -104,7 +100,7 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) { settings := l.GetSettings() address := fmt.Sprintf("0.0.0.0:%d", settings.Port) - server := New(ctx, address, l.logger, l.client, settings.Stealth, settings.Log, settings.User, settings.Password) + server := New(ctx, address, l.logger, settings.Stealth, settings.Log, settings.User, settings.Password) runCtx, runCancel := context.WithCancel(context.Background()) runWg := &sync.WaitGroup{} diff --git a/internal/httpproxy/server.go b/internal/httpproxy/server.go index 3da4bf0a..955009bc 100644 --- a/internal/httpproxy/server.go +++ b/internal/httpproxy/server.go @@ -20,13 +20,12 @@ type server struct { internalWG *sync.WaitGroup } -func New(ctx context.Context, address string, - logger logging.Logger, client *http.Client, +func New(ctx context.Context, address string, logger logging.Logger, stealth, verbose bool, username, password string) Server { wg := &sync.WaitGroup{} return &server{ address: address, - handler: newHandler(ctx, wg, client, logger, stealth, verbose, username, password), + handler: newHandler(ctx, wg, logger, stealth, verbose, username, password), logger: logger, internalWG: wg, }