sing-box/transport/v2raywebsocket/client.go

115 lines
3.0 KiB
Go
Raw Normal View History

2022-08-22 20:20:56 +08:00
package v2raywebsocket
import (
"context"
"net"
"net/http"
"net/url"
2023-11-09 16:59:44 +08:00
"strings"
2022-08-22 20:20:56 +08:00
"time"
"github.com/sagernet/sing-box/adapter"
2022-09-09 18:45:10 +08:00
"github.com/sagernet/sing-box/common/tls"
2023-11-09 16:59:44 +08:00
C "github.com/sagernet/sing-box/constant"
2022-08-22 20:20:56 +08:00
"github.com/sagernet/sing-box/option"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
2023-04-12 15:14:55 +08:00
sHTTP "github.com/sagernet/sing/protocol/http"
2023-11-09 16:59:44 +08:00
"github.com/sagernet/ws"
2022-08-22 20:20:56 +08:00
)
var _ adapter.V2RayClientTransport = (*Client)(nil)
type Client struct {
2023-11-09 16:59:44 +08:00
dialer N.Dialer
tlsConfig tls.Config
serverAddr M.Socksaddr
2023-07-11 15:12:26 +08:00
requestURL url.URL
2022-08-22 20:20:56 +08:00
headers http.Header
maxEarlyData uint32
earlyDataHeaderName string
}
2023-11-09 16:59:44 +08:00
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayWebsocketOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) {
2022-09-09 18:45:10 +08:00
if tlsConfig != nil {
2022-11-11 20:01:49 +08:00
if len(tlsConfig.NextProtos()) == 0 {
tlsConfig.SetNextProtos([]string{"http/1.1"})
}
2022-09-09 18:45:10 +08:00
}
2023-07-11 15:12:26 +08:00
var requestURL url.URL
2022-08-22 20:20:56 +08:00
if tlsConfig == nil {
2023-07-11 15:12:26 +08:00
requestURL.Scheme = "ws"
2022-08-22 20:20:56 +08:00
} else {
2023-07-11 15:12:26 +08:00
requestURL.Scheme = "wss"
2022-08-22 20:20:56 +08:00
}
2023-07-11 15:12:26 +08:00
requestURL.Host = serverAddr.String()
requestURL.Path = options.Path
err := sHTTP.URLSetPath(&requestURL, options.Path)
2023-04-12 15:14:55 +08:00
if err != nil {
2023-11-09 16:59:44 +08:00
return nil, E.Cause(err, "parse path")
}
if !strings.HasPrefix(requestURL.Path, "/") {
requestURL.Path = "/" + requestURL.Path
}
2022-08-22 20:20:56 +08:00
headers := make(http.Header)
for key, value := range options.Headers {
headers[key] = value
2023-11-09 16:59:44 +08:00
if key == "Host" {
if len(value) > 1 {
return nil, E.New("multiple Host headers")
}
requestURL.Host = value[0]
}
}
if headers.Get("User-Agent") == "" {
headers.Set("User-Agent", "Go-http-client/1.1")
2022-08-22 20:20:56 +08:00
}
return &Client{
2023-11-09 16:59:44 +08:00
dialer,
tlsConfig,
serverAddr,
2023-07-11 15:12:26 +08:00
requestURL,
2022-08-22 20:20:56 +08:00
headers,
options.MaxEarlyData,
options.EarlyDataHeaderName,
2023-11-09 16:59:44 +08: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
}
}
conn.SetDeadline(time.Now().Add(C.TCPTimeout))
var protocols []string
if protocolHeader := headers.Get("Sec-WebSocket-Protocol"); protocolHeader != "" {
protocols = []string{protocolHeader}
headers.Del("Sec-WebSocket-Protocol")
}
2023-11-09 16:59:44 +08:00
reader, _, err := ws.Dialer{Header: ws.HandshakeHeaderHTTP(headers), Protocols: protocols}.Upgrade(conn, requestURL)
conn.SetDeadline(time.Time{})
if err != nil {
return nil, err
}
return NewConn(conn, reader, nil, ws.StateClientSide), nil
2022-08-22 20:20:56 +08:00
}
func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
if c.maxEarlyData <= 0 {
2023-11-09 16:59:44 +08:00
conn, err := c.dialContext(ctx, &c.requestURL, c.headers)
if err != nil {
return nil, err
2022-08-22 20:20:56 +08:00
}
2023-11-09 16:59:44 +08:00
return conn, nil
2022-08-22 20:20:56 +08:00
} else {
2022-08-26 10:22:20 +08:00
return &EarlyWebsocketConn{Client: c, ctx: ctx, create: make(chan struct{})}, nil
2022-08-22 20:20:56 +08:00
}
}