gotcp

package module
v0.0.0-...-cce5b70 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2020 License: MIT Imports: 15 Imported by: 0

README

gotcp

A Go package for quickly building tcp servers

Usage

###Install

go get github.com/gansidui/gotcp

###Examples

Document

Doc

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConnClosing   = errors.New("use of closed network connection")
	ErrWriteBlocking = errors.New("write packet was blocking")
	ErrReadBlocking  = errors.New("read packet was blocking")
)

Error type

View Source
var (
	ErrInvalidUpstream = errors.New("upstream connection address not trusted for PROXY information")
)
View Source
var PROXY_HEADERS = []string{"X-Forwarded-For", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR"}

Functions

This section is empty.

Types

type Config

type Config struct {
	Recover                bool   // print fatal stack, helpfull for debug
	PacketSendChanLimit    uint32 // the limit of packet send channel
	PacketReceiveChanLimit uint32 // the limit of packet receive channel
}

type Conn

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

Conn exposes a set of callbacks for the various events that occur on a connection

func (*Conn) AsyncWritePacket

func (c *Conn) AsyncWritePacket(p Packet, timeout time.Duration) (err error)

AsyncWritePacket async writes a packet, this method will never block

func (*Conn) Close

func (c *Conn) Close()

Close closes the connection

func (*Conn) Do

func (c *Conn) Do()

Do it

func (*Conn) GetClientAddr

func (c *Conn) GetClientAddr() string

GetClientAddr returns the raw net.TCPConn peer client address

func (*Conn) GetExtraData

func (c *Conn) GetExtraData() interface{}

GetExtraData gets the extra data from the Conn

func (*Conn) GetRawConn

func (c *Conn) GetRawConn() net.Conn

GetRawConn returns the raw net.TCPConn from the Conn

func (*Conn) IsClosed

func (c *Conn) IsClosed() bool

IsClosed indicates whether or not the connection is closed

func (*Conn) PutExtraData

func (c *Conn) PutExtraData(data interface{})

PutExtraData puts the extra data with the Conn

func (*Conn) WritePacket

func (c *Conn) WritePacket(p Packet) (err error)

type ConnCallback

type ConnCallback interface {
	// OnConnect is called when the connection was accepted,
	// If the return value of false is closed
	OnConnect(*Conn) bool

	// OnMessage is called when the connection receives a packet,
	// If the return value of false is closed
	OnMessage(*Conn, Packet) bool

	// OnClose is called when the connection closed
	OnClose(*Conn)
}

ConnCallback is an interface of methods that are used as callbacks on a connection

type Packet

type Packet interface {
	Serialize() []byte
}

type Protocol

type Protocol interface {
	ReadPacket(conn net.Conn) (Packet, error)
}

type ProxyConn

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

Conn is used to wrap and underlying connection which may be speaking the Proxy Protocol. If it is, the RemoteAddr() will return the address of the client instead of the proxy address.

func NewConn

func NewConn(conn net.Conn, timeout time.Duration) *ProxyConn

NewConn is used to wrap a net.Conn that may be speaking the proxy protocol into a proxyproto.Conn

func (*ProxyConn) Close

func (p *ProxyConn) Close() error

func (*ProxyConn) LocalAddr

func (p *ProxyConn) LocalAddr() net.Addr

func (*ProxyConn) Read

func (p *ProxyConn) Read(b []byte) (int, error)

Read is check for the proxy protocol header when doing the initial scan. If there is an error parsing the header, it is returned and the socket is closed.

func (*ProxyConn) RemoteAddr

func (p *ProxyConn) RemoteAddr() net.Addr

RemoteAddr returns the address of the client if the proxy protocol is being used, otherwise just returns the address of the socket peer. If there is an error parsing the header, the address of the client is not returned, and the socket is closed. Once implication of this is that the call could block if the client is slow. Using a Deadline is recommended if this is called before Read()

func (*ProxyConn) SetDeadline

func (p *ProxyConn) SetDeadline(t time.Time) error

func (*ProxyConn) SetReadDeadline

func (p *ProxyConn) SetReadDeadline(t time.Time) error

func (*ProxyConn) SetWriteDeadline

func (p *ProxyConn) SetWriteDeadline(t time.Time) error

func (*ProxyConn) Write

func (p *ProxyConn) Write(b []byte) (int, error)

type ProxyListener

type ProxyListener struct {
	Listener           *net.TCPListener
	ProxyHeaderTimeout time.Duration
	SourceCheck        SourceChecker
}

Listener is used to wrap an underlying listener, whose connections may be using the HAProxy Proxy Protocol (version 1). If the connection is using the protocol, the RemoteAddr() will return the correct client address.

Optionally define ProxyHeaderTimeout to set a maximum time to receive the Proxy Protocol Header. Zero means no timeout.

func (*ProxyListener) AcceptTCP

func (p *ProxyListener) AcceptTCP() (net.Conn, error)

Accept waits for and returns the next connection to the listener.

func (*ProxyListener) Addr

func (p *ProxyListener) Addr() net.Addr

Addr returns the underlying listener's network address.

func (*ProxyListener) Close

func (p *ProxyListener) Close() error

Close closes the underlying listener.

func (*ProxyListener) SetDeadline

func (p *ProxyListener) SetDeadline(t time.Time) error

type Server

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

func NewServer

func NewServer(config *Config, callback ConnCallback, protocol Protocol) *Server

NewServer creates a server

func (*Server) Start

func (s *Server) Start(listener *net.TCPListener, acceptTimeout time.Duration)

Start starts service

func (*Server) StartProxyTcp

func (s *Server) StartProxyTcp(addr string, acceptTimeout time.Duration)

func (*Server) StartProxyV2Tcp

func (s *Server) StartProxyV2Tcp(addr string, acceptTimeout time.Duration)

func (*Server) StartProxyV2Ws

func (s *Server) StartProxyV2Ws(addr string, path string)

func (*Server) StartTcp

func (s *Server) StartTcp(addr string, acceptTimeout time.Duration)

func (*Server) StartWs

func (s *Server) StartWs(addr string, path string)

func (*Server) Stop

func (s *Server) Stop()

Stop stops service

type SourceChecker

type SourceChecker func(net.Addr) (bool, error)

SourceChecker can be used to decide whether to trust the PROXY info or pass the original connection address through. If set, the connecting address is passed in as an argument. If the function returns an error due to the source being disallowed, it should return ErrInvalidUpstream.

Behavior is as follows: * If error is not nil, the call to Accept() will fail. If the reason for triggering this failure is due to a disallowed source, it should return ErrInvalidUpstream. * If bool is true, the PROXY-set address is used. * If bool is false, the connection's remote address is used, rather than the address claimed in the PROXY info.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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