2016-12-23 00:30:46 +01:00
|
|
|
package websocket
|
2016-08-13 21:44:36 +08:00
|
|
|
|
|
|
|
import (
|
2017-01-26 20:46:44 +01:00
|
|
|
"context"
|
2017-10-20 22:45:14 +02:00
|
|
|
"time"
|
2016-08-13 21:44:36 +08:00
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2017-02-01 21:35:40 +01:00
|
|
|
"v2ray.com/core/app/log"
|
2017-02-08 18:30:16 +01:00
|
|
|
"v2ray.com/core/common"
|
2017-08-29 12:56:57 +02:00
|
|
|
"v2ray.com/core/common/net"
|
2016-08-20 20:55:45 +02:00
|
|
|
"v2ray.com/core/transport/internet"
|
2017-04-26 10:59:18 +02:00
|
|
|
"v2ray.com/core/transport/internet/tls"
|
2016-08-13 21:44:36 +08:00
|
|
|
)
|
|
|
|
|
2017-04-26 10:59:18 +02:00
|
|
|
// Dial dials a WebSocket connection to the given destination.
|
2017-08-29 12:56:57 +02:00
|
|
|
func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
|
2017-04-09 01:43:25 +02:00
|
|
|
log.Trace(newError("creating connection to ", dest))
|
2016-10-02 23:43:58 +02:00
|
|
|
|
2017-04-07 21:54:40 +02:00
|
|
|
conn, err := dialWebsocket(ctx, dest)
|
|
|
|
if err != nil {
|
2017-09-29 18:10:17 +02:00
|
|
|
return nil, newError("failed to dial WebSocket").Base(err)
|
2016-08-13 21:44:36 +08:00
|
|
|
}
|
2017-04-07 21:54:40 +02:00
|
|
|
return internet.Connection(conn), nil
|
2016-08-13 21:44:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-01-12 12:54:34 +01:00
|
|
|
common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_WebSocket, Dial))
|
2016-08-13 21:44:36 +08:00
|
|
|
}
|
|
|
|
|
2017-08-29 12:56:57 +02:00
|
|
|
func dialWebsocket(ctx context.Context, dest net.Destination) (net.Conn, error) {
|
2017-01-26 20:46:44 +01:00
|
|
|
src := internet.DialerSourceFromContext(ctx)
|
|
|
|
wsSettings := internet.TransportSettingsFromContext(ctx).(*Config)
|
2016-10-02 23:43:58 +02:00
|
|
|
|
2017-10-20 22:45:14 +02:00
|
|
|
dialer := &websocket.Dialer{
|
|
|
|
NetDial: func(network, addr string) (net.Conn, error) {
|
|
|
|
return internet.DialSystem(ctx, src, dest)
|
|
|
|
},
|
2017-12-16 02:04:51 +01:00
|
|
|
ReadBufferSize: 4 * 1024,
|
|
|
|
WriteBufferSize: 4 * 1024,
|
2017-10-20 22:45:14 +02:00
|
|
|
HandshakeTimeout: time.Second * 8,
|
2016-09-30 16:53:40 +02:00
|
|
|
}
|
2016-08-13 22:50:24 +08:00
|
|
|
|
2016-09-30 16:53:40 +02:00
|
|
|
protocol := "ws"
|
2016-08-13 21:44:36 +08:00
|
|
|
|
2017-01-26 20:46:44 +01:00
|
|
|
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil {
|
2017-04-26 10:59:18 +02:00
|
|
|
tlsConfig, ok := securitySettings.(*tls.Config)
|
2016-10-16 14:22:21 +02:00
|
|
|
if ok {
|
2017-01-26 20:46:44 +01:00
|
|
|
protocol = "wss"
|
2016-10-16 14:22:21 +02:00
|
|
|
if dest.Address.Family().IsDomain() {
|
2017-10-26 11:43:02 +02:00
|
|
|
tlsConfig.OverrideServerNameIfEmpty(dest.Address.Domain())
|
2016-10-16 14:22:21 +02:00
|
|
|
}
|
2017-10-26 11:43:02 +02:00
|
|
|
dialer.TLSClientConfig = tlsConfig.GetTLSConfig()
|
2016-09-30 16:53:40 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-13 21:44:36 +08:00
|
|
|
|
2017-01-08 16:01:28 +01:00
|
|
|
host := dest.NetAddr()
|
|
|
|
if (protocol == "ws" && dest.Port == 80) || (protocol == "wss" && dest.Port == 443) {
|
|
|
|
host = dest.Address.String()
|
|
|
|
}
|
2017-02-10 11:47:53 +01:00
|
|
|
uri := protocol + "://" + host + wsSettings.GetNormailzedPath()
|
2016-08-14 14:11:51 +08:00
|
|
|
|
2017-10-13 23:54:04 +08:00
|
|
|
conn, resp, err := dialer.Dial(uri, wsSettings.GetRequestHeader())
|
2016-08-13 21:44:36 +08:00
|
|
|
if err != nil {
|
2017-02-10 11:41:50 +01:00
|
|
|
var reason string
|
2016-08-14 20:41:26 +08:00
|
|
|
if resp != nil {
|
2017-02-10 11:41:50 +01:00
|
|
|
reason = resp.Status
|
2016-08-14 20:41:26 +08:00
|
|
|
}
|
2017-04-09 01:43:25 +02:00
|
|
|
return nil, newError("failed to dial to (", uri, "): ", reason).Base(err)
|
2016-08-13 21:44:36 +08:00
|
|
|
}
|
2017-02-09 11:42:17 +01:00
|
|
|
|
2017-10-21 09:45:27 +02:00
|
|
|
return newConnection(conn), nil
|
2016-08-13 21:44:36 +08:00
|
|
|
}
|