kuiperbelt

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2018 License: MIT Imports: 23 Imported by: 0

README

kuiperbelt

Build Status

The proxy server that converts WebSocket to HTTP/1.x.

Install & Usage

NOTICE: README.ja.md is more details and tutorial in Japanese.

Installation

I recommend installing from binary releases.

This installation easy and well to do using Songmu/ghg.

$ ghg get kuiperbelt/kuiperbelt
$ export PATH=$(ghg bin):$PATH

Also, you can use the kuiperbelt in docker.

$ docker pull kuiperbelt/kuiperbelt:latest
Configuration

A configuration is in YAML format.

port: 12345 # listen to the port
sock: "" # If set sock path this option, a kuiperbelt is to use UNIX domain socket.
callback:
  # A callback endpoint for starts WebSocket connection this useful for authentication.
  # When your application returning "HTTP/1.1 OK 200" in this callback, a connection upgrade to WebSocket.
  # However your callback returning other than "200" ("404", "403" and "500"...), this connection is a disconnect.
  connect: "http://localhost:12346/connect"
  # A close callback is similar working to the connect callback. A kuiperbelt send request this callback when closed connection by a client or timeout of idle(if set `idle_timeout`).
  close: "http:/localhost:12346/close"
  # If set this and push message from client, POST to this url with-in message.
  receive: "http:/localhost:12346/receive"
  timeout: 10s    # timeout of callback response
# A log level of access log is `info`. But suppress this when this option is true.
suppress_access_log: false 
# This option can change a header name of session id.
session_header: "X-Kuiperbelt-Session"
# An "X-Kuiperbelt-Endpoint" header in connect callback is indicating an endpoint of kuiperbelt.
# Your application can use this value when multi-host of kuiperbelt.
# By default, this value is from `hostname` command. But if you can not use the value from `hostname`(ex. in docker), by using this option you can set suitable values.
endpoint: "localhost:12345"
proxy_set_header:
  "X-Foo": "Foo"  # set to callback request header
  "X-Bar": ""     # remove from callback request header
send_timeout: 0    # timeout of sending a message to a client. 0 is off.
send_queue_size: 0 # queue size of message to client. this value is per a cliet.
# This option is to use for checking Origin header.
# If set `none`, not check.
# If set `same_origin`, check equals Origin to Host.
# If set `same_hostname`, check equals hostname of Origin to hostname of Host, ignoring port.
origin_policy: none
# If the idle state continues this value, disconnect automatically.
# In this case, working close callback. 0 is disable this feature.
idle_timeout: 0 

Also, if you using docker image, you can set these options by environment variables.

The corresponding environment variable is following _example/config_by_env.yml.

Launch

The launch with specifies configuration file.

$ ekbo -config=config.yml

Or, launch in docker.

docker run -p 9180:9180 -e EKBO_CONNECT_CALLBACK_URL=http://yourwebapp/connect kuiperbelt/kuiperbelt

Also, an example application in kuiperbelt/webchat-example. You can launch this application by docker-compose.

Features

Web API
for client(ex. browser, native apps)
  • GET /connect - starts WebSocket connection
    • You can use the query string or header for authentication. These values pass through to the connect callback.
for backend application
  • POST /send - send message to connection of WebSocket
    • X-Kuiperbelt-Session in request header: target session id
    • request body: pass through to a client by WebSocket.
  • POST /close - close connection of WebSocket
    • X-Kuiperbelt-Session in request header: target session id
    • request body: pass through to a client by WebSocket. useful to goodbye message.
for monitoring
  • GET /ping - useful for the health check.
  • GET /stats - metrics of kuiperbelt. living connections, error rate, etc...
Callback

The callback is similar to webhook.

  • connect callback - request when starts WebSocket.
    • response body: pass through to a client by WebSocket. useful to hello message.
  • close callback - request when closed connection by client or idle.

Author

License

The MIT License

Copyright (c) 2015 TANIWAKI Makoto / (c) 2015 KAYAC Inc.

Documentation

Index

Constants

View Source
const (
	DefaultPort         = "9180"
	DefaultOriginPolicy = "none"
)
View Source
const (
	ENDPOINT_HEADER_NAME               = "X-Kuiperbelt-Endpoint"
	CALLBACK_CLIENT_MAX_CONNS_PER_HOST = 32
)

Variables

View Source
var (
	Version string
	Log     *zap.Logger
)

Functions

func NewLoggingHandler

func NewLoggingHandler(h http.Handler) http.Handler

func Run

func Run(port, sock, configFilename string)

Types

type Callback

type Callback struct {
	Connect string        `yaml:"connect"`
	Close   string        `yaml:"close"`
	Timeout time.Duration `yaml:"timeout"`
	Receive string        `yaml:"receive"`
}

type Config

type Config struct {
	Callback          Callback          `yaml:"callback"`
	SessionHeader     string            `yaml:"session_header"`
	Port              string            `yaml:"port"`
	Sock              string            `yaml:"sock"`
	Endpoint          string            `yaml:"endpoint"`
	StrictBroadcast   bool              `yaml:"strict_broadcast"`
	ProxySetHeader    map[string]string `yaml:"proxy_set_header"`
	SendTimeout       time.Duration     `yaml:"send_timeout"`
	SendQueueSize     int               `yaml:"send_queue_size"`
	OriginPolicy      string            `yaml:"origin_policy"`
	IdleTimeout       time.Duration     `yaml:"idle_timeout"`
	SuppressAccessLog bool              `yaml:"suppress_access_log"`
	Path              Path              `yaml:"path"`
}

