dialer.go 2.4 KB

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