Skip to content

Commit 2ba4c4c

Browse files
committed
v0.1.11 add fixUrl/urlPrefix
1 parent acacd3c commit 2ba4c4c

10 files changed

+154
-5
lines changed

HISTORY.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## v0.1.11 add fixUrl/urlPrefix
2+
3+
- add fixUrl/urlPrefix: ```./fofa --size 1 --fields "host" --urlPrefix "redis://" protocol=redis```
4+
- add accountDebug option, it doesn't print account information at console when error by default, can be opened by set ```./fofa -accountDebug account```
5+
16
## v0.1.10 fix page
27

38
- fix page issue

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ if size is larger than your account free limit, you can set `-deductMode` to dec
6262
./fofa search -o a.txt 'port=6379'
6363
```
6464

65+
- fix host to url:
66+
67+
```shell
68+
./fofa --size 2 --fields "host" title=Gitblit
69+
47.92.145.232:8998
70+
https://114.55.35.145:8443
71+
./fofa --size 2 --fields "host" --fixUrl title=Gitblit
72+
http://47.92.145.232:8998
73+
https://114.55.35.145:8443
74+
./fofa --size 2 --fields "host" --fixUrl title=Gitblit
75+
```
76+
use another url prefix:
77+
```shell
78+
./fofa --size 1 --fields "host" --fixUrl --urlPrefix "redis://" protocol=redis
79+
redis://152.136.145.87:6379
80+
```
81+
6582
- verbose mode
6683

6784
```shell

client.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ type Client struct {
3535
logger *logrus.Logger
3636
ctx context.Context // use to cancel requests
3737

38-
onResults func(results [][]string) // when fetch results callback
38+
onResults func(results [][]string) // when fetch results callback
39+
accountDebug bool // 调试账号明文信息
3940
}
4041

4142
// Update merge config from config url
@@ -105,6 +106,14 @@ func WithOnResults(onResults func(results [][]string)) ClientOption {
105106
}
106107
}
107108

109+
// WithAccountDebug 是否错误里面显示账号密码原始信息
110+
func WithAccountDebug(v bool) ClientOption {
111+
return func(c *Client) error {
112+
c.accountDebug = v
113+
return nil
114+
}
115+
}
116+
108117
// NewClient from fofa connection string to config
109118
// and with env config merge
110119
func NewClient(options ...ClientOption) (*Client, error) {

client_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"github.com/stretchr/testify/assert"
77
"net/http"
88
"net/http/httptest"
9+
"net/url"
910
"os"
1011
"strconv"
12+
"strings"
1113
"testing"
1214
)
1315

@@ -228,4 +230,17 @@ func TestNewClient(t *testing.T) {
228230
}})
229231
cli, err = NewClient(WithURL(ts.URL+"/?email="+account.Email+"&key=1&version=v1"), WithLogger(logger))
230232
assert.True(t, len(logs) > 0)
233+
234+
// 账号调试信息
235+
account = validAccounts[2]
236+
invalidUrl := "https://" + strings.Split(ts.URL, "://")[1] + "/?email=" + account.Email + "&key=" + account.Key + "&version=v1"
237+
cli, err = NewClient(WithURL(invalidUrl), WithAccountDebug(true))
238+
var u string
239+
u, err = url.QueryUnescape(err.Error())
240+
assert.Nil(t, err)
241+
assert.Contains(t, u, account.Email)
242+
cli, err = NewClient(WithURL(invalidUrl))
243+
u, err = url.QueryUnescape(err.Error())
244+
assert.Nil(t, err)
245+
assert.NotContains(t, u, account.Email)
231246
}

cmd/fofa/cmd/cmd.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ var GlobalOptions = []cli.Flag{
7070
Name: "verbose",
7171
Usage: "print more information",
7272
},
73+
&cli.BoolFlag{
74+
Name: "accountDebug",
75+
Usage: "print account in error log",
76+
},
7377
}
7478

7579
//// isSubCmd 判断是否指定的子命令
@@ -104,13 +108,17 @@ func BeforAction(context *cli.Context) error {
104108
if context.Bool("verbose") {
105109
logrus.SetLevel(logrus.DebugLevel)
106110
}
111+
var accountDebug bool
112+
if context.Bool("accountDebug") {
113+
accountDebug = true
114+
}
107115

108116
//// icon no need client
109117
//if isSubCmd(os.Args[1:], "icon") {
110118
// return nil
111119
//}
112120

113-
fofaCli, err = gofofa.NewClient(gofofa.WithURL(fofaURL))
121+
fofaCli, err = gofofa.NewClient(gofofa.WithURL(fofaURL), gofofa.WithAccountDebug(accountDebug))
114122
if err != nil {
115123
return err
116124
}

cmd/fofa/cmd/search.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"errors"
55
"fmt"
6+
"github.com/LubyRuffy/gofofa"
67
"github.com/LubyRuffy/gofofa/pkg/outformats"
78
"github.com/sirupsen/logrus"
89
"github.com/urfave/cli/v2"
@@ -17,6 +18,8 @@ var (
1718
format string // out format
1819
outFile string // out file
1920
deductMode string // deduct Mode
21+
fixUrl bool // each host fix as url, like 1.1.1.1,80 will change to http://1.1.1.1
22+
urlPrefix string // each host fix as url, like 1.1.1.1,80 will change to http://1.1.1.1
2023
)
2124

2225
// search subcommand
@@ -57,6 +60,18 @@ var searchCmd = &cli.Command{
5760
Usage: "DeductModeFree or DeductModeFCoin",
5861
Destination: &deductMode,
5962
},
63+
&cli.BoolFlag{
64+
Name: "fixUrl",
65+
Value: false,
66+
Usage: "each host fix as url, like 1.1.1.1,80 will change to http://1.1.1.1",
67+
Destination: &fixUrl,
68+
},
69+
&cli.StringFlag{
70+
Name: "urlPrefix",
71+
Value: "http://",
72+
Usage: "prefix of url, default is http://, can be redis:// and so on ",
73+
Destination: &urlPrefix,
74+
},
6075
},
6176
Action: SearchAction,
6277
}
@@ -128,7 +143,10 @@ func SearchAction(ctx *cli.Context) error {
128143
}
129144

