dialer.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package ws
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "net"
  6. "github.com/gorilla/websocket"
  7. "github.com/v2ray/v2ray-core/common/log"
  8. v2net "github.com/v2ray/v2ray-core/common/net"
  9. "github.com/v2ray/v2ray-core/transport/internet"
  10. )
  11. var (
  12. globalCache = NewConnectionCache()
  13. )
  14. func Dial(src v2net.Address, dest v2net.Destination) (internet.Connection, error) {
  15. log.Info("Dailing WS to ", dest)
  16. if src == nil {
  17. src = v2net.AnyIP
  18. }
  19. id := src.String() + "-" + dest.NetAddr()
  20. var conn *wsconn
  21. if dest.IsTCP() && effectiveConfig.ConnectionReuse {
  22. connt := globalCache.Get(id)
  23. if connt != nil {
  24. conn = connt.(*wsconn)
  25. }
  26. }
  27. if conn == nil {
  28. var err error
  29. conn, err = wsDial(src, dest)
  30. if err != nil {
  31. log.Warning("WS Dial failed:" + err.Error())
  32. return nil, err
  33. }
  34. }
  35. return NewConnection(id, conn, globalCache), nil
  36. }
  37. func init() {
  38. internet.WSDialer = Dial
  39. }
  40. func wsDial(src v2net.Address, dest v2net.Destination) (*wsconn, error) {
  41. //internet.DialToDest(src, dest)
  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 re 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/%v", pto, dst.NetAddr(), dst.Port(), path)
  96. }(dest, effpto, effectiveConfig.Path)
  97. conn, _, err := dialer.Dial(uri, nil)
  98. if err != nil {
  99. return nil, err
  100. }
  101. return func() internet.Connection {
  102. connv2ray := &wsconn{wsc: conn, connClosing: false}
  103. connv2ray.setup()
  104. return connv2ray
  105. }().(*wsconn), nil
  106. }