Skip to content

Commit

Permalink
upstream: add helper protocol {tcp|tls}+pipeline, h3
Browse files Browse the repository at this point in the history
  • Loading branch information
IrineSistiana committed Oct 29, 2023
1 parent d8e5220 commit 2d1cd20
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions pkg/upstream/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 2d1cd20

Please sign in to comment.