remotetrustconfig.go 823 B

12345678910111213141516171819202122232425262728293031
  1. package remotetrust
  2. import (
  3. "fmt"
  4. "github.com/SongZihuan/huan-proxy/src/config/configerr"
  5. "github.com/SongZihuan/huan-proxy/src/utils"
  6. )
  7. type RemoteTrustConfig struct {
  8. RemoteTrust utils.StringBool `yaml:"remotetrust"`
  9. TrustedIPs []string `yaml:"trustedips"`
  10. }
  11. func (p *RemoteTrustConfig) SetDefault() {
  12. p.RemoteTrust.SetDefaultDisable()
  13. if p.RemoteTrust.IsEnable() && len(p.TrustedIPs) == 0 {
  14. p.TrustedIPs = []string{"127.0.0.0/8", "::1"}
  15. }
  16. }
  17. func (p *RemoteTrustConfig) Check() configerr.ConfigError {
  18. if p.RemoteTrust.IsEnable() {
  19. for _, ip := range p.TrustedIPs {
  20. if !utils.ValidIPv4(ip) && !utils.ValidIPv6(ip) && !utils.IsValidIPv4CIDR(ip) && !utils.IsValidIPv6CIDR(ip) {
  21. return configerr.NewConfigError(fmt.Sprintf("bad proxy trusts ip address: %s", ip))
  22. }
  23. }
  24. }
  25. return nil
  26. }