2022-01-06 06:40:23 -05:00
|
|
|
package env
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
2023-05-29 20:43:06 +00:00
|
|
|
"github.com/qdm12/gosettings/sources/env"
|
2022-01-06 06:40:23 -05:00
|
|
|
)
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
func (s *Source) ReadHealth() (health settings.Health, err error) {
|
2023-05-29 20:43:06 +00:00
|
|
|
health.ServerAddress = env.Get("HEALTH_SERVER_ADDRESS")
|
2023-05-30 12:46:10 +00:00
|
|
|
_, health.TargetAddress = s.getEnvWithRetro("HEALTH_TARGET_ADDRESS", []string{"HEALTH_ADDRESS_TO_PING"})
|
2022-01-06 06:40:23 -05:00
|
|
|
|
2023-05-07 09:35:51 +00:00
|
|
|
successWaitPtr, err := envToDurationPtr("HEALTH_SUCCESS_WAIT_DURATION")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return health, fmt.Errorf("environment variable HEALTH_SUCCESS_WAIT_DURATION: %w", err)
|
|
|
|
|
} else if successWaitPtr != nil {
|
|
|
|
|
health.SuccessWait = *successWaitPtr
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
health.VPN.Initial, err = s.readDurationWithRetro(
|
2022-01-06 06:40:23 -05:00
|
|
|
"HEALTH_VPN_DURATION_INITIAL",
|
|
|
|
|
"HEALTH_OPENVPN_DURATION_INITIAL")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return health, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
health.VPN.Addition, err = s.readDurationWithRetro(
|
2022-01-06 06:40:23 -05:00
|
|
|
"HEALTH_VPN_DURATION_ADDITION",
|
|
|
|
|
"HEALTH_OPENVPN_DURATION_ADDITION")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return health, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return health, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
func (s *Source) readDurationWithRetro(envKey, retroEnvKey string) (d *time.Duration, err error) {
|
2023-05-30 12:46:10 +00:00
|
|
|
envKey, value := s.getEnvWithRetro(envKey, []string{retroEnvKey})
|
2022-08-26 15:16:51 +00:00
|
|
|
if value == "" {
|
2022-01-29 14:55:56 +00:00
|
|
|
return nil, nil //nolint:nilnil
|
2022-01-06 06:40:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d = new(time.Duration)
|
2022-08-26 15:16:51 +00:00
|
|
|
*d, err = time.ParseDuration(value)
|
2022-01-06 06:40:23 -05:00
|
|
|
if err != nil {
|
2022-02-20 02:58:16 +00:00
|
|
|
return nil, fmt.Errorf("environment variable %s: %w", envKey, err)
|
2022-01-06 06:40:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
|
}
|