Skip to content

Commit

Permalink
Use mapstructure instead of YAML for PortRanges
Browse files Browse the repository at this point in the history
Turns out Viper does not support YAML unmarshalling directly but goes through a map structure as an intermediate: spf13/viper#338
  • Loading branch information
Ullaakut committed Jul 5, 2020
1 parent 8d05084 commit dfd1cd1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.dll
*.so
*.dylib
goneypot

# Test binary, built with `go test -c`
*.test
Expand Down
48 changes: 32 additions & 16 deletions pkg/configuration/configuration.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package configuration

import "errors"

type Configuration struct {
ICMP bool `yaml:"icmp"`
TCP *TCPConfig `yaml:"tcp"`
Expand All @@ -8,19 +10,27 @@ type Configuration struct {
}

type TCPConfig struct {
Ports PortRanges `yaml:"ports"`
Ports PortRanges `mapstructure:"ports"`
// TODO: Fake service configuration.
}

// UnmarshalYAML implements the Unmarshaler interface of the yaml pkg.
func (t *TCPConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
var yamlPortRangeSequence []string
if err := unmarshal(&yamlPortRangeSequence); err != nil {
return err
// UnmarshalMap implements the Unmarshaler interface.
func (t *TCPConfig) UnmarshalMap(value interface{}) error {
var (
tcpCfg map[string]interface{}
ok bool
)
if tcpCfg, ok = value.(map[string]interface{}); !ok {
return errors.New("invalid TCP port range")
}

portRanges, ok := tcpCfg["ports"].([]string)
if !ok {
return errors.New("invalid TCP port range")
}

var err error
t.Ports, err = NewPortRanges(yamlPortRangeSequence)
t.Ports, err = NewPortRanges(portRanges)
if err != nil {
return err
}
Expand All @@ -29,22 +39,28 @@ func (t *TCPConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
}

type UDPConfig struct {
Ports PortRanges `yaml:"ports"`
Ports PortRanges `mapstructure:"ports"`
// TODO: Fake service configuration.
}

// UnmarshalYAML implements the Unmarshaler interface of the yaml pkg.
func (u *UDPConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
var yamlPortRangeSequence []string
if err := unmarshal(&yamlPortRangeSequence); err != nil {
return err
// UnmarshalMap implements the Unmarshaler interface.
func (u *UDPConfig) UnmarshalMap(value interface{}) error {
var (
udpCfg map[string]interface{}
ok bool
)
if udpCfg, ok = value.(map[string]interface{}); !ok {
return errors.New("invalid UDP port range")
}

portRanges, ok := udpCfg["ports"].([]string)
if !ok {
return errors.New("invalid UDP port range")
}

var err error
u.Ports, err = NewPortRanges(yamlPortRangeSequence)
t.Ports, err = NewPortRanges(portRanges)
if err != nil {
return err
}

return nil
}
2 changes: 2 additions & 0 deletions pkg/configuration/portranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func NewPortRanges(s []string) (PortRanges, error) {
var result PortRanges

for _, portRange := range s {
fmt.Println("Port range found:", s)

ports := strings.Split(portRange, "-")

// Case where only one port is specified.
Expand Down

0 comments on commit dfd1cd1

Please sign in to comment.