network

package
v0.0.0-...-7938e08 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AddrIPv4   uint8 = 1
	AddrDomain       = 3
	AddrIPv6         = 4
)

Address types

View Source
const (
	SOCKS5Version     = 5
	SOCKS5UserPassVer = 1
)

SOCKS5 Version = 5

View Source
const (
	MethodNoAuth uint8 = iota
	MethodGSSAPI
	MethodUserPass
	MethodNoAcceptable uint8 = 0xFF
)

Methods

View Source
const (
	SOCKS5CmdConnect uint8 = iota + 1
	SOCKS5CmdBind
	SOCKS5CmdUDP
	SOCKS5CmdUDPOverTCP
)

SOCKS5 Commands

View Source
const (
	SOCKS5RespSucceeded uint8 = iota
	SOCKS5RespFailure
	SOCKS5RespAllowed
	SOCKS5RespNetUnreachable
	SOCKS5RespHostUnreachable
	SOCKS5RespConnRefused
	SOCKS5RespTTLExpired
	SOCKS5RespCmdUnsupported
	SOCKS5RespAddrUnsupported
)

SOCKS5 Response codes

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 CopyBuffer

func CopyBuffer(dst io.Writer, src io.Reader, bufSize int) error

func DoHttp

func DoHttp(req *http.Request) (*http.Response, error)

DoHttp the basic of http request ability

func ErrorResponse

func ErrorResponse(message string, statusCode int, w http.ResponseWriter)

func GetBuffer

func GetBuffer(size int) []byte

GetBuffer returns a buffer of specified size.

func GetUrl

func GetUrl(req *http.Request) string

GetUrl 从 req 类中获取完整 URL信息

func HealthResponse

func HealthResponse(message string, w http.ResponseWriter)

HealthResponse wrapper a response with plain message

func HttpBasicAuth

func HttpBasicAuth(auth string, verify func(string, string) bool) bool

func IsIPv4

func IsIPv4(address string) bool

func PutBuffer

func PutBuffer(b []byte)

func ReadMethods

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

ReadMethods returns methods Method selection

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

func StrEQ

func StrEQ(s1, s2 string) bool

StrEQ returns whether s1 and s2 are equal

func StrInSlice

func StrInSlice(str string, slice []string) bool

StrInSlice return whether str in slice

func Transport

func Transport(rw1, rw2 io.ReadWriter) error

Transport rw1 and rw2

func UnGzip

func UnGzip(body []byte) []byte

UnGzip do UnGzip

func VerifyByHtpasswd

func VerifyByHtpasswd(users string) func(string, string) bool

VerifyByHtpasswd returns a verifier that verify by a htpasswd file

func VerifyByMap

func VerifyByMap(users map[string]string) func(string, string) bool

VerifyByMap returns an verifier that verify by an username-password map

func WriteMethod

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

WriteMethod send the selected method to the client

func WriteMethods

func WriteMethods(methods []uint8, w io.Writer) error

WriteMethods send method select request to the server

Types

type Addr

type Addr struct {
	Type uint8
	Host string
	Port uint16
}

Addr has following struct

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

func NewAddr

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

NewAddr creates an address object

func NewAddrFromAddr

func NewAddrFromAddr(ln, conn net.Addr) (addr *Addr, err error)

NewAddrFromAddr creates an address object

func NewAddrFromPair

func NewAddrFromPair(host string, port int) (addr *Addr)

NewAddrFromPair creates an address object from host and port pair

func (*Addr) Decode

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

Decode an address from the stream

func (*Addr) Encode

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

Encode an address to the stream

func (*Addr) Length

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

Length of the address

func (*Addr) String

func (addr *Addr) String() string

type BufferedConn

type BufferedConn struct {
	net.Conn
	Br *bufio.Reader
}

func (*BufferedConn) Peek

func (c *BufferedConn) Peek(n int) ([]byte, error)

func (*BufferedConn) Read

func (c *BufferedConn) Read(b []byte) (int, error)

type ListenConfig

type ListenConfig struct {
	net.ListenConfig
}

type Reply

type Reply struct {
	Rep  uint8
	Addr *Addr
}

Reply is a 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

NewReply creates a socks5 reply

func ReadReply

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

ReadReply reads a reply from the stream

func (*Reply) String

func (r *Reply) String() string

func (*Reply) Write

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

type SOCKS5Request

type SOCKS5Request struct {
	Cmd  uint8
	Addr *Addr
}

SOCKS5Request represent a socks5 request The SOCKSv5 request

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

func NewSOCKS5Request

func NewSOCKS5Request(cmd uint8, addr *Addr) *SOCKS5Request

NewSOCKS5Request creates an request object

func ReadSOCKS5Request

func ReadSOCKS5Request(r io.Reader) (*SOCKS5Request, error)

ReadSOCKS5Request reads request from the stream

func (*SOCKS5Request) String

func (r *SOCKS5Request) String() string

func (*SOCKS5Request) Write

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

type UDPDatagram

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

UDPDatagram represent an UDP request

func NewUDPDatagram

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

NewUDPDatagram creates an UDPDatagram

func ReadUDPDatagram

func ReadUDPDatagram(r io.Reader) (*UDPDatagram, error)

ReadUDPDatagram reads an UDPDatagram from the stream

func (*UDPDatagram) Write

func (d *UDPDatagram) Write(w io.Writer) error

type UDPHeader

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

UDPHeader is the header of an 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

NewUDPHeader creates an UDPHeader

func (*UDPHeader) String

func (h *UDPHeader) String() string

func (*UDPHeader) Write

func (h *UDPHeader) Write(w io.Writer) error

type UserPassRequest

type UserPassRequest struct {
	Version  byte
	Username string
	Password string
}

UserPassRequest 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
}

UserPassResponse 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

Jump to

Keyboard shortcuts

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