connection.go 3.2 KB

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