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