func NewConfig

func NewConfig(filename string) (*Config, error)

type Message

type Message struct {
	Body        []byte
	ContentType string
	Session     string
	LastWord    bool
}

Message is a message container for communicating through sessions.

type Path added in v1.4.0

type Path struct {
	Connect string `yaml:"connect"`
	Close   string `yaml:"close"`
	Stats   string `yaml:"stats"`
	Send    string `yaml:"send"`
	Ping    string `yaml:"ping"`
}

type Proxy

type Proxy struct {
	Config Config
	Stats  *Stats
	Pool   *SessionPool
}

func NewProxy

func NewProxy(c Config, s *Stats, p *SessionPool) *Proxy

func (*Proxy) CloseHandlerFunc

func (p *Proxy) CloseHandlerFunc(w http.ResponseWriter, r *http.Request)

CloseHandlerFunc handles POST /close request.

func (*Proxy) PingHandlerFunc

func (p *Proxy) PingHandlerFunc(w http.ResponseWriter, r *http.Request)

PingHandlerFunc handles ping request.

func (*Proxy) Register

func (p *Proxy) Register()

func (*Proxy) SendHandlerFunc

func (p *Proxy) SendHandlerFunc(w http.ResponseWriter, r *http.Request)

SendHandlerFunc handles POST /send request.

type Receiver added in v1.4.0

type Receiver interface {
	Receive(context.Context, receivedMessage) error
}

Receiver is proxy message from a client

type Session

type Session interface {
	Send() chan<- Message
	Key() string
	Close() error
	Closed() <-chan struct{}
}

Session is an interface for sessions.

type SessionPool added in v1.1.0

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

SessionPool is a pool of sessions.

func (*SessionPool) Add added in v1.1.0

func (p *SessionPool) Add(s Session)

Add add new session into the SessionPool.

func (*SessionPool) Delete added in v1.1.0

func (p *SessionPool) Delete(key string) error

Delete deletes a session.

func (*SessionPool) Get added in v1.1.0

func (p *SessionPool) Get(key string) (Session, error)

Get gets a session from the SessionPool.

func (*SessionPool) List added in v1.1.0

func (p *SessionPool) List() []Session

List returns a slice of all sessions in the pool.

type Stats

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

func NewStats

func NewStats() *Stats

func (*Stats) ClosedEvent added in v1.1.0

func (s *Stats) ClosedEvent()

func (*Stats) ClosingConnections added in v1.1.0

func (s *Stats) ClosingConnections() int64

func (*Stats) ClosingEvent added in v1.1.0

func (s *Stats) ClosingEvent()

func (*Stats) ConnectErrorEvent

func (s *Stats) ConnectErrorEvent()

func (*Stats) ConnectErrors

func (s *Stats) ConnectErrors() int64

func (*Stats) ConnectEvent

func (s *Stats) ConnectEvent()

func (*Stats) Connections

func (s *Stats) Connections() int64

func (*Stats) DisconnectEvent

func (s *Stats) DisconnectEvent()

func (*Stats) Dump

func (s *Stats) Dump(w io.Writer) error

func (*Stats) DumpText

func (s *Stats) DumpText(w io.Writer) error

func (*Stats) MessageErrorEvent

func (s *Stats) MessageErrorEvent()

func (*Stats) MessageErrors

func (s *Stats) MessageErrors() int64

func (*Stats) MessageEvent

func (s *Stats) MessageEvent()

func (*Stats) TotalConnections

func (s *Stats) TotalConnections() int64

func (*Stats) TotalMessages

func (s *Stats) TotalMessages() int64

type WebSocketServer

type WebSocketServer struct {
	Config Config
	Stats  *Stats
	Pool   *SessionPool
	// contains filtered or unexported fields
}

func NewWebSocketServer

func NewWebSocketServer(c Config, s *Stats, p *SessionPool) *WebSocketServer

func (*WebSocketServer) ConnectCallbackHandler

func (s *WebSocketServer) ConnectCallbackHandler(w http.ResponseWriter, r *http.Request) (*http.Response, error)

func (*WebSocketServer) Handler

func (s *WebSocketServer) Handler(w http.ResponseWriter, r *http.Request)

Handler handles websocket connection requests.

func (*WebSocketServer) NewWebSocketHandler

func (s *WebSocketServer) NewWebSocketHandler(resp *http.Response) (func(ws *websocket.Conn), error)

func (*WebSocketServer) NewWebSocketSession

func (s *WebSocketServer) NewWebSocketSession(key string, ws *websocket.Conn) (*WebSocketSession, error)

func (*WebSocketServer) Register

func (s *WebSocketServer) Register()

func (*WebSocketServer) Shutdown added in v1.1.0

func (s *WebSocketServer) Shutdown(ctx context.Context) error

func (*WebSocketServer) StatsHandler

func (s *WebSocketServer) StatsHandler(w http.ResponseWriter, r *http.Request)

type WebSocketSession

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

func (*WebSocketSession) Close

func (s *WebSocketSession) Close() error

Close closes the session.

func (*WebSocketSession) Closed added in v1.1.0

func (s *WebSocketSession) Closed() <-chan struct{}

func (*WebSocketSession) Key

func (s *WebSocketSession) Key() string

Key returns the session key.

func (*WebSocketSession) Send added in v1.1.0

func (s *WebSocketSession) Send() chan<- Message

Send returns the channel for sending messages.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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