diff options
| -rw-r--r-- | _doc/resolver.adoc | 49 | ||||
| -rw-r--r-- | _test/etc/rescached/hosts.d/hosts | 4 | ||||
| -rw-r--r-- | client.go | 87 | ||||
| -rw-r--r-- | cmd/resolver/main.go | 56 | ||||
| -rw-r--r-- | cmd/resolver/resolver.go | 64 |
5 files changed, 258 insertions, 2 deletions
diff --git a/_doc/resolver.adoc b/_doc/resolver.adoc index d4eb5e9..98bb5e3 100644 --- a/_doc/resolver.adoc +++ b/_doc/resolver.adoc @@ -151,6 +151,25 @@ If the argument is "-", the new environment is read from stdin. If the environment is valid, the server will be restarted. -- +hosts.d create <name>:: ++ +-- +Create new hosts file inside the hosts.d directory with specific file +name. +-- + +hosts.d delete <name>:: ++ +-- +Delete hosts file inside the hosts.d directory by file name. +-- + +hosts.d get <name>:: ++ +-- +Get the content of hosts file inside the hosts.d directory by file name. +-- + == EXIT STATUS @@ -210,6 +229,36 @@ Update the server environment by reading JSON from standard input, $ cat /tmp/env.json | resolver env update - +Create new hosts file named "myhosts" inside the hosts.d directory, + + $ resolver hosts.d create myhosts + OK + +Delete hosts file named "myhosts" inside the hosts.d directory, + + $ resolver hosts.d delete myhosts + OK + +Get the content of hosts file named "myhosts" inside the hosts.d directory, + + $ resolver hosts.d get myhosts + [ + { + "Value": "127.0.0.1", + "Name": "localhost", + "Type": 1, + "Class": 1, + "TTL": 604800 + }, + { + "Value": "::1", + "Name": "localhost", + "Type": 28, + "Class": 1, + "TTL": 604800 + } + ] + == AUTHOR diff --git a/_test/etc/rescached/hosts.d/hosts b/_test/etc/rescached/hosts.d/hosts new file mode 100644 index 0000000..106dfff --- /dev/null +++ b/_test/etc/rescached/hosts.d/hosts @@ -0,0 +1,4 @@ +# Static table lookup for hostnames. +# See hosts(5) for details. +127.0.0.1 localhost +::1 localhost @@ -261,3 +261,90 @@ func (cl *Client) EnvUpdate(envIn *Environment) (envOut *Environment, err error) } return envOut, nil } + +// HostsdCreate create new hosts file inside the hosts.d with requested name. +func (cl *Client) HostsdCreate(name string) (hostsFile *dns.HostsFile, err error) { + var ( + logp = "HostsdCreate" + res = libhttp.EndpointResponse{ + Data: &hostsFile, + } + params = url.Values{} + + resb []byte + ) + + params.Set(paramNameName, name) + + _, resb, err = cl.PostForm(apiHostsd, nil, params) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) + } + + err = json.Unmarshal(resb, &res) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) + } + if res.Code != http.StatusOK { + return nil, fmt.Errorf("%s: %d %s", logp, res.Code, res.Message) + } + return hostsFile, nil +} + +// HostsdDelete delete hosts file inside the hosts.d by file name. +func (cl *Client) HostsdDelete(name string) (hostsFile *dns.HostsFile, err error) { + var ( + logp = "HostsdDelete" + res = libhttp.EndpointResponse{ + Data: &hostsFile, + } + params = url.Values{} + + resb []byte + ) + + params.Set(paramNameName, name) + + _, resb, err = cl.Delete(apiHostsd, nil, params) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) + } + + err = json.Unmarshal(resb, &res) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) + } + if res.Code != http.StatusOK { + return nil, fmt.Errorf("%s: %d %s", logp, res.Code, res.Message) + } + return hostsFile, nil +} + +// HostsdGet get the content of hosts file inside the hosts.d by file name. +func (cl *Client) HostsdGet(name string) (listrr []*dns.ResourceRecord, err error) { + var ( + logp = "HostsdGet" + res = libhttp.EndpointResponse{ + Data: &listrr, + } + params = url.Values{} + + resb []byte + ) + + params.Set(paramNameName, name) + + _, resb, err = cl.Get(apiHostsd, nil, params) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) + } + + err = json.Unmarshal(resb, &res) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) + } + if res.Code != http.StatusOK { + return nil, fmt.Errorf("%s: %d %s", logp, res.Code, res.Message) + } + return listrr, nil +} diff --git a/cmd/resolver/main.go b/cmd/resolver/main.go index 0678b21..9ffc60b 100644 --- a/cmd/resolver/main.go +++ b/cmd/resolver/main.go @@ -18,10 +18,14 @@ const ( cmdBlockd = "block.d" cmdCaches = "caches" cmdEnv = "env" + cmdHostsd = "hosts.d" cmdQuery = "query" - subCmdEnable = "enable" + subCmdCreate = "create" + subCmdDelete = "delete" subCmdDisable = "disable" + subCmdEnable = "enable" + subCmdGet = "get" subCmdRemove = "remove" subCmdSearch = "search" subCmdUpdate = "update" @@ -154,6 +158,9 @@ func main() { os.Exit(2) } + case cmdHostsd: + rsol.doCmdHostsd(args[1:]) + case cmdQuery: args = args[1:] if len(args) == 0 { @@ -262,6 +269,20 @@ env update <path-to-file / "-"> If the argument is "-", the new environment is read from stdin. If the environment is valid, the server will be restarted. +hosts.d create <name> + + Create new hosts file inside the hosts.d directory with specific file + name. + +hosts.d delete <name> + + Delete hosts file inside the hosts.d directory by file name. + +hosts.d get <name> + + Get the content of hosts file inside the hosts.d directory by file + name. + == Examples @@ -314,5 +335,36 @@ Update the server environment from JSON file in /tmp/env.json, Update the server environment by reading JSON from standard input, - $ cat /tmp/env.json | resolver env update -`) + $ cat /tmp/env.json | resolver env update - + +Create new hosts file named "myhosts" inside the hosts.d directory, + + $ resolver hosts.d create myhosts + OK + +Delete hosts file named "myhosts" inside the hosts.d directory, + + $ resolver hosts.d delete myhosts + OK + +Get the content of hosts file named "myhosts" inside the hosts.d directory, + + $ resolver hosts.d get myhosts + [ + { + "Value": "127.0.0.1", + "Name": "localhost", + "Type": 1, + "Class": 1, + "TTL": 604800 + }, + { + "Value": "::1", + "Name": "localhost", + "Type": 28, + "Class": 1, + "TTL": 604800 + } + ] +`) } diff --git a/cmd/resolver/resolver.go b/cmd/resolver/resolver.go index bdebe0d..47f5e15 100644 --- a/cmd/resolver/resolver.go +++ b/cmd/resolver/resolver.go @@ -242,6 +242,70 @@ func (rsol *resolver) doCmdEnvUpdate(fileOrStdin string) (err error) { return nil } +func (rsol *resolver) doCmdHostsd(args []string) { + if len(args) == 0 { + log.Fatalf("resolver: %s: missing argument", rsol.cmd) + } + + var ( + subCmd = strings.ToLower(args[0]) + + resc *rescached.Client + listrr []*dns.ResourceRecord + jsonb []byte + err error + ) + switch subCmd { + case subCmdCreate: + args = args[1:] + if len(args) == 0 { + log.Fatalf("resolver: %s %s: missing hosts name argument", rsol.cmd, subCmd) + } + + resc = rsol.newRescachedClient() + _, err = resc.HostsdCreate(args[0]) + if err != nil { + log.Fatalf("resolver: %s", err) + } + fmt.Println("OK") + + case subCmdDelete: + args = args[1:] + if len(args) == 0 { + log.Fatalf("resolver: %s %s: missing hosts name argument", rsol.cmd, subCmd) + } + + resc = rsol.newRescachedClient() + _, err = resc.HostsdDelete(args[0]) + if err != nil { + log.Fatalf("resolver: %s", err) + } + fmt.Println("OK") + + case subCmdGet: + args = args[1:] + if len(args) == 0 { + log.Fatalf("resolver: %s %s: missing hosts name argument", rsol.cmd, subCmd) + } + + resc = rsol.newRescachedClient() + listrr, err = resc.HostsdGet(args[0]) + if err != nil { + log.Fatalf("resolver: %s", err) + } + + jsonb, err = json.MarshalIndent(listrr, "", " ") + if err != nil { + log.Fatalf("resolver: %s", err) + } + + fmt.Println(string(jsonb)) + + default: + log.Fatalf("resolver: %s: unknown sub command: %s", rsol.cmd, subCmd) + } +} + func (rsol *resolver) doCmdQuery(args []string) { var ( maxAttempts = defAttempts |
