summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2020-08-23 04:22:00 +0700
committerShulhan <m.shulhan@gmail.com>2020-08-23 04:22:13 +0700
commit5be49a28290b394f21dc03ac0dbc791357d9bd3f (patch)
tree0f6fdc9f9f0a6c403fb3959342cdb9e9bab734bd
parentefe5044fa4d4997e36dbfa5e6f6b13f996910336 (diff)
downloadrescached-5be49a28290b394f21dc03ac0dbc791357d9bd3f.tar.xz
all: replace hosts with dns.ResourceRecord
Both type hold the same fields, so use one from dns library.
-rw-r--r--host.go34
-rw-r--r--hosts_file.go25
-rw-r--r--httpd.go8
3 files changed, 17 insertions, 50 deletions
diff --git a/host.go b/host.go
deleted file mode 100644
index 196bf61..0000000
--- a/host.go
+++ /dev/null
@@ -1,34 +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.
-
-package rescached
-
-import "github.com/shuLhan/share/lib/dns"
-
-// host contains simplified DNS record.
-type host struct {
- Name string
- Type int
- Class int
- Value string
- TTL int
-}
-
-func convertRRToHost(from *dns.ResourceRecord) (to *host) {
- to = &host{
- Name: string(from.Name),
- Type: int(from.Type),
- Class: int(from.Class),
- TTL: int(from.TTL),
- }
- switch from.Type {
- case dns.QueryTypeA, dns.QueryTypeNS, dns.QueryTypeCNAME,
- dns.QueryTypeMB, dns.QueryTypeMG, dns.QueryTypeMR,
- dns.QueryTypeNULL, dns.QueryTypePTR, dns.QueryTypeTXT,
- dns.QueryTypeAAAA:
- to.Value = from.Value.(string)
- }
-
- return to
-}
diff --git a/hosts_file.go b/hosts_file.go
index c88f55e..cfa0743 100644
--- a/hosts_file.go
+++ b/hosts_file.go
@@ -15,7 +15,7 @@ import (
type hostsFile struct {
Name string
Path string
- hosts []*host
+ hosts []*dns.ResourceRecord
out *os.File
}
@@ -27,7 +27,7 @@ func convertHostsFile(from *dns.HostsFile) (to *hostsFile) {
to = &hostsFile{
Name: from.Name,
Path: from.Path,
- hosts: make([]*host, 0, len(from.Messages)),
+ hosts: make([]*dns.ResourceRecord, 0, len(from.Messages)),
}
for _, msg := range from.Messages {
@@ -35,18 +35,15 @@ func convertHostsFile(from *dns.HostsFile) (to *hostsFile) {
continue
}
- host := convertRRToHost(&msg.Answer[0])
- if host != nil {
- to.hosts = append(to.hosts, host)
- }
+ to.hosts = append(to.hosts, &msg.Answer[0])
}
return to
}
-func newHostsFile(name string, hosts []*host) (hfile *hostsFile, err error) {
+func newHostsFile(name string, hosts []*dns.ResourceRecord) (hfile *hostsFile, err error) {
if hosts == nil {
- hosts = make([]*host, 0)
+ hosts = make([]*dns.ResourceRecord, 0)
}
hfile = &hostsFile{
@@ -80,7 +77,9 @@ func (hfile *hostsFile) names() (names []string) {
return names
}
-func (hfile *hostsFile) update(hosts []*host) (msgs []*dns.Message, err error) {
+func (hfile *hostsFile) update(hosts []*dns.ResourceRecord) (
+ msgs []*dns.Message, err error,
+) {
if hfile.out == nil {
hfile.out, err = os.OpenFile(hfile.Path,
os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
@@ -97,12 +96,16 @@ func (hfile *hostsFile) update(hosts []*host) (msgs []*dns.Message, err error) {
hfile.hosts = hfile.hosts[:0]
for _, host := range hosts {
- if len(host.Name) == 0 || len(host.Value) == 0 {
+ if len(host.Name) == 0 || host.Value == nil {
+ continue
+ }
+ hostValue, ok := host.Value.(string)
+ if !ok {
continue
}
msg := dns.NewMessageAddress(
[]byte(host.Name),
- [][]byte{[]byte(host.Value)},
+ [][]byte{[]byte(hostValue)},
)
if msg == nil {
continue
diff --git a/httpd.go b/httpd.go
index 2df3ad0..99a164e 100644
--- a/httpd.go
+++ b/httpd.go
@@ -428,17 +428,15 @@ func (srv *Server) apiHostsFileGet(
) (
resbody []byte, err error,
) {
- hosts := make([]*host, 0)
name := httpreq.Form.Get(paramNameName)
for _, hfile := range srv.env.HostsFiles {
if hfile.Name == name {
- hosts = hfile.hosts
- break
+ return json.Marshal(&hfile.hosts)
}
}
- return json.Marshal(&hosts)
+ return []byte("[]"), nil
}
func (srv *Server) apiHostsFileUpdate(
@@ -447,7 +445,7 @@ func (srv *Server) apiHostsFileUpdate(
resbody []byte, err error,
) {
var (
- hosts = make([]*host, 0)
+ hosts = make([]*dns.ResourceRecord, 0)
name = httpreq.Form.Get(paramNameName)
found bool
hfile *hostsFile