Skip to content

Commit

Permalink
implement http payload for packetgen
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Apr 9, 2022
1 parent cda7560 commit fa3d25f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
7 changes: 1 addition & 6 deletions src/core/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"go.uber.org/zap"

"github.com/Arriven/db1000n/src/utils"
"github.com/Arriven/db1000n/src/utils/metrics"
"github.com/Arriven/db1000n/src/utils/templates"
)

Expand All @@ -48,7 +47,7 @@ type RequestConfig struct {
}

// InitRequest is used to populate data from request config to fasthttp.Request
func InitRequest(c RequestConfig, req *fasthttp.Request) int64 {
func InitRequest(c RequestConfig, req *fasthttp.Request) {
req.SetRequestURI(c.Path)
req.Header.SetMethod(c.Method)
req.SetBodyString(c.Body)
Expand All @@ -62,10 +61,6 @@ func InitRequest(c RequestConfig, req *fasthttp.Request) int64 {
for key, value := range c.Cookies {
req.Header.SetCookie(key, value)
}

dataSize, _ := req.WriteTo(metrics.NopWriter{})

return dataSize
}

type Client interface {
Expand Down
55 changes: 40 additions & 15 deletions src/core/packetgen/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
package packetgen

import (
"bytes"
"fmt"

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/valyala/fasthttp"

"github.com/Arriven/db1000n/src/core/http"
"github.com/Arriven/db1000n/src/utils"
)

Expand All @@ -45,20 +48,12 @@ func BuildPayload(c LayerConfig) (gopacket.Layer, error) {
}

return gopacket.Payload([]byte(packetConfig.Payload)), nil
case "http":
return buildHTTPPacket(c.Data)
case "icmpv4":
var packetConfig ICMPV4PacketConfig
if err := utils.Decode(c.Data, &packetConfig); err != nil {
return nil, err
}

return buildICMPV4Packet(packetConfig), nil
return buildICMPV4Packet(c.Data)
case "dns":
var packetConfig DNSPacketConfig
if err := utils.Decode(c.Data, &packetConfig); err != nil {
return nil, err
}

return buildDNSPacket(packetConfig), nil
return buildDNSPacket(c.Data)
default:
return nil, fmt.Errorf("unsupported layer type %s", c.Type)
}
Expand All @@ -70,12 +65,17 @@ type ICMPV4PacketConfig struct {
Seq uint16
}

func buildICMPV4Packet(c ICMPV4PacketConfig) *layers.ICMPv4 {
func buildICMPV4Packet(data map[string]any) (*layers.ICMPv4, error) {
var c ICMPV4PacketConfig
if err := utils.Decode(data, &c); err != nil {
return nil, err
}

return &layers.ICMPv4{
TypeCode: layers.ICMPv4TypeCode(c.TypeCode),
Id: c.ID,
Seq: c.Seq,
}
}, nil
}

// DNSQuestion wraps a single request (question) within a DNS query.
Expand Down Expand Up @@ -103,7 +103,12 @@ type DNSPacketConfig struct {
Questions []DNSQuestion
}

func buildDNSPacket(c DNSPacketConfig) *layers.DNS {
func buildDNSPacket(data map[string]any) (*layers.DNS, error) {
var c DNSPacketConfig
if err := utils.Decode(data, &c); err != nil {
return nil, err
}

questions := make([]layers.DNSQuestion, 0, len(c.Questions))
for _, question := range c.Questions {
questions = append(questions, layers.DNSQuestion{Name: []byte(question.Name), Type: question.Type, Class: question.Class})
Expand All @@ -122,5 +127,25 @@ func buildDNSPacket(c DNSPacketConfig) *layers.DNS {

QDCount: utils.NonNilOrDefault(c.QDCount, uint16(len(c.Questions))),
Questions: questions,
}, nil
}

func buildHTTPPacket(data map[string]any) (gopacket.Payload, error) {
var c http.RequestConfig

if err := utils.Decode(data, &c); err != nil {
return nil, err
}

var req fasthttp.Request

http.InitRequest(c, &req)

var buf bytes.Buffer

if _, err := req.WriteTo(&buf); err != nil {
return nil, err
}

return gopacket.Payload(buf.Bytes()), nil
}
8 changes: 6 additions & 2 deletions src/job/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func singleRequestJob(ctx context.Context, logger *zap.Logger, globalConfig *Glo
log.Printf("Sent single http request to %v", requestConfig.Path)
}

dataSize := http.InitRequest(requestConfig, req)
http.InitRequest(requestConfig, req)

dataSize, _ := req.WriteTo(metrics.NopWriter{})

metrics.Default.Write(metrics.Traffic, uuid.New().String(), uint64(dataSize))

Expand Down Expand Up @@ -153,7 +155,9 @@ func fastHTTPJob(ctx context.Context, logger *zap.Logger, globalConfig *GlobalCo
return nil, fmt.Errorf("error executing request template: %w", err)
}

dataSize := http.InitRequest(requestConfig, req)
http.InitRequest(requestConfig, req)

dataSize, _ := req.WriteTo(metrics.NopWriter{})

trafficMonitor.Add(uint64(dataSize))

Expand Down

0 comments on commit fa3d25f

Please sign in to comment.