socks5

package
v2.8.48 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2026 License: MIT Imports: 13 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 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

View Source
const DefaultHandshakeTimeout = 10 * time.Second

DefaultHandshakeTimeout bounds the SOCKS greeting, authentication and CONNECT exchange when the caller's context carries no earlier deadline.

View Source
const DefaultPort = "1080"

DefaultPort is the IANA-registered SOCKS port used when a proxy URL omits one.

Variables

View Source
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

func NewDialer(p *Proxy, opts Options) *Dialer

NewDialer returns a Dialer for p. It panics if p is nil (a configuration bug, never request-controlled).

func (*Dialer) DialContext

func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error)

DialContext connects to address (`host:port`) through the proxy. Its signature matches `http.Transport.DialContext`.

func (*Dialer) Proxy

func (d *Dialer) Proxy() *Proxy

Proxy returns the proxy this Dialer tunnels through.

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

func Parse(raw string) (*Proxy, error)

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) Addr

func (p *Proxy) Addr() string

Addr returns the proxy's own `host:port`.

func (Proxy) GoString

func (p Proxy) GoString() string

GoString keeps `%#v` from dumping the unexported credential fields.

func (*Proxy) HasAuth

func (p *Proxy) HasAuth() bool

HasAuth reports whether RFC 1929 username/password authentication is configured.

func (Proxy) LogValue

func (p Proxy) LogValue() slog.Value

LogValue implements slog.LogValuer with the redacted form.

func (*Proxy) Redacted

func (p *Proxy) Redacted() string

Redacted renders the proxy URL with credentials masked: `socks5h://***:***@proxy.lan:1080`. The username is masked too, because operators often put tokens there.

func (*Proxy) RemoteDNS

func (p *Proxy) RemoteDNS() bool

RemoteDNS reports whether the proxy resolves target hostnames (`socks5h`).

func (Proxy) Scheme

func (p Proxy) Scheme() string

Scheme returns `socks5h` or `socks5`.

func (Proxy) String

func (p Proxy) String() string

String implements fmt.Stringer with the redacted form. Value receivers cover both `Proxy` and `*Proxy`, so a dereferenced copy formatted with `%v` / `%+v` / `%#v` is masked too.

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.

Jump to

Keyboard shortcuts

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