connection.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package net
  2. import (
  3. "io"
  4. "net"
  5. "time"
  6. "github.com/v2fly/v2ray-core/v4/common"
  7. "github.com/v2fly/v2ray-core/v4/common/buf"
  8. "github.com/v2fly/v2ray-core/v4/common/errors"
  9. "github.com/v2fly/v2ray-core/v4/common/signal/done"
  10. )
  11. type ConnectionOption func(*connection)
  12. func ConnectionLocalAddr(a net.Addr) ConnectionOption {
  13. return func(c *connection) {
  14. c.local = a
  15. }
  16. }
  17. func ConnectionRemoteAddr(a net.Addr) ConnectionOption {
  18. return func(c *connection) {
  19. c.remote = a
  20. }
  21. }
  22. func ConnectionInput(writer io.Writer) ConnectionOption {
  23. return func(c *connection) {
  24. c.writer = buf.NewWriter(writer)
  25. }
  26. }
  27. func ConnectionInputMulti(writer buf.Writer) ConnectionOption {
  28. return func(c *connection) {
  29. c.writer = writer
  30. }
  31. }
  32. func ConnectionOutput(reader io.Reader) ConnectionOption {
  33. return func(c *connection) {
  34. c.reader = &buf.BufferedReader{Reader: buf.NewReader(reader)}
  35. }
  36. }
  37. func ConnectionOutputMulti(reader buf.Reader) ConnectionOption {
  38. return func(c *connection) {
  39. c.reader = &buf.BufferedReader{Reader: reader}
  40. }
  41. }
  42. func ConnectionOutputMultiUDP(reader buf.Reader) ConnectionOption {
  43. return func(c *connection) {
  44. c.reader = &buf.BufferedReader{
  45. Reader: reader,
  46. Spliter: buf.SplitFirstBytes,
  47. }
  48. }
  49. }
  50. func ConnectionOnClose(n io.Closer) ConnectionOption {
  51. return func(c *connection) {
  52. c.onClose = n
  53. }
  54. }
  55. func NewConnection(opts ...ConnectionOption) net.Conn {
  56. c := &connection{
  57. done: done.New(),
  58. local: &net.TCPAddr{
  59. IP: []byte{0, 0, 0, 0},
  60. Port: 0,
  61. },
  62. remote: &net.TCPAddr{
  63. IP: []byte{0, 0, 0, 0},
  64. Port: 0,
  65. },
  66. }
  67. for _, opt := range opts {
  68. opt(c)
  69. }
  70. return c
  71. }
  72. type connection struct {
  73. reader *buf.BufferedReader
  74. writer buf.Writer
  75. done *done.Instance
  76. onClose io.Closer
  77. local Addr
  78. remote Addr
  79. }
  80. func (c *connection) Read(b []byte) (int, error) {
  81. return c.reader.Read(b)
  82. }
  83. // ReadMultiBuffer implements buf.Reader.
  84. func (c *connection) ReadMultiBuffer() (buf.MultiBuffer, error) {
  85. return c.reader.ReadMultiBuffer()
  86. }
  87. // Write implements net.Conn.Write().
  88. func (c *connection) Write(b []byte) (int, error) {
  89. if c.done.Done() {
  90. return 0, io.ErrClosedPipe
  91. }
  92. if len(b)/buf.Size+1 > 64*1024*1024 {
  93. return 0, errors.New("value too large")
  94. }
  95. l := len(b)
  96. sliceSize := l/buf.Size + 1
  97. mb := make(buf.MultiBuffer, 0, sliceSize)
  98. mb = buf.MergeBytes(mb, b)
  99. return l, c.writer.WriteMultiBuffer(mb)
  100. }
  101. func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
  102. if c.done.Done() {
  103. buf.ReleaseMulti(mb)
  104. return io.ErrClosedPipe
  105. }
  106. return c.writer.WriteMultiBuffer(mb)
  107. }
  108. // Close implements net.Conn.Close().
  109. func (c *connection) Close() error {
  110. common.Must(c.done.Close())
  111. common.Interrupt(c.reader)
  112. common.Close(c.writer)
  113. if c.onClose != nil {
  114. return c.onClose.Close()
  115. }
  116. return nil
  117. }
  118. // LocalAddr implements net.Conn.LocalAddr().
  119. func (c *connection) LocalAddr() net.Addr {
  120. return c.local
  121. }
  122. // RemoteAddr implements net.Conn.RemoteAddr().
  123. func (c *connection) RemoteAddr() net.Addr {
  124. return c.remote
  125. }
  126. // SetDeadline implements net.Conn.SetDeadline().
  127. func (c *connection) SetDeadline(t time.Time) error {
  128. return nil
  129. }
  130. // SetReadDeadline implements net.Conn.SetReadDeadline().
  131. func (c *connection) SetReadDeadline(t time.Time) error {
  132. return nil
  133. }
  134. // SetWriteDeadline implements net.Conn.SetWriteDeadline().
  135. func (c *connection) SetWriteDeadline(t time.Time) error {
  136. return nil
  137. }