Skip to content

Commit 84d6b94

Browse files
committed
biu
0 parents  commit 84d6b94

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

README.MD

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## 构建
2+
```shell
3+
go mod download
4+
go build -o biu-cli cmd/biu-cli.go
5+
```
6+
7+
## 初始化配置
8+
访问个人中心->安全设置 生成新的API密钥
9+
10+
```shell
11+
biu-cli -ak API密钥 -host biu地址(https://x.x.x.x)
12+
13+
```
14+
15+
## 使用
16+
- 查看已有项目
17+
```shell
18+
biu-cli
19+
```
20+
- 添加目标到已有项目中
21+
```shell
22+
cat ip.txt|biu-cli -pid 项目ID
23+
cat domain.txt|biu-cli -pid 项目ID
24+
```

bin/.gitkeep

Whitespace-only changes.

cmd/biu-cli.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package main
2+
3+
import (
4+
"crypto/tls"
5+
"flag"
6+
"fmt"
7+
"github.com/go-resty/resty/v2"
8+
"github.com/joho/godotenv"
9+
"github.com/tidwall/gjson"
10+
"golang.org/x/crypto/ssh/terminal"
11+
"io/ioutil"
12+
"log"
13+
"os"
14+
"strings"
15+
)
16+
17+
var (
18+
biu string
19+
ak string
20+
pid string
21+
pageSize int
22+
client = resty.New()
23+
)
24+
25+
func addTargetToProject(target string) {
26+
if target != "" {
27+
resp, err := client.R().
28+
SetHeader("Content-Type", "application/json").
29+
SetHeader("Biu-Api-Key", ak).
30+
SetBody(`{"asset": "` + target + `" }`).
31+
Patch(fmt.Sprintf("%s/api/project/optimize?project_id=%s", biu, pid))
32+
if err != nil {
33+
fmt.Print(err)
34+
}
35+
if resp.StatusCode() == 200 {
36+
fmt.Println(fmt.Sprintf("添加成功 %s", target))
37+
38+
}
39+
}
40+
}
41+
func listProjects() {
42+
resp, err := client.R().
43+
SetHeader("Biu-Api-Key", ak).
44+
Get(fmt.Sprintf("%s/api/project?limit=%d&from=1&public=false", biu, pageSize))
45+
if err != nil {
46+
fmt.Print(err)
47+
}
48+
if resp.StatusCode() == 200 {
49+
fmt.Println(fmt.Sprintf("编号\t项目ID \t名称"))
50+
value := gjson.Get(string(resp.Body()), "result")
51+
for index, result := range value.Array() {
52+
fmt.Println(fmt.Sprintf("%d\t%s\t%s", index, result.Get("md5"), result.Get("name")))
53+
}
54+
55+
}
56+
}
57+
58+
func initEnv() {
59+
homeDir, _ := os.UserHomeDir()
60+
envPath := fmt.Sprintf("%s/.biu.env", homeDir)
61+
err := godotenv.Load(fmt.Sprintf(envPath))
62+
if err != nil {
63+
if ak != "" && biu != "" {
64+
var DefaultServerOptions = map[string]string{
65+
"BIU_AK": ak,
66+
"BIU_HOST": biu,
67+
}
68+
err := godotenv.Write(DefaultServerOptions, envPath)
69+
if err != nil {
70+
fmt.Println("配置初始化成功")
71+
}
72+
} else {
73+
log.Fatal("请初始化配置: biu-cli -ak xxx -host https://x.x.x.x")
74+
75+
}
76+
} else {
77+
ak = os.Getenv("BIU_AK")
78+
biu = os.Getenv("BIU_HOST")
79+
80+
}
81+
82+
}
83+
func main() {
84+
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
85+
flag.StringVar(&ak, "ak", "", "biu api key")
86+
flag.StringVar(&biu, "host", "", "biu host url: https://x.x.x.x")
87+
flag.StringVar(&pid, "pid", "", "biu project id")
88+
flag.IntVar(&pageSize, "l", 20, "pageSize")
89+
flag.Parse()
90+
initEnv()
91+
if pid == "" {
92+
listProjects()
93+
} else {
94+
data := flag.Args()
95+
if !terminal.IsTerminal(0) {
96+
b, err := ioutil.ReadAll(os.Stdin)
97+
if err == nil {
98+
data = append(data, string(b))
99+
}
100+
}
101+
targets := strings.Split(strings.Join(data, " "), "\n")
102+
for _, target := range targets {
103+
go addTargetToProject(target)
104+
}
105+
}
106+
107+
}

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module biu-cli
2+
3+
go 1.15
4+
5+
require (
6+
github.com/go-resty/resty/v2 v2.6.0
7+
github.com/joho/godotenv v1.3.0
8+
github.com/tidwall/gjson v1.8.0
9+
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
10+
)

releases.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags '-w -s' -gcflags '-N -l' -o bin/biu-cli_linux_amd64 cmd/biu-cli.go
2+
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags '-w -s' -gcflags '-N -l' -o bin/biu-cli_win_amd64.exe cmd/biu-cli.go
3+
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags '-w -s' -gcflags '-N -l' -o bin/biu-cli_darwin_amd64 cmd/biu-cli.go

0 commit comments

Comments
 (0)