connection.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //go:build !confonly
  2. // +build !confonly
  3. package websocket
  4. import (
  5. "context"
  6. "io"
  7. "net"
  8. "time"
  9. "github.com/gorilla/websocket"
  10. "github.com/v2fly/v2ray-core/v4/common/buf"
  11. "github.com/v2fly/v2ray-core/v4/common/errors"
  12. "github.com/v2fly/v2ray-core/v4/common/serial"
  13. )
  14. var _ buf.Writer = (*connection)(nil)
  15. // connection is a wrapper for net.Conn over WebSocket connection.
  16. type connection struct {
  17. conn *websocket.Conn
  18. reader io.Reader
  19. remoteAddr net.Addr
  20. shouldWait bool
  21. delayedDialFinish context.Context
  22. finishedDial context.CancelFunc
  23. dialer DelayedDialer
  24. }
  25. type DelayedDialer interface {
  26. Dial(earlyData []byte) (*websocket.Conn, error)
  27. }
  28. func newConnection(conn *websocket.Conn, remoteAddr net.Addr) *connection {
  29. return &connection{
  30. conn: conn,
  31. remoteAddr: remoteAddr,
  32. }
  33. }
  34. func newConnectionWithEarlyData(conn *websocket.Conn, remoteAddr net.Addr, earlyData io.Reader) *connection {
  35. return &connection{
  36. conn: conn,
  37. remoteAddr: remoteAddr,
  38. reader: earlyData,
  39. }
  40. }
  41. func newConnectionWithDelayedDial(dialer DelayedDialer) *connection {
  42. delayedDialContext, CancellFunc := context.WithCancel(context.Background())
  43. return &connection{
  44. shouldWait: true,
  45. delayedDialFinish: delayedDialContext,
  46. finishedDial: CancellFunc,
  47. dialer: dialer,
  48. }
  49. }
  50. func newRelayedConnectionWithDelayedDial(dialer DelayedDialerForwarded) *connectionForwarder {
  51. delayedDialContext, CancellFunc := context.WithCancel(context.Background())
  52. return &connectionForwarder{
  53. shouldWait: true,
  54. delayedDialFinish: delayedDialContext,
  55. finishedDial: CancellFunc,
  56. dialer: dialer,
  57. }
  58. }
  59. func newRelayedConnection(conn io.ReadWriteCloser) *connectionForwarder {
  60. return &connectionForwarder{
  61. ReadWriteCloser: conn,
  62. shouldWait: false,
  63. }
  64. }
  65. // Read implements net.Conn.Read()
  66. func (c *connection) Read(b []byte) (int, error) {
  67. for {
  68. reader, err := c.getReader()
  69. if err != nil {
  70. return 0, err
  71. }
  72. nBytes, err := reader.Read(b)
  73. if errors.Cause(err) == io.EOF {
  74. c.reader = nil
  75. continue
  76. }
  77. return nBytes, err
  78. }
  79. }
  80. func (c *connection) getReader() (io.Reader, error) {
  81. if c.shouldWait {
  82. <-c.delayedDialFinish.Done()
  83. if c.conn == nil {
  84. return nil, newError("unable to read delayed dial websocket connection as it do not exist")
  85. }
  86. }
  87. if c.reader != nil {
  88. return c.reader, nil
  89. }
  90. _, reader, err := c.conn.NextReader()
  91. if err != nil {
  92. return nil, err
  93. }
  94. c.reader = reader
  95. return reader, nil
  96. }
  97. // Write implements io.Writer.
  98. func (c *connection) Write(b []byte) (int, error) {
  99. if c.shouldWait {
  100. var err error
  101. c.conn, err = c.dialer.Dial(b)
  102. c.finishedDial()
  103. if err != nil {
  104. return 0, newError("Unable to proceed with delayed write").Base(err)
  105. }
  106. c.remoteAddr = c.conn.RemoteAddr()
  107. c.shouldWait = false
  108. return len(b), nil
  109. }
  110. if err := c.conn.WriteMessage(websocket.BinaryMessage, b); err != nil {
  111. return 0, err
  112. }
  113. return len(b), nil
  114. }
  115. func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
  116. mb = buf.Compact(mb)
  117. mb, err := buf.WriteMultiBuffer(c, mb)
  118. buf.ReleaseMulti(mb)
  119. return err
  120. }
  121. func (c *connection) Close() error {
  122. if c.shouldWait {
  123. <-c.delayedDialFinish.Done()
  124. if c.conn == nil {
  125. return newError("unable to close delayed dial websocket connection as it do not exist")
  126. }
  127. }
  128. var errors []interface{}
  129. if err := c.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5)); err != nil {
  130. errors = append(errors, err)
  131. }
  132. if err := c.conn.Close(); err != nil {
  133. errors = append(errors, err)
  134. }
  135. if len(errors) > 0 {
  136. return newError("failed to close connection").Base(newError(serial.Concat(errors...)))
  137. }
  138. return nil
  139. }
  140. func (c *connection) LocalAddr() net.Addr {
  141. if c.shouldWait {
  142. <-c.delayedDialFinish.Done()
  143. if c.conn == nil {
  144. newError("websocket transport is not materialized when LocalAddr() is called").AtWarning().WriteToLog()
  145. return &net.UnixAddr{
  146. Name: "@placeholder",
  147. Net: "unix",
  148. }
  149. }
  150. }
  151. return c.conn.LocalAddr()
  152. }
  153. func (c *connection) RemoteAddr() net.Addr {
  154. return c.remoteAddr
  155. }
  156. func (c *connection) SetDeadline(t time.Time) error {
  157. if err := c.SetReadDeadline(t); err != nil {
  158. return err
  159. }
  160. return c.SetWriteDeadline(t)
  161. }
  162. func (c *connection) SetReadDeadline(t time.Time) error {
  163. if c.shouldWait {
  164. <-c.delayedDialFinish.Done()
  165. if c.conn == nil {
  166. newError("websocket transport is not materialized when SetReadDeadline() is called").AtWarning().WriteToLog()
  167. return nil
  168. }
  169. }
  170. return c.conn.SetReadDeadline(t)
  171. }
  172. func (c *connection) SetWriteDeadline(t time.Time) error {
  173. if c.shouldWait {
  174. <-c.delayedDialFinish.Done()
  175. if c.conn == nil {
  176. newError("websocket transport is not materialized when SetWriteDeadline() is called").AtWarning().WriteToLog()
  177. return nil
  178. }
  179. }
  180. return c.conn.SetWriteDeadline(t)
  181. }