Skip to content
This repository has been archived by the owner on Sep 23, 2019. It is now read-only.

增加支持post,put,delete接口监控 #4

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ agent会定时从web组件获取待监控url列表,发起模拟访问,然后
# set $GOPATH and $GOROOT
mkdir -p $GOPATH/src/github.com/urlooker
cd $GOPATH/src/github.com/urlooker
git clone https://github.com/URLooker/agent.git
git clone https://github.com/xiaolezheng/agent.git
cd agent
go get ./...
./control build
Expand Down
45 changes: 36 additions & 9 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
)

const (
NO_ERROR = 0
REQ_TIMEOUT = 1
NO_ERROR = 0
REQ_TIMEOUT = 1
INVALID_RESP_CODE = 2
KEYWORD_UNMATCH = 3
DNS_ERROR = 4
KEYWORD_UNMATCH = 3
DNS_ERROR = 4
)

func CheckTargetStatus(item *webg.DetectedItem) {
Expand All @@ -32,7 +32,7 @@ func CheckTargetStatus(item *webg.DetectedItem) {
g.CheckResultQueue.PushFront(checkResult)
}

func checkTargetStatus(item *webg.DetectedItem) (itemCheckResult *webg.CheckResult) {
func doCheckTargetStatus(item *webg.DetectedItem, req *httplib.BeegoHTTPRequest) (itemCheckResult *webg.CheckResult) {
itemCheckResult = &webg.CheckResult{
Sid: item.Sid,
Domain: item.Domain,
Expand All @@ -43,12 +43,16 @@ func checkTargetStatus(item *webg.DetectedItem) (itemCheckResult *webg.CheckResu
RespTime: item.Timeout,
RespCode: "0",
}

reqStartTime := time.Now()
req := httplib.Get(item.Target)

req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
req.SetTimeout(3*time.Second, 10*time.Second)
req.Header("Content-Type", "application/x-www-form-urlencoded; param=value")
req.SetTimeout(3 * time.Second, 10 * time.Second)
req.Header("Content-Type", "application/json")
req.SetHost(item.Domain)
if len(item.PostData) > 0 && item.Method != "GET" {
req.Body(item.PostData)
}
if item.Data != "" {
req.Header("Cookie", item.Data)
}
Expand All @@ -69,6 +73,7 @@ func checkTargetStatus(item *webg.DetectedItem) (itemCheckResult *webg.CheckResu
respTime := int(time.Now().Sub(reqStartTime).Nanoseconds() / 1000000)
itemCheckResult.RespTime = respTime

log.Println("[req_status]:", respCode + "|" + item.Target + "|" + strconv.Itoa(respTime) + "|" + strconv.Itoa(item.Timeout))
if respTime > item.Timeout {
itemCheckResult.Status = REQ_TIMEOUT
return
Expand All @@ -77,7 +82,9 @@ func checkTargetStatus(item *webg.DetectedItem) (itemCheckResult *webg.CheckResu
if strings.Index(respCode, item.ExpectCode) == 0 || (len(item.ExpectCode) == 0 && respCode == "200") {
if len(item.Keywords) > 0 {
contents, _ := ioutil.ReadAll(resp.Body)
if !strings.Contains(string(contents), item.Keywords) {
contentStr := string(contents)
if !strings.Contains(contentStr, item.Keywords) {
log.Println("[result is not expected]: ", item.Target + "|" + item.Keywords + "|" + contentStr)
itemCheckResult.Status = KEYWORD_UNMATCH
return
}
Expand All @@ -91,3 +98,23 @@ func checkTargetStatus(item *webg.DetectedItem) (itemCheckResult *webg.CheckResu
}
return
}

func checkTargetStatus(item *webg.DetectedItem) (itemCheckResult *webg.CheckResult) {
method := item.Method
switch method {
case "GET":
req := httplib.Get(item.Target)
return doCheckTargetStatus(item, req)
case "POST":
req := httplib.Post(item.Target)
return doCheckTargetStatus(item, req)
case "PUT":
req := httplib.Put(item.Target)
return doCheckTargetStatus(item, req)
case "DELETE":
req := httplib.Delete(item.Target)
return doCheckTargetStatus(item, req)
}

return
}