connection.go 4.8 KB

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