2016-12-22 23:30:46 +00:00
|
|
|
package websocket
|
2016-08-13 13:44:36 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-26 19:46:44 +00:00
|
|
|
"context"
|
2016-08-13 13:44:36 +00:00
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2017-02-01 20:35:40 +00:00
|
|
|
"v2ray.com/core/app/log"
|
2017-02-08 17:30:16 +00:00
|
|
|
"v2ray.com/core/common"
|
2017-02-10 10:41:50 +00:00
|
|
|
"v2ray.com/core/common/errors"
|
2016-08-20 18:55:45 +00:00
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/transport/internet"
|
2017-01-04 14:34:21 +00:00
|
|
|
"v2ray.com/core/transport/internet/internal"
|
2016-10-02 21:43:58 +00:00
|
|
|
v2tls "v2ray.com/core/transport/internet/tls"
|
2016-08-13 13:44:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-01-04 14:34:21 +00:00
|
|
|
globalCache = internal.NewConnectionPool()
|
2016-08-13 13:44:36 +00:00
|
|
|
)
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, error) {
|
|
|
|
log.Info("WebSocket|Dialer: Creating connection to ", dest)
|
|
|
|
src := internet.DialerSourceFromContext(ctx)
|
|
|
|
wsSettings := internet.TransportSettingsFromContext(ctx).(*Config)
|
2016-10-02 21:43:58 +00:00
|
|
|
|
2017-01-04 14:34:21 +00:00
|
|
|
id := internal.NewConnectionID(src, dest)
|
2017-02-08 17:30:16 +00:00
|
|
|
var conn net.Conn
|
2017-01-01 20:19:12 +00:00
|
|
|
if dest.Network == v2net.Network_TCP && wsSettings.IsConnectionReuse() {
|
2017-02-08 17:30:16 +00:00
|
|
|
conn = globalCache.Get(id)
|
2016-08-13 13:44:36 +00:00
|
|
|
}
|
|
|
|
if conn == nil {
|
|
|
|
var err error
|
2017-02-16 16:13:20 +00:00
|
|
|
conn, err = dialWebsocket(ctx, dest)
|
2016-08-13 13:44:36 +00:00
|
|
|
if err != nil {
|
2017-02-16 16:13:20 +00:00
|
|
|
return nil, errors.Base(err).Message("WebSocket|Dialer: Dial failed.")
|
2016-08-13 13:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-04 14:34:21 +00:00
|
|
|
return internal.NewConnection(id, conn, globalCache, internal.ReuseConnection(wsSettings.IsConnectionReuse())), nil
|
2016-08-13 13:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-01-12 11:54:34 +00:00
|
|
|
common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_WebSocket, Dial))
|
2016-08-13 13:44:36 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 16:13:20 +00:00
|
|
|
func dialWebsocket(ctx context.Context, dest v2net.Destination) (net.Conn, error) {
|
2017-01-26 19:46:44 +00:00
|
|
|
src := internet.DialerSourceFromContext(ctx)
|
|
|
|
wsSettings := internet.TransportSettingsFromContext(ctx).(*Config)
|
2016-10-02 21:43:58 +00:00
|
|
|
|
2016-08-13 13:44:36 +00:00
|
|
|
commonDial := func(network, addr string) (net.Conn, error) {
|
2017-02-23 22:48:47 +00:00
|
|
|
return internet.DialSystem(ctx, src, dest)
|
2016-08-13 13:44:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 14:53:40 +00:00
|
|
|
dialer := websocket.Dialer{
|
|
|
|
NetDial: commonDial,
|
2017-01-27 12:50:38 +00:00
|
|
|
ReadBufferSize: 32 * 1024,
|
|
|
|
WriteBufferSize: 32 * 1024,
|
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
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil {
|
2016-10-16 12:22:21 +00:00
|
|
|
tlsConfig, ok := securitySettings.(*v2tls.Config)
|
|
|
|
if ok {
|
2017-01-26 19:46:44 +00:00
|
|
|
protocol = "wss"
|
2016-10-16 12:22:21 +00:00
|
|
|
dialer.TLSClientConfig = tlsConfig.GetTLSConfig()
|
|
|
|
if dest.Address.Family().IsDomain() {
|
|
|
|
dialer.TLSClientConfig.ServerName = dest.Address.Domain()
|
|
|
|
}
|
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()
|
|
|
|
}
|
2017-02-10 10:47:53 +00:00
|
|
|
uri := protocol + "://" + host + wsSettings.GetNormailzedPath()
|
2016-08-14 06:11:51 +00:00
|
|
|
|
|
|
|
conn, resp, err := dialer.Dial(uri, nil)
|
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-02-10 10:57:59 +00:00
|
|
|
return nil, errors.Base(err).Message("WebSocket|Dialer: Failed to dial to (", uri, "): ", reason)
|
2016-08-13 13:44:36 +00:00
|
|
|
}
|
2017-02-09 10:42:17 +00:00
|
|
|
|
2017-02-16 15:53:31 +00:00
|
|
|
return &connection{
|
2017-02-09 10:42:17 +00:00
|
|
|
wsc: conn,
|
|
|
|
}, nil
|
2016-08-13 13:44:36 +00:00
|
|
|
}
|