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"
|
2017-01-26 14:46:44 -05:00
|
|
|
"context"
|
2021-03-19 18:36:11 -04:00
|
|
|
"encoding/base64"
|
|
|
|
"io"
|
2022-12-16 14:16:00 -05:00
|
|
|
gonet "net"
|
2021-04-30 20:14:16 -04:00
|
|
|
"net/http"
|
2017-10-20 16:45:14 -04:00
|
|
|
"time"
|
2016-08-13 09:44:36 -04:00
|
|
|
|
2019-10-18 22:05:40 -04:00
|
|
|
"github.com/gorilla/websocket"
|
2021-02-16 15:31:50 -05:00
|
|
|
|
2022-01-02 10:16:23 -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.
|
2018-11-21 08:54:40 -05:00
|
|
|
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
|
2018-06-24 19:09:02 -04:00
|
|
|
newError("creating connection to ", dest).WriteToLog(session.ExportIDToError(ctx))
|
2016-10-02 17:43:58 -04:00
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
conn, err := dialWebsocket(ctx, dest, streamSettings)
|
2017-04-07 15:54:40 -04:00
|
|
|
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
|
|
|
}
|
2017-04-07 15:54:40 -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
|
|
|
}
|
|
|
|
|
2018-11-21 08:54:40 -05: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) {
|
2018-11-21 08:54:40 -05:00
|
|
|
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
|
|
|
|
2022-12-16 14:16:00 -05: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"
|
2022-12-16 14:16:00 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-30 20:14:16 -04:00
|
|
|
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
|
|
|
|
2017-12-18 14:34:00 -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)
|
|
|
|
}
|
|
|
|
|
2021-04-30 20:14:16 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-04-30 20:14:16 -04:00
|
|
|
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
|
|
|
|
}
|