diff options
| author | Shulhan <ms@kilabit.info> | 2022-05-13 21:14:29 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-05-13 21:14:29 +0700 |
| commit | 20c3f80e7dfd9e453d757199beb2137c09a9f536 (patch) | |
| tree | 82711df23adee40b578a4da395a9588877132d88 | |
| parent | c9de13ce2432dee7d58fb45c4cecbf026786a48d (diff) | |
| download | rescached-20c3f80e7dfd9e453d757199beb2137c09a9f536.tar.xz | |
cmd/resolver: implement command to delete record on hosts file
The command has the following signature,
resolver hosts.d rr delete <name> <domain>
Given the hosts name "hosts" and domain "my.hosts" it will delete
all records that have domain name "my.hosts" inside the file.
| -rw-r--r-- | _doc/resolver.adoc | 30 | ||||
| -rw-r--r-- | _sys/usr/share/man/man1/resolver.1.gz | bin | 2811 -> 2906 bytes | |||
| -rw-r--r-- | client.go | 31 | ||||
| -rw-r--r-- | cmd/resolver/main.go | 15 | ||||
| -rw-r--r-- | cmd/resolver/resolver.go | 18 | ||||
| -rw-r--r-- | go.mod | 2 | ||||
| -rw-r--r-- | go.sum | 4 | ||||
| -rw-r--r-- | httpd.go | 6 |
8 files changed, 101 insertions, 5 deletions
diff --git a/_doc/resolver.adoc b/_doc/resolver.adoc index f0d40de..9951ad5 100644 --- a/_doc/resolver.adoc +++ b/_doc/resolver.adoc @@ -181,6 +181,13 @@ instead of replaced. -- +hosts.d rr delete <name> <domain>:: ++ +-- +Delete record from hosts file "name" by domain name. +-- + + == EXIT STATUS Upon exit and success +resolver+ will return 0, or 1 otherwise. @@ -188,6 +195,8 @@ Upon exit and success +resolver+ will return 0, or 1 otherwise. == EXAMPLES +=== QUERY EXAMPLES + Query the IPv4 address for kilabit.info, $ resolver query kilabit.info @@ -215,6 +224,9 @@ Inspect the rescached's caches on server at http://127.0.0.1:5380, $ resolver -server=http://127.0.0.1:5380 caches + +=== MANAGING CACHES + Search caches that contains "bit" on the domain name, $ resolver caches search bit @@ -227,6 +239,9 @@ Remove all caches in the server, $ resolver caches remove all + +=== MANAGING ENVIRONMENT + Fetch and print current server environment, $ resolver env @@ -239,6 +254,9 @@ Update the server environment by reading JSON from standard input, $ cat /tmp/env.json | resolver env update - + +=== MANAGING HOSTS.D + Create new hosts file named "myhosts" inside the hosts.d directory, $ resolver hosts.d create myhosts @@ -269,6 +287,8 @@ Get the content of hosts file named "myhosts" inside the hosts.d directory, } ] +=== MANAGING RECORD ON HOSTS.D + Add new record "127.0.0.1 my.hosts" to hosts file named "hosts", $ resolver hosts.d rr add hosts my.hosts 127.0.0.1 @@ -280,6 +300,16 @@ Add new record "127.0.0.1 my.hosts" to hosts file named "hosts", "TTL": 604800 } +Delete record "my.hosts" from hosts file "hosts", + + $ resolver hosts.d rr delete hosts my.hosts + { + "Value": "127.0.0.1", + "Name": "my.hosts", + "Type": 1, + "Class": 1, + "TTL": 604800 + } == AUTHOR diff --git a/_sys/usr/share/man/man1/resolver.1.gz b/_sys/usr/share/man/man1/resolver.1.gz Binary files differindex 61dc96c..5a6d304 100644 --- a/_sys/usr/share/man/man1/resolver.1.gz +++ b/_sys/usr/share/man/man1/resolver.1.gz @@ -380,3 +380,34 @@ func (cl *Client) HostsdRecordAdd(hostsName, domain, value string) (record *dns. return record, nil } + +// HostsdRecordDelete delete a record from hosts file by domain name. +func (cl *Client) HostsdRecordDelete(hostsName, domain string) (record *dns.ResourceRecord, err error) { + var ( + logp = "HostsdRecordDelete" + res = libhttp.EndpointResponse{ + Data: &record, + } + params = url.Values{} + + resb []byte + ) + + params.Set(paramNameName, hostsName) + params.Set(paramNameDomain, domain) + + _, resb, err = cl.Delete(apiHostsdRR, 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 record, nil +} diff --git a/cmd/resolver/main.go b/cmd/resolver/main.go index 2c40b90..fac894c 100644 --- a/cmd/resolver/main.go +++ b/cmd/resolver/main.go @@ -206,6 +206,10 @@ hosts.d rr add <name> <domain> <value> If the domain name already exists, the new record will be appended instead of replaced. +hosts.d rr delete <name> <domain> + + Delete record from hosts file "name" by domain name. + == Examples @@ -299,5 +303,16 @@ Add new record "127.0.0.1 my.hosts" to hosts file named "hosts", "Type": 1, "Class": 1, "TTL": 604800 + } + +Delete record "my.hosts" from hosts file "hosts", + + $ resolver hosts.d rr delete hosts my.hosts + { + "Value": "127.0.0.1", + "Name": "my.hosts", + "Type": 1, + "Class": 1, + "TTL": 604800 }`) } diff --git a/cmd/resolver/resolver.go b/cmd/resolver/resolver.go index 62ce953..792f8d9 100644 --- a/cmd/resolver/resolver.go +++ b/cmd/resolver/resolver.go @@ -362,6 +362,24 @@ func (rsol *resolver) doCmdHostsdRecord(args []string) { fmt.Println(string(jsonb)) case subCmdDelete: + args = args[1:] + if len(args) <= 1 { + log.Fatalf("resolver: %s %s: missing arguments", rsol.cmd, subCmd) + } + + resc = rsol.newRescachedClient() + record, err = resc.HostsdRecordDelete(args[0], args[1]) + if err != nil { + log.Fatalf("resolver: %s", err) + } + + jsonb, err = json.MarshalIndent(record, "", " ") + if err != nil { + log.Fatalf("resolver: %s", err) + } + + fmt.Println(string(jsonb)) + default: log.Fatalf("resolver: %s %s: unknown command %s", rsol.cmd, subCmdRR, subCmd) } @@ -5,6 +5,6 @@ module github.com/shuLhan/rescached-go/v4 go 1.16 -require github.com/shuLhan/share v0.36.1-0.20220418175009-e5c85cdbfb18 +require github.com/shuLhan/share v0.37.1-0.20220512163842-15f02872a61e //replace github.com/shuLhan/share => ../share @@ -1,5 +1,5 @@ -github.com/shuLhan/share v0.36.1-0.20220418175009-e5c85cdbfb18 h1:kC1DQeauBOs830pBOx0oD7u4WWdV74dpy1/QR3kPNqE= -github.com/shuLhan/share v0.36.1-0.20220418175009-e5c85cdbfb18/go.mod h1:laKGR1DNboj8+INRIC9VFYRiVEu/IIjrLurUmTHXkw0= +github.com/shuLhan/share v0.37.1-0.20220512163842-15f02872a61e h1:1EyRREadyiHwpNsz8z0YUFQ5ifGbiXG9nBg1JitnL7M= +github.com/shuLhan/share v0.37.1-0.20220512163842-15f02872a61e/go.mod h1:laKGR1DNboj8+INRIC9VFYRiVEu/IIjrLurUmTHXkw0= golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk= @@ -915,6 +915,7 @@ func (srv *Server) apiHostsdRecordDelete(epr *libhttp.EndpointRequest) (resbody domainName = epr.HttpRequest.Form.Get(paramNameDomain) hfile *dns.HostsFile + rr *dns.ResourceRecord found bool ) @@ -935,8 +936,8 @@ func (srv *Server) apiHostsdRecordDelete(epr *libhttp.EndpointRequest) (resbody return nil, &res } - found = hfile.RemoveRecord(domainName) - if !found { + rr = hfile.RemoveRecord(domainName) + if rr == nil { res.Message = "unknown domain name: " + domainName return nil, &res } @@ -951,6 +952,7 @@ func (srv *Server) apiHostsdRecordDelete(epr *libhttp.EndpointRequest) (resbody res.Code = http.StatusOK res.Message = "domain name '" + domainName + "' has been removed from hosts file" + res.Data = rr return json.Marshal(&res) } |
