|
| 1 | +package quic |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/xtls/quic-go" |
| 9 | + "github.com/xtls/xray-core/common" |
| 10 | + "github.com/xtls/xray-core/common/errors" |
| 11 | + "github.com/xtls/xray-core/common/net" |
| 12 | + "github.com/xtls/xray-core/transport/internet" |
| 13 | + "github.com/xtls/xray-core/transport/internet/stat" |
| 14 | + "github.com/xtls/xray-core/transport/internet/tls" |
| 15 | +) |
| 16 | + |
| 17 | +type connectionContext struct { |
| 18 | + rawConn *net.UDPConn |
| 19 | + conn quic.Connection |
| 20 | +} |
| 21 | + |
| 22 | +type clientConnections struct { |
| 23 | + access sync.Mutex |
| 24 | + conns map[net.Destination][]*connectionContext |
| 25 | + // cleanup *task.Periodic |
| 26 | +} |
| 27 | + |
| 28 | +func isActive(s quic.Connection) bool { |
| 29 | + select { |
| 30 | + case <-s.Context().Done(): |
| 31 | + return false |
| 32 | + default: |
| 33 | + return true |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) { |
| 38 | + tlsConfig := tls.ConfigFromStreamSettings(streamSettings) |
| 39 | + if tlsConfig == nil { |
| 40 | + tlsConfig = &tls.Config{ |
| 41 | + ServerName: internalDomain, |
| 42 | + AllowInsecure: true, |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + var destAddr *net.UDPAddr |
| 47 | + if dest.Address.Family().IsIP() { |
| 48 | + destAddr = &net.UDPAddr{ |
| 49 | + IP: dest.Address.IP(), |
| 50 | + Port: int(dest.Port), |
| 51 | + } |
| 52 | + } else { |
| 53 | + dialerIp := internet.DestIpAddress() |
| 54 | + if dialerIp != nil { |
| 55 | + destAddr = &net.UDPAddr{ |
| 56 | + IP: dialerIp, |
| 57 | + Port: int(dest.Port), |
| 58 | + } |
| 59 | + errors.LogInfo(ctx, "quic Dial use dialer dest addr: ", destAddr) |
| 60 | + } else { |
| 61 | + addr, err := net.ResolveUDPAddr("udp", dest.NetAddr()) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + destAddr = addr |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + config := streamSettings.ProtocolSettings.(*Config) |
| 70 | + |
| 71 | + return client.openConnection(ctx, destAddr, config, tlsConfig, streamSettings.SocketSettings) |
| 72 | +} |
| 73 | + |
| 74 | +func (s *clientConnections) openConnection(ctx context.Context, destAddr net.Addr, config *Config, tlsConfig *tls.Config, sockopt *internet.SocketConfig) (stat.Connection, error) { |
| 75 | + s.access.Lock() |
| 76 | + defer s.access.Unlock() |
| 77 | + |
| 78 | + if s.conns == nil { |
| 79 | + s.conns = make(map[net.Destination][]*connectionContext) |
| 80 | + } |
| 81 | + |
| 82 | + dest := net.DestinationFromAddr(destAddr) |
| 83 | + |
| 84 | + var conns []*connectionContext |
| 85 | + if s, found := s.conns[dest]; found { |
| 86 | + conns = s |
| 87 | + } |
| 88 | + |
| 89 | + if len(conns) > 0 { |
| 90 | + s := conns[len(conns)-1] |
| 91 | + if isActive(s.conn) { |
| 92 | + return &interConn{ |
| 93 | + ctx: ctx, |
| 94 | + quicConn: s.conn, |
| 95 | + local: s.conn.LocalAddr(), |
| 96 | + remote: destAddr, |
| 97 | + }, nil |
| 98 | + } else { |
| 99 | + errors.LogInfo(ctx, "current quic connection is not active!") |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + errors.LogInfo(ctx, "dialing quic to ", dest) |
| 104 | + rawConn, err := internet.DialSystem(ctx, dest, sockopt) |
| 105 | + if err != nil { |
| 106 | + return nil, errors.New("failed to dial to dest: ", err).AtWarning().Base(err) |
| 107 | + } |
| 108 | + |
| 109 | + quicConfig := &quic.Config{ |
| 110 | + KeepAlivePeriod: 0, |
| 111 | + HandshakeIdleTimeout: time.Second * 8, |
| 112 | + MaxIdleTimeout: time.Second * 300, |
| 113 | + EnableDatagrams: true, |
| 114 | + } |
| 115 | + |
| 116 | + var udpConn *net.UDPConn |
| 117 | + switch conn := rawConn.(type) { |
| 118 | + case *net.UDPConn: |
| 119 | + udpConn = conn |
| 120 | + case *internet.PacketConnWrapper: |
| 121 | + udpConn = conn.Conn.(*net.UDPConn) |
| 122 | + default: |
| 123 | + rawConn.Close() |
| 124 | + return nil, errors.New("QUIC with sockopt is unsupported").AtWarning() |
| 125 | + } |
| 126 | + |
| 127 | + tr := quic.Transport{ |
| 128 | + ConnectionIDLength: 12, |
| 129 | + Conn: udpConn, |
| 130 | + } |
| 131 | + conn, err := tr.Dial(context.Background(), destAddr, tlsConfig.GetTLSConfig(tls.WithDestination(dest)), quicConfig) |
| 132 | + if err != nil { |
| 133 | + udpConn.Close() |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + |
| 137 | + context := &connectionContext{ |
| 138 | + conn: conn, |
| 139 | + rawConn: udpConn, |
| 140 | + } |
| 141 | + s.conns[dest] = append(conns, context) |
| 142 | + return &interConn{ |
| 143 | + ctx: ctx, |
| 144 | + quicConn: context.conn, |
| 145 | + local: context.conn.LocalAddr(), |
| 146 | + remote: destAddr, |
| 147 | + }, nil |
| 148 | +} |
| 149 | + |
| 150 | +var client clientConnections |
| 151 | + |
| 152 | +func init() { |
| 153 | + common.Must(internet.RegisterTransportDialer(protocolName, Dial)) |
| 154 | +} |
0 commit comments