1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-29 02:35:23 +00:00
v2fly/transport/internet/websocket/dialer.go
2017-10-13 23:54:04 +08:00

75 lines
1.9 KiB
Go

package websocket
import (
"context"
"github.com/gorilla/websocket"
"v2ray.com/core/app/log"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"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) {
log.Trace(newError("creating connection to ", dest))
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(internet.TransportProtocol_WebSocket, Dial))
}
func dialWebsocket(ctx context.Context, dest net.Destination) (net.Conn, error) {
src := internet.DialerSourceFromContext(ctx)
wsSettings := internet.TransportSettingsFromContext(ctx).(*Config)
commonDial := func(network, addr string) (net.Conn, error) {
return internet.DialSystem(ctx, src, dest)
}
dialer := websocket.Dialer{
NetDial: commonDial,
ReadBufferSize: 32 * 1024,
WriteBufferSize: 32 * 1024,
}
protocol := "ws"
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil {
tlsConfig, ok := securitySettings.(*tls.Config)
if ok {
protocol = "wss"
dialer.TLSClientConfig = tlsConfig.GetTLSConfig()
if dest.Address.Family().IsDomain() {
dialer.TLSClientConfig.ServerName = dest.Address.Domain()
}
}
}
host := dest.NetAddr()
if (protocol == "ws" && dest.Port == 80) || (protocol == "wss" && dest.Port == 443) {
host = dest.Address.String()
}
uri := protocol + "://" + host + wsSettings.GetNormailzedPath()
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 &connection{
wsc: conn,
}, nil
}