implement type filtering
This commit is contained in:
parent
a46be915f1
commit
a79af2ba31
@ -61,6 +61,7 @@ it run as user nobody, who cannot bind to port 43 itself.
|
|||||||
- version: `$ whois -h <server> -q version`
|
- version: `$ whois -h <server> -q version`
|
||||||
- sources: `$ whois -h <server> -q sources`
|
- sources: `$ whois -h <server> -q sources`
|
||||||
- types: `$ whois -h <server> -q types`
|
- types: `$ whois -h <server> -q types`
|
||||||
|
- type filtering: `$ whois -h 172.23.75.6 -T aut-num,person Mic92-DN42 AS4242420092`
|
||||||
|
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
@ -36,17 +36,32 @@ func shellSplit(s string) []string {
|
|||||||
|
|
||||||
type Flags struct {
|
type Flags struct {
|
||||||
ServerInfo string
|
ServerInfo string
|
||||||
|
TypeSchema string
|
||||||
|
Types map[string]bool
|
||||||
Args []string
|
Args []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFlags(request string) (*Flags, *flag.FlagSet, error) {
|
func parseFlags(request string) (*Flags, *flag.FlagSet, error) {
|
||||||
args := shellSplit(request)
|
args := shellSplit(request)
|
||||||
set := flag.NewFlagSet("whois42d", flag.ContinueOnError)
|
set := flag.NewFlagSet("whois42d", flag.ContinueOnError)
|
||||||
|
var typeField string
|
||||||
f := Flags{}
|
f := Flags{}
|
||||||
set.StringVar(&f.ServerInfo, "q", "", "[version|sources|types] query specified server info")
|
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 {
|
if err := set.Parse(args); err != nil {
|
||||||
return nil, set, err
|
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()
|
f.Args = set.Args()
|
||||||
return &f, set, nil
|
return &f, set, nil
|
||||||
}
|
}
|
||||||
|
@ -56,9 +56,13 @@ var whoisTypes = []Type{
|
|||||||
{"as-block", regexp.MustCompile(`\d+_\d+`), UPPER},
|
{"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
|
found := false
|
||||||
for _, t := range whoisTypes {
|
for _, t := range whoisTypes {
|
||||||
|
if len(flags.Types) > 0 && !flags.Types[t.Name] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
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))
|
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
|
flags := query.Flags
|
||||||
if flags.ServerInfo != "" {
|
if flags.ServerInfo != "" {
|
||||||
r.printServerInfo(conn, strings.TrimSpace(flags.ServerInfo))
|
printServerInfo(conn, strings.TrimSpace(flags.ServerInfo))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
found := false
|
found := false
|
||||||
for _, obj := range query.Objects {
|
for _, obj := range query.Objects {
|
||||||
if r.handleObject(conn, obj) {
|
if r.handleObject(conn, obj, flags) {
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -176,7 +180,7 @@ func parseQuery(conn *net.TCPConn) *Query {
|
|||||||
return &query
|
return &query
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) printServerInfo(conn *net.TCPConn, what string) {
|
func printServerInfo(conn *net.TCPConn, what string) {
|
||||||
switch what {
|
switch what {
|
||||||
case "version":
|
case "version":
|
||||||
fmt.Fprintf(conn, "%% whois42d v%d\n", VERSION)
|
fmt.Fprintf(conn, "%% whois42d v%d\n", VERSION)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user