connection.go 3.4 KB

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