Documentation
¶
Overview ¶
Package netproxy resolves how jms reaches the network: directly, or through an explicit HTTP CONNECT / SOCKS5 proxy.
Design contract (DESIGN.md「代理策略」): jms defaults to a DIRECT connection and never consults HTTP_PROXY / HTTPS_PROXY / NO_PROXY. The environment-variable convention is the wrong default for a bastion client: a developer machine running a transparent-proxy tool (Clash, Surge, corporate MITM) exports those variables globally, and the tool's own DIRECT rules then apply to the bastion address. The result is a request that leaves the process through a proxy nobody asked for, with the proxy's own error (typically an empty 502) standing in for the real network result.
The failure this prevents is subtle and worth stating plainly: because only the REST client used to inherit http.DefaultTransport, the TCP probe dialled directly while the login went through the proxy. Probe and login measured two different networks, so the failover policy in 「总体架构」 reasoned about the wrong one. Every transport now shares one policy, resolved here.
Dependency direction: netproxy depends on nothing else in this module, and adds no third-party dependency: the SOCKS5 and HTTP CONNECT handshakes are small enough to own outright.
Index ¶
- type Proxy
- func (p Proxy) DialContext(timeout time.Duration) func(ctx context.Context, network, addr string) (net.Conn, error)
- func (p Proxy) HTTPProxyFunc() func(*http.Request) (*url.URL, error)
- func (p Proxy) IsDirect() bool
- func (p Proxy) IsSOCKS() bool
- func (p Proxy) Scheme() string
- func (p Proxy) String() string
- func (p Proxy) Transport() *http.Transport
- func (p Proxy) URL() *url.URL
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Proxy ¶
type Proxy struct {
// contains filtered or unexported fields
}
Proxy is a resolved proxy policy: either direct, or one explicit proxy URL. The zero value is direct, which is the default.
func Parse ¶
Parse resolves a configured proxy string into a policy.
An empty (or whitespace-only) string is the direct policy — the same value as Direct(). The words "direct", "none" and "off" are also accepted so a per-server entry can explicitly opt out of a global proxy: "inherit the global default" and "force a direct connection" are different intentions and must not collapse into the same syntax.
Accepted schemes are http, https and socks5; anything else is rejected rather than silently ignored, because a typo that silently disables the proxy is exactly the bug this package exists to prevent.
func (Proxy) DialContext ¶
func (p Proxy) DialContext(timeout time.Duration) func(ctx context.Context, network, addr string) (net.Conn, error)
DialContext returns a dial function bound to the policy.
For a direct policy it is net.Dialer.DialContext, so behaviour is byte-for-byte what it was before this package existed. For a SOCKS5 proxy it routes through the proxy; for an HTTP proxy it issues a CONNECT and returns the tunnelled connection.
The returned function never performs an environment lookup: the policy is captured at construction time.
func (Proxy) HTTPProxyFunc ¶
HTTPProxyFunc exposes the policy as an http.ProxyFromEnvironment-shaped function, for callers that must set the field themselves (the gorilla WebSocket dialer).
func (Proxy) String ¶
String renders the policy for logs and audit events: "direct" or the proxy URL.
func (Proxy) Transport ¶
Transport returns an *http.Transport that honours the policy.
It is built from a clone of http.DefaultTransport so the tuned defaults (connection pooling, HTTP/2, sane timeouts) are kept, then the Proxy field is set EXPLICITLY: nil for a direct policy. That explicit nil is the whole point — leaving the field unset is what made the REST client inherit ProxyFromEnvironment in the first place.