2022-01-06 06:40:23 -05:00
|
|
|
package env
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2023-05-20 19:58:18 +00:00
|
|
|
"net/netip"
|
2022-01-06 06:40:23 -05:00
|
|
|
|
|
|
|
|
"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) readWireguardSelection() (
|
2022-01-06 06:40:23 -05:00
|
|
|
selection settings.WireguardSelection, err error) {
|
2022-08-26 15:16:51 +00:00
|
|
|
selection.EndpointIP, err = s.readWireguardEndpointIP()
|
2022-01-06 06:40:23 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return selection, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
selection.EndpointPort, err = s.readWireguardCustomPort()
|
2022-01-06 06:40:23 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return selection, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-01 08:22:55 +00:00
|
|
|
selection.PublicKey = s.env.String("WIREGUARD_PUBLIC_KEY", env.ForceLowercase(false))
|
2022-01-06 06:40:23 -05:00
|
|
|
|
|
|
|
|
return selection, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 19:58:18 +00:00
|
|
|
func (s *Source) readWireguardEndpointIP() (endpointIP netip.Addr, err error) {
|
2023-05-30 12:46:10 +00:00
|
|
|
key, value := s.getEnvWithRetro("VPN_ENDPOINT_IP", []string{"WIREGUARD_ENDPOINT_IP"})
|
2023-05-30 15:21:09 +00:00
|
|
|
if value == nil {
|
2023-05-20 19:58:18 +00:00
|
|
|
return endpointIP, nil
|
2022-01-28 00:09:58 +00:00
|
|
|
}
|
|
|
|
|
|
2023-05-30 15:21:09 +00:00
|
|
|
endpointIP, err = netip.ParseAddr(*value)
|
2023-05-20 19:58:18 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return endpointIP, fmt.Errorf("environment variable %s: %w", key, err)
|
2022-01-06 06:40:23 -05:00
|
|
|
}
|
2022-01-28 00:09:58 +00:00
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
return endpointIP, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
func (s *Source) readWireguardCustomPort() (customPort *uint16, err error) {
|
2023-05-30 15:21:09 +00:00
|
|
|
envKey, _ := s.getEnvWithRetro("VPN_ENDPOINT_PORT", []string{"WIREGUARD_ENDPOINT_PORT"})
|
2023-06-01 08:22:55 +00:00
|
|
|
return s.env.Uint16Ptr(envKey)
|
2022-01-06 06:40:23 -05:00
|
|
|
}
|