2016-12-22 18:30:46 -05:00
|
|
|
package websocket
|
2016-08-13 09:44:36 -04:00
|
|
|
|
|
|
|
import (
|
2016-08-14 02:11:51 -04:00
|
|
|
"io/ioutil"
|
2016-08-13 09:44:36 -04:00
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2017-01-03 09:16:48 -05:00
|
|
|
"v2ray.com/core/common"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/log"
|
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/transport/internet"
|
2017-01-04 09:34:21 -05:00
|
|
|
"v2ray.com/core/transport/internet/internal"
|
2016-10-02 17:43:58 -04:00
|
|
|
v2tls "v2ray.com/core/transport/internet/tls"
|
2016-08-13 09:44:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-01-04 09:34:21 -05:00
|
|
|
globalCache = internal.NewConnectionPool()
|
2016-08-13 09:44:36 -04:00
|
|
|
)
|
|
|
|
|
2016-09-30 10:53:40 -04:00
|
|
|
func Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
|
2016-08-17 17:12:10 -04:00
|
|
|
log.Info("WebSocket|Dailer: Creating connection to ", dest)
|
2016-08-13 09:44:36 -04:00
|
|
|
if src == nil {
|
|
|
|
src = v2net.AnyIP
|
|
|
|
}
|
2016-10-02 17:43:58 -04:00
|
|
|
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wsSettings := networkSettings.(*Config)
|
|
|
|
|
2017-01-04 09:34:21 -05:00
|
|
|
id := internal.NewConnectionID(src, dest)
|
2016-08-13 09:44:36 -04:00
|
|
|
var conn *wsconn
|
2017-01-01 15:19:12 -05:00
|
|
|
if dest.Network == v2net.Network_TCP && wsSettings.IsConnectionReuse() {
|
2016-08-13 09:44:36 -04:00
|
|
|
connt := globalCache.Get(id)
|
|
|
|
if connt != nil {
|
|
|
|
conn = connt.(*wsconn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if conn == nil {
|
|
|
|
var err error
|
2016-09-30 10:53:40 -04:00
|
|
|
conn, err = wsDial(src, dest, options)
|
2016-08-13 09:44:36 -04:00
|
|
|
if err != nil {
|
2016-08-17 17:12:10 -04:00
|
|
|
log.Warning("WebSocket|Dialer: Dial failed: ", err)
|
2016-08-13 09:44:36 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-01-04 09:34:21 -05:00
|
|
|
return internal.NewConnection(id, conn, globalCache, internal.ReuseConnection(wsSettings.IsConnectionReuse())), nil
|
2016-08-13 09:44:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-01-03 09:16:48 -05:00
|
|
|
common.Must(internet.RegisterNetworkDialer(v2net.Network_WebSocket, Dial))
|
2016-08-13 09:44:36 -04:00
|
|
|
}
|
|
|
|
|
2016-09-30 10:53:40 -04:00
|
|
|
func wsDial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (*wsconn, error) {
|
2016-10-02 17:43:58 -04:00
|
|
|
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wsSettings := networkSettings.(*Config)
|
|
|
|
|
2016-08-13 09:44:36 -04:00
|
|
|
commonDial := func(network, addr string) (net.Conn, error) {
|
2017-01-03 09:16:48 -05:00
|
|
|
return internet.DialSystem(src, dest)
|
2016-08-13 09:44:36 -04:00
|
|
|
}
|
|
|
|
|
2016-09-30 10:53:40 -04:00
|
|
|
dialer := websocket.Dialer{
|
|
|
|
NetDial: commonDial,
|
|
|
|
ReadBufferSize: 65536,
|
|
|
|
WriteBufferSize: 65536,
|
|
|
|
}
|
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
|
|
|
|
2016-10-16 08:22:21 -04:00
|
|
|
if options.Stream != nil && options.Stream.HasSecuritySettings() {
|
2016-09-30 10:53:40 -04:00
|
|
|
protocol = "wss"
|
2016-10-02 17:43:58 -04:00
|
|
|
securitySettings, err := options.Stream.GetEffectiveSecuritySettings()
|
|
|
|
if err != nil {
|
2016-10-16 08:22:21 -04:00
|
|
|
log.Error("WebSocket: Failed to create security settings: ", err)
|
2016-10-02 17:43:58 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-16 08:22:21 -04:00
|
|
|
tlsConfig, ok := securitySettings.(*v2tls.Config)
|
|
|
|
if ok {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
uri := protocol + "://" + host + "/" + wsSettings.Path
|
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 {
|
2016-08-14 08:41:26 -04:00
|
|
|
if resp != nil {
|
|
|
|
reason, reasonerr := ioutil.ReadAll(resp.Body)
|
|
|
|
log.Info(string(reason), reasonerr)
|
|
|
|
}
|
2016-08-13 09:44:36 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return func() internet.Connection {
|
2016-10-02 17:43:58 -04:00
|
|
|
connv2ray := &wsconn{
|
|
|
|
wsc: conn,
|
|
|
|
connClosing: false,
|
|
|
|
config: wsSettings,
|
|
|
|
}
|
2016-08-13 09:44:36 -04:00
|
|
|
connv2ray.setup()
|
|
|
|
return connv2ray
|
|
|
|
}().(*wsconn), nil
|
|
|
|
}
|