130145
// do search
131-
res, err := fofaCli.HostSearch(query, size, fields)
146+
res, err := fofaCli.HostSearch(query, size, fields, gofofa.SearchOptions{
147+
FixUrl: fixUrl,
148+
UrlPrefix: urlPrefix,
149+
})
132150
if err != nil {
133151
return err
134152
}

host.go

+40-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type HostResults struct {
2020
Results interface{} `json:"results"`
2121
}
2222

23-
// HostStats /host api results
23+
// HostStatsData /host api results
2424
type HostStatsData struct {
2525
Error bool `json:"error"`
2626
Errmsg string `json:"errmsg"`
@@ -37,11 +37,35 @@ type HostStatsData struct {
3737
UpdateTime string `json:"update_time"`
3838
}
3939

40+
// SearchOptions options of search, for post processors
41+
type SearchOptions struct {
42+
FixUrl bool // each host fix as url, like 1.1.1.1,80 will change to http://1.1.1.1, https://1.1.1.1:8443 will no change
43+
UrlPrefix string // default is http://
44+
}
45+
46+
// fixHostToUrl 替换host为url
47+
func fixHostToUrl(res [][]string, fields []string, hostIndex int, urlPrefix string) [][]string {
48+
newRes := make([][]string, 0, len(res))
49+
for _, row := range res {
50+
newRow := make([]string, 0, len(fields))
51+
for j, r := range row {
52+
if j == hostIndex {
53+
if !strings.Contains(r, "://") {
54+
r = urlPrefix + r
55+
}
56+
}
57+
newRow = append(newRow, r)
58+
}
59+
newRes = append(newRes, newRow)
60+
}
61+
return newRes
62+
}
63+
4064
// HostSearch search fofa host data
4165
// query fofa query string
4266
// size data size: -1 means all,0 means just data total info, >0 means actual size
4367
// fields field of fofa host struct
44-
func (c *Client) HostSearch(query string, size int, fields []string) (res [][]string, err error) {
68+
func (c *Client) HostSearch(query string, size int, fields []string, options ...SearchOptions) (res [][]string, err error) {
4569
// check level
4670
if c.freeSize() == 0 {
4771
// 不是会员
@@ -139,6 +163,20 @@ func (c *Client) HostSearch(query string, size int, fields []string) (res [][]st
139163
page++ // 翻页
140164
}
141165

166+
// 后处理
167+
if len(options) > 0 && options[0].FixUrl {
168+
urlPrefix := options[0].UrlPrefix
169+
if urlPrefix == "" {
170+
urlPrefix = "http://"
171+
}
172+
for i, f := range fields {
173+
if f == "host" {
174+
res = fixHostToUrl(res, fields, i, urlPrefix)
175+
break
176+
}
177+
}
178+
}
179+
142180
return
143181
}
144182

host_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ func TestClient_HostSearch(t *testing.T) {
8787
res, err = cli.HostSearch("aaa=bbb", 10, nil)
8888
assert.Contains(t, err.Error(), "[820000] FOFA Query Syntax Incorrect")
8989

90+
// 带有fixurl
91+
res, err = cli.HostSearch("port=80", 10, []string{"host"}, SearchOptions{
92+
FixUrl: true,
93+
UrlPrefix: "",
94+
})
95+
assert.Nil(t, err)
96+
assert.Equal(t, 10, len(res))
97+
assert.Contains(t, res[0][0], "http://")
98+
res, err = cli.HostSearch("port=80", 10, []string{"host"}, SearchOptions{
99+
FixUrl: true,
100+
UrlPrefix: "redis://",
101+
})
102+
assert.Nil(t, err)
103+
assert.Equal(t, 10, len(res))
104+
assert.Contains(t, res[0][0], "redis://")
105+
90106
// 请求失败
91107
cli = &Client{
92108
Server: "http://fofa.info:66666",

request.go

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ func (c *Client) fetchBody(apiURI string, params map[string]string) (body []byte
5252

5353
resp, err = c.httpClient.Do(req)
5454
if err != nil {
55+
if !c.accountDebug {
56+
// 替换账号明文信息
57+
if e, ok := err.(*url.Error); ok {
58+
newClient := c
59+
newClient.Email = "<email>"
60+
newClient.Key = "<key>"
61+
e.URL = newClient.buildURL(apiURI, params)
62+
err = e
63+
}
64+
}
5565
return
5666
}
5767
defer resp.Body.Close()

scripts/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 发布流程
2+
3+
## 修改代码
4+
5+
## 运行单元测试,确保100%的覆盖度
6+
7+
## 修改 HISTORY.md 版本说明文件,确保功能每一项都有记录
8+
9+
## 修改 README.md 更新功能说明,确保每一项都有运行的示例
10+
11+
## 提交代码
12+
13+
## 运行发布脚本 ./tag_release.sh

0 commit comments

Comments
 (0)