Skip to content

Commit

Permalink
Merge pull request #613 from kongfei605/udp_update
Browse files Browse the repository at this point in the history
udp test as ncat
  • Loading branch information
kongfei605 authored Aug 19, 2023
2 parents 53a6730 + 1fa648e commit 5fe6c76
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions inputs/net_response/net_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"math"
"net"
"net/textproto"
"strings"
Expand Down Expand Up @@ -53,15 +54,15 @@ func (ins *Instance) Init() error {
}

if ins.ReadTimeout == 0 {
ins.ReadTimeout = config.Duration(time.Second)
ins.ReadTimeout = config.Duration(3 * time.Second)
}

if ins.Protocol == "udp" && ins.Send == "" {
return errors.New("send string cannot be empty when protocol is udp")
ins.Send = "X"
}

if ins.Protocol == "udp" && ins.Expect == "" {
return errors.New("expected string cannot be empty when protocol is udp")
ins.Expect = ""
}

for i := 0; i < len(ins.Targets); i++ {
Expand Down Expand Up @@ -287,6 +288,22 @@ func (ins *Instance) UDPGather(address string) (map[string]string, map[string]in
if gerr := conn.SetReadDeadline(time.Now().Add(time.Duration(ins.ReadTimeout))); gerr != nil {
return nil, nil, gerr
}
if ins.Expect == "" {
t := math.Max(float64(time.Duration(ins.ReadTimeout)/time.Second), 3)
for i := 0; i < int(t); i++ {
time.Sleep(1 * time.Second)
_, err = conn.Write(msg)
if err != nil && config.Config.DebugMode {
log.Printf("E! write udp failed, address: %s, error: %s", address, err)
}
if err != nil && strings.Contains(err.Error(), "refused") {
fields["result_code"] = ConnectionFailed
return tags, fields, nil
}
}
fields["result_code"] = Success
return tags, fields, nil
}
// Read
buf := make([]byte, 1024)
_, _, err = conn.ReadFromUDP(buf)
Expand Down

0 comments on commit 5fe6c76

Please sign in to comment.