Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
优化代码,更换弃用的函数
Browse files Browse the repository at this point in the history
  • Loading branch information
rootphantomer committed Sep 28, 2022
1 parent 092717a commit 9415512
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 72 deletions.
77 changes: 41 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,52 +29,57 @@ rust 项目代码 移步这里——<https://github.com/360quake/quake_rs/>

## 更新日志

- 2022-09-28 v2.0.1:

- 优化代码,更换即将弃用的函数

- 2022-09-19 v2.0:

- 支持其他项目可以引入 quake 模块
- import "github.com/360quake/quake_go"
- 也可以使用这个项目的模块(<https://github.com/jinycoo/go-quake>
- 支持其他项目可以引入 quake 模块
- import "github.com/360quake/quake_go"
- 也可以使用这个项目的模块(<https://github.com/jinycoo/go-quake>

- 2022-09-14 v1.7:

- 使用 golang 的泛型,减少了代码的冗余和重复性
- 要求 golang version >= 1.18
- 使用 golang 的泛型,减少了代码的冗余和重复性
- 要求 golang version >= 1.18

- 2022-09-14 v1.6:

- 新增主机数据接口,增加主机数据实时查询功能(example:./quake host 'service:http' -st 0 -sz 20)
- 优化命令行提示和代码逻辑
- 新增主机数据接口,增加主机数据实时查询功能(example:./quake host 'service:http' -st 0 -sz 20)
- 优化命令行提示和代码逻辑

- 2022-09-10 v1.5:

- 替换解析的 response 的 json 由 struct 变为 map,方便可以自定义需要返回的结果
- 支持-fe 多个可选字段,单独输出结果集
- 替换解析的 response 的 json 由 struct 变为 map,方便可以自定义需要返回的结果
- 支持-fe 多个可选字段,单独输出结果集

- 2022-09-07 v1.4:

- 替换 hflag 来解析命令行参数,使参数拓展开发更简单
- 更新 query 接口为 search
- 支持 -ft 参数,可以使用载入一个逐行有搜索语法的 txt 文本,然后统一结果输出 ip:port
- 替换 hflag 来解析命令行参数,使参数拓展开发更简单
- 更新 query 接口为 search
- 支持 -ft 参数,可以使用载入一个逐行有搜索语法的 txt 文本,然后统一结果输出 ip:port

- 2022-09-07 v1.3:

- 支持-ic,-s,-e(ignore_cache,start_time,end_time)参数传入,start_time 初始化是当年年初 01-01,end_time 默认初始化是 now()
- 优化发包的结构体解析
- 支持-ic,-s,-e(ignore_cache,start_time,end_time)参数传入,start_time 初始化是当年年初 01-01,end_time 默认初始化是
now()
- 优化发包的结构体解析

- 2022-09-06 v1.2:

- 默认输出格式为 ip:port(见下图)
- 新增-t=body 可以输出 body 信息
- 默认输出格式为 ip:port(见下图)
- 新增-t=body 可以输出 body 信息

- 2022-09-06 v1.1:

- 简化参数传参格式
- 将 token 固化在当前目录
- 简化参数传参格式
- 将 token 固化在当前目录

- 2022-09-01 v1.0:

- 新增 info 接口功能
- 新增 service 接口功能
- 新增 info 接口功能
- 新增 service 接口功能

## 使用

Expand Down Expand Up @@ -120,27 +125,27 @@ go get -u github.com/360quake/quake_go
package main

import (
"fmt"
"fmt"

"github.com/360quake/quake_go/utils"
"github.com/360quake/quake_go/utils"
)

func main() {

var reqjson utils.Reqjson
reqjson.Query = hflag.GetString("args")
reqjson.Start = hflag.GetString("start")
reqjson.Size = hflag.GetString("size")
reqjson.Start_time = hflag.GetTime("start_time")
reqjson.End_time = hflag.GetTime("end_time")
reqjson.Ignore_cache = hflag.GetBool("ignore_cache")
reqjson.Field = hflag.GetString("field")
reqjson.Query_txt = hflag.GetString("file_txt")

body := utils.InfoGet("<token>")
body = utils.SearchServicePost(reqjson, "<token>")
body = utils.HostSearchPost(reqjson, "<token>")
fmt.Println(body)
var reqjson utils.Reqjson
reqjson.Query = hflag.GetString("args")
reqjson.Start = hflag.GetString("start")
reqjson.Size = hflag.GetString("size")
reqjson.Start_time = hflag.GetTime("start_time")
reqjson.End_time = hflag.GetTime("end_time")
reqjson.Ignore_cache = hflag.GetBool("ignore_cache")
reqjson.Field = hflag.GetString("field")
reqjson.Query_txt = hflag.GetString("file_txt")

body := utils.InfoGet("<token>")
body = utils.SearchServicePost(reqjson, "<token>")
body = utils.HostSearchPost(reqjson, "<token>")
fmt.Println(body)
}

返回包都是string解析需要自己解析
Expand Down
32 changes: 16 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ func hflagInit() {
reqjson.Query = hflag.GetString("args")
reqjson.Start = hflag.GetString("start")
reqjson.Size = hflag.GetString("size")
reqjson.Start_time = hflag.GetTime("start_time")
reqjson.End_time = hflag.GetTime("end_time")
reqjson.Ignore_cache = hflag.GetBool("ignore_cache")
reqjson.StartTime = hflag.GetTime("start_time")
reqjson.EndTime = hflag.GetTime("end_time")
reqjson.IgnoreCache = hflag.GetBool("ignore_cache")
reqjson.Field = hflag.GetString("field")
reqjson.Query_txt = hflag.GetString("file_txt")
reqjson.QueryTxt = hflag.GetString("file_txt")
if sizelen, _ := strconv.Atoi(reqjson.Size); sizelen > 50 {
fmt.Println("size only less than or equal to 50")
return
Expand All @@ -73,30 +73,30 @@ func hflagInit() {
return
}
info := utils.InfoGet(token.Token)
data_result, user_result := utils.InfoLoadJson(info)
fmt.Println("#用户名:", user_result["username"])
fmt.Println("#邮 箱:", user_result["email"])
fmt.Println("#手机:", data_result["mobile_phone"])
fmt.Println("#月度积分:", data_result["month_remaining_credit"])
fmt.Println("#长效积分:", data_result["constant_credit"])
fmt.Println("#Token:", data_result["token"])
dataResult, userResult := utils.InfoLoadJson(info)
fmt.Println("#用户名:", userResult["username"])
fmt.Println("#邮 箱:", userResult["email"])
fmt.Println("#手机:", dataResult["mobile_phone"])
fmt.Println("#月度积分:", dataResult["month_remaining_credit"])
fmt.Println("#长效积分:", dataResult["constant_credit"])
fmt.Println("#Token:", dataResult["token"])
case "search":
token, status := utils.ReadYaml("./config.yaml")
if !status {
return
}
body := utils.SearchServicePost(reqjson, token.Token)
data_result := utils.RespLoadJson[utils.SearchJson](body).Data
dataResult := utils.RespLoadJson[utils.SearchJson](body).Data
if reqjson.Field != "" && reqjson.Field != "ip,port" {
for index, value := range data_result {
for index, value := range dataResult {
if value.Service.HTTP[reqjson.Field] == nil {
fmt.Println(strconv.Itoa(index+1) + "# " + value.IP + ":" + " " + strconv.Itoa(value.Port))
} else {
fmt.Println(strconv.Itoa(index+1) + "# " + value.IP + ":" + strconv.Itoa(value.Port) + " " + value.Service.HTTP[reqjson.Field].(string))
}
}
} else {
for index, value := range data_result {
for index, value := range dataResult {
fmt.Println(strconv.Itoa(index+1) + "# " + value.IP + ":" + strconv.Itoa(value.Port))
}
}
Expand All @@ -106,8 +106,8 @@ func hflagInit() {
return
}
body := utils.HostSearchPost(reqjson, token.Token)
data_result := utils.RespLoadJson[utils.SearchJson](body).Data
for index, value := range data_result {
dataResult := utils.RespLoadJson[utils.SearchJson](body).Data
for index, value := range dataResult {
fmt.Println(strconv.Itoa(index+1) + "# " + value.IP)
}
// case "favicon":
Expand Down
16 changes: 8 additions & 8 deletions utils/Reqjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ package utils
import . "time"

type Reqjson struct {
Query string `json:"query"`
Start string `json:"start,omitempty"`
Size string `json:"size,omitempty"`
Ignore_cache bool `json:"ignore_cache,omitempty"`
Start_time Time `json:"start_time,omitempty"`
End_time Time `json:"end_time,omitempty"`
Field string `json:"-"`
Query_txt string `json:"-"`
Query string `json:"query"`
Start string `json:"start,omitempty"`
Size string `json:"size,omitempty"`
IgnoreCache bool `json:"ignore_cache,omitempty"`
StartTime Time `json:"start_time,omitempty"`
EndTime Time `json:"end_time,omitempty"`
Field string `json:"-"`
QueryTxt string `json:"-"`
}
12 changes: 7 additions & 5 deletions utils/YamlAction.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package utils

import (
"fmt"
"io/ioutil"
"os"

"gopkg.in/yaml.v2"
Expand All @@ -28,8 +27,8 @@ func WriteYaml(path string, token string) {
tokeninfo.Token = token
data, err := yaml.Marshal(tokeninfo)
//config赋值777操作
err = ioutil.WriteFile(path, data, 0777)
if err = ioutil.WriteFile(path, data, 0777); err != nil {
err = os.WriteFile(path, data, 0777)
if err = os.WriteFile(path, data, 0777); err != nil {
fmt.Printf("token init fail!")
} else {
fmt.Printf("token init success!")
Expand All @@ -43,8 +42,11 @@ func ReadYaml(path string) (TokenInfo, bool) {
token.Token = ""
_, err := os.Stat(path)
if err == nil {
content, _ := ioutil.ReadFile(path)
yaml.Unmarshal(content, &token)
content, _ := os.ReadFile(path)
err := yaml.Unmarshal(content, &token)
if err != nil {
return TokenInfo{}, false
}
return token, true
}
if os.IsNotExist(err) { //如果返回的错误类型使用os.isNotExist()判断为true,说明文件或者文件夹不存在
Expand Down
8 changes: 4 additions & 4 deletions utils/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func SearchServicePost(reqjson Reqjson, token string) string {
// "end_time": "2021-02-01 00:00:00"
// }'
uri := "/search/quake_service"
if reqjson.Query_txt != "" {
bytedata, _ := ReadLine(reqjson.Query_txt)
if reqjson.QueryTxt != "" {
bytedata, _ := ReadLine(reqjson.QueryTxt)
tmp := ""
for _, v := range bytedata {
if tmp == "" {
Expand Down Expand Up @@ -122,8 +122,8 @@ func HostSearchPost(reqjson Reqjson, token string) string {
// }'

uri := "/search/quake_host"
if reqjson.Query_txt != "" {
bytedata, _ := ReadLine(reqjson.Query_txt)
if reqjson.QueryTxt != "" {
bytedata, _ := ReadLine(reqjson.QueryTxt)
tmp := ""
for _, v := range bytedata {
if tmp == "" {
Expand Down
11 changes: 8 additions & 3 deletions utils/commond.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package utils
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
)
Expand All @@ -40,9 +40,14 @@ func Apis(url string, method string, data []byte, token string) string {
fmt.Println(err)
return err.Error()
}
defer res.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
panic(err)
}
}(res.Body)

body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return err.Error()
Expand Down

0 comments on commit 9415512

Please sign in to comment.