@@ -17,16 +17,19 @@ import (
17
17
)
18
18
19
19
var (
20
- isSearch bool
21
- biu string
22
- ak string
23
- pnew string
24
- pid string
25
- icp string
26
- ip string
27
- pageSize int
28
- client = resty .New ()
29
- version = "v0.5"
20
+ isSearch bool
21
+ tycCookie string
22
+ portsRanges = "21-22,80,443,1433,2181,2409,3306,3389,5601,6379,8009,8080,8443,8888,9200,27017"
23
+ biuHost string
24
+ ak string
25
+ pnew string
26
+ pid string
27
+ icp string
28
+ ip string
29
+ verbose bool
30
+ pageSize int
31
+ client = resty .New ()
32
+ version = "v0.6"
30
33
)
31
34
32
35
func biuPrint (header []string , data [][]string ) {
@@ -54,13 +57,13 @@ func biuClient() *resty.Request {
54
57
55
58
}
56
59
57
- func icpSearch () {
58
- resp , err := client .R ().Get (fmt .Sprintf ("https://beian.tianyancha.com/search/%s" , icp ))
60
+ func icpSearch (page int ) {
61
+ resp , err := client .R ().SetHeader ( "Cookie" , tycCookie ). Get (fmt .Sprintf ("https://beian.tianyancha.com/search/%s/p%d " , icp , page ))
59
62
if err != nil {
60
63
fmt .Print (err )
61
64
}
62
65
if resp .StatusCode () == 200 {
63
- reg , err := regexp .Compile (`<span class="ranking-ym" rel="nofollow">([a-z0-9-\.]+)` )
66
+ reg , err := regexp .Compile (`<span class="ranking-ym" rel="nofollow">([a-z0-9-\.]+\.[a-z]+ )` )
64
67
if err == nil {
65
68
match := reg .FindAllString (resp .String (), - 1 )
66
69
if len (match ) > 0 {
@@ -75,6 +78,10 @@ func icpSearch() {
75
78
}
76
79
}
77
80
}
81
+ if strings .Contains (resp .String (), fmt .Sprintf ("https://beian.tianyancha.com/search/%s/p%d" , icp , page + 1 )) {
82
+ page = page + 1
83
+ icpSearch (page )
84
+ }
78
85
79
86
}
80
87
}
@@ -85,7 +92,7 @@ func addTargetToProject(target string) {
85
92
resp , err := biuClient ().
86
93
SetHeader ("Content-Type" , "application/json" ).
87
94
SetBody (`{"asset": "` + target + `" }` ).
88
- Patch (fmt .Sprintf ("%s/api/project/optimize?project_id=%s" , biu , pid ))
95
+ Patch (fmt .Sprintf ("%s/api/project/optimize?project_id=%s" , biuHost , pid ))
89
96
if err != nil {
90
97
fmt .Print (err )
91
98
}
@@ -97,11 +104,14 @@ func addTargetToProject(target string) {
97
104
}
98
105
func listProjects () {
99
106
resp , err := biuClient ().
100
- Get (fmt .Sprintf ("%s/api/project?limit=%d&from=1&public=false" , biu , pageSize ))
107
+ Get (fmt .Sprintf ("%s/api/project?limit=%d&from=1&public=false" , biuHost , pageSize ))
101
108
if err != nil {
102
109
fmt .Print (err )
103
110
}
104
111
if resp .StatusCode () == 200 {
112
+ if verbose {
113
+ fmt .Println (fmt .Sprintf ("%s/project" , biuHost ))
114
+ }
105
115
fmt .Println (fmt .Sprintf ("编号\t 项目ID \t 名称" ))
106
116
value := gjson .Get (string (resp .Body ()), "result" )
107
117
for index , result := range value .Array () {
@@ -111,26 +121,31 @@ func listProjects() {
111
121
}
112
122
}
113
123
func addProject () {
114
- resp , err := biuClient ().
115
- SetHeader ("Content-Type" , "application/json" ).
116
- SetBody (`{"asset":"","name":"` + pnew + `","ports":"21-22,80,443,1433,2181,2409,3306,3389,5601,6379,8009,8080,8443,8888,9200,27017","public":false,"scan":true,"organizations":[],"include_subdomain":true,"include_ip":true,"include_history":true,"period":0,"tags":[],"cover":null}` ).
117
- Post (fmt .Sprintf ("%s/api/project" , biu ))
118
- if err != nil {
119
- fmt .Print (err )
120
- }
121
- if resp .StatusCode () == 200 {
122
- result := gjson .Get (string (resp .Body ()), "result" )
123
- msg := gjson .Get (string (resp .Body ()), "msg" ).Value ()
124
- fmt .Println (msg )
125
- fmt .Println (result .Get ("project_id" ).Value ())
126
- pid = result .Get ("project_id" ).Str
124
+ if pid == "" {
125
+ resp , err := biuClient ().
126
+ SetHeader ("Content-Type" , "application/json" ).
127
+ SetBody (`{"asset":"","name":"` + pnew + `","ports":"` + portsRanges + `","public":false,"scan":true,"organizations":[],"include_subdomain":true,"include_ip":true,"include_history":true,"period":0,"tags":[],"cover":null}` ).
128
+ Post (fmt .Sprintf ("%s/api/project" , biuHost ))
129
+ if err != nil {
130
+ fmt .Print (err )
131
+ }
132
+ if resp .StatusCode () == 200 {
133
+ result := gjson .Get (string (resp .Body ()), "result" )
134
+ msg := gjson .Get (string (resp .Body ()), "msg" ).Value ()
135
+ fmt .Println (msg )
136
+ fmt .Println (result .Get ("project_id" ).Value ())
137
+ pid = result .Get ("project_id" ).Str
138
+ if verbose {
139
+ fmt .Println (fmt .Sprintf ("%s/assets/port?project_id=%s" , biuHost , pid ))
140
+ }
141
+ }
127
142
}
128
143
}
129
144
130
145
func searchIP (ipaddr string ) {
131
146
fmt .Println (ipaddr )
132
147
resp , err := biuClient ().
133
- Get (fmt .Sprintf ("%s/api/asset/search?target=%s" , biu , ipaddr ))
148
+ Get (fmt .Sprintf ("%s/api/asset/search?target=%s" , biuHost , ipaddr ))
134
149
if err != nil {
135
150
fmt .Print (err )
136
151
}
@@ -202,35 +217,40 @@ func initEnv() {
202
217
envPath := fmt .Sprintf ("%s/.biu.env" , homeDir )
203
218
err := godotenv .Load (fmt .Sprintf (envPath ))
204
219
if err != nil {
205
- if ak != "" && biu != "" {
220
+ if ak != "" && biuHost != "" {
206
221
var DefaultServerOptions = map [string ]string {
207
222
"BIU_AK" : ak ,
208
- "BIU_HOST" : biu ,
223
+ "BIU_HOST" : biuHost ,
224
+ "BIU_PORTS" : portsRanges ,
225
+ "TYC_COOKIE" : tycCookie ,
209
226
}
210
227
err := godotenv .Write (DefaultServerOptions , envPath )
211
228
if err != nil {
212
229
fmt .Println ("配置初始化成功" )
213
230
}
214
231
} else {
215
- log .Fatal ("请初始化配置: biu-cli -ak xxx -host https://x.x.x.x" )
232
+ log .Fatal (fmt . Sprintf ( "请初始化配置: biu-cli -ak xxx -host https://x.x.x.x \n 文件路径: %s" , envPath ) )
216
233
217
234
}
218
235
} else {
219
236
ak = os .Getenv ("BIU_AK" )
220
- biu = os .Getenv ("BIU_HOST" )
237
+ biuHost = os .Getenv ("BIU_HOST" )
238
+ portsRanges = os .Getenv ("BIU_PORTS" )
239
+ tycCookie = os .Getenv ("TYC_COOKIE" )
221
240
222
241
}
223
242
224
243
}
225
244
func main () {
226
245
client .SetTLSClientConfig (& tls.Config {InsecureSkipVerify : true })
227
246
flag .StringVar (& ak , "ak" , "" , "biu api key" )
228
- flag .StringVar (& biu , "host" , "" , "biu host url: https://x.x.x.x" )
247
+ flag .StringVar (& biuHost , "host" , "" , "biu host url: https://x.x.x.x" )
229
248
flag .StringVar (& pnew , "pnew" , "" , "biu new project name" )
230
249
flag .StringVar (& pid , "pid" , "" , "biu project id" )
231
250
flag .StringVar (& icp , "icp" , "" , "备案名称查询域名" )
232
251
flag .StringVar (& ip , "ip" , "" , "biu search ip" )
233
252
flag .BoolVar (& isSearch , "s" , false , "biu 搜索模式" )
253
+ flag .BoolVar (& verbose , "v" , false , "输出更多信息" )
234
254
flag .IntVar (& pageSize , "l" , 20 , "pageSize" )
235
255
flag .Parse ()
236
256
initEnv ()
@@ -249,7 +269,7 @@ func main() {
249
269
}
250
270
251
271
} else if icp != "" {
252
- icpSearch ()
272
+ icpSearch (1 )
253
273
} else {
254
274
if pid == "" {
255
275
if pnew == "" {
0 commit comments