netproxy

package
v0.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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 Direct

func Direct() Proxy

Direct returns the direct-connection policy (the default).

func Parse

func Parse(raw string) (Proxy, error)

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

func (p Proxy) HTTPProxyFunc() func(*http.Request) (*url.URL, error)

HTTPProxyFunc exposes the policy as an http.ProxyFromEnvironment-shaped function, for callers that must set the field themselves (the gorilla WebSocket dialer).

func (Proxy) IsDirect

func (p Proxy) IsDirect() bool

IsDirect reports whether the policy connects directly.

func (Proxy) IsSOCKS

func (p Proxy) IsSOCKS() bool

IsSOCKS reports whether the proxy speaks SOCKS5.

func (Proxy) Scheme

func (p Proxy) Scheme() string

Scheme returns the proxy scheme, or "" when the policy is direct.

func (Proxy) String

func (p Proxy) String() string

String renders the policy for logs and audit events: "direct" or the proxy URL.

func (Proxy) Transport

func (p Proxy) Transport() *http.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.

func (Proxy) URL

func (p Proxy) URL() *url.URL

URL returns the proxy URL, or nil when the policy is direct.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL