wss

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: MIT Imports: 20 Imported by: 2

Documentation

Index

Constants

View Source
const (
	TagData = iota
	TagEstOk
	TagEstErr
	TagNoMore
)
View Source
const (
	ProxyTypeSocks5 = iota
	ProxyTypeHttp
	ProxyTypeHttps
)
View Source
const (
	WsTpVer   = "version"
	WsTpBeats = "heart_beat"
	WsTpClose = "finish"
	WsTpData  = "data"
	WsTpEst   = "est" // establish
)
View Source
const CompVersion = 0x003
View Source
const CoreVersion = "0.6.1"
View Source
const VersionCode = 0x004

version of protocol.

Variables

View Source
var ConnCloseByClient = errors.New("conn closed by client")
View Source
var StoppedError = errors.New("listener stopped")

Functions

func HttpRequestHeader added in v0.3.0

func HttpRequestHeader(buffer *bytes.Buffer, req *http.Request)

func HttpRespHeader added in v0.3.0

func HttpRespHeader(buffer *bytes.Buffer, resp *http.Response)

func NegVersionServer

func NegVersionServer(ctx context.Context, wsConn *websocket.Conn, enableStatusPage bool) error

send version information to client from server

func NewWebSocketWriter added in v0.5.0

func NewWebSocketWriter(wsc *ConcurrentWebSocket, id ksuid.KSUID, ctx context.Context) *webSocketWriter

func NewWebSocketWriterWithMutex added in v0.5.0

func NewWebSocketWriterWithMutex(wsc *ConcurrentWebSocket, id ksuid.KSUID, ctx context.Context) *webSocketWriter

func ProxyTypeStr added in v0.3.0

func ProxyTypeStr(tp int) string

Types

type Base64WSBufferWriter

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

write data to WebSocket server or client

func (*Base64WSBufferWriter) Flush

func (b *Base64WSBufferWriter) Flush(messageType int, id ksuid.KSUID, cws ConcurrentWebSocketInterface) (int, error)

flush all data in this buff into WebSocket.

func (*Base64WSBufferWriter) Write

func (b *Base64WSBufferWriter) Write(p []byte) (int, error)

implement Write interface to write bytes from ssh server into bytes.Buffer.

type BufferedWR added in v0.3.0

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

func NewBufferWR added in v0.3.0

func NewBufferWR() *BufferedWR

func (*BufferedWR) Close added in v0.3.0

func (h *BufferedWR) Close() error

func (*BufferedWR) Read added in v0.3.0

func (h *BufferedWR) Read(p []byte) (int, error)

read data from buffer make sure there is no more one goroutine reading

func (*BufferedWR) Write added in v0.3.0

func (h *BufferedWR) Write(p []byte) (int, error)

implement Write interface to write bytes from ssh server into bytes.Buffer.

type ChanDone added in v0.5.0

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

data type used in DefaultProxyEst to pass data to channel

type Client

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

client part of wssocks

func NewClient added in v0.3.1

func NewClient() *Client

func (*Client) Close added in v0.3.1

func (client *Client) Close(wait bool) error

Close stops listening on the TCP address, But the active links are not closed and wait them to finish.

func (*Client) ListenAndServe added in v0.3.1

func (client *Client) ListenAndServe(record *ConnRecord, wsc *WebSocketClient, address string, enableHttp bool, onConnected func()) error

listen on local address:port and forward socks5 requests to wssocks server.

func (*Client) Reply

func (client *Client) Reply(conn net.Conn, enableHttp bool) ([]byte, int, string, error)

parse target address and proxy type, and response to socks5/https client

type ClientData added in v0.3.0

type ClientData ServerData

type ConcurrentWebSocket

type ConcurrentWebSocket struct {
	WsConn *websocket.Conn
}

add lock to websocket connection to make sure only one goroutine can write this websocket.

func (*ConcurrentWebSocket) WSClose

func (wsc *ConcurrentWebSocket) WSClose() error

close websocket connection

func (*ConcurrentWebSocket) WriteProxyMessage

func (wsc *ConcurrentWebSocket) WriteProxyMessage(ctx context.Context, id ksuid.KSUID, tag int, data []byte) error

write message to websocket, the data is fixed format @ProxyData id: connection id data: data to be written

type ConcurrentWebSocketInterface

type ConcurrentWebSocketInterface interface {
	WSClose() error
	WriteWSJSON(data interface{}) error
}

type ConnRecord added in v0.3.1

type ConnRecord struct {
	ConnSize  uint            // total size of current connections
	Addresses map[string]uint // current connections as well as its count
	Writer    *io.Writer      // terminal writer  todo defer Flush
	OnChange  func(status ConnStatus)
	Mutex     *sync.Mutex
}

