| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package websocket
- import (
- "context"
- "time"
- "github.com/gorilla/websocket"
- "v2ray.com/core/common"
- "v2ray.com/core/common/net"
- "v2ray.com/core/common/session"
- "v2ray.com/core/transport/internet"
- "v2ray.com/core/transport/internet/tls"
- )
- // Dial dials a WebSocket connection to the given destination.
- func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
- newError("creating connection to ", dest).WriteToLog(session.ExportIDToError(ctx))
- conn, err := dialWebsocket(ctx, dest)
- 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) (net.Conn, error) {
- src := internet.DialerSourceFromContext(ctx)
- wsSettings := internet.StreamSettingsFromContext(ctx).ProtocolSettings.(*Config)
- dialer := &websocket.Dialer{
- NetDial: func(network, addr string) (net.Conn, error) {
- return internet.DialSystem(ctx, src, dest)
- },
- ReadBufferSize: 4 * 1024,
- WriteBufferSize: 4 * 1024,
- HandshakeTimeout: time.Second * 8,
- }
- protocol := "ws"
- if config := tls.ConfigFromContext(ctx); config != nil {
- protocol = "wss"
- dialer.TLSClientConfig = config.GetTLSConfig(tls.WithDestination(dest))
- }
- 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
- }
|