mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-04-22 23:27:15 +08:00
chore: better DialQuic interface
This commit is contained in:
@@ -258,7 +258,7 @@ func (w *Masque) run(ctx context.Context) error {
|
||||
}
|
||||
} else {
|
||||
var quicConn *quic.Conn
|
||||
pc, quicConn, err = common.DialQuic(ctx, w.addr, w.DialOptions(), w.dialer, w.tlsConfig, w.quicConfig)
|
||||
pc, quicConn, err = common.DialQuic(ctx, w.addr, w.DialOptions(), w.dialer, w.tlsConfig, w.quicConfig, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -593,7 +593,7 @@ func NewVless(option VlessOption) (*Vless, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, quicConn, err := common.DialQuicEarly(ctx, v.addr, v.DialOptions(), v.dialer, tlsConfig, cfg)
|
||||
_, quicConn, err := common.DialQuic(ctx, v.addr, v.DialOptions(), v.dialer, tlsConfig, cfg, true)
|
||||
return quicConn, nil
|
||||
},
|
||||
v.option.ALPN,
|
||||
@@ -733,7 +733,7 @@ func NewVless(option VlessOption) (*Vless, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, quicConn, err := common.DialQuicEarly(ctx, downloadAddr, v.DialOptions(), v.dialer, tlsConfig, cfg)
|
||||
_, quicConn, err := common.DialQuic(ctx, downloadAddr, v.DialOptions(), v.dialer, tlsConfig, cfg, true)
|
||||
return quicConn, nil
|
||||
},
|
||||
downloadALPN,
|
||||
|
||||
@@ -34,7 +34,7 @@ func (c *Client) quicRoundTripper(tlsConfig *vmess.TLSConfig, congestionControlN
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, quicConn, err := common.DialQuicEarly(ctx, addr, c.dialOptions(), c.dialer, tlsCfg, cfg)
|
||||
_, quicConn, err := common.DialQuic(ctx, addr, c.dialOptions(), c.dialer, tlsCfg, cfg, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -7,23 +7,16 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/metacubex/mihomo/component/dialer"
|
||||
C "github.com/metacubex/mihomo/constant"
|
||||
|
||||
"github.com/metacubex/quic-go"
|
||||
"github.com/metacubex/tls"
|
||||
)
|
||||
|
||||
// DialQuicEarly dials a new connection, attempting to use 0-RTT if possible.
|
||||
func DialQuicEarly(ctx context.Context, address string, opts []dialer.Option, dialer C.Dialer, tlsConf *tls.Config, conf *quic.Config) (net.PacketConn, *quic.Conn, error) {
|
||||
return dialQuic(ctx, address, opts, dialer, tlsConf, conf, true)
|
||||
type PacketDialer interface {
|
||||
ListenPacket(ctx context.Context, network, address string, rAddrPort netip.AddrPort) (net.PacketConn, error)
|
||||
}
|
||||
|
||||
// DialQuic dials a new connection to a remote host (not using 0-RTT).
|
||||
func DialQuic(ctx context.Context, address string, opts []dialer.Option, dialer C.Dialer, tlsConf *tls.Config, conf *quic.Config) (net.PacketConn, *quic.Conn, error) {
|
||||
return dialQuic(ctx, address, opts, dialer, tlsConf, conf, false)
|
||||
}
|
||||
|
||||
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) {
|
||||
func DialQuic(ctx context.Context, address string, opts []dialer.Option, pDialer PacketDialer, tlsConf *tls.Config, conf *quic.Config, early bool) (net.PacketConn, *quic.Conn, error) {
|
||||
d := dialer.NewDialer(
|
||||
dialer.WithOptions(opts...),
|
||||
dialer.WithNetDialer(dialer.NetDialerFunc(func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
@@ -32,7 +25,7 @@ func dialQuic(ctx context.Context, address string, opts []dialer.Option, cDialer
|
||||
return nil, err
|
||||
}
|
||||
udpAddr := net.UDPAddrFromAddrPort(addrPort)
|
||||
packetConn, err := cDialer.ListenPacket(ctx, "udp", "", udpAddr.AddrPort())
|
||||
packetConn, err := pDialer.ListenPacket(ctx, "udp", "", udpAddr.AddrPort())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user