Skip to content

Commit

Permalink
Add support for dynamically-chosen IP:Ports for both the unboundtest …
Browse files Browse the repository at this point in the history
…and unbound services.

Also support other paths to unbound and unbound.conf
  • Loading branch information
jcjones authored and jsha committed Dec 7, 2023
1 parent b231a53 commit 15b8684
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ COPY index.html root.key /work/
COPY unbound.conf /etc/unbound/
WORKDIR /work/
EXPOSE 1232
CMD ["/usr/bin/unboundtest", "-config", "/etc/unbound/unbound.conf"]
CMD ["/usr/bin/unboundtest", "-unboundConfig", "/etc/unbound/unbound.conf"]
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,18 @@ docker run unboundtest

Then use `docker ps` and `docker inspect` to find the IP address of the
unboundtest container, and visit that IP address on port 1232.

## CLI
```
Usage of unboundtest:
-listen string
The address on which to listen for incoming Web requests (default ":1232")
-unboundAddress string
The address the unbound.conf instructs Unbound to listen on (default "127.0.0.1:1053")
-unboundConfig string
The path to the unbound.conf file (default "unbound.conf")
-unboundExec string
The path to the unbound executable (default "unbound")
-index string
The path to the index.html (default "index.html")
```
16 changes: 10 additions & 6 deletions unboundtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,33 @@ import (
"github.com/miekg/dns"
)

var unboundConfig = flag.String("config", "unbound.conf", "Path to unbound.conf")

// A regexp for reasonable close-to-valid DNS names
var dnsish = regexp.MustCompile("^[A-Za-z0-9-_.]+$")

// Only one Unbound should run at once, otherwise listen port will collide
var unboundMutex sync.Mutex

var listenAddr = flag.String("listen", ":1232", "The address on which to listen for incoming Web requests")
var unboundAddr = flag.String("unboundAddress", "127.0.0.1:1053", "The address the unbound.conf instructs Unbound to listen on")
var unboundConfig = flag.String("unboundConfig", "unbound.conf", "The path to the unbound.conf file")
var unboundExec = flag.String("unboundExec", "unbound", "The path to the unbound executable")
var indexFile = flag.String("index", "index.html", "The path to index.html")

func main() {
flag.Parse()
http.HandleFunc("/", indexHandler)
http.HandleFunc("/conf", configHandler)
http.HandleFunc("/q", queryHandler)
http.HandleFunc("/m/", memoryHandler)
http.ListenAndServe(":1232", nil)
http.ListenAndServe(*listenAddr, nil)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
file, err := os.Open("index.html")
file, err := os.Open(*indexFile)
if err != nil {
fmt.Fprintln(w, err)
return
Expand Down Expand Up @@ -136,7 +140,7 @@ func doQuery(ctx context.Context, q string, typ uint16, w io.Writer) error {
q = q + "."
}
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
cmd := exec.CommandContext(ctx, "unbound", "-d", "-c", *unboundConfig)
cmd := exec.CommandContext(ctx, *unboundExec, "-d", "-c", *unboundConfig)
defer func() {
cancel()
cmd.Wait()
Expand Down Expand Up @@ -177,7 +181,7 @@ func doQuery(ctx context.Context, q string, typ uint16, w io.Writer) error {
// Also retry on timeouts.
c.Timeout = time.Second * 30
for {
in, _, err := c.ExchangeContext(ctx, m, "127.0.0.1:1053")
in, _, err := c.ExchangeContext(ctx, m, *unboundAddr)
if err != nil {
return err
}
Expand Down

0 comments on commit 15b8684

Please sign in to comment.