mirror of
https://github.com/xlionjuan/searxng-mcp-go.git
synced 2026-11-17 04:17:51 +00:00
* fix: honor standard proxy environment variables Add to the HTTP transport so that HTTP_PROXY, HTTPS_PROXY, and NO_PROXY env vars are respected. Previously the transport was built from zero values with Proxy left nil, meaning requests never went through a proxy even when the environment was configured for one. This made SearXNG unreachable in environments that require outbound HTTP through a proxy. Also adds a unit test verifying the Proxy field is set and returns nil for loopback/localhost addresses (Go's built-in bypass). Updates the installation documentation with proxy configuration examples. Closes #489 * fix: switch to DefaultTransport.Clone for HTTP/2 support and add subprocess tests Clone http.DefaultTransport instead of building a transport from zero values. This preserves ForceAttemptHTTP2 (now enabled) and inherits ProxyFromEnvironment automatically. Add subprocess-based tests for proxy env var integration: - TestNewHTTPClient_ProxyEnvHonored: verifies HTTP_PROXY is honored - TestNewHTTPClient_NoProxyBypass: verifies NO_PROXY bypass Subprocess isolation avoids http.ProxyFromEnvironment's global sync.Once cache, which cannot be reset from outside net/http. Also tighten the INSTALL.md wording from 'read once at startup' to 'cached after first proxy evaluation'. Ref #489 * fix: use t.Context() for subprocess tests and isolate proxy env - Replace context.Background() with t.Context() + 30s timeout so stuck subprocesses don't leak goroutines - Add proxySubprocessCmd helper that filters proxy env vars from the parent, preventing NO_PROXY interference in the child - Explicitly set MaxIdleConns on the DefaultTransport clone (was implicitly inherited; now pinned to project constant) * fix: remove fallback for codecov, add proxy routing and transport field tests * fix: safe type assertion, set TLSHandshakeTimeout and IdleConnTimeout explicitly * fix: restore fallback path and add fallback test, reply to CodeRabbit
87 lines
2.8 KiB
Go
87 lines
2.8 KiB
Go
package searxng
|
|
|
|
import (
|
|
"io"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// getDefaultHTTPClient returns the shared default HTTP client.
|
|
var getDefaultHTTPClient = sync.OnceValue(func() *http.Client {
|
|
return newHTTPClient(DefaultTimeout)
|
|
})
|
|
|
|
const (
|
|
transportDialTimeout = 10 * time.Second
|
|
transportKeepAlive = 30 * time.Second
|
|
transportTLSHandshakeTimeout = 10 * time.Second
|
|
transportResponseHeaderTimeout = 30 * time.Second
|
|
transportIdleConnTimeout = 90 * time.Second
|
|
transportMaxIdleConns = 100
|
|
transportMaxIdleConnsPerHost = 10
|
|
)
|
|
|
|
func newHTTPClient(timeout time.Duration) *http.Client {
|
|
// Clone DefaultTransport to inherit ForceAttemptHTTP2, ProxyFromEnvironment,
|
|
// and other standard settings, then override project-specific fields.
|
|
defaultTransport, ok := http.DefaultTransport.(*http.Transport)
|
|
if !ok {
|
|
// Fallback: construct from scratch if DefaultTransport was replaced
|
|
// with a non-*http.Transport implementation.
|
|
return &http.Client{
|
|
Timeout: timeout,
|
|
Transport: &http.Transport{
|
|
Proxy: http.ProxyFromEnvironment,
|
|
DialContext: (&net.Dialer{
|
|
Timeout: transportDialTimeout,
|
|
KeepAlive: transportKeepAlive,
|
|
}).DialContext,
|
|
TLSHandshakeTimeout: transportTLSHandshakeTimeout,
|
|
ResponseHeaderTimeout: transportResponseHeaderTimeout,
|
|
IdleConnTimeout: transportIdleConnTimeout,
|
|
MaxIdleConns: transportMaxIdleConns,
|
|
MaxIdleConnsPerHost: transportMaxIdleConnsPerHost,
|
|
},
|
|
CheckRedirect: enforceSearchRedirectPolicy,
|
|
}
|
|
}
|
|
|
|
clone := defaultTransport.Clone()
|
|
clone.DialContext = (&net.Dialer{
|
|
Timeout: transportDialTimeout,
|
|
KeepAlive: transportKeepAlive,
|
|
}).DialContext
|
|
clone.TLSHandshakeTimeout = transportTLSHandshakeTimeout
|
|
clone.ResponseHeaderTimeout = transportResponseHeaderTimeout
|
|
clone.IdleConnTimeout = transportIdleConnTimeout
|
|
clone.MaxIdleConns = transportMaxIdleConns
|
|
clone.MaxIdleConnsPerHost = transportMaxIdleConnsPerHost
|
|
|
|
return &http.Client{
|
|
Timeout: timeout,
|
|
Transport: clone,
|
|
CheckRedirect: enforceSearchRedirectPolicy,
|
|
}
|
|
}
|
|
|
|
func closeResponseBody(resp *http.Response, logger *slog.Logger) {
|
|
if resp == nil || resp.Body == nil {
|
|
return
|
|
}
|
|
|
|
// Drain any unread data before closing to allow HTTP keep-alive
|
|
// connection reuse. When readBodyWithLimit truncates at
|
|
// MaxResponseBodySize, the unconsumed tail prevents the Go HTTP
|
|
// transport from recycling the connection. A LimitReader bounds
|
|
// the drain to defend against a slow or malicious server.
|
|
//nolint:errcheck // drain is intentionally discarded for keep-alive
|
|
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, int64(MaxResponseBodySize)))
|
|
|
|
err := resp.Body.Close()
|
|
if err != nil {
|
|
logger.Debug("failed to close response body", "error", err)
|
|
}
|
|
}
|