sockd

package
v0.0.0-...-44b4573 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: MPL-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IOTimeout              = 120 * time.Second
	PayloadSizeMask        = 16*1024 - 1
	LenPayloadSize         = 2
	LenDerivedPassword     = 32
	MaxPacketSize          = 64 * 1024
	MagicKeyDerivationInfo = "ss-subkey"
	ProxyDestAddrTypeV4    = 1
	ProxyDestAddrTypeName  = 3
	ProxyDestAddrTypeV6    = 4
	LenProxyConnectRequest = 1 + 1 + 1 + 254 + 2
)

Variables

View Source
var (
	ZeroBytes           [128]byte
	RandSeed            = int(time.Now().UnixNano())
	BlockedReservedCIDR = []net.IPNet{
		{IP: net.IPv4(0, 0, 0, 0), Mask: net.CIDRMask(32, 32)},
		{IP: net.IPv4(10, 0, 0, 0), Mask: net.CIDRMask(8, 32)},
		{IP: net.IPv4(100, 64, 0, 0), Mask: net.CIDRMask(10, 32)},
		{IP: net.IPv4(127, 0, 0, 0), Mask: net.CIDRMask(8, 32)},
		{IP: net.IPv4(169, 254, 0, 0), Mask: net.CIDRMask(16, 32)},
		{IP: net.IPv4(172, 16, 0, 0), Mask: net.CIDRMask(12, 32)},
		{IP: net.IPv4(192, 0, 0, 0), Mask: net.CIDRMask(24, 32)},
		{IP: net.IPv4(192, 0, 2, 0), Mask: net.CIDRMask(24, 32)},
		{IP: net.IPv4(192, 168, 0, 0), Mask: net.CIDRMask(16, 32)},
		{IP: net.IPv4(198, 18, 0, 0), Mask: net.CIDRMask(15, 32)},
		{IP: net.IPv4(198, 51, 100, 0), Mask: net.CIDRMask(24, 32)},
		{IP: net.IPv4(203, 0, 113, 0), Mask: net.CIDRMask(24, 32)},
		{IP: net.IPv4(240, 0, 0, 0), Mask: net.CIDRMask(4, 32)},
	}
	ErrMalformedPacket = errors.New("received a malformed packet")
)

Functions

func AEADBlockCipher

func AEADBlockCipher(preSharedKey, salt []byte) (cipher.AEAD, error)

func CopyWithTimeout

func CopyWithTimeout(destConn net.PacketConn, client net.Addr, srv net.PacketConn) error

func DecryptUDPPacket

func DecryptUDPPacket(n int, buf []byte, derivedPassword []byte) (int, error)

func GetDerivedKey

func GetDerivedKey(password string) []byte

func IncreaseNounce

func IncreaseNounce(nounceBuf []byte)

func IsReservedAddr

func IsReservedAddr(addr net.IP) bool

func ListenPacket

func ListenPacket(network, address string, handleFunc func(net.PacketConn) net.PacketConn) (net.PacketConn, error)

func PipeTCPConnection

func PipeTCPConnection(src, dest net.Conn, doWriteRand bool)

PipeTCPConnection receives data from the first connection and copies the data into the second connection. The function returns after the first connection is closed or other IO error occurs, and before returning the function closes the second connection and optionally writes a random amount of data into the supposedly already terminated first connection.

func RandNum

func RandNum(absMin, variableLower, randMore int) int

func RandomText

func RandomText(length int) string

RandomText returns a string consisting of letters and spaces only.

func ReadWithRetry

func ReadWithRetry(conn net.Conn, buf []byte) (n int, err error)

ReadWithRetry makes at most 3 attempts to read incoming data from the connection. If an IO error occurs, the connection will be closed.

func TestSockd

func TestSockd(sockd *Daemon, t testingstub.T)

func WriteRandomToTCP

func WriteRandomToTCP(conn net.Conn) (totalBytes int)

WriteRandomToTCP writes a random amount of data (up to couple of KB) to the connection.

func WriteRandomToUDP

func WriteRandomToUDP(srv *net.UDPConn, client *net.UDPAddr) (totalBytes int)

func WriteWithRetry

func WriteWithRetry(conn net.Conn, buf []byte) (totalWritten int, err error)

WriteWithRetry divides the data buffer into several portions and makes at most 3 attempts to deliver each portion. If an IO error occurs, the connection will be closed.

Types

type Daemon

type Daemon struct {
	Address    string `json:"Address"`
	Password   string `json:"Password"`
	PerIPLimit int    `json:"PerIPLimit"`
	TCPPorts   []int  `json:"TCPPorts"`
	UDPPorts   []int  `json:"UDPPorts"`

	// DNSDaemon is an initialised DNS daemon. It must not be nil.
	DNSDaemon *dnsd.Daemon `json:"-"`
	// contains filtered or unexported fields
}

Daemon is intentionally undocumented magic ^____^

func (*Daemon) Initialise

func (daemon *Daemon) Initialise() error

func (*Daemon) StartAndBlock

func (daemon *Daemon) StartAndBlock() error

func (*Daemon) Stop

func (daemon *Daemon) Stop()

Stop terminates all TCP and UDP servers.

type EncryptedReader

type EncryptedReader struct {
	io.Reader
	cipher.AEAD
	// contains filtered or unexported fields
}

