tunnel

package
v0.0.0-...-08f68e1 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Host        string
	Port        int
	Domain      string
	Secure      bool
	Timeout     int
	IdleTimeout int
	Users       []UserConfig
}

Config configures an Server

func LoadConfiguration

func LoadConfiguration(path string) (config *Config, err error)

LoadConfiguration loads configuration from a YAML file

func NewConfig

func NewConfig() (config *Config)

NewConfig creates a new ProxyConfig

func (Config) GetAddr

func (c Config) GetAddr() string

GetAddr returns the address to specify a HTTP server address

func (Config) GetTimeout

func (c Config) GetTimeout() time.Duration

GetTimeout returns the time.Duration converted to millisecond

type Connection

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

Connection manages a single websocket connection from the peer. wsp supports multiple connections from a single peer at the same time.

func NewConnection

func NewConnection(pool *Pool, ws *websocket.Conn) *Connection

NewConnection returns a new Connection.

func (*Connection) Close

func (connection *Connection) Close()

Close the connection

func (*Connection) ProxyRequest

func (connection *Connection) ProxyRequest(c echo.Context) (err error)

Proxy a HTTP request through the Proxy over the websocket connection

func (*Connection) Release

func (connection *Connection) Release()

Release notifies that this connection is ready to use again

func (*Connection) Take

func (connection *Connection) Take() bool

Take notifies that this connection is going to be used

type ConnectionRequest

type ConnectionRequest struct {
	// Pick the connection for this subdomain
	Subdomain string

	Connection chan *Connection
}

ConnectionRequest is used to request a proxy connection from the dispatcher

func NewConnectionRequest

func NewConnectionRequest(timeout time.Duration, connectionFor string) (cr *ConnectionRequest)

NewConnectionRequest creates a new connection request

type ConnectionStatus

type ConnectionStatus int

ConnectionStatus is an enumeration type which represents the status of WebSocket connection.

const (
	// Idle state means it is opened but not working now.
	// The default value for Connection is Idle, so it is ok to use zero-value(int: 0) for Idle status.
	Idle ConnectionStatus = iota
	Busy
	Closed
)

type Pool

type Pool struct {
	ID             PoolID
	Subdomain      string
	LocalServer    string
	UserIdentifier string
	// contains filtered or unexported fields
}

Pool handles all connections from the peer.

func NewPool

func NewPool(server *Server, id PoolID, subdomain, localServer, userIdentifier string) *Pool

NewPool creates a new Pool

func (*Pool) Clean

func (pool *Pool) Clean()

Clean removes dead connection from the pool Look for dead connection in the pool This MUST be surrounded by pool.lock.Lock()

func (*Pool) IsEmpty

func (pool *Pool) IsEmpty() bool

IsEmpty clean the pool and return true if the pool is empty

func (*Pool) Offer

func (pool *Pool) Offer(connection *Connection)

Offer offers an idle connection to the server.

func (*Pool) Register

func (pool *Pool) Register(ws *websocket.Conn)

Register creates a new Connection and adds it to the pool

func (*Pool) SetSize

func (pool *Pool) SetSize(n int)

func (*Pool) Shutdown

func (pool *Pool) Shutdown()

Shutdown closes every connections in the pool and cleans it

func (*Pool) Size

func (pool *Pool) Size() (ps *PoolSize)

Size return the number of connection in each state in the pool

type PoolID

type PoolID string

PoolID represents the identifier of the connected WebSocket client.

type PoolSize

type PoolSize struct {
	Idle   int
	Busy   int
	Closed int
}

PoolSize is the number of connection in each state in the pool

type Server

type Server struct {
	Config *Config

	Upgrader websocket.Upgrader

	// In Pools, keep connections with WebSocket peers.
	Pools map[string]*Pool

	// A RWMutex is a reader/writer mutual exclusion Lock,
	// and it is for exclusive control with pools operation.
	//
	// This is locked when reading and writing pools, the timing is when:
	// 1. (rw) registering websocket clients in /register endpoint
	// 2. (rw) remove empty pools which has no connections
	// 3. (r) dispatching connection from available pools to clients requests
	//
	// And then it is released after each process is completed.
	Lock sync.RWMutex

	// Through Dispatcher channel it communicates between "server" thread and "Dispatcher" thread.
	// "server" thread sends the value to this channel when accepting requests in the endpoint /requests,
	// and "Dispatcher" thread reads this channel.
	Dispatcher chan *ConnectionRequest

	// DB connection
	DB *gorm.DB
	// contains filtered or unexported fields
}

Server is a Reverse HTTP Proxy over WebSocket This is the Server part, Clients will offer websocket connections, those will be pooled to transfer HTTP Request and response

func NewServer

func NewServer(configFile string, db *gorm.DB) (server *Server)

NewServer return a new Server instance

func (*Server) DispatchConnections

func (s *Server) DispatchConnections()

Dispatch connection from available pools to clients requests

func (*Server) GetDestinationURL

func (s *Server) GetDestinationURL(subdomain string) string

func (*Server) GetOrCreatePoolForUser

func (s *Server) GetOrCreatePoolForUser(subdomain, localServer, userIdentifier string, id PoolID) (*Pool, error)

func (*Server) GetSubdomainFromHost

func (s *Server) GetSubdomainFromHost(host string) (string, error)

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown stop the Server

func (*Server) Start

func (s *Server) Start()

Start Server HTTP server

type UserConfig

type UserConfig struct {
	Identifier string
	SecretKey  string
}

Jump to

Keyboard shortcuts

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