Files
sing-box-extended-mirror/cmd/sing-box/cmd_tools_fetch_http3.go
T

45 lines
1.0 KiB
Go
Raw Normal View History

2024-06-07 15:55:21 +08:00
//go:build with_quic
package main
import (
"context"
"crypto/tls"
"net/http"
"github.com/sagernet/quic-go"
"github.com/sagernet/quic-go/http3"
box "github.com/sagernet/sing-box"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
func initializeHTTP3Client(instance *box.Box) error {
2024-11-09 21:16:11 +08:00
dialer, err := createDialer(instance, commandToolsFlagOutbound)
2024-06-07 15:55:21 +08:00
if err != nil {
return err
}
http3Client = &http.Client{
2024-12-29 18:39:22 +08:00
Transport: &http3.Transport{
2025-09-16 00:58:54 +08:00
Dial: func(ctx context.Context, addr string, tlsCfg *tls.Config, cfg *quic.Config) (*quic.Conn, error) {
2024-06-07 15:55:21 +08:00
destination := M.ParseSocksaddr(addr)
udpConn, dErr := dialer.DialContext(ctx, N.NetworkUDP, destination)
if dErr != nil {
return nil, dErr
}
2026-08-05 22:22:00 +08:00
quicConn, dErr := quic.DialEarlyConn(ctx, udpConn, tlsCfg, cfg)
if dErr != nil {
udpConn.Close()
return nil, dErr
}
go func() {
<-quicConn.Context().Done()
udpConn.Close()
}()
return quicConn, nil
2024-06-07 15:55:21 +08:00
},
},
}
return nil
}