sing-box/transport/v2raywebsocket/client.go

130 lines
3.5 KiB
Go
Raw Permalink Normal View History

2022-08-22 12:20:56 +00:00
package v2raywebsocket
import (
"context"
"net"
"net/http"
"net/url"
2023-11-09 08:59:44 +00:00
"strings"
2022-08-22 12:20:56 +00:00
"time"
"github.com/sagernet/sing-box/adapter"
2022-09-09 10:45:10 +00:00
"github.com/sagernet/sing-box/common/tls"
2023-11-09 08:59:44 +00:00
C "github.com/sagernet/sing-box/constant"
2022-08-22 12:20:56 +00:00
"github.com/sagernet/sing-box/option"
2023-12-01 04:24:29 +00:00
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
"github.com/sagernet/sing/common/bufio/deadline"
2022-08-22 12:20:56 +00:00
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
2023-04-12 07:14:55 +00:00
sHTTP "github.com/sagernet/sing/protocol/http"
2023-11-09 08:59:44 +00:00
"github.com/sagernet/ws"
2022-08-22 12:20:56 +00:00
)
var _ adapter.V2RayClientTransport = (*Client)(nil)
type Client struct {
2023-11-09 08:59:44 +00:00
dialer N.Dialer
tlsConfig tls.Config
serverAddr M.Socksaddr
2023-07-11 07:12:26 +00:00
requestURL url.URL
2022-08-22 12:20:56 +00:00
headers http.Header
maxEarlyData uint32
earlyDataHeaderName string
}
2023-11-09 08:59:44 +00:00
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayWebsocketOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) {
2022-09-09 10:45:10 +00:00
if tlsConfig != nil {
2022-11-11 12:01:49 +00:00
if len(tlsConfig.NextProtos()) == 0 {
tlsConfig.SetNextProtos([]string{"http/1.1"})
}
2022-09-09 10:45:10 +00:00
}
2023-07-11 07:12:26 +00:00
var requestURL url.URL
2022-08-22 12:20:56 +00:00
if tlsConfig == nil {
2023-07-11 07:12:26 +00:00
requestURL.Scheme = "ws"
2022-08-22 12:20:56 +00:00
} else {
2023-07-11 07:12:26 +00:00
requestURL.Scheme = "wss"
2022-08-22 12:20:56 +00:00
}
2023-07-11 07:12:26 +00:00
requestURL.Host = serverAddr.String()
requestURL.Path = options.Path
err := sHTTP.URLSetPath(&requestURL, options.Path)
2023-04-12 07:14:55 +00:00
if err != nil {
2023-11-09 08:59:44 +00:00
return nil, E.Cause(err, "parse path")
}
if !strings.HasPrefix(requestURL.Path, "/") {
requestURL.Path = "/" + requestURL.Path
}
2023-12-22 12:37:49 +00:00
headers := options.Headers.Build()
if host := headers.Get("Host"); host != "" {
headers.Del("Host")
requestURL.Host = host
2023-11-09 08:59:44 +00:00
}
if headers.Get("User-Agent") == "" {
headers.Set("User-Agent", "Go-http-client/1.1")
2022-08-22 12:20:56 +00:00
}
return &Client{
2023-11-09 08:59:44 +00:00
dialer,
tlsConfig,
serverAddr,
2023-07-11 07:12:26 +00:00
requestURL,
2022-08-22 12:20:56 +00:00
headers,
options.MaxEarlyData,
options.EarlyDataHeaderName,
2023-11-09 08:59:44 +00:00
}, nil
}
func (c *Client) dialContext(ctx context.Context, requestURL *url.URL, headers http.Header) (*WebsocketConn, error) {
conn, err := c.dialer.DialContext(ctx, N.NetworkTCP, c.serverAddr)
if err != nil {
return nil, err
}
if c.tlsConfig != nil {
conn, err = tls.ClientHandshake(ctx, conn, c.tlsConfig)
if err != nil {
return nil, err
}
}
2023-12-01 04:24:29 +00:00
var deadlineConn net.Conn
if deadline.NeedAdditionalReadDeadline(conn) {
deadlineConn = deadline.NewConn(conn)
} else {
deadlineConn = conn
}
err = deadlineConn.SetDeadline(time.Now().Add(C.TCPTimeout))
if err != nil {
return nil, E.Cause(err, "set read deadline")
}
2023-11-09 08:59:44 +00:00
var protocols []string
if protocolHeader := headers.Get("Sec-WebSocket-Protocol"); protocolHeader != "" {
protocols = []string{protocolHeader}
headers.Del("Sec-WebSocket-Protocol")
}
2023-12-01 04:24:29 +00:00
reader, _, err := ws.Dialer{Header: ws.HandshakeHeaderHTTP(headers), Protocols: protocols}.Upgrade(deadlineConn, requestURL)
deadlineConn.SetDeadline(time.Time{})
2023-11-09 08:59:44 +00:00
if err != nil {
return nil, err
}
2023-12-01 04:24:29 +00:00
if reader != nil {
buffer := buf.NewSize(reader.Buffered())
_, err = buffer.ReadFullFrom(reader, buffer.Len())
if err != nil {
return nil, err
}
conn = bufio.NewCachedConn(conn, buffer)
}
return NewConn(conn, nil, ws.StateClientSide), nil
2022-08-22 12:20:56 +00:00
}
func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
if c.maxEarlyData <= 0 {
2023-11-09 08:59:44 +00:00
conn, err := c.dialContext(ctx, &c.requestURL, c.headers)
if err != nil {
return nil, err
2022-08-22 12:20:56 +00:00
}
2023-11-09 08:59:44 +00:00
return conn, nil
2022-08-22 12:20:56 +00:00
} else {
2022-08-26 02:22:20 +00:00
return &EarlyWebsocketConn{Client: c, ctx: ctx, create: make(chan struct{})}, nil
2022-08-22 12:20:56 +00:00
}
}