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

gosocks5

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: MIT Imports: 8 Imported by: 69

README

gosocks5

A zero-dependency SOCKS5 protocol library for Go, implementing RFC 1928 and RFC 1929 (username/password authentication).

Usage

Server
package main

import (
    "log"
    "net"

    "github.com/go-gost/gosocks5/server"
)

func main() {
    ln, err := net.Listen("tcp", ":1080")
    if err != nil {
        log.Fatal(err)
    }
    srv := &server.Server{Listener: ln}
    log.Fatal(srv.Serve(server.DefaultHandler))
}

With authentication:

import "net/url"

user, _ := url.Parse("http://user:pass@host")
users := []*url.Userinfo{user.User}
selector := server.NewServerSelector(users, gosocks5.MethodUserPass)

handler := &server.ServerHandler{Selector: selector}
srv.Serve(handler)
Client
import (
    "github.com/go-gost/gosocks5"
    "github.com/go-gost/gosocks5/client"
)

conn, err := client.Dial("server:1080")
if err != nil {
    log.Fatal(err)
}
defer conn.Close()

addr, _ := gosocks5.NewAddr("example.com:80")
req := gosocks5.NewRequest(gosocks5.CmdConnect, addr)
req.Write(conn)

reply, _ := gosocks5.ReadReply(conn)
// read/write conn as a normal net.Conn from here

With authentication and timeout:

import "net/url"

user, _ := url.Parse("http://user:pass@host")
selector := client.NewClientSelector(user.User, gosocks5.MethodUserPass)

conn, err := client.Dial("server:1080",
    client.SelectorDialOption(selector),
    client.TimeoutDialOption(30*time.Second),
)

Supported auth methods

Method Constant
No authentication MethodNoAuth
GSSAPI (reserved) MethodGSSAPI
Username/password MethodUserPass

License

MIT

Documentation

Overview

SOCKS Protocol Version 5 http://tools.ietf.org/html/rfc1928 http://tools.ietf.org/html/rfc1929

Index

Constants

View Source
const (
	Ver5        = 5
	UserPassVer = 1
)
View Source
const (
	MethodNoAuth uint8 = iota
	MethodGSSAPI
	MethodUserPass
	// X'03' to X'7F' IANA ASSIGNED
	// X'80' to X'FE' RESERVED FOR PRIVATE METHODS
	MethodNoAcceptable = 0xFF
)
View Source
const (
	CmdConnect uint8 = 1
	CmdBind    uint8 = 2
	CmdUdp     uint8 = 3
)
View Source
const (
	AddrIPv4   uint8 = 1
	AddrDomain uint8 = 3
	AddrIPv6   uint8 = 4
)
View Source
const (
	Succeeded uint8 = iota
	Failure
	NotAllowed
	NetUnreachable
	HostUnreachable
	ConnRefused
	TTLExpired
	CmdUnsupported
	AddrUnsupported
)

Variables

View Source
var (
	ErrBadVersion  = errors.New("bad version")
	ErrBadFormat   = errors.New("bad format")
	ErrBadAddrType = errors.New("bad address type")
	ErrShortBuffer = errors.New("short buffer")
	ErrBadMethod   = errors.New("bad method")
	ErrAuthFailure = errors.New("auth failure")
)

Functions

func ReadMethods

func ReadMethods(r io.Reader) ([]uint8, error)

Method selection

+----+----------+----------+
|VER | NMETHODS | METHODS  |
+----+----------+----------+
| 1  |    1     | 1 to 255 |
+----+----------+----------+

func WriteMethod

func WriteMethod(method uint8, w io.Writer) error

Types

type Addr

type Addr struct {
	Type uint8
	Host string
	Port uint16
}

Address

+------+----------+----------+
| ATYP |   ADDR   |   PORT   |
+------+----------+----------+
|  1   | Variable |    2     |
+------+----------+----------+

func NewAddr

func NewAddr(sa string) (addr *Addr, err error)

func (*Addr) Decode

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

func (*Addr) Encode

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

func (*Addr) Length

func (addr *Addr) Length() (n int)

func (*Addr) ParseFrom added in v0.3.1

func (addr *Addr) ParseFrom(saddr string) error

func (*Addr) ReadFrom added in v0.3.0

func (addr *Addr) ReadFrom(r io.Reader) (n int64, err error)

func (*Addr) String

func (addr *Addr) String() string

func (*Addr) WriteTo added in v0.3.0

func (addr *Addr) WriteTo(w io.Writer) (int64, error)

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

func ClientConn

func ClientConn(conn net.Conn, selector Selector) *Conn

func ServerConn

func ServerConn(conn net.Conn, selector Selector) *Conn

func (*Conn) Close

func (conn *Conn) Close() error

