sing-box/transport/v2raywebsocket/client.go

111 lines
2.9 KiB
Go
Raw Normal View History

2022-08-22 20:20:56 +08:00
package v2raywebsocket
import (
"context"
"net"
"net/http"
"net/url"
"time"
"github.com/sagernet/sing-box/adapter"
2022-09-09 18:45:10 +08:00
"github.com/sagernet/sing-box/common/tls"
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"
2022-09-13 10:41:10 +08:00
"github.com/sagernet/websocket"
2022-08-22 20:20:56 +08:00
)
var _ adapter.V2RayClientTransport = (*Client)(nil)
type Client struct {
dialer *websocket.Dialer
2023-07-11 15:12:26 +08:00
requestURL url.URL
requestURLString string
2022-08-22 20:20:56 +08:00
headers http.Header
maxEarlyData uint32
earlyDataHeaderName string
}
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayWebsocketOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) {
2022-08-22 20:20:56 +08:00
wsDialer := &websocket.Dialer{
ReadBufferSize: 4 * 1024,
WriteBufferSize: 4 * 1024,
HandshakeTimeout: time.Second * 8,
}
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
wsDialer.NetDialTLSContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := dialer.DialContext(ctx, network, M.ParseSocksaddr(addr))
if err != nil {
return nil, err
}
tlsConn, err := tls.ClientHandshake(ctx, conn, tlsConfig)
if err != nil {
return nil, err
}
return &deadConn{tlsConn}, nil
2022-09-09 18:45:10 +08:00
}
} else {
wsDialer.NetDialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := dialer.DialContext(ctx, network, M.ParseSocksaddr(addr))
if err != nil {
return nil, err
}
return &deadConn{conn}, nil
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 {
return nil, E.Cause(err, "parse path")
}
2022-08-22 20:20:56 +08:00
headers := make(http.Header)
for key, value := range options.Headers {
headers[key] = value
if key == "Host" {
if len(value) > 1 {
return nil, E.New("multiple Host headers")
}
requestURL.Host = value[0]
}
2022-08-22 20:20:56 +08:00
}
return &Client{
wsDialer,
2023-07-11 15:12:26 +08:00
requestURL,
requestURL.String(),
2022-08-22 20:20:56 +08:00
headers,
options.MaxEarlyData,
options.EarlyDataHeaderName,
}, nil
2022-08-22 20:20:56 +08:00
}
func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
if c.maxEarlyData <= 0 {
2023-07-11 15:12:26 +08:00
conn, response, err := c.dialer.DialContext(ctx, c.requestURLString, c.headers)
2022-08-22 20:20:56 +08:00
if err == nil {
2022-09-30 11:27:18 +08:00
return &WebsocketConn{Conn: conn, Writer: NewWriter(conn, false)}, nil
2022-08-22 20:20:56 +08:00
}
return nil, wrapDialError(response, err)
} 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
}
}
func wrapDialError(response *http.Response, err error) error {
if response == nil {
return err
}
return E.Extend(err, "HTTP ", response.StatusCode, " ", response.Status)
}