|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "net/netip" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/metacubex/mihomo/component/dialer" |
| 10 | + C "github.com/metacubex/mihomo/constant" |
| 11 | + |
| 12 | + "github.com/metacubex/quic-go" |
| 13 | + "github.com/metacubex/tls" |
| 14 | +) |
| 15 | + |
| 16 | +// DialQuicEarly dials a new connection, attempting to use 0-RTT if possible. |
| 17 | +func DialQuicEarly(ctx context.Context, address string, opts []dialer.Option, dialer C.Dialer, tlsConf *tls.Config, conf *quic.Config) (net.PacketConn, *quic.Conn, error) { |
| 18 | + return dialQuic(ctx, address, opts, dialer, tlsConf, conf, true) |
| 19 | +} |
| 20 | + |
| 21 | +// DialQuic dials a new connection to a remote host (not using 0-RTT). |
| 22 | +func DialQuic(ctx context.Context, address string, opts []dialer.Option, dialer C.Dialer, tlsConf *tls.Config, conf *quic.Config) (net.PacketConn, *quic.Conn, error) { |
| 23 | + return dialQuic(ctx, address, opts, dialer, tlsConf, conf, false) |
| 24 | +} |
| 25 | + |
| 26 | +func dialQuic(ctx context.Context, address string, opts []dialer.Option, cDialer C.Dialer, tlsConf *tls.Config, conf *quic.Config, early bool) (net.PacketConn, *quic.Conn, error) { |
| 27 | + d := dialer.NewDialer( |
| 28 | + dialer.WithOptions(opts...), |
| 29 | + dialer.WithNetDialer(dialer.NetDialerFunc(func(ctx context.Context, network, address string) (net.Conn, error) { |
| 30 | + addrPort, err := netip.ParseAddrPort(address) // the dialer will resolve the domain to ip |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + udpAddr := net.UDPAddrFromAddrPort(addrPort) |
| 35 | + packetConn, err := cDialer.ListenPacket(ctx, "udp", "", udpAddr.AddrPort()) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + transport := quic.Transport{Conn: packetConn} |
| 40 | + transport.SetCreatedConn(true) // auto close conn |
| 41 | + transport.SetSingleUse(true) // auto close transport |
| 42 | + |
| 43 | + var quicConn *quic.Conn |
| 44 | + if early { |
| 45 | + quicConn, err = transport.DialEarly(ctx, udpAddr, tlsConf, conf) |
| 46 | + } else { |
| 47 | + quicConn, err = transport.Dial(ctx, udpAddr, tlsConf, conf) |
| 48 | + } |
| 49 | + if err != nil { |
| 50 | + _ = packetConn.Close() |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + return quicNetConn{Conn: quicConn, pc: packetConn}, nil |
| 54 | + })), |
| 55 | + ) |
| 56 | + c, err := d.DialContext(ctx, "udp", address) |
| 57 | + if err != nil { |
| 58 | + return nil, nil, err |
| 59 | + } |
| 60 | + nc := c.(quicNetConn) |
| 61 | + return nc.pc, nc.Conn, nil |
| 62 | +} |
| 63 | + |
| 64 | +type quicNetConn struct { |
| 65 | + *quic.Conn |
| 66 | + pc net.PacketConn |
| 67 | +} |
| 68 | + |
| 69 | +func (q quicNetConn) Close() error { |
| 70 | + err := q.Conn.CloseWithError(0, "") |
| 71 | + _ = q.pc.Close() // always close the packetConn |
| 72 | + return err |
| 73 | +} |
| 74 | + |
| 75 | +func (q quicNetConn) Read(b []byte) (n int, err error) { |
| 76 | + panic("should not call Read on quicNetConn") |
| 77 | +} |
| 78 | + |
| 79 | +func (q quicNetConn) Write(b []byte) (n int, err error) { |
| 80 | + panic("should not call Write on quicNetConn") |
| 81 | +} |
| 82 | + |
| 83 | +func (q quicNetConn) SetDeadline(t time.Time) error { |
| 84 | + panic("should not call SetDeadline on quicNetConn") |
| 85 | +} |
| 86 | + |
| 87 | +func (q quicNetConn) SetReadDeadline(t time.Time) error { |
| 88 | + panic("should not call SetReadDeadline on quicNetConn") |
| 89 | +} |
| 90 | + |
| 91 | +func (q quicNetConn) SetWriteDeadline(t time.Time) error { |
| 92 | + panic("should not call SetWriteDeadline on quicNetConn") |
| 93 | +} |
| 94 | + |
| 95 | +var _ net.Conn = quicNetConn{} |
0 commit comments