1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-01 03:25:23 +00:00
v2fly/transport/internet/websocket/dialer.go

68 lines
1.8 KiB
Go
Raw Normal View History

2016-12-22 23:30:46 +00:00
package websocket
2016-08-13 13:44:36 +00:00
import (
"context"
2017-10-20 20:45:14 +00:00
"time"
2016-08-13 13:44:36 +00:00
2018-07-13 10:17:52 +00:00
"github.com/gorilla/websocket"
2018-04-20 20:45:44 +00:00
2017-02-08 17:30:16 +00:00
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/common/session"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/transport/internet"
2017-04-26 08:59:18 +00:00
"v2ray.com/core/transport/internet/tls"
2016-08-13 13:44:36 +00:00
)
2017-04-26 08:59:18 +00:00
// 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))
2016-10-02 21:43:58 +00:00
conn, err := dialWebsocket(ctx, dest)
if err != nil {
2017-09-29 16:10:17 +00:00
return nil, newError("failed to dial WebSocket").Base(err)
2016-08-13 13:44:36 +00:00
}
return internet.Connection(conn), nil
2016-08-13 13:44:36 +00:00
}
func init() {
2018-08-06 11:48:35 +00:00
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
2016-08-13 13:44:36 +00:00
}
func dialWebsocket(ctx context.Context, dest net.Destination) (net.Conn, error) {
src := internet.DialerSourceFromContext(ctx)
wsSettings := internet.TransportSettingsFromContext(ctx).(*Config)
2016-10-02 21:43:58 +00:00
2017-10-20 20:45:14 +00:00
dialer := &websocket.Dialer{
NetDial: func(network, addr string) (net.Conn, error) {
return internet.DialSystem(ctx, src, dest)
},
2017-12-16 01:04:51 +00:00
ReadBufferSize: 4 * 1024,
WriteBufferSize: 4 * 1024,
2017-10-20 20:45:14 +00:00
HandshakeTimeout: time.Second * 8,
2016-09-30 14:53:40 +00:00
}
2016-08-13 14:50:24 +00:00
2016-09-30 14:53:40 +00:00
protocol := "ws"
2016-08-13 13:44:36 +00:00
2018-02-28 14:15:22 +00:00
if config := tls.ConfigFromContext(ctx); config != nil {
2017-12-16 23:53:17 +00:00
protocol = "wss"
2018-02-28 14:15:22 +00:00
dialer.TLSClientConfig = config.GetTLSConfig(tls.WithDestination(dest))
2016-09-30 14:53:40 +00:00
}
2016-08-13 13:44:36 +00:00
2017-01-08 15:01:28 +00:00
host := dest.NetAddr()
if (protocol == "ws" && dest.Port == 80) || (protocol == "wss" && dest.Port == 443) {
host = dest.Address.String()
}
2018-03-02 08:26:14 +00:00
uri := protocol + "://" + host + wsSettings.GetNormalizedPath()
2016-08-14 06:11:51 +00:00
2017-10-13 15:54:04 +00:00
conn, resp, err := dialer.Dial(uri, wsSettings.GetRequestHeader())
2016-08-13 13:44:36 +00:00
if err != nil {
2017-02-10 10:41:50 +00:00
var reason string
2016-08-14 12:41:26 +00:00
if resp != nil {
2017-02-10 10:41:50 +00:00
reason = resp.Status
2016-08-14 12:41:26 +00:00
}
2017-04-08 23:43:25 +00:00
return nil, newError("failed to dial to (", uri, "): ", reason).Base(err)
2016-08-13 13:44:36 +00:00
}
2017-02-09 10:42:17 +00:00
return newConnection(conn, conn.RemoteAddr()), nil
2016-08-13 13:44:36 +00:00
}