session.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. func (kcpvc *KCPVconn) Read(b []byte) (int, error) {
  67. ifb := time.Now().Add(time.Duration(effectiveConfig.ReadTimeout) * time.Second)
  68. if ifb.After(kcpvc.conntokeep) {
  69. kcpvc.conntokeep = ifb
  70. }
  71. kcpvc.hc.SetDeadline(kcpvc.conntokeep)
  72. return kcpvc.hc.Read(b)
  73. }
  74. func (kcpvc *KCPVconn) Write(b []byte) (int, error) {
  75. ifb := time.Now().Add(time.Duration(effectiveConfig.WriteTimeout) * time.Second)
  76. if ifb.After(kcpvc.conntokeep) {
  77. kcpvc.conntokeep = ifb
  78. }
  79. kcpvc.hc.SetDeadline(kcpvc.conntokeep)
  80. return kcpvc.hc.Write(b)
  81. }
  82. /*ApplyConf will apply kcpvc.conf to current Socket
  83. It is recommmanded to call this func once and only once
  84. */
  85. func (kcpvc *KCPVconn) ApplyConf() error {
  86. nodelay, interval, resend, nc := 0, 40, 0, 0
  87. switch effectiveConfig.Mode {
  88. case "normal":
  89. nodelay, interval, resend, nc = 0, 30, 2, 1
  90. case "fast":
  91. nodelay, interval, resend, nc = 0, 20, 2, 1
  92. case "fast2":
  93. nodelay, interval, resend, nc = 1, 20, 2, 1
  94. case "fast3":
  95. nodelay, interval, resend, nc = 1, 10, 2, 1
  96. }
  97. kcpvc.hc.SetNoDelay(nodelay, interval, resend, nc)
  98. kcpvc.hc.SetWindowSize(effectiveConfig.Sndwnd, effectiveConfig.Rcvwnd)
  99. kcpvc.hc.SetMtu(effectiveConfig.Mtu)
  100. kcpvc.hc.SetACKNoDelay(effectiveConfig.Acknodelay)
  101. kcpvc.hc.SetDSCP(effectiveConfig.Dscp)
  102. return nil
  103. }
  104. /*Close Close the current conn
  105. We have to delay the close of Socket for a few second
  106. or the VMess EOF can be too late to send.
  107. */
  108. func (kcpvc *KCPVconn) Close() error {
  109. go func() {
  110. time.Sleep(2000 * time.Millisecond)
  111. kcpvc.hc.Close()
  112. }()
  113. return nil
  114. }
  115. func (kcpvc *KCPVconn) LocalAddr() net.Addr {
  116. return kcpvc.hc.LocalAddr()
  117. }
  118. func (kcpvc *KCPVconn) RemoteAddr() net.Addr {
  119. return kcpvc.hc.RemoteAddr()
  120. }
  121. func (kcpvc *KCPVconn) SetDeadline(t time.Time) error {
  122. return kcpvc.hc.SetDeadline(t)
  123. }
  124. func (kcpvc *KCPVconn) SetReadDeadline(t time.Time) error {
  125. return kcpvc.hc.SetReadDeadline(t)
  126. }
  127. func (kcpvc *KCPVconn) SetWriteDeadline(t time.Time) error {
  128. return kcpvc.hc.SetWriteDeadline(t)
  129. }
  130. func (this *KCPVconn) Reusable() bool {
  131. return false
  132. }
  133. func (this *KCPVconn) SetReusable(b bool) {
  134. }
  135. func ListenKCP(address v2net.Address, port v2net.Port) (internet.Listener, error) {
  136. conn, err := net.ListenUDP("udp", &net.UDPAddr{
  137. IP: address.IP(),
  138. Port: int(port),
  139. })
  140. if err != nil {
  141. return nil, err
  142. }
  143. block, _ := NewNoneBlockCrypt(nil)
  144. l := new(Listener)
  145. l.conn = conn
  146. l.sessions = make(map[string]*UDPSession)
  147. l.chAccepts = make(chan *UDPSession, 1024)
  148. l.chDeadlinks = make(chan net.Addr, 1024)
  149. l.die = make(chan struct{})
  150. l.block = block
  151. // caculate header size
  152. if l.block != nil {
  153. l.headerSize += cryptHeaderSize
  154. }
  155. go l.monitor()
  156. return &KCPVlistener{lst: l}, nil
  157. }
  158. func init() {
  159. internet.KCPListenFunc = ListenKCP
  160. }