session.go 4.0 KB

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