Generalize object retrieval
Generalize object retrieval in to objects and string pairs to enable multiple retrieval patterns ( like HTTP )
This commit is contained in:
parent
40b9c4eb99
commit
5226cd0c61
@ -10,26 +10,54 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type pathpair struct {
|
||||||
|
objtype string
|
||||||
|
obj string
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Registry) handleObject(conn *net.TCPConn, object Object, flags *Flags) bool {
|
func (r *Registry) handleObject(conn *net.TCPConn, object Object, flags *Flags) bool {
|
||||||
found := false
|
found := false
|
||||||
for _, t := range r.whoisTypes {
|
paths := r.findObjectPaths(object)
|
||||||
if len(flags.Types) > 0 && !flags.Types[t.Name] {
|
for _, p := range paths {
|
||||||
continue
|
r.printObject(conn, p.objtype, p.obj)
|
||||||
}
|
found = true
|
||||||
|
}
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Registry) findObjectPaths(object Object) []pathpair {
|
||||||
|
var paths []pathpair
|
||||||
|
for _, t := range r.whoisTypes {
|
||||||
if t.Kind == ROUTE || t.Kind == ROUTE6 {
|
if t.Kind == ROUTE || t.Kind == ROUTE6 {
|
||||||
if object[t.Kind] != nil {
|
if object[t.Kind] != nil {
|
||||||
found = found || r.printNet(conn, t.Name, object[t.Kind].(net.IP))
|
p := r.getObjFromIP(t.Name, object[t.Kind].(net.IP))
|
||||||
|
paths = append(paths, p...)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
arg := object[t.Kind].(string)
|
arg := object[t.Kind].(string)
|
||||||
if t.Pattern.MatchString(arg) {
|
if t.Pattern.MatchString(arg) {
|
||||||
r.printObject(conn, t.Name, arg)
|
paths = append(paths, pathpair{t.Name, arg})
|
||||||
found = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return found
|
return paths
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Registry) getObjFromIP(objType string, ip net.IP) []pathpair {
|
||||||
|
var paths []pathpair
|
||||||
|
routePath := path.Join(r.DataPath, objType)
|
||||||
|
cidrs, err := readCidrs(routePath)
|
||||||
|
if err != nil {
|
||||||
|
return paths
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cidrs {
|
||||||
|
if c.Contains(ip) {
|
||||||
|
obj := strings.Replace(c.String(), "/", "_", -1)
|
||||||
|
paths = append(paths, pathpair{objType, obj})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return paths
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) HandleQuery(conn *net.TCPConn) {
|
func (r *Registry) HandleQuery(conn *net.TCPConn) {
|
||||||
@ -119,18 +147,12 @@ func (r *Registry) printNet(conn *net.TCPConn, name string, ip net.IP) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) printObject(conn *net.TCPConn, objType string, obj string) {
|
func (r *Registry) printObject(conn *net.TCPConn, objType string, obj string) {
|
||||||
file := path.Join(r.DataPath, objType, obj)
|
content, path, err := r.retrieveObject(objType, obj)
|
||||||
|
|
||||||
f, err := os.Open(file)
|
|
||||||
defer f.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Fprintf(conn, "%% Information related to '%s':\n", file[len(r.DataPath)+1:])
|
fmt.Fprintf(conn, "%% Information related to '%s':\n", path)
|
||||||
conn.ReadFrom(f)
|
conn.Write(content)
|
||||||
fmt.Fprint(conn, "\n")
|
fmt.Fprint(conn, "\n")
|
||||||
}
|
}
|
||||||
|
@ -62,6 +62,20 @@ func New(DataPath string, Header string, DNSTopLevel string, RegistryTopLevel st
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Registry) retrieveObject(objType string, obj string) ([]byte, string, error) {
|
||||||
|
file := path.Join(r.DataPath, objType, obj)
|
||||||
|
f, err := os.Open(file)
|
||||||
|
defer f.Close()
|
||||||
|
if err != nil {
|
||||||
|
return []byte(""), string(""), err
|
||||||
|
}
|
||||||
|
fall, fallerr := ioutil.ReadAll(f)
|
||||||
|
if fallerr != nil {
|
||||||
|
return []byte(""), string(""), err
|
||||||
|
}
|
||||||
|
return fall, file[len(r.DataPath)+1:], nil
|
||||||
|
}
|
||||||
|
|
||||||
func readCidrs(path string) ([]net.IPNet, error) {
|
func readCidrs(path string) ([]net.IPNet, error) {
|
||||||
files, err := ioutil.ReadDir(path)
|
files, err := ioutil.ReadDir(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user