Skip to content

Commit b878a4b

Browse files
committed
dns: no negative TTL
1 parent 0c9f859 commit b878a4b

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/miekg/dns v1.1.58 // indirect
1616
github.com/natefinch/atomic v1.0.1 // indirect
1717
github.com/spf13/pflag v1.0.5 // indirect
18+
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
1819
golang.org/x/mod v0.14.0 // indirect
1920
golang.org/x/net v0.20.0 // indirect
2021
golang.org/x/sync v0.6.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
1616
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
1717
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
1818
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
19+
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
20+
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
1921
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
2022
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
2123
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=

output/dns.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func (o *DNSServer) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
187187
now := time.Now()
188188
for _, item := range items {
189189
netAddr := item.Address().Unmap()
190-
ttl := uint32(item.ExpiresAt().Sub(now).Seconds())
190+
ttl := uint32(util.Max(item.ExpiresAt().Sub(now).Seconds(), 0))
191191
switch qtype {
192192
case dns.TypeA:
193193
if netAddr.Is4() {

util/util.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/netip"
1010
"strings"
1111

12+
"golang.org/x/exp/constraints"
1213
"gopkg.in/yaml.v3"
1314
)
1415

@@ -117,3 +118,17 @@ func ResolveInterface(spec string) (*net.Interface, error) {
117118
}
118119
return nil, errors.New("specified interface not found")
119120
}
121+
122+
func Max[T constraints.Ordered](x, y T) T {
123+
if x >= y {
124+
return x
125+
}
126+
return y
127+
}
128+
129+
func Min[T constraints.Ordered](x, y T) T {
130+
if x <= y {
131+
return x
132+
}
133+
return y
134+
}

0 commit comments

Comments
 (0)