Documentation
¶
Overview ¶
Package proxy connects through SOCKS5, SOCKS4 and HTTP CONNECT proxies, bounded by the caller's context, and carries UDP datagrams through SOCKS5 UDP associations (Association).
Index ¶
- Constants
- Variables
- func SOCKS4(ctx context.Context, conn net.Conn, req []byte) error
- func SOCKS4Request(target, userID string, hostnameIP [4]byte) ([]byte, error)
- func SOCKS5(ctx context.Context, conn net.Conn, target string, methods []byte, ...) error
- func UDPAssociate(ctx context.Context, conn net.Conn, methods []byte, user *url.Userinfo) (string, error)
- type Answer
- type Association
- func (a *Association) Close() error
- func (a *Association) LocalAddr() net.Addr
- func (a *Association) ReadFrom(p []byte) (int, net.Addr, error)
- func (a *Association) SetDeadline(t time.Time) error
- func (a *Association) SetReadDeadline(t time.Time) error
- func (a *Association) SetWriteDeadline(t time.Time) error
- func (a *Association) WriteTo(b []byte, _ net.Addr) (int, error)
- type RefusedError
Constants ¶
const ( MethodNone = 0 MethodUser = 2 )
Variables ¶
var ErrAnswerTooLarge = errors.New("the proxy's answer header is longer than 64 KiB")
ErrAnswerTooLarge is returned for a CONNECT answer whose header is longer than 64 KiB.
var ErrAssociationEnded = errors.New("the SOCKS5 proxy ended the UDP association: its control connection closed")
ErrAssociationEnded is the error of an Association's reads and writes once its control connection has closed or failed: the proxy has dropped the association (RFC 1928, section 7).
Functions ¶
func SOCKS4 ¶ added in v0.5.0
SOCKS4 sends req, a SOCKS4Request, to a SOCKS4 proxy over conn and reads its answer: nil once the proxy grants the connection.
func SOCKS4Request ¶ added in v0.5.0
SOCKS4Request is the CONNECT request for target ("host:port") with userID in USERID: VN, CD, DSTPORT, DSTIP, USERID and NUL. An IPv4 address goes out as DSTIP; a host name as SOCKS4A, DSTIP hostnameIP (0.0.0.x, the browser's) and the name and NUL after USERID, for the proxy to resolve. SOCKS4 carries no IPv6 address, and NUL ends its fields: either is an error, found before anything is dialled.
func SOCKS5 ¶
func SOCKS5(ctx context.Context, conn net.Conn, target string, methods []byte, user *url.Userinfo) error
SOCKS5 asks a SOCKS5 proxy over conn for a connection to target, offering methods in order; host names are resolved by the proxy. user answers username/password authentication.
func UDPAssociate ¶
func UDPAssociate(ctx context.Context, conn net.Conn, methods []byte, user *url.Userinfo) (string, error)
UDPAssociate asks a SOCKS5 proxy over conn, which becomes the association's control connection, for a UDP association (RFC 1928, section 7), offering methods and authenticating as SOCKS5 does. It returns the relay's address as the proxy gives it ("host:port"); an unspecified IP there is the proxy's own address, which the caller substitutes. A refusal is a *RefusedError.
Types ¶
type Answer ¶
type Answer struct {
StatusCode int
// Status is the status line's code and text ("407 Proxy
// Authentication Required").
Status string
Header http.Header
// Close reports that the proxy closes the connection after it.
Close bool
}
Answer is a proxy's response to CONNECT, its body read.
func Connect ¶
func Connect(ctx context.Context, conn net.Conn, target string, header []string) (Answer, net.Conn, error)
Connect asks an HTTP proxy over conn for a tunnel to target ("host:port") with header lines sent as given. On 2xx the returned conn is the tunnel; otherwise conn stays usable unless the answer closes it.
type Association ¶
type Association struct {
// contains filtered or unexported fields
}
Association is a SOCKS5 UDP association carrying one connection's datagrams to one target. It is a net.PacketConn: a write goes to the target whatever address it names, behind the SOCKS5 UDP header, and a read is a datagram from the relay without it, reported from the peer address given to NewAssociation. Its socket is connected to the relay, so nothing it sends can go anywhere else. Fragments (FRAG other than 0) and malformed datagrams are dropped. It ends with its control connection: once that closes or fails, reads and writes fail with ErrAssociationEnded. Close closes both.
func NewAssociation ¶
func NewAssociation(ctrl net.Conn, udp *net.UDPConn, host string, port uint16, peer netip.AddrPort) (*Association, error)
NewAssociation starts an association over udp, a socket connected to the relay, for the target host at port: an IP address goes as one, a host name as one for the proxy to resolve. ctrl is the control connection UDPAssociate set it up on; peer is the source address reads report.
func (*Association) Close ¶
func (a *Association) Close() error
Close ends the association: its control connection and its socket.
func (*Association) LocalAddr ¶
func (a *Association) LocalAddr() net.Addr
LocalAddr is the socket's local address.
func (*Association) SetDeadline ¶
func (a *Association) SetDeadline(t time.Time) error
SetDeadline sets the socket's read and write deadlines.
func (*Association) SetReadDeadline ¶
func (a *Association) SetReadDeadline(t time.Time) error
SetReadDeadline sets the socket's read deadline.
func (*Association) SetWriteDeadline ¶
func (a *Association) SetWriteDeadline(t time.Time) error
SetWriteDeadline sets the socket's write deadline.
type RefusedError ¶
type RefusedError struct {
// UDP: the refused request was UDP ASSOCIATE.
UDP bool
// Reply is the reply code (RFC 1928, section 6).
Reply byte
}
RefusedError is a SOCKS5 proxy's refusal of a request: a reply other than "succeeded" to CONNECT or UDP ASSOCIATE.
func (*RefusedError) Error ¶
func (e *RefusedError) Error() string