Help us shape the future of Go! Take the 2026 Go Developer Survey

gosocks4

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: MIT Imports: 7 Imported by: 37

README

gosocks4

A Go library implementing the SOCKS Protocol Version 4 and the SOCKS 4A extension (domain name support).

Installation

go get github.com/go-gost/gosocks4

Usage

Reading a request (server-side)
import "github.com/go-gost/gosocks4"

func handleConnection(conn net.Conn) {
    req, err := gosocks4.ReadRequest(conn)
    if err != nil {
        // handle error
    }

    if req.Cmd == gosocks4.CmdConnect {
        // Dial the target
        target, _ := net.Dial("tcp", req.Addr.String())

        // Reply granted with the bound address
        reply := gosocks4.NewReply(gosocks4.Granted, &gosocks4.Addr{
            Type: gosocks4.AddrIPv4,
            Host: "0.0.0.0",
            Port: 0,
        })
        reply.Write(conn)

        // Relay traffic
        // ...
    }
}
Writing a request (client-side)
req := gosocks4.NewRequest(gosocks4.CmdConnect, &gosocks4.Addr{
    Type: gosocks4.AddrIPv4,
    Host: "93.184.216.34",
    Port: 80,
}, []byte("userid"))

err := req.Write(conn)
SOCKS 4A (domain names)
req := gosocks4.NewRequest(gosocks4.CmdConnect, &gosocks4.Addr{
    Type: gosocks4.AddrDomain,
    Host: "example.com",
    Port: 443,
}, []byte("userid"))

err := req.Write(conn)
Reading a reply (client-side)
reply, err := gosocks4.ReadReply(conn)
switch reply.Code {
case gosocks4.Granted:
    // Connection granted
case gosocks4.Failed, gosocks4.Rejected:
    // Request rejected
case gosocks4.RejectedUserid:
    // Auth failure
}

Types

Addr

SOCKS4 destination address.

type Addr struct {
    Type int      // AddrIPv4 or AddrDomain
    Host string   // IPv4 address or hostname
    Port uint16   // Destination port
}

Methods: Decode(b []byte) error, Encode(b []byte) error, String() string

Request

SOCKS4 CONNECT/BIND request.

type Request struct {
    Cmd    uint8   // CmdConnect or CmdBind
    Addr   *Addr   // Destination address
    Userid []byte  // User identifier
}

Methods: ReadRequest(r io.Reader) (*Request, error), Write(w io.Writer) error, String() string

Reply

SOCKS4 server reply.

type Reply struct {
    Code uint8   // Granted, Failed, Rejected, RejectedUserid
    Addr *Addr   // Bound address
}

Methods: ReadReply(r io.Reader) (*Reply, error), Write(w io.Writer) error, String() string

Constants

Constant Value Description
Ver4 4 Protocol version
CmdConnect 1 CONNECT command
CmdBind 2 BIND command
AddrIPv4 0 IPv4 address type
AddrDomain 1 Domain name type (SOCKS 4A)
Granted 90 Request granted
Failed 91 Request rejected or failed
Rejected 92 Request rejected (identd)
RejectedUserid 93 Request rejected (userid mismatch)

Wire Format

Request:
+----+----+----+----+----+----+----+----+----+----+....+----+
| VN | CD | DSTPORT |      DSTIP        | USERID       |NULL|
+----+----+----+----+----+----+----+----+----+----+....+----+
   1    1      2              4           variable       1

SOCKS 4A: if DSTIP is 0.0.0.x (x != 0), a null-terminated hostname
follows after the userid:

+----+----+----+----+----+----+----+----+----+....+----+
| HOSTNAME                                          |NULL|
+----+----+----+----+----+----+----+----+----+....+----+
              variable                                 1

Reply:
+----+----+----+----+----+----+----+----+
| VN | CD | DSTPORT |      DSTIP        |
+----+----+----+----+----+----+----+----+
   1    1      2              4

Errors

Error Description
ErrBadVersion Invalid protocol version byte
ErrBadAddrType Unknown address type or malformed IP
ErrShortDataLength Input buffer too short
ErrBadCmd Unknown command byte
ErrFieldTooLong Userid or hostname exceeds 255-byte limit

References

Documentation

Overview

SOCKS Protocol Version 4(a) https://www.openssh.com/txt/socks4.protocol https://www.openssh.com/txt/socks4a.protocol

Index

Constants

View Source
const (
	CmdConnect uint8 = 1
	CmdBind          = 2
)
View Source
const (
	AddrIPv4   = 0
	AddrDomain = 1
)
View Source
const (
	Granted        = 90
	Failed         = 91
	Rejected       = 92
	RejectedUserid = 93
)
View Source
const (
	Ver4 = 4
)

Variables

View Source
var (
	ErrBadVersion      = errors.New("Bad version")
	ErrBadAddrType     = errors.New("Bad address type")
	ErrShortDataLength = errors.New("Short data length")
	ErrBadCmd          = errors.New("Bad Command")
	ErrFieldTooLong    = errors.New("Field too long")
)

Functions

This section is empty.

Types

type Addr

type Addr struct {
	Type int
	Host string
	Port uint16
	// contains filtered or unexported fields
}

func (*Addr) Decode

func (addr *Addr) Decode(b []byte) error

func (*Addr) Encode

func (addr *Addr) Encode(b []byte) error

func (*Addr) String

func (addr *Addr) String() string

type Reply

type Reply struct {
	Code uint8
	Addr *Addr
}

+----+----+----+----+----+----+----+----+ | VN | CD | DSTPORT | DSTIP | +----+----+----+----+----+----+----+----+

1    1      2              4

func NewReply

func NewReply(code uint8, addr *Addr) *Reply

func ReadReply

func ReadReply(r io.Reader) (*Reply, error)

func (*Reply) String

func (r *Reply) String() string

func (*Reply) Write

func (r *Reply) Write(w io.Writer) (err error)

type Request

type Request struct {
	Cmd    uint8
	Addr   *Addr
	Userid []byte
}

+----+----+----+----+----+----+----+----+----+----+....+----+ | VN | CD | DSTPORT | DSTIP | USERID |NULL| +----+----+----+----+----+----+----+----+----+----+....+----+

1    1      2              4           variable       1

func NewRequest

func NewRequest(cmd uint8, addr *Addr, userid []byte) *Request

func ReadRequest

func ReadRequest(r io.Reader) (*Request, error)

func (*Request) String

func (r *Request) String() string

func (*Request) Write

func (r *Request) Write(w io.Writer) (err error)

Jump to

Keyboard shortcuts

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