Move generic functions from query to registry

These functions no longer have TCP based dependencies on them, they belong to the registry itself instead
This commit is contained in:
Zach 2018-09-02 18:41:53 -04:00
parent b2f5b7c32d
commit 3b64f480e2
2 changed files with 35 additions and 35 deletions

View File

@ -25,41 +25,6 @@ func (r *Registry) handleObject(conn *net.TCPConn, object Object, flags *Flags)
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 object[t.Kind] != nil {
p := r.getObjFromIP(t.Name, object[t.Kind].(net.IP))
paths = append(paths, p...)
}
} else {
arg := object[t.Kind].(string)
if t.Pattern.MatchString(arg) {
paths = append(paths, pathpair{t.Name, arg})
}
}
}
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) {
fmt.Fprint(conn, "% " + r.Header + "\n\n")

View File

@ -76,6 +76,41 @@ func (r *Registry) retrieveObject(objType string, obj string) ([]byte, string, e
return fall, file[len(r.DataPath)+1:], nil
}
func (r *Registry) findObjectPaths(object Object) []pathpair {
var paths []pathpair
for _, t := range r.whoisTypes {
if t.Kind == ROUTE || t.Kind == ROUTE6 {
if object[t.Kind] != nil {
p := r.getObjFromIP(t.Name, object[t.Kind].(net.IP))
paths = append(paths, p...)
}
} else {
arg := object[t.Kind].(string)
if t.Pattern.MatchString(arg) {
paths = append(paths, pathpair{t.Name, arg})
}
}
}
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 readCidrs(path string) ([]net.IPNet, error) {
files, err := ioutil.ReadDir(path)
if err != nil {