mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-10 02:53:11 +00:00
68 lines
2 KiB
Go
68 lines
2 KiB
Go
|
// +build !confonly
|
||
|
|
||
|
package websocket
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"time"
|
||
|
|
||
|
"github.com/gorilla/websocket"
|
||
|
"github.com/xtls/xray-core/v1/common"
|
||
|
"github.com/xtls/xray-core/v1/common/net"
|
||
|
"github.com/xtls/xray-core/v1/common/session"
|
||
|
"github.com/xtls/xray-core/v1/transport/internet"
|
||
|
"github.com/xtls/xray-core/v1/transport/internet/tls"
|
||
|
)
|
||
|
|
||
|
// 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))
|
||
|
|
||
|
conn, err := dialWebsocket(ctx, dest, streamSettings)
|
||
|
if err != nil {
|
||
|
return nil, newError("failed to dial WebSocket").Base(err)
|
||
|
}
|
||
|
return internet.Connection(conn), nil
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
|
||
|
}
|
||
|
|
||
|
func dialWebsocket(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (net.Conn, error) {
|
||
|
wsSettings := streamSettings.ProtocolSettings.(*Config)
|
||
|
|
||
|
dialer := &websocket.Dialer{
|
||
|
NetDial: func(network, addr string) (net.Conn, error) {
|
||
|
return internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
|
||
|
},
|
||
|
ReadBufferSize: 4 * 1024,
|
||
|
WriteBufferSize: 4 * 1024,
|
||
|
HandshakeTimeout: time.Second * 8,
|
||
|
}
|
||
|
|
||
|
protocol := "ws"
|
||
|
|
||
|
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
|
||
|
protocol = "wss"
|
||
|
dialer.TLSClientConfig = config.GetTLSConfig(tls.WithDestination(dest), tls.WithNextProto("http/1.1"))
|
||
|
}
|
||
|
|
||
|
host := dest.NetAddr()
|
||
|
if (protocol == "ws" && dest.Port == 80) || (protocol == "wss" && dest.Port == 443) {
|
||
|
host = dest.Address.String()
|
||
|
}
|
||
|
uri := protocol + "://" + host + wsSettings.GetNormalizedPath()
|
||
|
|
||
|
conn, resp, err := dialer.Dial(uri, wsSettings.GetRequestHeader())
|
||
|
if err != nil {
|
||
|
var reason string
|
||
|
if resp != nil {
|
||
|
reason = resp.Status
|
||
|
}
|
||
|
return nil, newError("failed to dial to (", uri, "): ", reason).Base(err)
|
||
|
}
|
||
|
|
||
|
return newConnection(conn, conn.RemoteAddr()), nil
|
||
|
}
|