mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.5 KiB
107 lines
2.5 KiB
8 years ago
|
package websocket
|
||
8 years ago
|
|
||
|
import (
|
||
8 years ago
|
"io/ioutil"
|
||
8 years ago
|
"net"
|
||
|
|
||
|
"github.com/gorilla/websocket"
|
||
8 years ago
|
"v2ray.com/core/common/log"
|
||
|
v2net "v2ray.com/core/common/net"
|
||
|
"v2ray.com/core/transport/internet"
|
||
8 years ago
|
v2tls "v2ray.com/core/transport/internet/tls"
|
||
8 years ago
|
)
|
||
|
|
||
|
var (
|
||
|
globalCache = NewConnectionCache()
|
||
|
)
|
||
|
|
||
8 years ago
|
func Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
|
||
8 years ago
|
log.Info("WebSocket|Dailer: Creating connection to ", dest)
|
||
8 years ago
|
if src == nil {
|
||
|
src = v2net.AnyIP
|
||
|
}
|
||
8 years ago
|
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
wsSettings := networkSettings.(*Config)
|
||
|
|
||
8 years ago
|
id := src.String() + "-" + dest.NetAddr()
|
||
|
var conn *wsconn
|
||
8 years ago
|
if dest.Network == v2net.Network_TCP && wsSettings.ConnectionReuse.IsEnabled() {
|
||
8 years ago
|
connt := globalCache.Get(id)
|
||
|
if connt != nil {
|
||
|
conn = connt.(*wsconn)
|
||
|
}
|
||
|
}
|
||
|
if conn == nil {
|
||
|
var err error
|
||
8 years ago
|
conn, err = wsDial(src, dest, options)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
log.Warning("WebSocket|Dialer: Dial failed: ", err)
|
||
8 years ago
|
return nil, err
|
||
|
}
|
||
|
}
|
||
8 years ago
|
return NewConnection(id, conn, globalCache, wsSettings), nil
|
||
8 years ago
|
}
|
||
|
|
||
|
func init() {
|
||
|
internet.WSDialer = Dial
|
||
|
}
|
||
|
|
||
8 years ago
|
func wsDial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (*wsconn, error) {
|
||
8 years ago
|
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
wsSettings := networkSettings.(*Config)
|
||
|
|
||
8 years ago
|
commonDial := func(network, addr string) (net.Conn, error) {
|
||
|
return internet.DialToDest(src, dest)
|
||
|
}
|
||
|
|
||
8 years ago
|
dialer := websocket.Dialer{
|
||
|
NetDial: commonDial,
|
||
|
ReadBufferSize: 65536,
|
||
|
WriteBufferSize: 65536,
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
protocol := "ws"
|
||
8 years ago
|
|
||
8 years ago
|
if options.Stream != nil && options.Stream.HasSecuritySettings() {
|
||
8 years ago
|
protocol = "wss"
|
||
8 years ago
|
securitySettings, err := options.Stream.GetEffectiveSecuritySettings()
|
||
|
if err != nil {
|
||
8 years ago
|
log.Error("WebSocket: Failed to create security settings: ", err)
|
||
8 years ago
|
return nil, err
|
||
|
}
|
||
8 years ago
|
tlsConfig, ok := securitySettings.(*v2tls.Config)
|
||
|
if ok {
|
||
|
dialer.TLSClientConfig = tlsConfig.GetTLSConfig()
|
||
|
if dest.Address.Family().IsDomain() {
|
||
|
dialer.TLSClientConfig.ServerName = dest.Address.Domain()
|
||
|
}
|
||
8 years ago
|
}
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
uri := protocol + "://" + dest.NetAddr() + "/" + wsSettings.Path
|
||
8 years ago
|
|
||
|
conn, resp, err := dialer.Dial(uri, nil)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
if resp != nil {
|
||
|
reason, reasonerr := ioutil.ReadAll(resp.Body)
|
||
|
log.Info(string(reason), reasonerr)
|
||
|
}
|
||
8 years ago
|
return nil, err
|
||
|
}
|
||
|
return func() internet.Connection {
|
||
8 years ago
|
connv2ray := &wsconn{
|
||
|
wsc: conn,
|
||
|
connClosing: false,
|
||
|
config: wsSettings,
|
||
|
}
|
||
8 years ago
|
connv2ray.setup()
|
||
|
return connv2ray
|
||
|
}().(*wsconn), nil
|
||
|
}
|