config.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package generichttp
  2. import (
  3. "errors"
  4. "time"
  5. "github.com/imgproxy/imgproxy/v3/ensure"
  6. "github.com/imgproxy/imgproxy/v3/env"
  7. )
  8. var (
  9. IMGPROXY_CLIENT_KEEP_ALIVE_TIMEOUT = env.Describe("IMGPROXY_CLIENT_KEEP_ALIVE_TIMEOUT", "seconds => 0")
  10. IMGPROXY_IGNORE_SSL_VERIFICATION = env.Describe("IMGPROXY_IGNORE_SSL_VERIFICATION", "boolean")
  11. IMGPROXY_ALLOW_LOOPBACK_SOURCE_ADDRESSES = env.Describe("IMGPROXY_ALLOW_LOOPBACK_SOURCE_ADDRESSES", "boolean")
  12. IMGPROXY_ALLOW_LINK_LOCAL_SOURCE_ADDRESSES = env.Describe("IMGPROXY_ALLOW_LINK_LOCAL_SOURCE_ADDRESSES", "boolean")
  13. IMGPROXY_ALLOW_PRIVATE_SOURCE_ADDRESSES = env.Describe("IMGPROXY_ALLOW_PRIVATE_SOURCE_ADDRESSES", "boolean")
  14. )
  15. // Config holds the configuration for the generic HTTP transport
  16. type Config struct {
  17. ClientKeepAliveTimeout time.Duration
  18. IgnoreSslVerification bool
  19. AllowLoopbackSourceAddresses bool
  20. AllowLinkLocalSourceAddresses bool
  21. AllowPrivateSourceAddresses bool
  22. }
  23. // NewDefaultConfig returns a new default configuration for the generic HTTP transport
  24. func NewDefaultConfig() Config {
  25. return Config{
  26. ClientKeepAliveTimeout: 90 * time.Second,
  27. IgnoreSslVerification: false,
  28. AllowLoopbackSourceAddresses: false,
  29. AllowLinkLocalSourceAddresses: false,
  30. AllowPrivateSourceAddresses: true,
  31. }
  32. }
  33. // LoadConfigFromEnv loads configuration from the global config package
  34. func LoadConfigFromEnv(c *Config) (*Config, error) {
  35. c = ensure.Ensure(c, NewDefaultConfig)
  36. err := errors.Join(
  37. env.Duration(&c.ClientKeepAliveTimeout, IMGPROXY_CLIENT_KEEP_ALIVE_TIMEOUT),
  38. env.Bool(&c.IgnoreSslVerification, IMGPROXY_IGNORE_SSL_VERIFICATION),
  39. env.Bool(&c.AllowLinkLocalSourceAddresses, IMGPROXY_ALLOW_LINK_LOCAL_SOURCE_ADDRESSES),
  40. env.Bool(&c.AllowLoopbackSourceAddresses, IMGPROXY_ALLOW_LOOPBACK_SOURCE_ADDRESSES),
  41. env.Bool(&c.AllowPrivateSourceAddresses, IMGPROXY_ALLOW_PRIVATE_SOURCE_ADDRESSES),
  42. )
  43. return c, err
  44. }
  45. // Validate checks the configuration for errors
  46. func (c *Config) Validate() error {
  47. if c.ClientKeepAliveTimeout < 0 {
  48. return IMGPROXY_CLIENT_KEEP_ALIVE_TIMEOUT.ErrorZeroOrNegative()
  49. }
  50. if c.IgnoreSslVerification {
  51. IMGPROXY_IGNORE_SSL_VERIFICATION.Warn("ignoring SSL verification is very unsafe") // ⎈
  52. }
  53. return nil
  54. }