Go API Documentation

github.com/TykTechnologies/tyk/tcp

No package summary is available.

Package

Files: 1. Third party imports: 0. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Constants

const (
	Active	ConnState	= iota
	Open
	Closed
)

Vars

var log = logger.Get().WithField("prefix", "tcp-proxy")

Types

ConnState

This type doesn't have documentation.

Field name Field type Comment
type

uint

No comment on field.
type ConnState uint

Modifier

Modifier define rules for tranforming incoming and outcoming TCP messages To filter response set data to empty To close connection, return error

Field name Field type Comment
ModifyRequest

func(src, dst net.Conn, data []byte) ([]byte, error)

No comment on field.
ModifyResponse

func(src, dst net.Conn, data []byte) ([]byte, error)

No comment on field.
type Modifier struct {
	ModifyRequest	func(src, dst net.Conn, data []byte) ([]byte, error)
	ModifyResponse	func(src, dst net.Conn, data []byte) ([]byte, error)
}

Proxy

This type doesn't have documentation.

Field name Field type Comment

sync.RWMutex

No comment on field.
DialTLS

func(network, addr string) (net.Conn, error)

No comment on field.
Dial

func(network, addr string) (net.Conn, error)

No comment on field.
TLSConfigTarget

*tls.Config

No comment on field.
ReadTimeout

time.Duration

No comment on field.
WriteTimeout

time.Duration

No comment on field.
muxer

map[string]*targetConfig

Domain to config mapping

SyncStats

func(Stat)

No comment on field.
StatsSyncInterval

time.Duration

Duration in which connection stats will be flushed. Defaults to one second.

type Proxy struct {
	sync.RWMutex

	DialTLS		func(network, addr string) (net.Conn, error)
	Dial		func(network, addr string) (net.Conn, error)
	TLSConfigTarget	*tls.Config

	ReadTimeout	time.Duration
	WriteTimeout	time.Duration

	// Domain to config mapping
	muxer		map[string]*targetConfig
	SyncStats	func(Stat)
	// Duration in which connection stats will be flushed. Defaults to one second.
	StatsSyncInterval	time.Duration
}

Stat

Stat defines basic statistics about a tcp connection

Field name Field type Comment
State

ConnState

No comment on field.
BytesIn

int64

No comment on field.
BytesOut

int64

No comment on field.
type Stat struct {
	State		ConnState
	BytesIn		int64
	BytesOut	int64
}

pipeOpts

This type doesn't have documentation.

Field name Field type Comment
modifier

func(net.Conn, net.Conn, []byte) ([]byte, error)

No comment on field.
onReadError

func(error)

No comment on field.
onWriteError

func(error)

No comment on field.
beforeExit

func()

No comment on field.
type pipeOpts struct {
	modifier	func(net.Conn, net.Conn, []byte) ([]byte, error)
	onReadError	func(error)
	onWriteError	func(error)
	beforeExit	func()
}

targetConfig

This type doesn't have documentation.

Field name Field type Comment
modifier

*Modifier

No comment on field.
target

string

No comment on field.
type targetConfig struct {
	modifier	*Modifier
	target		string
}

Functions

func IsSocketClosed

IsSocketClosed returns true if err is a result of reading from closed network connection

func IsSocketClosed(err error) bool {
	return strings.Contains(err.Error(), "use of closed network connection")
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: strings.Contains.

func (*Proxy) AddDomainHandler

func (p *Proxy) AddDomainHandler(domain, target string, modifier *Modifier) {
	p.Lock()
	defer p.Unlock()

	if p.muxer == nil {
		p.muxer = make(map[string]*targetConfig)
	}

	if modifier == nil {
		modifier = &Modifier{}
	}

	p.muxer[domain] = &targetConfig{
		modifier:	modifier,
		target:		target,
	}
}

Cognitive complexity: 6, Cyclomatic complexity: 3

func (*Proxy) RemoveDomainHandler

func (p *Proxy) RemoveDomainHandler(domain string) {
	p.Lock()
	defer p.Unlock()

	delete(p.muxer, domain)
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Proxy) Serve

func (p *Proxy) Serve(l net.Listener) error {
	for {
		conn, err := l.Accept()
		if err != nil {
			log.WithError(err).Warning("Can't accept connection")
			return err
		}
		go func() {
			if err := p.handleConn(conn); err != nil {
				log.WithError(err).Warning("Can't handle connection")
			}
		}()
	}
}

Cognitive complexity: 7, Cyclomatic complexity: 4

func (*Proxy) Swap

func (p *Proxy) Swap(new *Proxy) {
	p.Lock()
	defer p.Unlock()

	p.muxer = new.muxer
	p.TLSConfigTarget = new.TLSConfigTarget
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Stat) Flush

func (s *Stat) Flush() Stat {
	v := Stat{
		BytesIn:	atomic.LoadInt64(&s.BytesIn),
		BytesOut:	atomic.LoadInt64(&s.BytesOut),
	}
	atomic.StoreInt64(&s.BytesIn, 0)
	atomic.StoreInt64(&s.BytesOut, 0)
	return v
}

Cognitive complexity: 1, Cyclomatic complexity: 1

Uses: atomic.LoadInt64, atomic.StoreInt64.

Private functions

func clientConn

clientConn (c net.Conn) string

func formatAddress

formatAddress (a,b net.Addr) string

func upstreamConn

upstreamConn (c net.Conn) string

func getTargetConfig

getTargetConfig (conn net.Conn) (*targetConfig, error)
References: errors.New, tls.Conn.

func handleConn

handleConn (conn net.Conn) error
References: atomic.AddInt64, atomic.Value, context.Background, context.WithCancel, errors.Is, errors.New, io.EOF, net.Conn, net.Dial, sync.WaitGroup, time.NewTicker, time.Second, tls.Dial, url.Parse.

func pipe

pipe (src,dst net.Conn, opts pipeOpts)
References: time.Now, time.Time.


Tests

Files: 1. Third party imports: 0. Imports from organisation: 0. Tests: 3. Benchmarks: 0.

Test functions

TestProxyModifier

References: net.Conn, test.TCPTestCase, test.TcpMock, testing.T.

TestProxyMultiTarget

References: test.TCPTestCase, test.TcpMock, testing.T.

TestProxySyncStats

References: reflect.DeepEqual, test.TCPTestCase, test.TcpMock.