implement type filtering

This commit is contained in:
Jörg Thalheim 2016-01-23 21:52:20 +00:00 committed by root
parent a46be915f1
commit a79af2ba31
3 changed files with 24 additions and 4 deletions

View File

@ -61,6 +61,7 @@ it run as user nobody, who cannot bind to port 43 itself.
- version: `$ whois -h <server> -q version`
- sources: `$ whois -h <server> -q sources`
- types: `$ whois -h <server> -q types`
- type filtering: `$ whois -h 172.23.75.6 -T aut-num,person Mic92-DN42 AS4242420092`
## TODO

View File

@ -36,17 +36,32 @@ func shellSplit(s string) []string {
type Flags struct {
ServerInfo string
TypeSchema string
Types map[string]bool
Args []string
}
func parseFlags(request string) (*Flags, *flag.FlagSet, error) {
args := shellSplit(request)
set := flag.NewFlagSet("whois42d", flag.ContinueOnError)
var typeField string
f := Flags{}
set.StringVar(&f.ServerInfo, "q", "", "[version|sources|types] query specified server info")
set.StringVar(&f.TypeSchema, "t", "", "request template for object of TYPE")
set.StringVar(&typeField, "T", "", "TYPE[,TYPE]... only look for objects of TYPE")
if err := set.Parse(args); err != nil {
return nil, set, err
}
if typeField != "" {
types := strings.Split(typeField, ",")
f.Types = make(map[string]bool, len(types))
for _, t := range types {
f.Types[t] = true
}
}
f.Args = set.Args()
return &f, set, nil
}

View File

@ -56,9 +56,13 @@ var whoisTypes = []Type{
{"as-block", regexp.MustCompile(`\d+_\d+`), UPPER},
}
func (r *Registry) handleObject(conn *net.TCPConn, object Object) bool {
func (r *Registry) handleObject(conn *net.TCPConn, object Object, flags *Flags) bool {
found := false
for _, t := range whoisTypes {
if len(flags.Types) > 0 && !flags.Types[t.Name] {
continue
}
if t.Kind == ROUTE || t.Kind == ROUTE6 {
if object[t.Kind] != nil {
found = found || r.printNet(conn, t.Name, object[t.Kind].(net.IP))
@ -84,12 +88,12 @@ func (r *Registry) HandleQuery(conn *net.TCPConn) {
flags := query.Flags
if flags.ServerInfo != "" {
r.printServerInfo(conn, strings.TrimSpace(flags.ServerInfo))
printServerInfo(conn, strings.TrimSpace(flags.ServerInfo))
return
}
found := false
for _, obj := range query.Objects {
if r.handleObject(conn, obj) {
if r.handleObject(conn, obj, flags) {
found = true
}
}
@ -176,7 +180,7 @@ func parseQuery(conn *net.TCPConn) *Query {
return &query
}
func (r *Registry) printServerInfo(conn *net.TCPConn, what string) {
func printServerInfo(conn *net.TCPConn, what string) {
switch what {
case "version":
fmt.Fprintf(conn, "%% whois42d v%d\n", VERSION)