diff options
| author | Shulhan <ms@kilabit.info> | 2021-12-25 15:16:31 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-12-27 09:54:25 +0700 |
| commit | 69c25e7787664b1f7641a0846f9fb3796ccb79ad (patch) | |
| tree | 925b483c1976e6722b0d5cdb43afd9c72b764d48 | |
| parent | 35a0968dfe8f636617a6cd4eb74677a257880e7a (diff) | |
| download | rescached-69c25e7787664b1f7641a0846f9fb3796ccb79ad.tar.xz | |
cmd/rescached: add command "embed" and to run in development mode
This two commands is used internally for development.
The "embed" command embed all files inside "_www" directory into
Go file "memfs_generate.go".
This command replace "internal/generate_memfs.go".
The "dev" command run the rescached server in development mode using
"cmd/rescached/rescached.cfg.test" as the configuration.
The "dev" command listen on DNS port 5350, so to prevent conflict with
live rescached server, we run script _bin/nft_dnstest_chain.sh to redirect
UDP and TCP requests from port 53 to port 5350.
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | Makefile | 13 | ||||
| -rwxr-xr-x | _bin/nft_dnstest_chain.sh | 13 | ||||
| -rw-r--r-- | cmd/rescached/main.go | 97 | ||||
| -rw-r--r-- | cmd/rescached/rescached.cfg.test | 8 | ||||
| -rw-r--r-- | internal/generate_memfs.go | 39 |
6 files changed, 128 insertions, 45 deletions
@@ -1,6 +1,7 @@ /README.html /TODO -/_bin +/_bin/darwin_amd64 +/_bin/linux_amd64 /_doc/benchmark.html /cover.html /cover.out @@ -56,7 +56,7 @@ lint: -golangci-lint run --enable-all ./... memfs_generate.go: .FORCE - go run ./internal/generate_memfs.go + go run ./cmd/rescached embed doc: $(RESCACHED_MAN) $(RESCACHED_CFG_MAN) $(RESOLVER_MAN) @@ -82,6 +82,17 @@ clean: rm -f $(RESCACHED_BIN) $(RESOLVER_BIN) $(RESOLVERBENCH_BIN) ## +## Development tasks +## + +.PHONY: dev + +dev: + -sudo ./_bin/nft_dnstest_chain.sh; \ + go run ./cmd/rescached -config=cmd/rescached/rescached.cfg.test dev; \ + sudo ./_bin/nft_dnstest_chain.sh flush + +## ## Common tasks for installing and uninstalling program. ## diff --git a/_bin/nft_dnstest_chain.sh b/_bin/nft_dnstest_chain.sh new file mode 100755 index 0000000..8ab9a84 --- /dev/null +++ b/_bin/nft_dnstest_chain.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +if [[ "$1" == "flush" ]]; then + echo "nft: delete chain dnstest"; + nft delete chain ip nat dnstest; + exit 0 +fi + +## Forward port 53 to 5350 for testing. + +nft -- add chain ip nat dnstest { type nat hook output priority 0 \; } +nft add rule ip nat dnstest tcp dport 53 redirect to 5350 +nft add rule ip nat dnstest udp dport 53 redirect to 5350 diff --git a/cmd/rescached/main.go b/cmd/rescached/main.go index ebf0a62..8c9c8da 100644 --- a/cmd/rescached/main.go +++ b/cmd/rescached/main.go @@ -14,13 +14,22 @@ import ( "time" "github.com/shuLhan/share/lib/debug" + libio "github.com/shuLhan/share/lib/io" + "github.com/shuLhan/share/lib/memfs" + "github.com/shuLhan/share/lib/mlog" "github.com/shuLhan/rescached-go/v4" ) +const ( + cmdEmbed = "embed" // Command to generate embedded files. + cmdDev = "dev" // Command to run rescached for local development. +) + func main() { var ( fileConfig string + running chan bool ) log.SetFlags(0) @@ -34,6 +43,21 @@ func main() { log.Fatal(err) } + cmd := flag.Arg(0) + + switch cmd { + case cmdEmbed: + err = env.HttpdOptions.Memfs.GoEmbed() + if err != nil { + log.Fatal(err) + } + return + + case cmdDev: + running = make(chan bool) + go watchWww(env, running) + } + rcd, err := rescached.New(env) if err != nil { log.Fatal(err) @@ -52,7 +76,12 @@ func main() { signal.Notify(c, syscall.SIGQUIT, syscall.SIGSEGV, syscall.SIGTERM, syscall.SIGINT) <-c + if cmd == cmdDev { + running <- false + <-running + } rcd.Stop() + os.Exit(0) } func debugRuntime() { @@ -69,3 +98,71 @@ func debugRuntime() { memHeap.DiffHeapObjects) } } + +// +// watchWww watch any changes to files inside _www directory and regenerate +// the embed file "memfs_generate.go". +// +func watchWww(env *rescached.Environment, running chan bool) { + var ( + logp = "watchWww" + changeq = make(chan *libio.NodeState, 64) + dw = libio.DirWatcher{ + Options: *env.HttpdOptions.Memfs.Opts, + Callback: func(ns *libio.NodeState) { + changeq <- ns + }, + } + node *memfs.Node + nChanges int + err error + isRunning bool = true + ) + + dw.Start() + + tick := time.NewTicker(5 * time.Second) + + for isRunning { + select { + case ns := <-changeq: + node, err = env.HttpdOptions.Memfs.Get(ns.Node.Path) + if err != nil { + log.Printf("%s: %q: %s", logp, ns.Node.Path, err) + continue + } + if node != nil { + err = node.Update(nil, 0) + if err != nil { + mlog.Errf("%s: %q: %s", logp, node.Path, err) + continue + } + nChanges++ + } + + case <-tick.C: + if nChanges > 0 { + fmt.Printf("--- %d changes\n", nChanges) + err = env.HttpdOptions.Memfs.GoEmbed() + if err != nil { + log.Printf("%s", err) + } + nChanges = 0 + } + + case <-running: + isRunning = false + } + } + + // Run GoEmbed for the last time. + if nChanges > 0 { + fmt.Printf("--- %d changes\n", nChanges) + err = env.HttpdOptions.Memfs.GoEmbed() + if err != nil { + log.Printf("%s", err) + } + } + dw.Stop() + running <- false +} diff --git a/cmd/rescached/rescached.cfg.test b/cmd/rescached/rescached.cfg.test index 5a03d6d..41c43a3 100644 --- a/cmd/rescached/rescached.cfg.test +++ b/cmd/rescached/rescached.cfg.test @@ -6,7 +6,7 @@ [rescached] file.resolvconf= -debug=3 +debug=2 wui.listen = 127.0.0.1:5381 hosts_block = http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate[day]=&startdate[month]=&startdate[year]=&mimetype=plaintext @@ -15,10 +15,10 @@ hosts_block = http://winhelp2002.mvps.org/hosts.txt hosts_block = http://someonewhocares.org/hosts/hosts [dns "server"] -#parent=udp://18.136.35.199 -#parent=tcp://18.136.35.199 +#parent=udp://62.171.181.13 +#parent=tcp://62.171.181.13 ## DNS over TLS -parent=https://18.136.35.199 +parent=https://62.171.181.13 ## DNS over HTTPS #parent=https://kilabit.info/dns-query diff --git a/internal/generate_memfs.go b/internal/generate_memfs.go deleted file mode 100644 index 7d34285..0000000 --- a/internal/generate_memfs.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2020, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build ignore -// +build ignore - -package main - -import ( - "log" - - "github.com/shuLhan/share/lib/memfs" -) - -func main() { - opts := memfs.Options{ - Root: "_www", - Includes: []string{ - `.*\.html`, - `.*\.js`, - `.*\.css`, - `.*\.png`, - }, - Embed: memfs.EmbedOptions{ - PackageName: "rescached", - VarName: "memFS", - GoFileName: "memfs_generate.go", - }, - } - mfs, err := memfs.New(&opts) - if err != nil { - log.Fatal(err) - } - err = mfs.GoEmbed() - if err != nil { - log.Fatal(err) - } -} |
