v2ray-core/transport/internet/ws/dialer.go

107 lines
2.6 KiB
Go
Raw Normal View History

2016-08-13 13:44:36 +00:00
package ws
import (
"fmt"
2016-08-14 06:11:51 +00:00
"io/ioutil"
2016-08-13 13:44:36 +00:00
"net"
"github.com/gorilla/websocket"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/transport/internet"
2016-10-02 21:43:58 +00:00
v2tls "v2ray.com/core/transport/internet/tls"
2016-08-13 13:44:36 +00:00
)
var (
globalCache = NewConnectionCache()
)
2016-09-30 14:53:40 +00:00
func Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
2016-08-17 21:12:10 +00:00
log.Info("WebSocket|Dailer: Creating connection to ", dest)
2016-08-13 13:44:36 +00:00
if src == nil {
src = v2net.AnyIP
}
2016-10-02 21:43:58 +00:00
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
if err != nil {
return nil, err
}
wsSettings := networkSettings.(*Config)
2016-08-13 13:44:36 +00:00
id := src.String() + "-" + dest.NetAddr()
var conn *wsconn
2016-10-02 21:43:58 +00:00
if dest.Network == v2net.Network_TCP && wsSettings.ConnectionReuse {
2016-08-13 13:44:36 +00:00
connt := globalCache.Get(id)
if connt != nil {
conn = connt.(*wsconn)
}
}
if conn == nil {
var err error
2016-09-30 14:53:40 +00:00
conn, err = wsDial(src, dest, options)
2016-08-13 13:44:36 +00:00
if err != nil {
2016-08-17 21:12:10 +00:00
log.Warning("WebSocket|Dialer: Dial failed: ", err)
2016-08-13 13:44:36 +00:00
return nil, err
}
}
2016-10-02 21:43:58 +00:00
return NewConnection(id, conn, globalCache, wsSettings), nil
2016-08-13 13:44:36 +00:00
}
func init() {
internet.WSDialer = Dial
}
2016-09-30 14:53:40 +00:00
func wsDial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (*wsconn, error) {
2016-10-02 21:43:58 +00:00
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
if err != nil {
return nil, err
}
wsSettings := networkSettings.(*Config)
2016-08-13 13:44:36 +00:00
commonDial := func(network, addr string) (net.Conn, error) {
return internet.DialToDest(src, dest)
}
2016-09-30 14:53:40 +00:00
dialer := websocket.Dialer{
NetDial: commonDial,
ReadBufferSize: 65536,
WriteBufferSize: 65536,
}
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
2016-10-02 21:43:58 +00:00
if options.Stream != nil && options.Stream.SecurityType == internet.SecurityType_TLS {
2016-09-30 14:53:40 +00:00
protocol = "wss"
2016-10-02 21:43:58 +00:00
securitySettings, err := options.Stream.GetEffectiveSecuritySettings()
if err != nil {
log.Error("WebSocket: Failed to create apply TLS config: ", err)
return nil, err
}
dialer.TLSClientConfig = securitySettings.(*v2tls.Config).GetTLSConfig()
2016-09-30 14:53:40 +00:00
if dest.Address.Family().IsDomain() {
dialer.TLSClientConfig.ServerName = dest.Address.Domain()
}
}
2016-08-13 13:44:36 +00:00
uri := func(dst v2net.Destination, pto string, path string) string {
2016-08-15 06:30:38 +00:00
return fmt.Sprintf("%v://%v/%v", pto, dst.NetAddr(), path)
2016-10-02 21:43:58 +00:00
}(dest, protocol, wsSettings.Path)
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 {
2016-08-14 12:41:26 +00:00
if resp != nil {
reason, reasonerr := ioutil.ReadAll(resp.Body)
log.Info(string(reason), reasonerr)
}
2016-08-13 13:44:36 +00:00
return nil, err
}
return func() internet.Connection {
2016-10-02 21:43:58 +00:00
connv2ray := &wsconn{
wsc: conn,
connClosing: false,
config: wsSettings,
}
2016-08-13 13:44:36 +00:00
connv2ray.setup()
return connv2ray
}().(*wsconn), nil
}