func NewEncryptedReader

func NewEncryptedReader(reader io.Reader, blockCipher cipher.AEAD) *EncryptedReader

func (*EncryptedReader) Read

func (reader *EncryptedReader) Read(buf []byte) (int, error)

func (*EncryptedReader) WriteTo

func (reader *EncryptedReader) WriteTo(writer io.Writer) (n int64, err error)

type EncryptedTCPConn

type EncryptedTCPConn struct {
	net.Conn
	DerivedPassword []byte
	// contains filtered or unexported fields
}

func (*EncryptedTCPConn) Initialise

func (conn *EncryptedTCPConn) Initialise() error

func (*EncryptedTCPConn) InitialiseWriter

func (conn *EncryptedTCPConn) InitialiseWriter() error

func (*EncryptedTCPConn) Read

func (conn *EncryptedTCPConn) Read(buf []byte) (int, error)

func (*EncryptedTCPConn) ReadFrom

func (conn *EncryptedTCPConn) ReadFrom(reader io.Reader) (int64, error)

func (*EncryptedTCPConn) Write

func (conn *EncryptedTCPConn) Write(buf []byte) (int, error)

func (*EncryptedTCPConn) WriteTo

func (conn *EncryptedTCPConn) WriteTo(writer io.Writer) (int64, error)

type EncryptedUDPConn

type EncryptedUDPConn struct {
	net.PacketConn
	DerivedPassword []byte
	sync.Mutex
	// contains filtered or unexported fields
}

func (*EncryptedUDPConn) ReadFrom

func (encConn *EncryptedUDPConn) ReadFrom(buf []byte) (int, net.Addr, error)

func (*EncryptedUDPConn) WriteTo

func (encConn *EncryptedUDPConn) WriteTo(buf []byte, client net.Addr) (int, error)

type EncryptedWriter

type EncryptedWriter struct {
	io.Writer
	cipher.AEAD
	// contains filtered or unexported fields
}

func NewEncryptedWriter

func NewEncryptedWriter(writer io.Writer, blockCipher cipher.AEAD) *EncryptedWriter

func (*EncryptedWriter) ReadFrom

func (writer *EncryptedWriter) ReadFrom(reader io.Reader) (n int64, err error)

func (*EncryptedWriter) Write

func (writer *EncryptedWriter) Write(buf []byte) (int, error)

type SocksDestAddr

type SocksDestAddr []byte

func GetSocksAddr

func GetSocksAddr(netAddr net.Addr) SocksDestAddr

func ParseDestAddr

func ParseDestAddr(buf []byte) SocksDestAddr

func ReadProxyDestAddr

func ReadProxyDestAddr(client io.Reader, destWithPort []byte) (addr SocksDestAddr, err error)

func (SocksDestAddr) HostPort

func (addr SocksDestAddr) HostPort() (nameOrIP string, port int)

type TCPDaemon

type TCPDaemon struct {
	Address    string `json:"Address"`
	Password   string `json:"Password"`
	PerIPLimit int    `json:"PerIPLimit"`
	TCPPort    int    `json:"TCPPort"`

	DNSDaemon *dnsd.Daemon `json:"-"` // it is assumed to be already initialised
	// contains filtered or unexported fields
}

func (*TCPDaemon) GetTCPStatsCollector

func (daemon *TCPDaemon) GetTCPStatsCollector() *misc.Stats

func (*TCPDaemon) HandleTCPConnection

func (daemon *TCPDaemon) HandleTCPConnection(logger *lalog.Logger, ip string, client *net.TCPConn)

func (*TCPDaemon) Initialise

func (daemon *TCPDaemon) Initialise() error

func (*TCPDaemon) StartAndBlock

func (daemon *TCPDaemon) StartAndBlock() error

func (*TCPDaemon) Stop

func (daemon *TCPDaemon) Stop()

type UDPBacklog

type UDPBacklog struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*UDPBacklog) Add

func (backlog *UDPBacklog) Add(client net.Addr, dest, srv net.PacketConn)

func (*UDPBacklog) Delete

func (backlog *UDPBacklog) Delete(addr string) net.PacketConn

func (*UDPBacklog) Get

func (backlog *UDPBacklog) Get(addr string) net.PacketConn

type UDPDaemon

type UDPDaemon struct {
	Address    string
	Password   string
	PerIPLimit int
	UDPPort    int

	DNSDaemon *dnsd.Daemon
	// contains filtered or unexported fields
}

func (*UDPDaemon) GetUDPStatsCollector

func (daemon *UDPDaemon) GetUDPStatsCollector() *misc.Stats

func (*UDPDaemon) HandleUDPClient

func (daemon *UDPDaemon) HandleUDPClient(logger *lalog.Logger, ip string, client *net.UDPAddr, packet []byte, srv *net.UDPConn)

func (*UDPDaemon) Initialise

func (daemon *UDPDaemon) Initialise() error

func (*UDPDaemon) StartAndBlock

func (daemon *UDPDaemon) StartAndBlock() error

func (*UDPDaemon) Stop

func (daemon *UDPDaemon) Stop()

func (*UDPDaemon) WriteRand

func (daemon *UDPDaemon) WriteRand(server net.PacketConn, dest net.Addr)

Jump to

Keyboard shortcuts

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