connection.go 4.8 KB

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