Skip to content

Commit

Permalink
compatibility layer between snake and camel case
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Mar 31, 2022
1 parent 910895a commit 8de49ba
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 41 deletions.
2 changes: 1 addition & 1 deletion examples/config/advanced/packetgen-tcp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
protocol: "tcp"
address: "google.com:443"
tls_config:
InsecureSkipVerify: true # this is not snake case because I decode it straight into tls config
insecure_skip_verify: true # this is not snake case because I decode it straight into tls config
packet:
application:
type: raw
Expand Down
20 changes: 10 additions & 10 deletions src/core/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,20 @@ type Client interface {
}

type StaticHostConfig struct {
Addr string `mapstructure:"addr"`
IsTLS bool `mapstructure:"is_tls"`
Addr string
IsTLS bool
}

// ClientConfig is a http client configuration structure
type ClientConfig struct {
StaticHost *StaticHostConfig `mapstructure:"static_host"`
TLSClientConfig *tls.Config `mapstructure:"tls_config,omitempty"`
Timeout *time.Duration `mapstructure:"timeout"`
ReadTimeout *time.Duration `mapstructure:"read_timeout"`
WriteTimeout *time.Duration `mapstructure:"write_timeout"`
IdleTimeout *time.Duration `mapstructure:"idle_timeout"`
MaxIdleConns *int `mapstructure:"max_idle_connections"`
ProxyURLs string `mapstructure:"proxy_urls"`
StaticHost *StaticHostConfig
TLSClientConfig *tls.Config
Timeout *time.Duration
ReadTimeout *time.Duration
WriteTimeout *time.Duration
IdleTimeout *time.Duration
MaxIdleConns *int
ProxyURLs string
}

// NewClient creates a fasthttp client based on the config.
Expand Down
4 changes: 2 additions & 2 deletions src/core/packetgen/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ type netConnConfig struct {
Protocol string
Address string
Timeout time.Duration
ProxyURLs string `mapstructure:"proxy_urls"`
TLSClientConfig *tls.Config `mapstructure:"tls_config"`
ProxyURLs string
TLSClientConfig *tls.Config
}

