kcp.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package hub
  2. import (
  3. "errors"
  4. "net"
  5. "time"
  6. "github.com/v2ray/v2ray-core/common/log"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. "github.com/v2ray/v2ray-core/transport"
  9. "github.com/v2ray/v2ray-core/transport/hub/kcpv"
  10. "github.com/xtaci/kcp-go"
  11. )
  12. type KCPVlistener struct {
  13. lst *kcp.Listener
  14. conf *kcpv.Config
  15. previousSocketid map[int]uint32
  16. previousSocketid_mapid int
  17. }
  18. /*Accept Accept a KCP connection
  19. Since KCP is stateless, if package deliver after it was closed,
  20. It could be reconized as a new connection and call accept.
  21. If we can detect that the connection is of such a kind,
  22. we will discard that conn.
  23. */
  24. func (kvl *KCPVlistener) Accept() (net.Conn, error) {
  25. conn, err := kvl.lst.Accept()
  26. if err != nil {
  27. return nil, err
  28. }
  29. if kvl.previousSocketid == nil {
  30. kvl.previousSocketid = make(map[int]uint32)
  31. }
  32. var badbit bool = false
  33. for _, key := range kvl.previousSocketid {
  34. if key == conn.GetConv() {
  35. badbit = true
  36. }
  37. }
  38. if badbit {
  39. conn.Close()
  40. return nil, errors.New("KCP:ConnDup, Don't worry~")
  41. } else {
  42. kvl.previousSocketid_mapid++
  43. kvl.previousSocketid[kvl.previousSocketid_mapid] = conn.GetConv()
  44. /*
  45. Here we assume that count(connection) < 512
  46. This won't always true.
  47. More work might be necessary to deal with this in a better way.
  48. */
  49. if kvl.previousSocketid_mapid >= 512 {
  50. delete(kvl.previousSocketid, kvl.previousSocketid_mapid-512)
  51. }
  52. }
  53. kcv := &KCPVconn{hc: conn}
  54. kcv.conf = kvl.conf
  55. err = kcv.ApplyConf()
  56. if err != nil {
  57. return nil, err
  58. }
  59. return kcv, nil
  60. }
  61. func (kvl *KCPVlistener) Close() error {
  62. return kvl.lst.Close()
  63. }
  64. func (kvl *KCPVlistener) Addr() net.Addr {
  65. return kvl.lst.Addr()
  66. }
  67. type KCPVconn struct {
  68. hc *kcp.UDPSession
  69. conf *kcpv.Config
  70. conntokeep time.Time
  71. }
  72. //var counter int
  73. func (kcpvc *KCPVconn) Read(b []byte) (int, error) {
  74. ifb := time.Now().Add(time.Duration(kcpvc.conf.AdvancedConfigs.ReadTimeout) * time.Second)
  75. if ifb.After(kcpvc.conntokeep) {
  76. kcpvc.conntokeep = ifb
  77. }
  78. kcpvc.hc.SetDeadline(kcpvc.conntokeep)
  79. return kcpvc.hc.Read(b)
  80. }
  81. func (kcpvc *KCPVconn) Write(b []byte) (int, error) {
  82. ifb := time.Now().Add(time.Duration(kcpvc.conf.AdvancedConfigs.WriteTimeout) * time.Second)
  83. if ifb.After(kcpvc.conntokeep) {
  84. kcpvc.conntokeep = ifb
  85. }
  86. kcpvc.hc.SetDeadline(kcpvc.conntokeep)
  87. return kcpvc.hc.Write(b)
  88. }
  89. /*ApplyConf will apply kcpvc.conf to current Socket
  90. It is recommmanded to call this func once and only once
  91. */
  92. func (kcpvc *KCPVconn) ApplyConf() error {
  93. nodelay, interval, resend, nc := 0, 40, 0, 0
  94. if kcpvc.conf.Mode != "manual" {
  95. switch kcpvc.conf.Mode {
  96. case "normal":
  97. nodelay, interval, resend, nc = 0, 30, 2, 1
  98. case "fast":
  99. nodelay, interval, resend, nc = 0, 20, 2, 1
  100. case "fast2":
  101. nodelay, interval, resend, nc = 1, 20, 2, 1
  102. case "fast3":
  103. nodelay, interval, resend, nc = 1, 10, 2, 1
  104. }
  105. } else {
  106. log.Error("kcp: Failed to Apply configure: Manual mode is not supported.(yet!)")
  107. return errors.New("kcp: Manual Not Implemented")
  108. }
  109. kcpvc.hc.SetNoDelay(nodelay, interval, resend, nc)
  110. kcpvc.hc.SetWindowSize(kcpvc.conf.AdvancedConfigs.Sndwnd, kcpvc.conf.AdvancedConfigs.Rcvwnd)
  111. kcpvc.hc.SetMtu(kcpvc.conf.AdvancedConfigs.Mtu)
  112. kcpvc.hc.SetACKNoDelay(kcpvc.conf.AdvancedConfigs.Acknodelay)
  113. kcpvc.hc.SetDSCP(kcpvc.conf.AdvancedConfigs.Dscp)
  114. //counter++
  115. //log.Info(counter)
  116. return nil
  117. }
  118. /*Close Close the current conn
  119. We have to delay the close of Socket for a few second
  120. or the VMess EOF can be too late to send.
  121. */
  122. func (kcpvc *KCPVconn) Close() error {
  123. go func() {
  124. time.Sleep(2000 * time.Millisecond)
  125. //counter--
  126. //log.Info(counter)
  127. kcpvc.hc.Close()
  128. }()
  129. return nil
  130. }
  131. func (kcpvc *KCPVconn) LocalAddr() net.Addr {
  132. return kcpvc.hc.LocalAddr()
  133. }
  134. func (kcpvc *KCPVconn) RemoteAddr() net.Addr {
  135. return kcpvc.hc.RemoteAddr()
  136. }
  137. func (kcpvc *KCPVconn) SetDeadline(t time.Time) error {
  138. return kcpvc.hc.SetDeadline(t)
  139. }
  140. func (kcpvc *KCPVconn) SetReadDeadline(t time.Time) error {
  141. return kcpvc.hc.SetReadDeadline(t)
  142. }
  143. func (kcpvc *KCPVconn) SetWriteDeadline(t time.Time) error {
  144. return kcpvc.hc.SetWriteDeadline(t)
  145. }
  146. func DialKCP(dest v2net.Destination) (*KCPVconn, error) {
  147. kcpconf := transport.KcpConfig
  148. cpip, _ := kcpv.GetChipher(kcpconf.Key)
  149. kcv, err := kcp.DialWithOptions(kcpconf.AdvancedConfigs.Fec, dest.NetAddr(), cpip)
  150. if err != nil {
  151. return nil, err
  152. }
  153. kcvn := &KCPVconn{hc: kcv}
  154. kcvn.conf = kcpconf
  155. err = kcvn.ApplyConf()
  156. if err != nil {
  157. return nil, err
  158. }
  159. return kcvn, nil
  160. }
  161. func ListenKCP(address v2net.Address, port v2net.Port) (*KCPVlistener, error) {
  162. kcpconf := transport.KcpConfig
  163. cpip, _ := kcpv.GetChipher(kcpconf.Key)
  164. laddr := address.String() + ":" + port.String()
  165. kcl, err := kcp.ListenWithOptions(kcpconf.AdvancedConfigs.Fec, laddr, cpip)
  166. kcvl := &KCPVlistener{lst: kcl, conf: kcpconf}
  167. return kcvl, err
  168. }