Documentation ¶
Overview ¶
Package ws implements websocket handshake.
Reference ¶
websocket rfc: https://datatracker.ietf.org/doc/html/rfc6455/
下面把一个握手放在这里作为参考
请求 GET /chat HTTP/1.1
Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13 Origin: http://example.com
响应 HTTP/1.1 101 Switching Protocols
Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= Sec-WebSocket-Protocol: chat
总之,一个websocket的请求头直接就是一个 合法的http请求头,所以也没必要额外包一个http连接, 直接使用tcp/tls 连接即可。
websocket 库比较 https://yalantis.com/blog/how-to-build-websockets-in-go/
中文翻译: https://tonybai.com/2019/09/28/how-to-build-websockets-in-go/
总之 gobwas/ws 是最好的库. 本包使用 gobwas/ws
Index ¶
- Constants
- type Client
- type Conn
- func (c *Conn) CanSplice() (r bool, conn net.Conn)
- func (c *Conn) EverPossibleToSplice() bool
- func (c *Conn) Read(p []byte) (int, error)
- func (c *Conn) ReadFrom(r io.Reader) (written int64, err error)
- func (c *Conn) Write(p []byte) (n int, e error)
- func (c *Conn) WriteBuffers(buffers [][]byte) (int64, error)
- type Creator
- type EarlyDataConn
- type Server
Constants ¶
const MaxEarlyDataLen_Base64 = 2732
2048 /3 = 682.6666 , 683 * 4 = 2732, 若你不信,运行 we_test.go中的 TestBase64Len
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { UseEarlyData bool // contains filtered or unexported fields }
implements advLayer.Client
type Conn ¶
实现 net.Conn, io.ReaderFrom, utils.MultiWriter, netLayer.Splicer 因为 gobwas/ws 不包装conn,在写入和读取二进制时需要使用 较为底层的函数才行,并未被提供标准的Read和Write 因此我们包装一下,统一使用Read和Write函数 来读写 二进制数据。因为我们这里是代理, 所以我们默认 抛弃 websocket的 数据帧 长度。 如果以后考虑与 vless v1的 udp 相结合的话,则数据包长度 不能丢弃,需要使用另外的实现。
func (*Conn) EverPossibleToSplice ¶
type Creator ¶
type Creator struct{}
func (Creator) GetDefaultAlpn ¶
func (Creator) NewClientFromConf ¶
func (Creator) NewServerFromConf ¶
type EarlyDataConn ¶
type Server ¶
type Server struct { //upgrader *ws.Upgrader UseEarlyData bool Thepath string // contains filtered or unexported fields }