diff options
| author | Shulhan <ms@kilabit.info> | 2022-04-20 01:19:39 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-04-20 01:19:39 +0700 |
| commit | 80718f34042298ccef7db0396471d2e00d60d893 (patch) | |
| tree | 785ea6281d6de542b66add71e56b74e254ca05eb /cmd/resolver | |
| parent | 289bb8e6bd9e6d8dcad93f92a1602779170c7fe6 (diff) | |
| download | rescached-80718f34042298ccef7db0396471d2e00d60d893.tar.xz | |
cmd/resolver: implement command to update block.d hosts by its name
The following command: "resolver block.d update x" will fetch the
latest hosts file from the block.d provider "x" based on its registered
URL.
Diffstat (limited to 'cmd/resolver')
| -rw-r--r-- | cmd/resolver/main.go | 28 | ||||
| -rw-r--r-- | cmd/resolver/resolver.go | 27 |
2 files changed, 53 insertions, 2 deletions
diff --git a/cmd/resolver/main.go b/cmd/resolver/main.go index f2d4baf..a3ecbe5 100644 --- a/cmd/resolver/main.go +++ b/cmd/resolver/main.go @@ -15,6 +15,7 @@ import ( // List of valid commands. const ( + cmdBlockd = "block.d" cmdCaches = "caches" cmdEnv = "env" cmdQuery = "query" @@ -58,6 +59,26 @@ func main() { rsol.cmd = strings.ToLower(args[0]) switch rsol.cmd { + case cmdBlockd: + args = args[1:] + if len(args) == 0 { + log.Fatalf("resolver: %s: missing sub command", cmdBlockd) + } + + subCmd = strings.ToLower(args[0]) + + switch subCmd { + case subCmdUpdate: + args = args[1:] + if len(args) == 0 { + log.Fatalf("resolver: %s %s: missing argument", rsol.cmd, subCmd) + } + err = rsol.blockdUpdate(args[0]) + if err != nil { + log.Fatalf("resolver: %s %s: %s", rsol.cmd, subCmd, err) + } + } + case cmdCaches: args = args[1:] if len(args) == 0 { @@ -178,6 +199,13 @@ query <domain / ip-address> [type] [class] Valid class are either IN, CS, HS. Default value is IN. +block.d update <name> + + Fetch the latest hosts file from remote block.d URL defined by + its name. + On success, the hosts file will be updated and the server will be + restarted. + caches Fetch and print all caches from rescached server. diff --git a/cmd/resolver/resolver.go b/cmd/resolver/resolver.go index 697953d..76c5a13 100644 --- a/cmd/resolver/resolver.go +++ b/cmd/resolver/resolver.go @@ -45,6 +45,31 @@ type resolver struct { insecure bool } +// blockdUpdate fetch the latest hosts file from remote block.d URL defined by +// its name. +func (rsol *resolver) blockdUpdate(blockdName string) (err error) { + var ( + resc = rsol.newRescachedClient() + + hb interface{} + hbjson []byte + ) + + hb, err = resc.BlockdUpdate(blockdName) + if err != nil { + return err + } + + hbjson, err = json.MarshalIndent(hb, "", " ") + if err != nil { + return err + } + + fmt.Println(string(hbjson)) + + return nil +} + // doCmdCaches call the rescached HTTP API to fetch all caches. func (rsol *resolver) doCmdCaches() { var ( @@ -252,10 +277,8 @@ func (rsol *resolver) doCmdQuery(args []string) { } } -// // initSystemResolver read the system resolv.conf to create fallback DNS // resolver. -// func (rsol *resolver) initSystemResolver() (err error) { var ( logp = "initSystemResolver" |
