service

package
v0.0.0-...-30becf0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2020 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProtoUDP = iota
	ProtoTCP
	ProtoHTTP
	ProtoHTTPS
	ProtoQuic
)
View Source
const DefaultMaxDialTime = time.Second * 10

Variables

View Source
var (
	ErrEmptyInput    = errors.New("user input empty")
	ErrMissingProto  = errors.New("missing proto in uri")
	ErrMissingHost   = errors.New("missing host in uri")
	ErrMissingPort   = errors.New("missing port in uri")
	ErrInvalidFormat = errors.New("user input invalid")
)

It's best to leak as little from golang's errors as possible. Thus, these errors are sent to the user for bad inputs.

View Source
var ErrProtocolNotHandled = errors.New("protocol not handled")
View Source
var InternalErrorJson = []byte(`{"err": "internal error"}`)
View Source
var ProtoMap = map[string]int{
	"UDP":   ProtoUDP,
	"TCP":   ProtoTCP,
	"HTTP":  ProtoHTTP,
	"HTTPS": ProtoHTTPS,
	"QUIC":  ProtoQuic,
}

Functions

func DidError

func DidError(w http.ResponseWriter, errCode int, err error) bool

func GetRemoteIP

func GetRemoteIP(r *http.Request) string

TODO: This s not robust.

Need X-Forwarded-For support, which has security concerns.

func InitializeLogger

func InitializeLogger() (*zap.Logger, func() error)

func MakeHTTPSRedirectHandler

func MakeHTTPSRedirectHandler(dstPort string) func(w http.ResponseWriter, r *http.Request)

func ShittyPack

func ShittyPack(s string) string

func StartHTTPToHTTPSRedirector

func StartHTTPToHTTPSRedirector(ctx context.Context, httpBindAddr, targetAddr string, timeout time.Duration) error

Starts a HTTP to HTTPS redirector service on bindAddr.

- The server times out reads and writes longer than timeout. - The targetAddr should refer to the https address. - Cancelling the context shuts down the redirector.

Types

type QuicChecker

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

func NewQuicChecker

func NewQuicChecker(bindAddr string, maxHandhake time.Duration) (*QuicChecker, error)

Create a new QuicChecker service.

This does not do any certificate validation.

func (*QuicChecker) Check

func (q *QuicChecker) Check(addr *net.UDPAddr) *Resp

type Req

type Req struct {
	Host  string
	Proto int
}

func ParseReq

func ParseReq(userInput string) (*Req, error)

type Resp

type Resp struct {
	Success bool `json:"success"`
}

type Service

type Service struct {
	MaxDialTime time.Duration
	// contains filtered or unexported fields
}

func New

func New(opts ...ServiceOpt) (*Service, error)

func (*Service) CheckQuic

func (svc *Service) CheckQuic(req *Req) *Resp

func (*Service) CheckTCP

func (svc *Service) CheckTCP(req *Req) *Resp

func (*Service) CheckUDP

func (svc *Service) CheckUDP(req *Req) *Resp

func (*Service) Cleanup

func (svc *Service) Cleanup()

func (*Service) Execute

func (svc *Service) Execute(req *Req) (*Resp, error)

func (*Service) Home

func (svc *Service) Home(w http.ResponseWriter, r *http.Request)

func (*Service) ListenAndServe

func (svc *Service) ListenAndServe(ctx context.Context) error

func (*Service) LoadHomePage

func (svc *Service) LoadHomePage() error

func (*Service) Routes

func (svc *Service) Routes() *mux.Router

func (*Service) UUpHandler

func (svc *Service) UUpHandler(w http.ResponseWriter, r *http.Request)

type ServiceOpt

type ServiceOpt func(*Service) error

func DisableTCP

func DisableTCP() ServiceOpt

func WithQuicService

func WithQuicService(bindAddr string, maxDialTime time.Duration) ServiceOpt

func WithRouter

func WithRouter(r *mux.Router) ServiceOpt

func WithUDPChecker

func WithUDPChecker(bindAddr string, resendAttempts int, sleepBetweenResend time.Duration) ServiceOpt

type UDPChecker

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

func NewUDPChecker

func NewUDPChecker(bindAddr string, resendAttempts int, sleepBetweenResend time.Duration) (*UDPChecker, error)

func (*UDPChecker) Check

func (u *UDPChecker) Check(addr net.Addr, timeout time.Duration) *Resp

TODO: k-attempts

func (*UDPChecker) StartHandler

func (u *UDPChecker) StartHandler()

func (*UDPChecker) Stop

func (u *UDPChecker) Stop() error

type Waiter

type Waiter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewWaiter

func NewWaiter() *Waiter

Create a new waiter for UPD checking.

func (*Waiter) Complete

func (w *Waiter) Complete(success bool)

Complete the waiter with the success condition.

Multiple calls have no effect.

func (*Waiter) DecListeners

func (w *Waiter) DecListeners()

func (*Waiter) IfDisposable

func (w *Waiter) IfDisposable(f func())

func (*Waiter) IncListeners

func (w *Waiter) IncListeners()

func (*Waiter) Wait

func (w *Waiter) Wait(timeout time.Duration) bool

Wait until another routine completes or the waiter times out.

Return the success condition (false if timeout or the check failed).

type WaiterMap

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

func NewWaiterMap

func NewWaiterMap() *WaiterMap

func (*WaiterMap) Lookup

func (wm *WaiterMap) Lookup(addr net.Addr) *Waiter

TODO: Golang to upper?

func (*WaiterMap) WithWaiterResp

func (wm *WaiterMap) WithWaiterResp(addr net.Addr, timeout time.Duration, f func(w *Waiter)) bool

TODO: does a goroutine stop running if the request stops?

Jump to

Keyboard shortcuts

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