func (*Conn) Handleshake

func (conn *Conn) Handleshake() error

func (*Conn) ID added in v0.3.1

func (conn *Conn) ID() string

func (*Conn) LocalAddr

func (conn *Conn) LocalAddr() net.Addr

func (*Conn) Read

func (conn *Conn) Read(b []byte) (n int, err error)

func (*Conn) RemoteAddr

func (conn *Conn) RemoteAddr() net.Addr

func (*Conn) SetDeadline

func (conn *Conn) SetDeadline(t time.Time) error

func (*Conn) SetReadDeadline

func (conn *Conn) SetReadDeadline(t time.Time) error

func (*Conn) SetWriteDeadline

func (conn *Conn) SetWriteDeadline(t time.Time) error

func (*Conn) Write

func (conn *Conn) Write(b []byte) (n int, err error)

type Reply

type Reply struct {
	Rep  uint8
	Addr *Addr
}

The SOCKSv5 reply

+----+-----+-------+------+----------+----------+
|VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
+----+-----+-------+------+----------+----------+
| 1  |  1  | X'00' |  1   | Variable |    2     |
+----+-----+-------+------+----------+----------+

func NewReply

func NewReply(rep 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
}

The SOCKSv5 request

+----+-----+-------+------+----------+----------+
|VER | CMD |  RSV  | ATYP | DST.ADDR | DST.PORT |
+----+-----+-------+------+----------+----------+
| 1  |  1  | X'00' |  1   | Variable |    2     |
+----+-----+-------+------+----------+----------+

func NewRequest

func NewRequest(cmd uint8, addr *Addr) *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)

type Selector

type Selector interface {
	// return supported methods
	Methods() []uint8
	// select method
	Select(methods ...uint8) (method uint8)
	// on method selected
	OnSelected(method uint8, conn net.Conn) (string, net.Conn, error)
}

type UDPDatagram

type UDPDatagram struct {
	Header *UDPHeader
	Data   []byte
}

func NewUDPDatagram

func NewUDPDatagram(header *UDPHeader, data []byte) *UDPDatagram

func (*UDPDatagram) ReadFrom added in v0.3.1

func (d *UDPDatagram) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom reads UDP datagram from r.

func (*UDPDatagram) WriteTo added in v0.3.1

func (d *UDPDatagram) WriteTo(w io.Writer) (n int64, err error)

type UDPHeader

type UDPHeader struct {
	Rsv  uint16
	Frag uint8
	Addr *Addr
}

UDP request

+----+------+------+----------+----------+----------+
|RSV | FRAG | ATYP | DST.ADDR | DST.PORT |   DATA   |
+----+------+------+----------+----------+----------+
| 2  |  1   |  1   | Variable |    2     | Variable |
+----+------+------+----------+----------+----------+

func NewUDPHeader

func NewUDPHeader(rsv uint16, frag uint8, addr *Addr) *UDPHeader

func (*UDPHeader) ReadFrom added in v0.3.1

func (h *UDPHeader) ReadFrom(r io.Reader) (n int64, err error)

func (*UDPHeader) String

func (h *UDPHeader) String() string

func (*UDPHeader) WriteTo added in v0.3.1

func (h *UDPHeader) WriteTo(w io.Writer) (int64, error)

type UserPassRequest

type UserPassRequest struct {
	Version  byte
	Username string
	Password string
}

Username/Password authentication request

+----+------+----------+------+----------+
|VER | ULEN |  UNAME   | PLEN |  PASSWD  |
+----+------+----------+------+----------+
| 1  |  1   | 1 to 255 |  1   | 1 to 255 |
+----+------+----------+------+----------+

func NewUserPassRequest

func NewUserPassRequest(ver byte, u, p string) *UserPassRequest

func ReadUserPassRequest

func ReadUserPassRequest(r io.Reader) (*UserPassRequest, error)

func (*UserPassRequest) String

func (req *UserPassRequest) String() string

func (*UserPassRequest) Write

func (req *UserPassRequest) Write(w io.Writer) error

type UserPassResponse

type UserPassResponse struct {
	Version byte
	Status  byte
}

Username/Password authentication response

+----+--------+
|VER | STATUS |
+----+--------+
| 1  |   1    |
+----+--------+

func NewUserPassResponse

func NewUserPassResponse(ver, status byte) *UserPassResponse

func ReadUserPassResponse

func ReadUserPassResponse(r io.Reader) (*UserPassResponse, error)

func (*UserPassResponse) String

func (res *UserPassResponse) String() string

func (*UserPassResponse) Write

func (res *UserPassResponse) Write(w io.Writer) error

Directories

Path Synopsis
examples
client command
server command

Jump to

Keyboard shortcuts

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