-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathrdap.go
More file actions
101 lines (83 loc) · 2.36 KB
/
rdap.go
File metadata and controls
101 lines (83 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* ZAnnotate Copyright 2025 Regents of the University of Michigan
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package zannotate
import (
"context"
"flag"
"net"
"time"
"github.com/openrdap/rdap"
)
type RDAPAnnotatorFactory struct {
BasePluginConf
Timeout int // Timeout for each RDAP query, in seconds
}
type RDAPAnnotator struct {
Factory *RDAPAnnotatorFactory
Id int
rdapClient *rdap.Client
}
// RDAP Annotator Factory (Global)
func (a *RDAPAnnotatorFactory) AddFlags(flags *flag.FlagSet) {
flags.BoolVar(&a.Enabled, "rdap", false, "annotate with RDAP (successor to WHOIS) lookup")
flags.IntVar(&a.Threads, "rdap-threads", 5, "how many rdap processing threads to use")
flags.IntVar(&a.Timeout, "rdap-timeout", 5, "RDAP query timeout in seconds")
}
func (a *RDAPAnnotatorFactory) IsEnabled() bool {
return a.Enabled
}
func (a *RDAPAnnotatorFactory) GetWorkers() int {
return a.Threads
}
func (a *RDAPAnnotatorFactory) Initialize(_ *GlobalConf) error {
return nil
}
func (a *RDAPAnnotatorFactory) MakeAnnotator(i int) Annotator {
var v RDAPAnnotator
v.Factory = a
v.Id = i
v.rdapClient = &rdap.Client{}
return &v
}
func (a *RDAPAnnotatorFactory) Close() error {
return nil
}
// Routing Annotator (Per-Worker)
func (a *RDAPAnnotator) Initialize() error {
return nil
}
func (a *RDAPAnnotator) GetFieldName() string {
return "whois"
}
func (a *RDAPAnnotator) Annotate(ip net.IP) interface{} {
req := rdap.NewIPRequest(ip)
ctx, cancelFunc := context.WithDeadline(context.Background(), time.Now().Add(time.Duration(a.Factory.Timeout)*time.Second))
defer cancelFunc()
req = req.WithContext(ctx)
resp, err := a.rdapClient.Do(req)
if err != nil {
return nil
}
if len(resp.HTTP) == 0 {
return nil
}
return resp.Object
}
func (a *RDAPAnnotator) Close() error {
return nil
}
func init() {
s := new(RDAPAnnotatorFactory)
RegisterAnnotator(s)
}