Debian Codegraph @

cmd/atomdns/handlers/drunk/setup.go

raw
1 package drunk
2
3 import (
4 "fmt"
5 "strconv"
6 "strings"
7 "time"
8
9 "codeberg.org/miekg/dns/cmd/atomdns/internal/dnsserver"
10 )
11
12 func (d *Drunk) Setup(co *dnsserver.Controller) error {
13 d.drop = 4
14 drop := false
15 for co.Next() {
16 for co.NextBlock(0) {
17 switch co.Val() {
18 case "drop":
19 drop = true
20 args := co.RemainingArgs()
21 if len(args) > 1 {
22 return co.ArgErr()
23 }
24
25 if len(args) == 0 {
26 continue
27 }
28 if !strings.HasPrefix(args[0], "/") {
29 return co.PropErr(fmt.Errorf("no / found"))
30 }
31 drop, err := strconv.ParseInt(args[0][1:], 10, 32)
32 if err != nil || drop < 0 {
33 return co.PropErr(fmt.Errorf("not a (positive) number: %s", co.Val()[1:]))
34 }
35 d.drop = uint64(drop)
36 case "delay":
37 args := co.RemainingArgs()
38 if len(args) > 2 {
39 return co.ArgErr()
40 }
41
42 d.delay = 2
43 d.duration = 100 * time.Millisecond
44 if len(args) == 0 {
45 continue
46 }
47
48 if !strings.HasPrefix(args[0], "/") {
49 return co.PropErr(fmt.Errorf("no / found"))
50 }
51 delay, err := strconv.ParseInt(args[0][1:], 10, 32)
52 if err != nil || delay < 0 {
53 return co.PropErr(fmt.Errorf("not a (positive) number: %s", args[0][1:]))
54 }
55 d.delay = uint64(delay)
56
57 if len(args) > 1 {
58 duration, err := time.ParseDuration(args[1])
59 if err != nil {
60 return err
61 }
62 d.duration = duration
63 }
64 case "truncate":
65 args := co.RemainingArgs()
66 if len(args) > 1 {
67 return co.ArgErr()
68 }
69
70 d.truncate = 4
71 if len(args) == 0 {
72 continue
73 }
74
75 truncate, err := strconv.ParseInt(args[0][1:], 10, 32)
76 if err != nil || truncate < 0 {
77 return co.PropErr(fmt.Errorf("not a (positive) number: %s", co.Val()[1:]))
78 }
79 d.truncate = uint64(truncate)
80 default:
81 return co.PropErr()
82 }
83 }
84 }
85 if (d.delay > 0 || d.truncate > 0) && !drop { // delay is set, but we've haven't seen a drop keyword, remove default drop stuff
86 d.drop = 0
87 }
88 return nil
89 }