type netConn struct {
Expand Down
4 changes: 2 additions & 2 deletions src/core/packetgen/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func BuildLinkLayer(c LayerConfig) (gopacket.LinkLayer, error) {

// EthernetPacketConfig describes ethernet layer configuration
type EthernetPacketConfig struct {
SrcMAC string `mapstructure:"src_mac"`
DstMAC string `mapstructure:"dst_mac"`
SrcMAC string
DstMAC string
}

// buildEthernetPacket generates an layers.Ethernet and returns it with source MAC address and destination MAC address
Expand Down
6 changes: 3 additions & 3 deletions src/core/packetgen/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func BuildNetworkLayer(c LayerConfig) (gopacket.NetworkLayer, error) {

// IPPacketConfig describes ip layer configuration
type IPPacketConfig struct {
SrcIP string `mapstructure:"src_ip"`
DstIP string `mapstructure:"dst_ip"`
NextProtocol *int `mapstructure:"next"`
SrcIP string
DstIP string
NextProtocol *int
}

// buildIPV4Packet generates a layers.IPv4 and returns it with source IP address and destination IP address
Expand Down
4 changes: 2 additions & 2 deletions src/core/packetgen/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func BuildPayload(c LayerConfig) (gopacket.Layer, error) {
}

type ICMPV4PacketConfig struct {
TypeCode uint16 `mapstructure:"code"`
TypeCode uint16
ID uint16
Seq uint16
}
Expand All @@ -81,7 +81,7 @@ func buildICMPV4Packet(c ICMPV4PacketConfig) *layers.ICMPv4 {
type DNSPacketConfig struct {
ID uint16
Qr bool
OpCode uint8 `mapstructure:"code"`
OpCode uint8
QDCount uint16
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/packetgen/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ func TestSerialize(t *testing.T) {
"payload": map[string]interface{}{
"type": "icmpv4",
"data": map[string]interface{}{
"code": 130,
"seq": 1231231,
"id": 1231231231,
"type_code": 130,
"seq": 1231231,
"id": 1231231231,
},
},
"expected_result": 130,
Expand Down
8 changes: 4 additions & 4 deletions src/core/packetgen/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func BuildTransportLayer(c LayerConfig, network gopacket.NetworkLayer) (gopacket

// UDPPacketConfig describes udp layer configuration
type UDPPacketConfig struct {
SrcPort int `mapstructure:"src_port,string"`
DstPort int `mapstructure:"dst_port,string"`
SrcPort int
DstPort int
}

func buildUDPPacket(c UDPPacketConfig, network gopacket.NetworkLayer) *layers.UDP {
Expand Down Expand Up @@ -87,8 +87,8 @@ type TCPFlagsConfig struct {

// TCPPacketConfig describes tcp layer configuration
type TCPPacketConfig struct {
SrcPort int `mapstructure:"src_port,string"`
DstPort int `mapstructure:"dst_port,string"`
SrcPort int
DstPort int
Seq uint32
Ack uint32
Window uint16
Expand Down
4 changes: 2 additions & 2 deletions src/job/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func ParseConfig(c Config, args config.Args, global GlobalConfig) error {

// BasicJobConfig comment for linter
type BasicJobConfig struct {
IntervalMs int `mapstructure:"interval_ms,omitempty"`
Interval *time.Duration `mapstructure:"interval"`
IntervalMs int
Interval *time.Duration
utils.Counter
Backoff *utils.BackoffConfig
}
Expand Down
10 changes: 5 additions & 5 deletions src/job/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ type Args = map[string]interface{}

// Config for a single job.
type Config struct {
Name string `mapstructure:"name"`
Type string `mapstructure:"type"`
Count int `mapstructure:"count"`
Filter string `mapstructure:"filter"`
Args Args `mapstructure:"args"`
Name string
Type string
Count int
Filter string
Args Args
}

// MultiConfig for all jobs.
Expand Down
8 changes: 4 additions & 4 deletions src/job/dnsblast.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ import (

type dnsBlastConfig struct {
BasicJobConfig
RootDomain string `mapstructure:"root_domain"`
Protocol string `mapstructure:"protocol"` // "udp", "tcp", "tcp-tls"
SeedDomains []string `mapstructure:"seed_domains"`
ParallelQueries int `mapstructure:"parallel_queries"`
RootDomain string
Protocol string // "udp", "tcp", "tcp-tls"
SeedDomains []string
ParallelQueries int
}

func dnsBlastJob(ctx context.Context, logger *zap.Logger, globalConfig *GlobalConfig, args config.Args) (data interface{}, err error) {
Expand Down
2 changes: 1 addition & 1 deletion src/job/rawnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func parseRawNetJobArgs(ctx context.Context, logger *zap.Logger, globalConfig *G

Address string
Body string
ProxyURLs string `mapstructure:"proxy_urls"`
ProxyURLs string
Timeout *time.Duration
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *BackoffController) Reset() {
}

type Counter struct {
Count int `mapstructure:"count,omitempty"`
Count int

iter int
}
Expand Down
18 changes: 17 additions & 1 deletion src/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/exec"
"runtime"
"strconv"
"strings"
"time"

"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -100,8 +101,23 @@ func NonNilIntOrDefault(i *int, dflt int) int {
}

// Decode is an alias to a mapstructure.NewDecoder({Squash: true}).Decode()
// with WeaklyTypedInput set to true and MatchFunc that only compares aplhanumeric sequence in field names
func Decode(input interface{}, output interface{}) error {
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{Squash: true, WeaklyTypedInput: true, Result: output})
filter := func(r rune) rune {
if ('a' <= r && r <= 'z') ||
('A' <= r && r <= 'Z') ||
('0' <= r && r <= '9') {
return r
}

return -1
}

matchName := func(lhs, rhs string) bool {
return strings.EqualFold(strings.Map(filter, lhs), strings.Map(filter, rhs))
}

decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{Squash: true, WeaklyTypedInput: true, MatchName: matchName, Result: output})
if err != nil {
return err
}
Expand Down

0 comments on commit 8de49ba

Please sign in to comment.