dialer.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package quic
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. quic "github.com/lucas-clemente/quic-go"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/net"
  9. "v2ray.com/core/transport/internet"
  10. "v2ray.com/core/transport/internet/tls"
  11. )
  12. type sessionContext struct {
  13. rawConn *sysConn
  14. session quic.Session
  15. }
  16. type clientSessions struct {
  17. access sync.Mutex
  18. sessions map[net.Destination][]*sessionContext
  19. }
  20. func isActive(s quic.Session) bool {
  21. select {
  22. case <-s.Context().Done():
  23. return false
  24. default:
  25. return true
  26. }
  27. }
  28. func removeInactiveSessions(sessions []*sessionContext) []*sessionContext {
  29. activeSessions := make([]*sessionContext, 0, len(sessions))
  30. for _, s := range sessions {
  31. if isActive(s.session) {
  32. activeSessions = append(activeSessions, s)
  33. } else {
  34. s.rawConn.Close()
  35. }
  36. }
  37. if len(activeSessions) < len(sessions) {
  38. return activeSessions
  39. }
  40. return sessions
  41. }
  42. func openStream(sessions []*sessionContext) (quic.Stream, net.Addr) {
  43. for _, s := range sessions {
  44. if !isActive(s.session) {
  45. continue
  46. }
  47. stream, err := s.session.OpenStream()
  48. if err != nil {
  49. newError("failed to create stream").Base(err).AtWarning().WriteToLog()
  50. continue
  51. }
  52. return stream, s.session.LocalAddr()
  53. }
  54. return nil, nil
  55. }
  56. func (s *clientSessions) openConnection(destAddr net.Addr, config *Config, tlsConfig *tls.Config, sockopt *internet.SocketConfig) (internet.Connection, error) {
  57. s.access.Lock()
  58. defer s.access.Unlock()
  59. if s.sessions == nil {
  60. s.sessions = make(map[net.Destination][]*sessionContext)
  61. }
  62. dest := net.DestinationFromAddr(destAddr)
  63. var sessions []*sessionContext
  64. if s, found := s.sessions[dest]; found {
  65. sessions = s
  66. }
  67. {
  68. stream, local := openStream(sessions)
  69. if stream != nil {
  70. return &interConn{
  71. stream: stream,
  72. local: local,
  73. remote: destAddr,
  74. }, nil
  75. }
  76. }
  77. sessions = removeInactiveSessions(sessions)
  78. rawConn, err := internet.ListenSystemPacket(context.Background(), &net.UDPAddr{
  79. IP: []byte{0, 0, 0, 0},
  80. Port: 0,
  81. }, sockopt)
  82. if err != nil {
  83. return nil, err
  84. }
  85. quicConfig := &quic.Config{
  86. ConnectionIDLength: 8,
  87. HandshakeTimeout: time.Second * 8,
  88. IdleTimeout: time.Second * 30,
  89. MaxReceiveStreamFlowControlWindow: 128 * 1024,
  90. MaxReceiveConnectionFlowControlWindow: 2 * 1024 * 1024,
  91. MaxIncomingUniStreams: -1,
  92. MaxIncomingStreams: 32,
  93. }
  94. conn, err := wrapSysConn(rawConn, config)
  95. if err != nil {
  96. rawConn.Close()
  97. return nil, err
  98. }
  99. session, err := quic.DialContext(context.Background(), conn, destAddr, "", tlsConfig.GetTLSConfig(tls.WithDestination(dest)), quicConfig)
  100. if err != nil {
  101. conn.Close()
  102. return nil, err
  103. }
  104. s.sessions[dest] = append(sessions, &sessionContext{
  105. session: session,
  106. rawConn: conn,
  107. })
  108. stream, err := session.OpenStream()
  109. if err != nil {
  110. return nil, err
  111. }
  112. return &interConn{
  113. stream: stream,
  114. local: session.LocalAddr(),
  115. remote: destAddr,
  116. }, nil
  117. }
  118. var client clientSessions
  119. func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
  120. tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
  121. if tlsConfig == nil {
  122. tlsConfig = &tls.Config{
  123. ServerName: internalDomain,
  124. AllowInsecure: true,
  125. }
  126. }
  127. destAddr, err := net.ResolveUDPAddr("udp", dest.NetAddr())
  128. if err != nil {
  129. return nil, err
  130. }
  131. config := streamSettings.ProtocolSettings.(*Config)
  132. return client.openConnection(destAddr, config, tlsConfig, streamSettings.SocketSettings)
  133. }
  134. func init() {
  135. common.Must(internet.RegisterTransportDialer(protocolName, Dial))
  136. }