From 2d1cd202fd2955e690f95531beeede40f87c5acc Mon Sep 17 00:00:00 2001 From: Irine Sistiana <49315432+IrineSistiana@users.noreply.github.com> Date: Sun, 29 Oct 2023 10:53:36 +0800 Subject: [PATCH] upstream: add helper protocol {tcp|tls}+pipeline, h3 --- pkg/upstream/upstream.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/upstream/upstream.go b/pkg/upstream/upstream.go index c8dfe7a72..beff79a46 100644 --- a/pkg/upstream/upstream.go +++ b/pkg/upstream/upstream.go @@ -91,7 +91,7 @@ type Opt struct { IdleTimeout time.Duration // EnablePipeline enables query pipelining support as RFC 7766 6.2.1.1 suggested. - // Available for TCP, DoT upstream with IdleTimeout >= 0. + // Available for TCP, DoT upstream. // Note: There is no fallback. Make sure the server supports it. EnablePipeline bool @@ -121,8 +121,12 @@ type Opt struct { } // NewUpstream creates a upstream. -// addr has the format of: [protocol://]host[:port][/path] +// addr has the format of: [protocol://]host[:port][/path]. // Supported protocol: udp/tcp/tls/https/quic. Default protocol is udp. +// +// Helper protocol: +// - tcp+pipeline/tls+pipeline: Automatically set opt.EnablePipeline to true. +// - h3: Automatically set opt.EnableHTTP3 to true. func NewUpstream(addr string, opt Opt) (_ Upstream, err error) { if opt.Logger == nil { opt.Logger = mlog.Nop() @@ -140,6 +144,16 @@ func NewUpstream(addr string, opt Opt) (_ Upstream, err error) { return nil, fmt.Errorf("invalid server address, %w", err) } + // Apply helper protocol + switch addrURL.Scheme { + case "tcp+pipeline", "tls+pipeline": + addrURL.Scheme = addrURL.Scheme[:3] + opt.EnablePipeline = true + case "h3": + addrURL.Scheme = "https" + opt.EnableHTTP3 = true + } + // If host is a ipv6 without port, it will be in []. This will cause err when // split and join address and port. Try to remove brackets now. addrUrlHost := tryTrimIpv6Brackets(addrURL.Host) @@ -453,7 +467,7 @@ func NewUpstream(addr string, opt Opt) (_ Upstream, err error) { t = t1 } - u, err := doh.NewUpstream(addr, t, opt.Logger) + u, err := doh.NewUpstream(addrURL.String(), t, opt.Logger) if err != nil { return nil, fmt.Errorf("failed to create doh upstream, %w", err) }