-
Notifications
You must be signed in to change notification settings - Fork 203
/
uncover.go
201 lines (185 loc) · 5.58 KB
/
uncover.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package uncover
import (
"context"
"sync"
"time"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/uncover/sources"
"github.com/projectdiscovery/uncover/sources/agent/censys"
"github.com/projectdiscovery/uncover/sources/agent/criminalip"
"github.com/projectdiscovery/uncover/sources/agent/fofa"
"github.com/projectdiscovery/uncover/sources/agent/google"
"github.com/projectdiscovery/uncover/sources/agent/hunter"
"github.com/projectdiscovery/uncover/sources/agent/hunterhow"
"github.com/projectdiscovery/uncover/sources/agent/netlas"
"github.com/projectdiscovery/uncover/sources/agent/publicwww"
"github.com/projectdiscovery/uncover/sources/agent/quake"
"github.com/projectdiscovery/uncover/sources/agent/shodan"
"github.com/projectdiscovery/uncover/sources/agent/shodanidb"
"github.com/projectdiscovery/uncover/sources/agent/zoomeye"
errorutil "github.com/projectdiscovery/utils/errors"
stringsutil "github.com/projectdiscovery/utils/strings"
)
var DefaultChannelBuffSize = 32
type Options struct {
Agents []string // Uncover Agents to use
Queries []string // Queries to pass to Agents
Limit int
MaxRetry int
Timeout int
// Note these ratelimits are used as fallback in case agent
// ratelimit is not available in DefaultRateLimits
RateLimit uint // default 30 req
RateLimitUnit time.Duration // default unit
}
// Service handler of all uncover Agents
type Service struct {
Options *Options
Agents []sources.Agent
Session *sources.Session
Provider *sources.Provider
Keys sources.Keys
}
// New creates new uncover service instance
func New(opts *Options) (*Service, error) {
s := &Service{Agents: []sources.Agent{}, Options: opts}
for _, v := range opts.Agents {
switch v {
case "shodan":
s.Agents = append(s.Agents, &shodan.Agent{})
case "censys":
s.Agents = append(s.Agents, &censys.Agent{})
case "fofa":
s.Agents = append(s.Agents, &fofa.Agent{})
case "shodan-idb":
s.Agents = append(s.Agents, &shodanidb.Agent{})
case "quake":
s.Agents = append(s.Agents, &quake.Agent{})
case "hunter":
s.Agents = append(s.Agents, &hunter.Agent{})
case "zoomeye":
s.Agents = append(s.Agents, &zoomeye.Agent{})
case "netlas":
s.Agents = append(s.Agents, &netlas.Agent{})
case "criminalip":
s.Agents = append(s.Agents, &criminalip.Agent{})
case "publicwww":
s.Agents = append(s.Agents, &publicwww.Agent{})
case "hunterhow":
s.Agents = append(s.Agents, &hunterhow.Agent{})
case "google":
s.Agents = append(s.Agents, &google.Agent{})
}
}
s.Provider = sources.NewProvider()
s.Keys = s.Provider.GetKeys()
if opts.RateLimit == 0 {
opts.RateLimit = 30
}
if opts.RateLimitUnit == 0 {
opts.RateLimitUnit = time.Minute
}
var err error
s.Session, err = sources.NewSession(&s.Keys, opts.MaxRetry, opts.Timeout, 10, opts.Agents, opts.RateLimitUnit)
if err != nil {
return nil, err
}
return s, nil
}
func (s *Service) Execute(ctx context.Context) (<-chan sources.Result, error) {
// unlikely but as a precaution to handle random panics check all types
if err := s.nilCheck(); err != nil {
return nil, err
}
switch {
case len(s.Agents) == 0:
return nil, errorutil.NewWithTag("uncover", "no agent/source specified")
case !s.hasAnyAnonymousProvider() && !s.Provider.HasKeys():
return nil, errorutil.NewWithTag("uncover", "agents %v requires keys but no keys were found", s.Options.Agents)
}
megaChan := make(chan sources.Result, DefaultChannelBuffSize)
// iterate and run all sources
wg := &sync.WaitGroup{}
for _, q := range s.Options.Queries {
agentLabel:
for _, agent := range s.Agents {
keys := s.Provider.GetKeys()
if keys.Empty() && agent.Name() != "shodan-idb" {
gologger.Error().Msgf(agent.Name(), "agent given but keys not found")
continue agentLabel
}
ch, err := agent.Query(s.Session, &sources.Query{
Query: q,
Limit: s.Options.Limit,
})
if err != nil {
gologger.Error().Msgf("%s\n", err)
continue agentLabel
}
wg.Add(1)
go func(source, relay chan sources.Result, ctx context.Context) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case res, ok := <-source:
res.Timestamp = time.Now().Unix()
if !ok {
return
}
relay <- res
}
}
}(ch, megaChan, ctx)
}
}
// close channel when all sources return
go func(wg *sync.WaitGroup, megaChan chan sources.Result) {
wg.Wait()
defer close(megaChan)
}(wg, megaChan)
return megaChan, nil
}
// ExecuteWithWriters writes output to writer along with stdout
func (s *Service) ExecuteWithCallback(ctx context.Context, callback func(result sources.Result)) error {
ch, err := s.Execute(ctx)
if err != nil {
return err
}
if callback == nil {
return errorutil.NewWithTag("uncover", "result callback cannot be nil")
}
for {
select {
case <-ctx.Done():
return nil
case result, ok := <-ch:
if !ok {
return nil
}
callback(result)
}
}
}
// AllAgents returns all supported uncover Agents
func (s *Service) AllAgents() []string {
return []string{
"shodan", "censys", "fofa", "shodan-idb", "quake", "hunter", "zoomeye", "netlas", "criminalip", "publicwww", "hunterhow", "google",
}
}
func (s *Service) nilCheck() error {
if s.Provider == nil {
return errorutil.NewWithTag("uncover", "provider cannot be nil")
}
if s.Options == nil {
return errorutil.NewWithTag("uncover", "options cannot be nil")
}
if s.Session == nil {
return errorutil.NewWithTag("uncover", "session cannot be nil")
}
return nil
}
func (s *Service) hasAnyAnonymousProvider() bool {
return stringsutil.EqualFoldAny("shodan-idb", s.Options.Agents...)
}