Documentation
¶
Overview ¶
Package socks5 is EdgeFetch's in-repo SOCKS5 egress client (RFC 1928 CONNECT with RFC 1929 username/password authentication). It exists so crawl traffic can leave through an operator's SOCKS proxy without weakening the dial-time SSRF guard and without adding `golang.org/x/net/proxy` to `go.mod`.
Why not net/http's built-in SOCKS support ¶
`http.Transport.Proxy` with a `socks5://` URL treats `socks5` exactly like `socks5h`: it always sends the hostname to the proxy. The `net.Dialer.Control` hook (`politeness.RejectNonPublicPeer`) then only sees the proxy's IP, so the target is never checked, and a LAN or loopback proxy would itself be refused. EdgeFetch transports therefore keep `Proxy: nil` and use Dialer.DialContext as their `DialContext` instead.
SSRF model ¶
- `socks5://` resolves the target locally, runs the target policy on each `ip:port`, and sends the proxy the exact IP that passed. The proxy never resolves anything, so there is no rebinding window.
- `socks5h://` sends hostnames to the proxy (no local DNS lookup or leak). IP-literal targets are still checked, and local-only names (`localhost`, `*.local`, `*.internal`, `*.home.arpa`, single-label) are refused, but a public name that resolves to a private address at the proxy cannot be detected by EdgeFetch. Pair `socks5h` with egress filtering on the proxy host, or use `socks5` where SSRF guarantees matter most.
- The proxy address is operator configuration and is dialed without the public-peer check, so it may be on the LAN or loopback. Only SOCKS handshakes are sent to it, and a target equal to its own authority is refused.
Proxy masks credentials in every formatting path (String, GoString, LogValue), and parse errors never echo the input URL.
Index ¶
Constants ¶
const DefaultHandshakeTimeout = 10 * time.Second
DefaultHandshakeTimeout bounds the SOCKS greeting, authentication and CONNECT exchange when the caller's context carries no earlier deadline.
const DefaultPort = "1080"
DefaultPort is the IANA-registered SOCKS port used when a proxy URL omits one.
Variables ¶
var ErrLocalHostname = errors.New("refusing remote-DNS dial to a local-only hostname")
ErrLocalHostname is returned when a `socks5h` dial targets a name that only resolves inside a private network (`localhost`, `*.local`, `*.internal`, `*.home.arpa`, single-label names).
Functions ¶
This section is empty.
Types ¶
type Dialer ¶
type Dialer struct {
// contains filtered or unexported fields
}
Dialer tunnels TCP connections through one SOCKS5 proxy while keeping EdgeFetch's dial-time SSRF guarantees.
The proxy is the TCP peer, so `net.Dialer.Control` would only ever see the proxy's IP. Dialer therefore moves the target check above the proxy:
- `socks5`: resolve the target locally, run TargetControl on each `ip:port`, and send the proxy the exact IP that passed (ATYP IPv4/IPv6). There is no rebinding window, because the proxy never resolves anything.
- `socks5h`: IP-literal targets still pass TargetControl; hostnames are sent as ATYP DOMAINNAME after a local-only-name check. The proxy resolves them, so the final IP is the proxy operator's responsibility.
The only non-public peer Dialer ever connects to is the configured proxy address, and the only bytes it sends there are a SOCKS handshake. A target equal to the proxy's own authority is refused.
func NewDialer ¶
NewDialer returns a Dialer for p. It panics if p is nil (a configuration bug, never request-controlled).
func (*Dialer) DialContext ¶
DialContext connects to address (`host:port`) through the proxy. Its signature matches `http.Transport.DialContext`.
type Options ¶
type Options struct {
// TargetControl validates every target `ip:port` BEFORE it is handed to the proxy. It has the
// `net.Dialer.Control` signature (the RawConn argument is always nil) so the exact hook used for direct dials,
// such as `politeness.RejectNonPublicPeer`, can be reused. Nil means `politeness.RejectNonPublicPeer`.
TargetControl func(network, address string, c syscall.RawConn) error
// AllowLocalHostnames turns off the `socks5h` refusal of local-only hostnames. Use it only with
// `--allow-private`, where LAN targets are allowed on purpose.
AllowLocalHostnames bool
// LookupNetIP resolves target hostnames for `socks5` (local DNS). Nil means `net.DefaultResolver.LookupNetIP`.
LookupNetIP func(ctx context.Context, network, host string) ([]netip.Addr, error)
// ProxyDial opens the TCP connection to the proxy itself. It carries no public-peer check, because the proxy is
// operator configuration and may sit on the LAN or loopback. Nil means a `net.Dialer` with a 10s timeout.
ProxyDial func(ctx context.Context, network, address string) (net.Conn, error)
// HandshakeTimeout bounds the SOCKS exchange. Zero means DefaultHandshakeTimeout.
HandshakeTimeout time.Duration
}
Options tunes a Dialer. The zero value applies the strict public-only target policy.
type Proxy ¶
type Proxy struct {
// contains filtered or unexported fields
}
Proxy is a parsed `socks5://` or `socks5h://` egress proxy endpoint.
Its credentials live in unexported fields and every formatting path (String, GoString, LogValue, Redacted) masks them, so a Proxy can be logged or wrapped into an error without leaking `user:pass`.
func Parse ¶
Parse validates a proxy URL of the form `socks5://[user[:pass]@]host[:port]` or `socks5h://...`.
`socks5` resolves target hostnames locally so EdgeFetch can apply its public-IP SSRF check to the exact address it asks the proxy to reach. `socks5h` sends the hostname to the proxy, which resolves it (no local DNS leak).
Errors never echo the input: `net/url` parse errors repeat the full URL, password included.
func (*Proxy) HasAuth ¶
HasAuth reports whether RFC 1929 username/password authentication is configured.
func (*Proxy) Redacted ¶
Redacted renders the proxy URL with credentials masked: `socks5h://***:***@proxy.lan:1080`. The username is masked too, because operators often put tokens there.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package socks5test provides a minimal in-process RFC 1928 / RFC 1929 SOCKS5 server for tests.
|
Package socks5test provides a minimal in-process RFC 1928 / RFC 1929 SOCKS5 server for tests. |