Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: moved custom value types into types.go and various utils into…
… utils.go
- Loading branch information
Showing
3 changed files
with
88 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
// Networks is a list of IPNet entities | ||
type Networks []*net.IPNet | ||
|
||
// UnmarshalYAML implements the yaml.Unmarshaler interface. | ||
func (n *Networks) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
var s []string | ||
if err := unmarshal(&s); err != nil { | ||
return err | ||
} | ||
networks := make(Networks, len(s)) | ||
for i, s := range s { | ||
ipnet, err := stringToIPnet(s) | ||
if err != nil { | ||
return err | ||
} | ||
networks[i] = ipnet | ||
} | ||
*n = networks | ||
return nil | ||
} | ||
|
||
// Contains checks whether passed addr is in the range of networks | ||
func (n Networks) Contains(addr string) bool { | ||
if len(n) == 0 { | ||
return true | ||
} | ||
|
||
h, _, err := net.SplitHostPort(addr) | ||
if err != nil { | ||
panic(fmt.Sprintf("BUG: unexpected error while parsing RemoteAddr: %s", err)) | ||
} | ||
|
||
ip := net.ParseIP(h) | ||
if ip == nil { | ||
panic(fmt.Sprintf("BUG: unexpected error while parsing IP: %s", h)) | ||
} | ||
|
||
for _, ipnet := range n { | ||
if ipnet.Contains(ip) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strings" | ||
) | ||
|
||
const entireIPv4 = "0.0.0.0/0" | ||
|
||
func stringToIPnet(s string) (*net.IPNet, error) { | ||
if s == entireIPv4 { | ||
return nil, fmt.Errorf("suspicious mask specified \"0.0.0.0/0\". " + | ||
"If you want to allow all then just omit `allowed_networks` field") | ||
} | ||
ip := s | ||
if !strings.Contains(ip, `/`) { | ||
ip += "/32" | ||
} | ||
_, ipnet, err := net.ParseCIDR(ip) | ||
if err != nil { | ||
return nil, fmt.Errorf("wrong network group name or address %q: %s", s, err) | ||
} | ||
return ipnet, nil | ||
} | ||
|
||
func checkOverflow(m map[string]interface{}, ctx string) error { | ||
if len(m) > 0 { | ||
var keys []string | ||
for k := range m { | ||
keys = append(keys, k) | ||
} | ||
return fmt.Errorf("unknown fields in %s: %s", ctx, strings.Join(keys, ", ")) | ||
} | ||
return nil | ||
} |