conn.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2016 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package net
  15. import (
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "net"
  21. "sync"
  22. "time"
  23. "github.com/fatedier/frp/utils/log"
  24. kcp "github.com/xtaci/kcp-go"
  25. )
  26. // Conn is the interface of connections used in frp.
  27. type Conn interface {
  28. net.Conn
  29. log.Logger
  30. }
  31. type WrapLogConn struct {
  32. net.Conn
  33. log.Logger
  34. }
  35. func WrapConn(c net.Conn) Conn {
  36. return &WrapLogConn{
  37. Conn: c,
  38. Logger: log.NewPrefixLogger(""),
  39. }
  40. }
  41. type WrapReadWriteCloserConn struct {
  42. io.ReadWriteCloser
  43. log.Logger
  44. }
  45. func WrapReadWriteCloserToConn(rwc io.ReadWriteCloser) Conn {
  46. return &WrapReadWriteCloserConn{
  47. ReadWriteCloser: rwc,
  48. Logger: log.NewPrefixLogger(""),
  49. }
  50. }
  51. func (conn *WrapReadWriteCloserConn) LocalAddr() net.Addr {
  52. return (*net.TCPAddr)(nil)
  53. }
  54. func (conn *WrapReadWriteCloserConn) RemoteAddr() net.Addr {
  55. return (*net.TCPAddr)(nil)
  56. }
  57. func (conn *WrapReadWriteCloserConn) SetDeadline(t time.Time) error {
  58. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  59. }
  60. func (conn *WrapReadWriteCloserConn) SetReadDeadline(t time.Time) error {
  61. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  62. }
  63. func (conn *WrapReadWriteCloserConn) SetWriteDeadline(t time.Time) error {
  64. return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
  65. }
  66. func ConnectServer(protocol string, addr string) (c Conn, err error) {
  67. switch protocol {
  68. case "tcp":
  69. return ConnectTcpServer(addr)
  70. case "kcp":
  71. kcpConn, errRet := kcp.DialWithOptions(addr, nil, 10, 3)
  72. if errRet != nil {
  73. err = errRet
  74. return
  75. }
  76. kcpConn.SetStreamMode(true)
  77. kcpConn.SetWriteDelay(true)
  78. kcpConn.SetNoDelay(1, 20, 2, 1)
  79. kcpConn.SetWindowSize(128, 512)
  80. kcpConn.SetMtu(1350)
  81. kcpConn.SetACKNoDelay(false)
  82. kcpConn.SetReadBuffer(4194304)
  83. kcpConn.SetWriteBuffer(4194304)
  84. c = WrapConn(kcpConn)
  85. return
  86. default:
  87. return nil, fmt.Errorf("unsupport protocol: %s", protocol)
  88. }
  89. }
  90. func ConnectServerByHttpProxy(httpProxy string, protocol string, addr string) (c Conn, err error) {
  91. switch protocol {
  92. case "tcp":
  93. return ConnectTcpServerByHttpProxy(httpProxy, addr)
  94. case "kcp":
  95. // http proxy is not supported for kcp
  96. return ConnectServer(protocol, addr)
  97. default:
  98. return nil, fmt.Errorf("unsupport protocol: %s", protocol)
  99. }
  100. }
  101. type SharedConn struct {
  102. Conn
  103. sync.Mutex
  104. buf *bytes.Buffer
  105. }
  106. // the bytes you read in io.Reader, will be reserved in SharedConn
  107. func NewShareConn(conn Conn) (*SharedConn, io.Reader) {
  108. sc := &SharedConn{
  109. Conn: conn,
  110. buf: bytes.NewBuffer(make([]byte, 0, 1024)),
  111. }
  112. return sc, io.TeeReader(conn, sc.buf)
  113. }
  114. func (sc *SharedConn) Read(p []byte) (n int, err error) {
  115. sc.Lock()
  116. if sc.buf == nil {
  117. sc.Unlock()
  118. return sc.Conn.Read(p)
  119. }
  120. sc.Unlock()
  121. n, err = sc.buf.Read(p)
  122. if err == io.EOF {
  123. sc.Lock()
  124. sc.buf = nil
  125. sc.Unlock()
  126. var n2 int
  127. n2, err = sc.Conn.Read(p[n:])
  128. n += n2
  129. }
  130. return
  131. }
  132. func (sc *SharedConn) WriteBuff(buffer []byte) (err error) {
  133. sc.buf.Reset()
  134. _, err = sc.buf.Write(buffer)
  135. return err
  136. }