Skip to content

Commit 822ae3f

Browse files
committed
v0.6 支持更多配置
1 parent c2cd50b commit 822ae3f

File tree

2 files changed

+63
-35
lines changed

2 files changed

+63
-35
lines changed

README.MD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ go build -o biu-cli cmd/biu-cli.go
1010
```shell
1111
biu-cli -ak API密钥 -host biu地址(https://x.x.x.x)
1212
```
13+
## 进阶配置
14+
15+
```shell
16+
BIU_AK="API密钥"
17+
BIU_HOST="biu地址"
18+
BIU_PORTS="新建项目使用的端口范围"
19+
TYC_COOKIE="查询icp时用到天眼查的Cookie"
20+
```
1321

1422
## 使用
1523
- 查询IP

cmd/biu-cli.go

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ import (
1717
)
1818

1919
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"
3033
)
3134

3235
func biuPrint(header []string, data [][]string) {
@@ -54,13 +57,13 @@ func biuClient() *resty.Request {
5457

5558
}
5659

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))
5962
if err != nil {
6063
fmt.Print(err)
6164
}
6265
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]+)`)
6467
if err == nil {
6568
match := reg.FindAllString(resp.String(), -1)
6669
if len(match) > 0 {
@@ -75,6 +78,10 @@ func icpSearch() {
7578
}
7679
}
7780
}
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+
}
7885

7986
}
8087
}
@@ -85,7 +92,7 @@ func addTargetToProject(target string) {
8592
resp, err := biuClient().
8693
SetHeader("Content-Type", "application/json").
8794
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))
8996
if err != nil {
9097
fmt.Print(err)
9198
}
@@ -97,11 +104,14 @@ func addTargetToProject(target string) {
97104
}
98105
func listProjects() {
99106
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))
101108
if err != nil {
102109
fmt.Print(err)
103110
}
104111
if resp.StatusCode() == 200 {
112+
if verbose {
113+
fmt.Println(fmt.Sprintf("%s/project", biuHost))
114+
}
105115
fmt.Println(fmt.Sprintf("编号\t项目ID \t名称"))
106116
value := gjson.Get(string(resp.Body()), "result")
107117
for index, result := range value.Array() {
@@ -111,26 +121,31 @@ func listProjects() {
111121
}
112122
}
113123
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+
}
127142
}
128143
}
129144

130145
func searchIP(ipaddr string) {
131146
fmt.Println(ipaddr)
132147
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))
134149
if err != nil {
135150
fmt.Print(err)
136151
}
@@ -202,35 +217,40 @@ func initEnv() {
202217
envPath := fmt.Sprintf("%s/.biu.env", homeDir)
203218
err := godotenv.Load(fmt.Sprintf(envPath))
204219
if err != nil {
205-
if ak != "" && biu != "" {
220+
if ak != "" && biuHost != "" {
206221
var DefaultServerOptions = map[string]string{
207222
"BIU_AK": ak,
208-
"BIU_HOST": biu,
223+
"BIU_HOST": biuHost,
224+
"BIU_PORTS": portsRanges,
225+
"TYC_COOKIE": tycCookie,
209226
}
210227
err := godotenv.Write(DefaultServerOptions, envPath)
211228
if err != nil {
212229
fmt.Println("配置初始化成功")
213230
}
214231
} 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))
216233

217234
}
218235
} else {
219236
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")
221240

222241
}
223242

224243
}
225244
func main() {
226245
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
227246
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")
229248
flag.StringVar(&pnew, "pnew", "", "biu new project name")
230249
flag.StringVar(&pid, "pid", "", "biu project id")
231250
flag.StringVar(&icp, "icp", "", "备案名称查询域名")
232251
flag.StringVar(&ip, "ip", "", "biu search ip")
233252
flag.BoolVar(&isSearch, "s", false, "biu 搜索模式")
253+
flag.BoolVar(&verbose, "v", false, "输出更多信息")
234254
flag.IntVar(&pageSize, "l", 20, "pageSize")
235255
flag.Parse()
236256
initEnv()
@@ -249,7 +269,7 @@ func main() {
249269
}
250270

251271
} else if icp != "" {
252-
icpSearch()
272+
icpSearch(1)
253273
} else {
254274
if pid == "" {
255275
if pnew == "" {

0 commit comments

Comments
 (0)