|
9 | 9 | "io"
|
10 | 10 | "net"
|
11 | 11 | "net/http"
|
| 12 | + "net/netip" |
12 | 13 | "net/url"
|
13 | 14 | "runtime"
|
14 | 15 | "strconv"
|
@@ -67,6 +68,8 @@ type dnsOverHTTPS struct {
|
67 | 68 | dialer *dnsDialer
|
68 | 69 | addr string
|
69 | 70 | skipCertVerify bool
|
| 71 | + ecsPrefix netip.Prefix |
| 72 | + ecsOverride bool |
70 | 73 | }
|
71 | 74 |
|
72 | 75 | // type check
|
@@ -99,6 +102,28 @@ func newDoHClient(urlString string, r *Resolver, preferH3 bool, params map[strin
|
99 | 102 | doh.skipCertVerify = true
|
100 | 103 | }
|
101 | 104 |
|
| 105 | + if ecs := params["ecs"]; ecs != "" { |
| 106 | + prefix, err := netip.ParsePrefix(ecs) |
| 107 | + if err != nil { |
| 108 | + addr, err := netip.ParseAddr(ecs) |
| 109 | + if err != nil { |
| 110 | + log.Warnln("DOH config with invalid ecs: %s", ecs) |
| 111 | + } else { |
| 112 | + doh.ecsPrefix = netip.PrefixFrom(addr, addr.BitLen()) |
| 113 | + } |
| 114 | + } else { |
| 115 | + doh.ecsPrefix = prefix |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if doh.ecsPrefix.IsValid() { |
| 120 | + log.Debugln("DOH [%s] config with ecs: %s", doh.addr, doh.ecsPrefix) |
| 121 | + } |
| 122 | + |
| 123 | + if params["ecs-override"] == "true" { |
| 124 | + doh.ecsOverride = true |
| 125 | + } |
| 126 | + |
102 | 127 | runtime.SetFinalizer(doh, (*dnsOverHTTPS).Close)
|
103 | 128 |
|
104 | 129 | return doh
|
@@ -126,6 +151,10 @@ func (doh *dnsOverHTTPS) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.
|
126 | 151 | }
|
127 | 152 | }()
|
128 | 153 |
|
| 154 | + if doh.ecsPrefix.IsValid() { |
| 155 | + setEdns0Subnet(m, doh.ecsPrefix, doh.ecsOverride) |
| 156 | + } |
| 157 | + |
129 | 158 | // Check if there was already an active client before sending the request.
|
130 | 159 | // We'll only attempt to re-connect if there was one.
|
131 | 160 | client, isCached, err := doh.getClient(ctx)
|
|
0 commit comments