asn.go 804 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package config
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. var (
  7. ErrPrivateASN = errors.New("bot.ASNs: you have specified a private use ASN")
  8. )
  9. type ASNs struct {
  10. Match []uint32 `json:"match"`
  11. }
  12. func (a *ASNs) Valid() error {
  13. var errs []error
  14. for _, asn := range a.Match {
  15. if isPrivateASN(asn) {
  16. errs = append(errs, fmt.Errorf("%w: %d is private (see RFC 6996)", ErrPrivateASN, asn))
  17. }
  18. }
  19. if len(errs) != 0 {
  20. return fmt.Errorf("bot.ASNs: invalid ASN settings: %w", errors.Join(errs...))
  21. }
  22. return nil
  23. }
  24. // isPrivateASN checks if an ASN is in the private use area.
  25. //
  26. // Based on RFC 6996 and IANA allocations.
  27. func isPrivateASN(asn uint32) bool {
  28. switch {
  29. case asn >= 64512 && asn <= 65534:
  30. return true
  31. case asn >= 4200000000 && asn <= 4294967294:
  32. return true
  33. default:
  34. return false
  35. }
  36. }