resttunnel

package
v0.0.0-...-d0d75a4 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2022 License: MIT Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const ConfigurationPath = "restTunnel.yaml"

ConfigurationPath is the path to the file the configration will be located at.

View Source
const Interval = time.Second * 15

Interval between each analytic sample.

View Source
const MaxRedirects = 5

MaxRedirects is the maximum number of redirects before is it not attempted again.

View Source
const Samples = 720

Samples to hold. 5 seconds and 72 samples is 1 hour.

View Source
const VERSION = "1.2.1"

VERSION respects semantic versioning.

Variables

View Source
var ErrCallbackDoesNotExist = "Callback '%s' does not exist"

ErrCallbackDoesNotExist is raised when referencing a callback that does not exist.

View Source
var ErrCallbackNotFinished = "Callback '%s' has not completed request"

ErrCallbackNotFinished is raised when attempting to retrieve the callback on a request that is not done.

Functions

func AliveHandler

func AliveHandler(rt *RestTunnel) http.HandlerFunc

AliveHandler returns the RestTunnel version as a way of signifying it is ready to serve.

func AnalyticsHandler

func AnalyticsHandler(rt *RestTunnel) http.HandlerFunc

AnalyticsHandler handles returning the RestTunnel analytics.

func CallbacksHandler

func CallbacksHandler(rt *RestTunnel) http.HandlerFunc

CallbacksHandler handles returning a callback to a client.

func DurationTimestamp

func DurationTimestamp(d time.Duration) (output string)

DurationTimestamp outputs in a format similar to the timestamp String().

func IsDiscordAPIURI

func IsDiscordAPIURI(uri *fasthttp.URI) bool

IsDiscordAPIURI returns a boolean if the current URI is a discord API endpoint.

Types

type BaseResponse

type BaseResponse struct {
	Error   string      `json:"error,omitempty"`
	Success bool        `json:"success"`
	Data    interface{} `json:"data,omitempty"`
	Queued  *bool       `json:"queued,omitempty"`
	UUID    *uuid.UUID  `json:"uuid,omitempty"`
}

BaseResponse is the structure of all REST requests.

type ErrorResponse

type ErrorResponse struct {
	Error   string    `json:"error"`
	Success bool      `json:"success"`
	Queued  bool      `json:"queued,omitempty"` // Boolean if the request has already been queued
	UUID    uuid.UUID `json:"uuid,omitempty"`
}

ErrorResponse is the structure error messages are returned to by the client. We do not have a Success response as the raw response is sent.

type MethodRouter

type MethodRouter struct {
	*mux.Router
}

MethodRouter beepboop.

func NewMethodRouter

func NewMethodRouter() *MethodRouter

NewMethodRouter creates a new method router.

func (*MethodRouter) HandleFunc

func (mr *MethodRouter) HandleFunc(path string,
	f func(http.ResponseWriter, *http.Request), methods ...string) *mux.Route

HandleFunc registers a route that handles both paths and methods.

type Queue

type Queue struct {
	Expiration time.Time

	JobActive   *abool.AtomicBool
	JobsHandled *int64

	Bucket *bucket.Bucket

	PriorityEvents chan *structs.TunnelRequest
	Events         chan *structs.TunnelRequest
	// contains filtered or unexported fields
}

Queue represents a Deque with a priority queue.

type RestTunnel

type RestTunnel struct {
	Configuration *TunnelConfiguration `json:"configuration"`

	Logger zerolog.Logger `json:"-"`

	Start time.Time `json:"uptime"`

	HTTP *fasthttp.Client `json:"-"`

	Buckets map[string]*bucket.Bucket `json:"buckets"`

	Queues map[string]*Queue `json:"queue"`

	Callbacks map[uuid.UUID]*structs.TunnelResponse `json:"callbacks"`

	// Analytics for ratelimits
	AnalyticsHit  *accumulator.Accumulator
	AnalyticsMiss *accumulator.Accumulator

	// Requests waiting and an atomic cache
	AnalyticsWaiting *accumulator.Accumulator

	// Analytics for requests and callbacks buffer
	AnalyticsRequests  *accumulator.Accumulator
	AnalyticsCallbacks *accumulator.Accumulator

	// Uses total response time and requests to calculate average
	AnalyticsAverageResponse *accumulator.Accumulator
	AverageResponse          *accumulator.Accumulator

	Router *MethodRouter
	// contains filtered or unexported fields
}

