-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.go
148 lines (123 loc) · 3.26 KB
/
client.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
/*Package gofofa fofa client in Go
env settings:
- FOFA_CLIENT_URL full fofa connnection string, format: <url>/?email=<email>&key=<key>&version=<v2>
- FOFA_SERVER fofa server
- FOFA_EMAIL fofa account email
- FOFA_KEY fofa account key
*/
package gofofa
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
"net/http"
"net/url"
)
const (
defaultServer = "https://fofa.info"
defaultAPIVersion = "v1"
)
// Client of fofa connection
type Client struct {
Server string // can set local server for debugging, format: <scheme>://<host>
APIVersion string // api version
Email string // fofa email
Key string // fofa key
Account AccountInfo // fofa account info
DeductMode DeductMode // Deduct Mode
httpClient *http.Client //
logger *logrus.Logger
ctx context.Context // use to cancel requests
onResults func(results [][]string) // when fetch results callback
accountDebug bool // 调试账号明文信息
}
// Update merge config from config url
func (c *Client) Update(configURL string) error {
u, err := url.Parse(configURL)
if err != nil {
return err
}
c.Server = u.Scheme + "://" + u.Host
if u.Query().Has("email") {
c.Email = u.Query().Get("email")
}
if u.Query().Has("key") {
c.Key = u.Query().Get("key")
}
if u.Query().Has("version") {
c.APIVersion = u.Query().Get("version")
}
return nil
}
// URL generate fofa connection url string
func (c *Client) URL() string {
return fmt.Sprintf("%s/?email=%s&key=%s&version=%s", c.Server, c.Email, c.Key, c.APIVersion)
}
// GetContext 获取context,用于中止任务
func (c *Client) GetContext() context.Context {
return c.ctx
}
// SetContext 设置context,用于中止任务
func (c *Client) SetContext(ctx context.Context) {
c.ctx = ctx
}
type ClientOption func(c *Client) error
// WithURL configURL format: <url>/?email=<email>&key=<key>&version=<v2>&tlsdisabled=false&debuglevel=0
func WithURL(configURL string) ClientOption {
return func(c *Client) error {
// merge from config
if len(configURL) > 0 {
return c.Update(configURL)
}
return nil
}
}
// WithLogger set logger
func WithLogger(logger *logrus.Logger) ClientOption {
return func(c *Client) error {
c.logger = logger
return nil
}
}
// WithOnResults set on results callback
func WithOnResults(onResults func(results [][]string)) ClientOption {
return func(c *Client) error {
c.onResults = onResults
return nil
}
}
// WithAccountDebug 是否错误里面显示账号密码原始信息
func WithAccountDebug(v bool) ClientOption {
return func(c *Client) error {
c.accountDebug = v
return nil
}
}
// NewClient from fofa connection string to config
// and with env config merge
func NewClient(options ...ClientOption) (*Client, error) {
// read from env
c, err := newClientFromEnv()
if err != nil {
return c, err
}
c.logger = logrus.New()
for _, opt := range options {
err = opt(c)
if err != nil {
return nil, err
}
}
// fetch one time to make sure network is ok
c.httpClient = &http.Client{}
c.Account, err = c.AccountInfo()
if err != nil {
c.logger.Warnf("account invalid")
return c, err
}
if c.Account.Error {
c.logger.Warnf("auth failed")
return c, fmt.Errorf("auth failed: '%s', make sure key is valid", c.Account.ErrMsg)
}
return c, nil
}