sing-box/transport/v2raywebsocket/conn.go

277 lines
5.4 KiB
Go
Raw Normal View History

2022-08-22 12:20:56 +00:00
package v2raywebsocket
import (
2023-11-09 08:59:44 +00:00
"bufio"
2022-08-26 02:22:20 +00:00
"context"
2022-08-22 12:20:56 +00:00
"encoding/base64"
"io"
"net"
"os"
2023-09-16 14:30:42 +00:00
"sync"
2022-08-22 12:20:56 +00:00
"time"
2022-08-25 02:44:58 +00:00
C "github.com/sagernet/sing-box/constant"
2022-10-07 12:18:50 +00:00
"github.com/sagernet/sing/common"
2022-09-23 09:21:24 +00:00
"github.com/sagernet/sing/common/buf"
2023-11-09 08:59:44 +00:00
"github.com/sagernet/sing/common/debug"
2022-08-22 12:20:56 +00:00
E "github.com/sagernet/sing/common/exceptions"
2023-11-09 08:59:44 +00:00
"github.com/sagernet/ws"
"github.com/sagernet/ws/wsutil"
2022-08-22 12:20:56 +00:00
)
type WebsocketConn struct {
2023-11-09 08:59:44 +00:00
net.Conn
2022-09-23 09:21:24 +00:00
*Writer
2023-11-09 08:59:44 +00:00
state ws.State
reader *wsutil.Reader
controlHandler wsutil.FrameHandlerFunc
remoteAddr net.Addr
2022-08-22 12:20:56 +00:00
}
2023-11-09 08:59:44 +00:00
func NewConn(conn net.Conn, br *bufio.Reader, remoteAddr net.Addr, state ws.State) *WebsocketConn {
controlHandler := wsutil.ControlFrameHandler(conn, state)
var reader io.Reader
if br != nil && br.Buffered() > 0 {
reader = br
} else {
reader = conn
}
2022-09-23 09:21:24 +00:00
return &WebsocketConn{
2023-11-09 08:59:44 +00:00
Conn: conn,
state: state,
reader: &wsutil.Reader{
Source: reader,
State: state,
SkipHeaderCheck: !debug.Enabled,
OnIntermediate: controlHandler,
},
controlHandler: controlHandler,
remoteAddr: remoteAddr,
Writer: NewWriter(conn, state),
2022-09-23 09:21:24 +00:00
}
}
2022-08-25 02:44:58 +00:00
func (c *WebsocketConn) Close() error {
2023-11-09 08:59:44 +00:00
c.Conn.SetWriteDeadline(time.Now().Add(C.TCPTimeout))
frame := ws.NewCloseFrame(ws.NewCloseFrameBody(
ws.StatusNormalClosure, "",
))
if c.state == ws.StateClientSide {
frame = ws.MaskFrameInPlace(frame)
2022-08-25 02:44:58 +00:00
}
2023-11-09 08:59:44 +00:00
ws.WriteFrame(c.Conn, frame)
c.Conn.Close()
2022-08-25 02:44:58 +00:00
return nil
}
2022-08-22 12:20:56 +00:00
func (c *WebsocketConn) Read(b []byte) (n int, err error) {
2023-11-09 08:59:44 +00:00
var header ws.Header
2022-08-22 12:20:56 +00:00
for {
2023-11-09 08:59:44 +00:00
n, err = c.reader.Read(b)
if n > 0 {
err = nil
return
}
if !E.IsMulti(err, io.EOF, wsutil.ErrNoFrameAdvance) {
return
}
header, err = c.reader.NextFrame()
if err != nil {
return
}
if header.OpCode.IsControl() {
err = c.controlHandler(header, c.reader)
2022-08-22 12:20:56 +00:00
if err != nil {
return
}
2023-11-09 08:59:44 +00:00
continue
2022-08-22 12:20:56 +00:00
}
2023-11-09 08:59:44 +00:00
if header.OpCode&ws.OpBinary == 0 {
err = c.reader.Discard()
if err != nil {
return
}
2022-08-22 12:20:56 +00:00
continue
}
2023-11-09 08:59:44 +00:00
}
}
func (c *WebsocketConn) Write(p []byte) (n int, err error) {
err = wsutil.WriteMessage(c.Conn, c.state, ws.OpBinary, p)
if err != nil {
2022-08-22 12:20:56 +00:00
return
}
2023-11-09 08:59:44 +00:00
n = len(p)
return
2022-08-22 12:20:56 +00:00
}
func (c *WebsocketConn) RemoteAddr() net.Addr {
if c.remoteAddr != nil {
return c.remoteAddr
}
return c.Conn.RemoteAddr()
}
func (c *WebsocketConn) SetDeadline(t time.Time) error {
return os.ErrInvalid
}
2023-04-11 02:35:44 +00:00
func (c *WebsocketConn) SetReadDeadline(t time.Time) error {
return os.ErrInvalid
}
func (c *WebsocketConn) SetWriteDeadline(t time.Time) error {
return os.ErrInvalid
}
2023-04-19 13:48:54 +00:00
func (c *WebsocketConn) NeedAdditionalReadDeadline() bool {
return true
}
2022-10-07 12:18:50 +00:00
func (c *WebsocketConn) Upstream() any {
2023-11-09 08:59:44 +00:00
return c.Conn
2022-09-23 09:21:24 +00:00
}
2022-08-22 12:20:56 +00:00
type EarlyWebsocketConn struct {
*Client
2022-08-26 02:22:20 +00:00
ctx context.Context
2022-08-22 12:20:56 +00:00
conn *WebsocketConn
2023-09-16 14:30:42 +00:00
access sync.Mutex
2022-08-22 12:20:56 +00:00
create chan struct{}
2023-07-11 07:12:26 +00:00
err error
2022-08-22 12:20:56 +00:00
}
func (c *EarlyWebsocketConn) Read(b []byte) (n int, err error) {
if c.conn == nil {
<-c.create
2023-07-11 07:12:26 +00:00
if c.err != nil {
return 0, c.err
}
2022-08-22 12:20:56 +00:00
}
return c.conn.Read(b)
}
2023-07-11 07:12:26 +00:00
func (c *EarlyWebsocketConn) writeRequest(content []byte) error {
2022-08-22 12:20:56 +00:00
var (
earlyData []byte
lateData []byte
2023-11-09 08:59:44 +00:00
conn *WebsocketConn
2023-07-11 07:12:26 +00:00
err error
2022-08-22 12:20:56 +00:00
)
2023-07-11 07:12:26 +00:00
if len(content) > int(c.maxEarlyData) {
earlyData = content[:c.maxEarlyData]
lateData = content[c.maxEarlyData:]
2022-08-22 12:20:56 +00:00
} else {
2023-07-11 07:12:26 +00:00
earlyData = content
2022-08-22 12:20:56 +00:00
}
if len(earlyData) > 0 {
earlyDataString := base64.RawURLEncoding.EncodeToString(earlyData)
if c.earlyDataHeaderName == "" {
2023-07-11 07:12:26 +00:00
requestURL := c.requestURL
requestURL.Path += earlyDataString
2023-11-09 08:59:44 +00:00
conn, err = c.dialContext(c.ctx, &requestURL, c.headers)
2022-08-22 12:20:56 +00:00
} else {
headers := c.headers.Clone()
headers.Set(c.earlyDataHeaderName, earlyDataString)
2023-11-09 08:59:44 +00:00
conn, err = c.dialContext(c.ctx, &c.requestURL, headers)
2022-08-22 12:20:56 +00:00
}
} else {
2023-11-09 08:59:44 +00:00
conn, err = c.dialContext(c.ctx, &c.requestURL, c.headers)
2022-08-22 12:20:56 +00:00
}
if err != nil {
2023-11-09 08:59:44 +00:00
return err
2022-08-22 12:20:56 +00:00
}
if len(lateData) > 0 {
2023-11-09 08:59:44 +00:00
_, err = conn.Write(lateData)
if err != nil {
return err
}
2022-08-22 12:20:56 +00:00
}
2023-11-09 08:59:44 +00:00
c.conn = conn
return nil
2023-07-11 07:12:26 +00:00
}
func (c *EarlyWebsocketConn) Write(b []byte) (n int, err error) {
2023-09-16 14:30:42 +00:00
if c.conn != nil {
return c.conn.Write(b)
}
c.access.Lock()
defer c.access.Unlock()
2023-11-13 05:27:59 +00:00
if c.err != nil {
return 0, c.err
}
2023-07-11 07:12:26 +00:00
if c.conn != nil {
return c.conn.Write(b)
}
err = c.writeRequest(b)
c.err = err
close(c.create)
2022-08-22 12:20:56 +00:00
if err != nil {
return
}
return len(b), nil
}
2022-09-23 09:21:24 +00:00
func (c *EarlyWebsocketConn) WriteBuffer(buffer *buf.Buffer) error {
2023-09-16 14:30:42 +00:00
if c.conn != nil {
return c.conn.WriteBuffer(buffer)
}
c.access.Lock()
defer c.access.Unlock()
2022-09-23 09:21:24 +00:00
if c.conn != nil {
return c.conn.WriteBuffer(buffer)
}
2023-11-13 05:27:59 +00:00
if c.err != nil {
return c.err
}
2023-07-11 07:12:26 +00:00
err := c.writeRequest(buffer.Bytes())
c.err = err
2022-09-23 09:21:24 +00:00
close(c.create)
return err
}
2022-08-22 12:20:56 +00:00
func (c *EarlyWebsocketConn) Close() error {
if c.conn == nil {
return nil
}
return c.conn.Close()
}
func (c *EarlyWebsocketConn) LocalAddr() net.Addr {
if c.conn == nil {
return nil
}
return c.conn.LocalAddr()
}
func (c *EarlyWebsocketConn) RemoteAddr() net.Addr {
if c.conn == nil {
return nil
}
return c.conn.RemoteAddr()
}
func (c *EarlyWebsocketConn) SetDeadline(t time.Time) error {
2023-04-11 02:35:44 +00:00
return os.ErrInvalid
2022-08-22 12:20:56 +00:00
}
func (c *EarlyWebsocketConn) SetReadDeadline(t time.Time) error {
2023-04-11 02:35:44 +00:00
return os.ErrInvalid
2022-08-22 12:20:56 +00:00
}
func (c *EarlyWebsocketConn) SetWriteDeadline(t time.Time) error {
2023-04-11 02:35:44 +00:00
return os.ErrInvalid
2022-08-22 12:20:56 +00:00
}
2022-08-25 02:44:58 +00:00
2023-04-19 13:48:54 +00:00
func (c *EarlyWebsocketConn) NeedAdditionalReadDeadline() bool {
return true
}
2022-10-07 12:18:50 +00:00
func (c *EarlyWebsocketConn) Upstream() any {
return common.PtrOrNil(c.conn)
}
func (c *EarlyWebsocketConn) LazyHeadroom() bool {
return c.conn == nil
2022-09-23 09:21:24 +00:00
}