1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 09:17:55 -04:00
v2fly/transport/internet/ws/dialer.go

140 lines
2.6 KiB
Go
Raw Normal View History

2016-08-13 09:44:36 -04:00
package ws
import (
2016-08-13 10:50:24 -04:00
"crypto/tls"
2016-08-13 09:44:36 -04:00
"fmt"
2016-08-14 02:11:51 -04:00
"io/ioutil"
2016-08-13 09:44:36 -04:00
"net"
"github.com/gorilla/websocket"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/transport/internet"
)
var (
globalCache = NewConnectionCache()
)
func Dial(src v2net.Address, dest v2net.Destination) (internet.Connection, error) {
log.Info("Dailing WS to ", dest)
if src == nil {
src = v2net.AnyIP
}
id := src.String() + "-" + dest.NetAddr()
var conn *wsconn
if dest.IsTCP() && effectiveConfig.ConnectionReuse {
connt := globalCache.Get(id)
if connt != nil {
conn = connt.(*wsconn)
}
}
if conn == nil {
var err error
conn, err = wsDial(src, dest)
if err != nil {
log.Warning("WS Dial failed:" + err.Error())
return nil, err
}
}
return NewConnection(id, conn, globalCache), nil
}
func init() {
internet.WSDialer = Dial
}
func wsDial(src v2net.Address, dest v2net.Destination) (*wsconn, error) {
commonDial := func(network, addr string) (net.Conn, error) {
return internet.DialToDest(src, dest)
}
2016-08-13 10:50:24 -04:00
tlsconf := &tls.Config{ServerName: dest.Address().Domain()}
dialer := websocket.Dialer{NetDial: commonDial, ReadBufferSize: 65536, WriteBufferSize: 65536, TLSClientConfig: tlsconf}
2016-08-13 09:44:36 -04:00
2016-08-15 05:47:15 -04:00
effpto := calcPto(dest)
2016-08-13 09:44:36 -04:00
uri := func(dst v2net.Destination, pto string, path string) string {
2016-08-15 02:30:38 -04:00
return fmt.Sprintf("%v://%v/%v", pto, dst.NetAddr(), path)
2016-08-13 09:44:36 -04:00
}(dest, effpto, effectiveConfig.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 {
connv2ray := &wsconn{wsc: conn, connClosing: false}
connv2ray.setup()
return connv2ray
}().(*wsconn), nil
}
2016-08-15 05:47:15 -04:00
func calcPto(dst v2net.Destination) string {
if effectiveConfig.Pto != "" {
return effectiveConfig.Pto
}
switch dst.Port().Value() {
/*
Since the value is not given explicitly,
We are guessing it now.
HTTP Port:
80
8080
8880
2052
2082
2086
2095
HTTPS Port:
443
2053
2083
2087
2096
8443
if the port you are using is not well-known,
specify it to avoid this process.
We will return "CRASH"turn "unknown" if we can't guess it, cause Dial to fail.
*/
case 80:
fallthrough
case 8080:
fallthrough
case 8880:
fallthrough
case 2052:
fallthrough
case 2082:
fallthrough
case 2086:
fallthrough
case 2095:
return "ws"
case 443:
fallthrough
case 2053:
fallthrough
case 2083:
fallthrough
case 2087:
fallthrough
case 2096:
fallthrough
case 8443:
return "wss"
default:
return "unknown"
}
}