struct record the connection size of each target host

func NewConnRecord added in v0.3.1

func NewConnRecord() *ConnRecord

func (*ConnRecord) Update added in v0.3.1

func (cr *ConnRecord) Update(status ConnStatus)

type ConnStatus added in v0.3.1

type ConnStatus struct {
	Address string
	IsNew   bool
	Type    int
}

connection status when a connection is added or removed.

type Connector

type Connector struct {
	Conn io.ReadWriteCloser
}

type DefaultProxyEst added in v0.5.0

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

interface implementation for socks5 and https proxy.

func (*DefaultProxyEst) Close added in v0.5.0

func (e *DefaultProxyEst) Close(tell bool) error

type HeartBeat added in v0.3.1

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

func NewHeartBeat added in v0.3.1

func NewHeartBeat(wsc *WebSocketClient) (*HeartBeat, context.Context)

func (*HeartBeat) Close added in v0.3.1

func (hb *HeartBeat) Close()

close heartbeat sending

func (*HeartBeat) Start added in v0.3.1

func (hb *HeartBeat) Start(ctx context.Context, writeTimeout time.Duration) error

start sending heart beat to server.

type HttpClient added in v0.3.0

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

func NewHttpProxy added in v0.3.0

func NewHttpProxy(wsc *WebSocketClient, cr *ConnRecord) HttpClient

func (*HttpClient) ProxyType added in v0.3.0

func (client *HttpClient) ProxyType() int

func (*HttpClient) ServeHTTP added in v0.3.0

func (client *HttpClient) ServeHTTP(w http.ResponseWriter, req *http.Request)

type HttpProxyEst added in v0.5.0

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

func (*HttpProxyEst) Close added in v0.5.0

func (h *HttpProxyEst) Close(tell bool) error

type HttpsClient added in v0.3.0

type HttpsClient struct {
}

func (*HttpsClient) EstablishData added in v0.3.0

func (client *HttpsClient) EstablishData(origin []byte) ([]byte, error)

func (*HttpsClient) ParseHeader added in v0.3.0

func (client *HttpsClient) ParseHeader(conn net.Conn, header []byte) (string, error)

parsing https header, and return address and parsing error

func (*HttpsClient) ProxyType added in v0.3.0

func (client *HttpsClient) ProxyType() int

func (*HttpsClient) Trigger added in v0.3.0

func (client *HttpsClient) Trigger(data []byte) bool

type Hub added in v0.5.0

type Hub struct {
	ConcurrentWebSocket
	// contains filtered or unexported fields
}

Hub maintains the set of active proxy clients in server side for a user

func (*Hub) Close added in v0.5.0

func (h *Hub) Close()

func (*Hub) CloseProxyConn added in v0.5.0

func (h *Hub) CloseProxyConn(id ksuid.KSUID) error

Close proxy connection with remote host. It can be called when receiving tell close message from client

func (*Hub) GetConnectorSize added in v0.5.0

func (h *Hub) GetConnectorSize() int

return the proxies handled by this hub/websocket connetion

func (*Hub) GetProxyById added in v0.5.0

func (h *Hub) GetProxyById(id ksuid.KSUID) *ProxyServer

func (*Hub) RemoveProxy added in v0.5.0

func (h *Hub) RemoveProxy(id ksuid.KSUID)

type HubCollection added in v0.5.0

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

HubCollection is a set of hubs. It handle several hubs. Each hub can map to a websocket connection, which also handle several proxies instance.

func NewHubCollection added in v0.5.0

func NewHubCollection() *HubCollection

func (*HubCollection) GetConnCount added in v0.5.0

func (hc *HubCollection) GetConnCount() (int, int)

count the client size and proxy connection size.

func (*HubCollection) NewHub added in v0.5.0

func (hc *HubCollection) NewHub(conn *websocket.Conn) *Hub

create a hub and add it to hub collection

func (*HubCollection) RemoveProxy added in v0.5.0

func (hc *HubCollection) RemoveProxy(id ksuid.KSUID)

remove a hub specified by its id.

type ProxyClient

type ProxyClient struct {
	Id ksuid.KSUID
	// contains filtered or unexported fields
}

proxy client handle one connection, send data to proxy server vai websocket.

func (*ProxyClient) Establish added in v0.3.0

func (p *ProxyClient) Establish(wsc *WebSocketClient, firstSendData []byte, proxyType int, addr string) error

tell wssocks proxy server to establish a proxy connection by sending server proxy address, type, initial data.

type ProxyData

type ProxyData struct {
	Tag        int    `json:"tag"`
	DataBase64 string `json:"base64"`
}

Proxy data (from server to client or from client to server)

type ProxyEstMessage

