2020-11-25 11:01:53 +00:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
|
|
"github.com/xtls/xray-core/common/errors"
|
|
|
|
"github.com/xtls/xray-core/common/serial"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
2021-10-19 16:57:14 +00:00
|
|
|
var _ buf.Writer = (*connection)(nil)
|
2020-11-25 11:01:53 +00:00
|
|
|
|
|
|
|
// connection is a wrapper for net.Conn over WebSocket connection.
|
2024-07-17 10:40:25 +00:00
|
|
|
// remoteAddr is used to pass "virtual" remote IP addresses in X-Forwarded-For.
|
|
|
|
// so we shouldn't directly read it form conn.
|
2020-11-25 11:01:53 +00:00
|
|
|
type connection struct {
|
2024-07-17 10:40:25 +00:00
|
|
|
conn *websocket.Conn
|
|
|
|
reader io.Reader
|
|
|
|
remoteAddr net.Addr
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
2024-11-29 02:08:08 +00:00
|
|
|
func NewConnection(conn *websocket.Conn, remoteAddr net.Addr, extraReader io.Reader, heartbeatPeriod uint32) *connection {
|
|
|
|
if heartbeatPeriod != 0 {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Duration(heartbeatPeriod) * time.Second)
|
|
|
|
if err := conn.WriteControl(websocket.PingMessage, []byte{}, time.Time{}); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
return &connection{
|
2024-07-17 10:40:25 +00:00
|
|
|
conn: conn,
|
|
|
|
remoteAddr: remoteAddr,
|
|
|
|
reader: extraReader,
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read implements net.Conn.Read()
|
|
|
|
func (c *connection) Read(b []byte) (int, error) {
|
|
|
|
for {
|
|
|
|
reader, err := c.getReader()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nBytes, err := reader.Read(b)
|
|
|
|
if errors.Cause(err) == io.EOF {
|
|
|
|
c.reader = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nBytes, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) getReader() (io.Reader, error) {
|
|
|
|
if c.reader != nil {
|
|
|
|
return c.reader, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, reader, err := c.conn.NextReader()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.reader = reader
|
|
|
|
return reader, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements io.Writer.
|
|
|
|
func (c *connection) Write(b []byte) (int, error) {
|
|
|
|
if err := c.conn.WriteMessage(websocket.BinaryMessage, b); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
|
|
|
mb = buf.Compact(mb)
|
|
|
|
mb, err := buf.WriteMultiBuffer(c, mb)
|
|
|
|
buf.ReleaseMulti(mb)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) Close() error {
|
2024-06-29 18:32:57 +00:00
|
|
|
var errs []interface{}
|
2020-11-25 11:01:53 +00:00
|
|
|
if err := c.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5)); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
errs = append(errs, err)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
if err := c.conn.Close(); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
errs = append(errs, err)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
2024-06-29 18:32:57 +00:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return errors.New("failed to close connection").Base(errors.New(serial.Concat(errs...)))
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) LocalAddr() net.Addr {
|
|
|
|
return c.conn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) RemoteAddr() net.Addr {
|
2024-07-17 10:40:25 +00:00
|
|
|
return c.remoteAddr
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) SetDeadline(t time.Time) error {
|
|
|
|
if err := c.SetReadDeadline(t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.SetWriteDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) SetReadDeadline(t time.Time) error {
|
|
|
|
return c.conn.SetReadDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) SetWriteDeadline(t time.Time) error {
|
|
|
|
return c.conn.SetWriteDeadline(t)
|
|
|
|
}
|