dialer.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package ws
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "io/ioutil"
  6. "net"
  7. "github.com/gorilla/websocket"
  8. "github.com/v2ray/v2ray-core/common/log"
  9. v2net "github.com/v2ray/v2ray-core/common/net"
  10. "github.com/v2ray/v2ray-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("Dailing WS to ", dest)
  17. if src == nil {
  18. src = v2net.AnyIP
  19. }
  20. id := src.String() + "-" + dest.NetAddr()
  21. var conn *wsconn
  22. if dest.IsTCP() && 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("WS Dial failed:" + err.Error())
  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()}
  46. dialer := websocket.Dialer{NetDial: commonDial, ReadBufferSize: 65536, WriteBufferSize: 65536, TLSClientConfig: tlsconf}
  47. effpto := func(dst v2net.Destination) string {
  48. if effectiveConfig.Pto != "" {
  49. return effectiveConfig.Pto
  50. }
  51. switch dst.Port().Value() {
  52. /*
  53. Since the value is not given explicitly,
  54. We are guessing it now.
  55. HTTP Port:
  56. 80
  57. 8080
  58. 8880
  59. 2052
  60. 2082
  61. 2086
  62. 2095
  63. HTTPS Port:
  64. 443
  65. 2053
  66. 2083
  67. 2087
  68. 2096
  69. 8443
  70. if the port you are using is not well-known,
  71. specify it to avoid this process.
  72. We will return "CRASH"turn "unknown" if we can't guess it, cause Dial to fail.
  73. */
  74. case 80:
  75. case 8080:
  76. case 8880:
  77. case 2052:
  78. case 2082:
  79. case 2086:
  80. case 2095:
  81. return "ws"
  82. case 443:
  83. case 2053:
  84. case 2083:
  85. case 2087:
  86. case 2096:
  87. case 8443:
  88. return "wss"
  89. default:
  90. return "unknown"
  91. }
  92. panic("Runtime unstable. Please report this bug to developers.")
  93. }(dest)
  94. uri := func(dst v2net.Destination, pto string, path string) string {
  95. return fmt.Sprintf("%v://%v/%v", pto, dst.NetAddr(), path)
  96. }(dest, effpto, effectiveConfig.Path)
  97. conn, resp, err := dialer.Dial(uri, nil)
  98. if err != nil {
  99. if resp != nil {
  100. reason, reasonerr := ioutil.ReadAll(resp.Body)
  101. log.Info(string(reason), reasonerr)
  102. }
  103. return nil, err
  104. }
  105. return func() internet.Connection {
  106. connv2ray := &wsconn{wsc: conn, connClosing: false}
  107. connv2ray.setup()
  108. return connv2ray
  109. }().(*wsconn), nil
  110. }