mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
Bound httpupgrade and websocket upgrade handshakes by context and timeout
This commit is contained in:
@@ -10,7 +10,9 @@ import (
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/common/tls"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/common/buf"
|
||||
"github.com/sagernet/sing/common/bufio"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
@@ -77,7 +79,7 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
|
||||
func (c *Client) DialContext(ctx context.Context) (result net.Conn, err error) {
|
||||
conn, err := c.dialer.DialContext(ctx, N.NetworkTCP, c.serverAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -90,6 +92,18 @@ func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
|
||||
}
|
||||
request.Header.Set("Connection", "Upgrade")
|
||||
request.Header.Set("Upgrade", "websocket")
|
||||
ctx, cancel := context.WithTimeout(ctx, C.TCPTimeout)
|
||||
defer cancel()
|
||||
stop := context.AfterFunc(ctx, func() {
|
||||
conn.Close()
|
||||
})
|
||||
defer func() {
|
||||
if !stop() {
|
||||
common.Close(result)
|
||||
result = nil
|
||||
err = ctx.Err()
|
||||
}
|
||||
}()
|
||||
err = request.Write(conn)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
@@ -116,7 +130,7 @@ func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
|
||||
conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
conn = bufio.NewCachedConn(conn, buffer)
|
||||
return bufio.NewCachedConn(conn, buffer), nil
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/common/tls"
|
||||
@@ -14,7 +13,6 @@ import (
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing/common/buf"
|
||||
"github.com/sagernet/sing/common/bufio"
|
||||
"github.com/sagernet/sing/common/bufio/deadline"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
@@ -81,21 +79,14 @@ func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
|
||||
if c.maxEarlyData > 0 {
|
||||
return &EarlyWebsocketConn{Client: c, rawConn: conn, create: make(chan struct{})}, nil
|
||||
}
|
||||
websocketConn, err := c.upgrade(conn, &c.requestURL, c.headers)
|
||||
websocketConn, err := c.upgrade(ctx, conn, &c.requestURL, c.headers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return websocketConn, nil
|
||||
}
|
||||
|
||||
func (c *Client) upgrade(conn net.Conn, requestURL *url.URL, headers http.Header) (*WebsocketConn, error) {
|
||||
var deadlineConn net.Conn
|
||||
if deadline.NeedAdditionalReadDeadline(conn) {
|
||||
deadlineConn = deadline.NewConn(conn)
|
||||
} else {
|
||||
deadlineConn = conn
|
||||
}
|
||||
deadlineConn.SetDeadline(time.Now().Add(C.TCPTimeout))
|
||||
func (c *Client) upgrade(ctx context.Context, conn net.Conn, requestURL *url.URL, headers http.Header) (*WebsocketConn, error) {
|
||||
var protocols []string
|
||||
protocolHeader := headers.Get("Sec-WebSocket-Protocol")
|
||||
if protocolHeader != "" {
|
||||
@@ -103,8 +94,15 @@ func (c *Client) upgrade(conn net.Conn, requestURL *url.URL, headers http.Header
|
||||
headers = headers.Clone()
|
||||
headers.Del("Sec-WebSocket-Protocol")
|
||||
}
|
||||
reader, _, err := ws.Dialer{Header: ws.HandshakeHeaderHTTP(headers), Protocols: protocols}.Upgrade(deadlineConn, requestURL)
|
||||
deadlineConn.SetDeadline(time.Time{})
|
||||
ctx, cancel := context.WithTimeout(ctx, C.TCPTimeout)
|
||||
defer cancel()
|
||||
stop := context.AfterFunc(ctx, func() {
|
||||
conn.Close()
|
||||
})
|
||||
reader, _, err := ws.Dialer{Header: ws.HandshakeHeaderHTTP(headers), Protocols: protocols}.Upgrade(conn, requestURL)
|
||||
if !stop() {
|
||||
err = ctx.Err()
|
||||
}
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return nil, err
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package v2raywebsocket
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"io"
|
||||
@@ -155,8 +156,6 @@ func (c *EarlyWebsocketConn) writeRequest(content []byte) error {
|
||||
var (
|
||||
earlyData []byte
|
||||
lateData []byte
|
||||
conn *WebsocketConn
|
||||
err error
|
||||
)
|
||||
if len(content) > int(c.maxEarlyData) {
|
||||
earlyData = content[:c.maxEarlyData]
|
||||
@@ -164,20 +163,18 @@ func (c *EarlyWebsocketConn) writeRequest(content []byte) error {
|
||||
} else {
|
||||
earlyData = content
|
||||
}
|
||||
requestURL := c.requestURL
|
||||
headers := c.headers
|
||||
if len(earlyData) > 0 {
|
||||
earlyDataString := base64.RawURLEncoding.EncodeToString(earlyData)
|
||||
if c.earlyDataHeaderName == "" {
|
||||
requestURL := c.requestURL
|
||||
requestURL.Path += earlyDataString
|
||||
conn, err = c.upgrade(c.rawConn, &requestURL, c.headers)
|
||||
} else {
|
||||
headers := c.headers.Clone()
|
||||
headers = headers.Clone()
|
||||
headers.Set(c.earlyDataHeaderName, earlyDataString)
|
||||
conn, err = c.upgrade(c.rawConn, &c.requestURL, headers)
|
||||
}
|
||||
} else {
|
||||
conn, err = c.upgrade(c.rawConn, &c.requestURL, c.headers)
|
||||
}
|
||||
conn, err := c.upgrade(context.Background(), c.rawConn, &requestURL, headers)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user