type ProxyEstMessage struct {
	Type       int    `json:"proxy_type"`
	Addr       string `json:"addr"`
	WithData   bool   `json:"with_data"`
	DataBase64 string `json:"base64"` // establish with initialized data.
}

Proxy message for establishing connection

type ProxyEstablish added in v0.5.0

type ProxyEstablish interface {

	// close connection
	// tell: whether to send close message to proxy client
	Close(tell bool) error
	// contains filtered or unexported methods
}

interface of establishing proxy connection with target

type ProxyInterface added in v0.3.0

type ProxyInterface interface {
	ProxyType() int
	// return a bool value to indicate whether it is the matched protocol.
	Trigger(data []byte) bool
	// parse protocol header bytes, return target host.
	ParseHeader(conn net.Conn, header []byte) (string, error)
	// return data transformed in connection establishing step.
	EstablishData(origin []byte) ([]byte, error)
}

interface of proxy client, supported types: http/https/socks5

type ProxyRegister added in v0.5.0

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

type ProxyServer added in v0.3.0

type ProxyServer struct {
	Id       ksuid.KSUID // id of proxy connection
	ProxyIns ProxyEstablish
}

type ServerData added in v0.3.0

type ServerData struct {
	Tag  int
	Data []byte
}

type ServerWS

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

func NewServeWS added in v0.5.0

func NewServeWS(hc *HubCollection, config WebsocksServerConfig) *ServerWS

return a a function handling websocket requests from the peer.

func (*ServerWS) ServeHTTP added in v0.5.0

func (s *ServerWS) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Socks5Client added in v0.3.0

type Socks5Client struct {
}

func (*Socks5Client) EstablishData added in v0.3.0

func (client *Socks5Client) EstablishData(origin []byte) ([]byte, error)

func (*Socks5Client) ParseHeader added in v0.3.0

func (client *Socks5Client) ParseHeader(conn net.Conn, header []byte) (string, error)

parsing socks5 header, and return address and parsing error

func (*Socks5Client) ProxyType added in v0.3.0

func (client *Socks5Client) ProxyType() int

func (*Socks5Client) Trigger added in v0.3.0

func (client *Socks5Client) Trigger(data []byte) bool

type VersionNeg

type VersionNeg struct {
	Version          string `json:"version"`
	CompVersion      uint   `json:"comp_version"` // Compatible version code
	VersionCode      uint   `json:"version_code"`
	EnableStatusPage bool   `json:"status_page"`
}

func ExchangeVersion

func ExchangeVersion(ctx context.Context, wsConn *websocket.Conn) (VersionNeg, error)

negotiate client and server version after websocket connection is established, client can receive a message from server with server version number.

type WebSocketClient

type WebSocketClient struct {
	ConcurrentWebSocket
	// contains filtered or unexported fields
}

WebSocketClient is a collection of proxy clients. It can add/remove proxy clients from this collection, and dispatch web socket message to a specific proxy client.

func NewWebSocketClient

func NewWebSocketClient(ctx context.Context, addr string, hc *http.Client, header http.Header) (*WebSocketClient, error)

Establish websocket connection. And initialize proxies container.

func (*WebSocketClient) Close

func (wsc *WebSocketClient) Close() error

func (*WebSocketClient) ConnSize

func (wsc *WebSocketClient) ConnSize() int

get the connection size

func (*WebSocketClient) GetProxyById

func (wsc *WebSocketClient) GetProxyById(id ksuid.KSUID) *ProxyClient

func (*WebSocketClient) ListenIncomeMsg

func (wsc *WebSocketClient) ListenIncomeMsg(readLimit int64) error

listen income websocket messages and dispatch to different proxies.

func (*WebSocketClient) NewProxy

func (wsc *WebSocketClient) NewProxy(onData func(ksuid.KSUID, ServerData),
	onClosed func(ksuid.KSUID, bool), onError func(ksuid.KSUID, error)) *ProxyClient

create a new proxy with unique id

func (*WebSocketClient) RemoveProxy added in v0.3.0

func (wsc *WebSocketClient) RemoveProxy(id ksuid.KSUID)

remove current proxy by id

func (*WebSocketClient) TellClose

func (wsc *WebSocketClient) TellClose(id ksuid.KSUID) error

tell the remote proxy server to close this connection.

type WebSocketMessage

type WebSocketMessage struct {
	Id   string      `json:"id"`
	Type string      `json:"type"`
	Data interface{} `json:"data"` // json.RawMessage
}

type WebsocksServerConfig added in v0.3.0

type WebsocksServerConfig struct {
	EnableHttp       bool
	EnableConnKey    bool   // bale connection key
	ConnKey          string // connection key
	EnableStatusPage bool   // enable/disable status page
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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