dialer.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package ws
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "io/ioutil"
  6. "net"
  7. "github.com/gorilla/websocket"
  8. "v2ray.com/core/common/log"
  9. v2net "v2ray.com/core/common/net"
  10. "v2ray.com/core/transport/internet"
  11. )
  12. var (
  13. globalCache = NewConnectionCache()
  14. )
  15. func Dial(src v2net.Address, dest v2net.Destination) (internet.Connection, error) {
  16. log.Info("WebSocket|Dailer: Creating connection to ", dest)
  17. if src == nil {
  18. src = v2net.AnyIP
  19. }
  20. id := src.String() + "-" + dest.NetAddr()
  21. var conn *wsconn
  22. if dest.Network() == v2net.TCPNetwork && effectiveConfig.ConnectionReuse {
  23. connt := globalCache.Get(id)
  24. if connt != nil {
  25. conn = connt.(*wsconn)
  26. }
  27. }
  28. if conn == nil {
  29. var err error
  30. conn, err = wsDial(src, dest)
  31. if err != nil {
  32. log.Warning("WebSocket|Dialer: Dial failed: ", err)
  33. return nil, err
  34. }
  35. }
  36. return NewConnection(id, conn, globalCache), nil
  37. }
  38. func init() {
  39. internet.WSDialer = Dial
  40. }
  41. func wsDial(src v2net.Address, dest v2net.Destination) (*wsconn, error) {
  42. commonDial := func(network, addr string) (net.Conn, error) {
  43. return internet.DialToDest(src, dest)
  44. }
  45. tlsconf := &tls.Config{ServerName: dest.Address().Domain(), InsecureSkipVerify: effectiveConfig.DeveloperInsecureSkipVerify}
  46. dialer := websocket.Dialer{NetDial: commonDial, ReadBufferSize: 65536, WriteBufferSize: 65536, TLSClientConfig: tlsconf}
  47. effpto := calcPto(dest)
  48. uri := func(dst v2net.Destination, pto string, path string) string {
  49. return fmt.Sprintf("%v://%v/%v", pto, dst.NetAddr(), path)
  50. }(dest, effpto, effectiveConfig.Path)
  51. conn, resp, err := dialer.Dial(uri, nil)
  52. if err != nil {
  53. if resp != nil {
  54. reason, reasonerr := ioutil.ReadAll(resp.Body)
  55. log.Info(string(reason), reasonerr)
  56. }
  57. return nil, err
  58. }
  59. return func() internet.Connection {
  60. connv2ray := &wsconn{wsc: conn, connClosing: false}
  61. connv2ray.setup()
  62. return connv2ray
  63. }().(*wsconn), nil
  64. }
  65. func calcPto(dst v2net.Destination) string {
  66. if effectiveConfig.Pto != "" {
  67. return effectiveConfig.Pto
  68. }
  69. switch dst.Port().Value() {
  70. /*
  71. Since the value is not given explicitly,
  72. We are guessing it now.
  73. HTTP Port:
  74. 80
  75. 8080
  76. 8880
  77. 2052
  78. 2082
  79. 2086
  80. 2095
  81. HTTPS Port:
  82. 443
  83. 2053
  84. 2083
  85. 2087
  86. 2096
  87. 8443
  88. if the port you are using is not well-known,
  89. specify it to avoid this process.
  90. We will return "CRASH"turn "unknown" if we can't guess it, cause Dial to fail.
  91. */
  92. case 80, 8080, 8880, 2052, 2082, 2086, 2095:
  93. return "ws"
  94. case 443, 2053, 2083, 2087, 2096, 8443:
  95. return "wss"
  96. default:
  97. return "unknown"
  98. }
  99. }