1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-20 10:56:07 -04:00
v2fly/transport/internet/websocket/dialer.go

217 lines
6.8 KiB
Go
Raw Normal View History

2016-12-22 18:30:46 -05:00
package websocket
2016-08-13 09:44:36 -04:00
import (
2021-03-19 18:36:11 -04:00
"bytes"
"context"
2021-03-19 18:36:11 -04:00
"encoding/base64"
"io"
gonet "net"
"net/http"
2017-10-20 16:45:14 -04:00
"time"
2016-08-13 09:44:36 -04:00
"github.com/gorilla/websocket"
2021-02-16 15:31:50 -05:00
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/session"
"github.com/v2fly/v2ray-core/v5/features/extension"
"github.com/v2fly/v2ray-core/v5/transport/internet"
2022-12-16 15:50:53 -05:00
"github.com/v2fly/v2ray-core/v5/transport/internet/security"
2016-08-13 09:44:36 -04:00
)
2017-04-26 04:59:18 -04:00
// Dial dials a WebSocket connection to the given destination.
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
newError("creating connection to ", dest).WriteToLog(session.ExportIDToError(ctx))
2016-10-02 17:43:58 -04:00
conn, err := dialWebsocket(ctx, dest, streamSettings)
if err != nil {
2017-09-29 12:10:17 -04:00
return nil, newError("failed to dial WebSocket").Base(err)
2016-08-13 09:44:36 -04:00
}
return internet.Connection(conn), nil
2016-08-13 09:44:36 -04:00
}
func init() {
2018-08-06 07:48:35 -04:00
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
2016-08-13 09:44:36 -04:00
}
func dialWebsocket(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (net.Conn, error) {
wsSettings := streamSettings.ProtocolSettings.(*Config)
2016-10-02 17:43:58 -04:00
2017-10-20 16:45:14 -04:00
dialer := &websocket.Dialer{
NetDial: func(network, addr string) (net.Conn, error) {
return internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
2017-10-20 16:45:14 -04:00
},
2017-12-15 20:04:51 -05:00
ReadBufferSize: 4 * 1024,
WriteBufferSize: 4 * 1024,
2017-10-20 16:45:14 -04:00
HandshakeTimeout: time.Second * 8,
2016-09-30 10:53:40 -04:00
}
2016-08-13 10:50:24 -04:00
2016-09-30 10:53:40 -04:00
protocol := "ws"
2016-08-13 09:44:36 -04:00
securityEngine, err := security.CreateSecurityEngineFromSettings(ctx, streamSettings)
if err != nil {
return nil, newError("unable to create security engine").Base(err)
}
if securityEngine != nil {
2017-12-16 18:53:17 -05:00
protocol = "wss"
dialer.NetDialTLSContext = func(ctx context.Context, network, addr string) (gonet.Conn, error) {
conn, err := dialer.NetDial(network, addr)
if err != nil {
return nil, newError("dial TLS connection failed").Base(err)
}
conn, err = securityEngine.Client(conn,
security.OptionWithDestination{Dest: dest},
security.OptionWithALPN{ALPNs: []string{"http/1.1"}})
if err != nil {
return nil, newError("unable to create security protocol client from security engine").Base(err)
}
return conn, nil
}
2016-09-30 10:53:40 -04:00
}
2016-08-13 09:44:36 -04:00
2017-01-08 10:01:28 -05:00
host := dest.NetAddr()
if (protocol == "ws" && dest.Port == 80) || (protocol == "wss" && dest.Port == 443) {
host = dest.Address.String()
}
2018-03-02 03:26:14 -05:00
uri := protocol + "://" + host + wsSettings.GetNormalizedPath()
2016-08-14 02:11:51 -04:00
2021-03-19 18:36:11 -04:00
if wsSettings.UseBrowserForwarding {
2021-04-30 19:06:10 -04:00
var forwarder extension.BrowserForwarder
err := core.RequireFeatures(ctx, func(Forwarder extension.BrowserForwarder) {
2021-03-19 18:36:11 -04:00
forwarder = Forwarder
})
if err != nil {
return nil, newError("cannot find browser forwarder service").Base(err)
}
if wsSettings.MaxEarlyData != 0 {
return newRelayedConnectionWithDelayedDial(&dialerWithEarlyDataRelayed{
forwarder: forwarder,
uriBase: uri,
config: wsSettings,
}), nil
}
conn, err := forwarder.DialWebsocket(uri, nil)
if err != nil {
return nil, newError("cannot dial with browser forwarder service").Base(err)
}
return newRelayedConnection(conn), nil
}
if wsSettings.MaxEarlyData != 0 {
return newConnectionWithDelayedDial(&dialerWithEarlyData{
dialer: dialer,
uriBase: uri,
config: wsSettings,
}), nil
}
conn, resp, err := dialer.Dial(uri, wsSettings.GetRequestHeader()) // nolint: bodyclose
2016-08-13 09:44:36 -04:00
if err != nil {
2017-02-10 05:41:50 -05:00
var reason string
2016-08-14 08:41:26 -04:00
if resp != nil {
2017-02-10 05:41:50 -05:00
reason = resp.Status
2016-08-14 08:41:26 -04:00
}
2017-04-08 19:43:25 -04:00
return nil, newError("failed to dial to (", uri, "): ", reason).Base(err)
2016-08-13 09:44:36 -04:00
}
2017-02-09 05:42:17 -05:00
return newConnection(conn, conn.RemoteAddr()), nil
2016-08-13 09:44:36 -04:00
}
2021-03-19 18:36:11 -04:00
type dialerWithEarlyData struct {
dialer *websocket.Dialer
uriBase string
config *Config
}
func (d dialerWithEarlyData) Dial(earlyData []byte) (*websocket.Conn, error) {
earlyDataBuf := bytes.NewBuffer(nil)
base64EarlyDataEncoder := base64.NewEncoder(base64.RawURLEncoding, earlyDataBuf)
earlydata := bytes.NewReader(earlyData)
limitedEarlyDatareader := io.LimitReader(earlydata, int64(d.config.MaxEarlyData))
n, encerr := io.Copy(base64EarlyDataEncoder, limitedEarlyDatareader)
if encerr != nil {
return nil, newError("websocket delayed dialer cannot encode early data").Base(encerr)
}
if errc := base64EarlyDataEncoder.Close(); errc != nil {
return nil, newError("websocket delayed dialer cannot encode early data tail").Base(errc)
}
dialFunction := func() (*websocket.Conn, *http.Response, error) {
return d.dialer.Dial(d.uriBase+earlyDataBuf.String(), d.config.GetRequestHeader())
}
if d.config.EarlyDataHeaderName != "" {
dialFunction = func() (*websocket.Conn, *http.Response, error) {
earlyDataStr := earlyDataBuf.String()
currentHeader := d.config.GetRequestHeader()
currentHeader.Set(d.config.EarlyDataHeaderName, earlyDataStr)
return d.dialer.Dial(d.uriBase, currentHeader)
}
}
conn, resp, err := dialFunction() // nolint: bodyclose
2021-03-19 18:36:11 -04:00
if err != nil {
var reason string
if resp != nil {
reason = resp.Status
}
return nil, newError("failed to dial to (", d.uriBase, ") with early data: ", reason).Base(err)
}
if n != int64(len(earlyData)) {
if errWrite := conn.WriteMessage(websocket.BinaryMessage, earlyData[n:]); errWrite != nil {
return nil, newError("failed to dial to (", d.uriBase, ") with early data as write of remainder early data failed: ").Base(err)
}
}
return conn, nil
}
type dialerWithEarlyDataRelayed struct {
2021-04-30 19:06:10 -04:00
forwarder extension.BrowserForwarder
2021-03-19 18:36:11 -04:00
uriBase string
config *Config
}
func (d dialerWithEarlyDataRelayed) Dial(earlyData []byte) (io.ReadWriteCloser, error) {
earlyDataBuf := bytes.NewBuffer(nil)
base64EarlyDataEncoder := base64.NewEncoder(base64.RawURLEncoding, earlyDataBuf)
earlydata := bytes.NewReader(earlyData)
limitedEarlyDatareader := io.LimitReader(earlydata, int64(d.config.MaxEarlyData))
n, encerr := io.Copy(base64EarlyDataEncoder, limitedEarlyDatareader)
if encerr != nil {
return nil, newError("websocket delayed dialer cannot encode early data").Base(encerr)
}
if errc := base64EarlyDataEncoder.Close(); errc != nil {
return nil, newError("websocket delayed dialer cannot encode early data tail").Base(errc)
}
dialFunction := func() (io.ReadWriteCloser, error) {
return d.forwarder.DialWebsocket(d.uriBase+earlyDataBuf.String(), d.config.GetRequestHeader())
}
if d.config.EarlyDataHeaderName != "" {
earlyDataStr := earlyDataBuf.String()
currentHeader := d.config.GetRequestHeader()
currentHeader.Set(d.config.EarlyDataHeaderName, earlyDataStr)
return d.forwarder.DialWebsocket(d.uriBase, currentHeader)
}
conn, err := dialFunction()
2021-03-19 18:36:11 -04:00
if err != nil {
var reason string
return nil, newError("failed to dial to (", d.uriBase, ") with early data: ", reason).Base(err)
}
if n != int64(len(earlyData)) {
if _, errWrite := conn.Write(earlyData[n:]); errWrite != nil {
return nil, newError("failed to dial to (", d.uriBase, ") with early data as write of remainder early data failed: ").Base(err)
}
}
return conn, nil
}