RestTunnel represents the global application state.

func NewTunnel

func NewTunnel(logger io.Writer) (rt *RestTunnel, err error)

NewTunnel creates a RestTunnel instance.

func (*RestTunnel) CollectCallbacks

func (rt *RestTunnel) CollectCallbacks() (cleaned int, dur time.Duration)

CollectCallbacks handles cleaning old callbacks.

func (*RestTunnel) CollectQueues

func (rt *RestTunnel) CollectQueues() (cleaned int, dur time.Duration)

CollectQueues handles cleaning old queues.

func (*RestTunnel) DecodeBody

func (rt *RestTunnel) DecodeBody(resp *fasthttp.Response) (body []byte, err error)

DecodeBody returns a decoded fasthttp.Response body using the Content-Encoding header.

func (*RestTunnel) HandleQueueJob

func (rt *RestTunnel) HandleQueueJob(tr *structs.TunnelRequest)

HandleQueueJob handles a TunnelRequest gathered from a queue.

func (*RestTunnel) HandleRequest

func (rt *RestTunnel) HandleRequest(ctx *fasthttp.RequestCtx)

HandleRequest handles a HTTP request given to RestTunnel.

func (*RestTunnel) LoadConfiguration

func (rt *RestTunnel) LoadConfiguration(path string) (configuration *TunnelConfiguration, err error)

LoadConfiguration loads the configuration for RestTunnel.

func (*RestTunnel) Open

func (rt *RestTunnel) Open() (err error)

Open starts up a HTTP server.

func (*RestTunnel) StartQueueJob

func (rt *RestTunnel) StartQueueJob(q *Queue)

StartQueueJob starts a queue job.

func (*RestTunnel) TakeOutTheTrash

func (rt *RestTunnel) TakeOutTheTrash()

TakeOutTheTrash handles cleaning old entries.

func (*RestTunnel) TraverseBucket

func (rt *RestTunnel) TraverseBucket(bucketStr string) (_bucket *bucket.Bucket, bucketStack []string, err error)

TraverseBucket returns the original bucket from the bucket alias. Returns current bucket, slice of buckets traversed through and error.

func (*RestTunnel) TunnelHTTPRequest

func (rt *RestTunnel) TunnelHTTPRequest(ctx *fasthttp.RequestCtx)

TunnelHTTPRequest handles creating a tunnel request.

type TunnelConfiguration

type TunnelConfiguration struct {
	Host string `json:"host" yaml:"host"`

	State struct {
		CallbackExpiration int `json:"callback_expiration" yaml:"callback_expiration"`
		QueueExpiration    int `json:"queue_expiration" yaml:"queue_expiration"`
	} `json:"state" yaml:"state"`

	ReverseRoute struct {
		Enabled bool   `json:"enabled" yaml:"enabled"`
		Host    string `json:"host" yaml:"host"`
	} `json:"reverse_route" yaml:"reverse_route"`

	Logging struct {
		ConsoleLoggingEnabled bool `json:"console_logging" yaml:"console_logging"`
		FileLoggingEnabled    bool `json:"file_logging" yaml:"file_logging"`

		EncodeAsJSON bool `json:"encode_as_json" yaml:"encode_as_json"` // Make the framework log as json

		Directory  string `json:"directory" yaml:"directory"`     // Directory to log into
		Filename   string `json:"filename" yaml:"filename"`       // Name of logfile
		MaxSize    int    `json:"max_size" yaml:"max_size"`       // Size in MB before a new file
		MaxBackups int    `json:"max_backups" yaml:"max_backups"` // Number of files to keep
		MaxAge     int    `json:"max_age" yaml:"max_age"`         // Number of days to keep a logfile
	} `json:"logging" yaml:"logging"`
}

TunnelConfiguration represents the configuration for RestTunnel.

Jump to

Keyboard shortcuts

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