Skip to content

Commit 2bf2525

Browse files
committed
chore(config): split server filter validation for features and subscription tier
- `validateSubscriptionTierFilters` function - `validateFeatureFilters` function - idea introduced in #2182
1 parent 26705f5 commit 2bf2525

File tree

1 file changed

+40
-47
lines changed

1 file changed

+40
-47
lines changed

internal/configuration/settings/serverselection.go

+40-47
Original file line numberDiff line numberDiff line change
@@ -107,55 +107,14 @@ func (ss *ServerSelection) validate(vpnServiceProvider string,
107107
return fmt.Errorf("for VPN service provider %s: %w", vpnServiceProvider, err)
108108
}
109109

110-
if *ss.OwnedOnly &&
111-
vpnServiceProvider != providers.Mullvad {
112-
return fmt.Errorf("%w: for VPN service provider %s",
113-
ErrOwnedOnlyNotSupported, vpnServiceProvider)
114-
}
115-
116-
if *ss.FreeOnly &&
117-
!helpers.IsOneOf(vpnServiceProvider,
118-
providers.Protonvpn,
119-
providers.VPNUnlimited,
120-
) {
121-
return fmt.Errorf("%w: for VPN service provider %s",
122-
ErrFreeOnlyNotSupported, vpnServiceProvider)
123-
}
124-
125-
if *ss.PremiumOnly &&
126-
!helpers.IsOneOf(vpnServiceProvider,
127-
providers.VPNSecure,
128-
) {
129-
return fmt.Errorf("%w: for VPN service provider %s",
130-
ErrPremiumOnlyNotSupported, vpnServiceProvider)
131-
}
132-
133-
if *ss.FreeOnly && *ss.PremiumOnly {
134-
return fmt.Errorf("%w", ErrFreePremiumBothSet)
135-
}
136-
137-
if *ss.StreamOnly &&
138-
!helpers.IsOneOf(vpnServiceProvider,
139-
providers.Protonvpn,
140-
providers.VPNUnlimited,
141-
) {
142-
return fmt.Errorf("%w: for VPN service provider %s",
143-
ErrStreamOnlyNotSupported, vpnServiceProvider)
144-
}
145-
146-
if *ss.MultiHopOnly &&
147-
vpnServiceProvider != providers.Surfshark {
148-
return fmt.Errorf("%w: for VPN service provider %s",
149-
ErrMultiHopOnlyNotSupported, vpnServiceProvider)
110+
err = validateSubscriptionTierFilters(*ss, vpnServiceProvider)
111+
if err != nil {
112+
return fmt.Errorf("for VPN service provider %s: %w", vpnServiceProvider, err)
150113
}
151114

152-
if *ss.PortForwardOnly &&
153-
vpnServiceProvider != providers.PrivateInternetAccess {
154-
// ProtonVPN also supports port forwarding, but on all their servers, so these
155-
// don't have the port forwarding boolean field. As a consequence, we only allow
156-
// the use of PortForwardOnly for Private Internet Access.
157-
return fmt.Errorf("%w: for VPN service provider %s",
158-
ErrPortForwardOnlyNotSupported, vpnServiceProvider)
115+
err = validateFeatureFilters(*ss, vpnServiceProvider)
116+
if err != nil {
117+
return fmt.Errorf("for VPN service provider %s: %w", vpnServiceProvider, err)
159118
}
160119

161120
if ss.VPN == vpn.OpenVPN {
@@ -242,6 +201,40 @@ func validateServerFilters(settings ServerSelection, filterChoices models.Filter
242201
return nil
243202
}
244203

204+
func validateSubscriptionTierFilters(settings ServerSelection, vpnServiceProvider string) error {
205+
switch {
206+
case *settings.FreeOnly &&
207+
!helpers.IsOneOf(vpnServiceProvider, providers.Protonvpn, providers.VPNUnlimited):
208+
return fmt.Errorf("%w", ErrFreeOnlyNotSupported)
209+
case *settings.PremiumOnly &&
210+
!helpers.IsOneOf(vpnServiceProvider, providers.VPNSecure):
211+
return fmt.Errorf("%w", ErrPremiumOnlyNotSupported)
212+
case *settings.FreeOnly && *settings.PremiumOnly:
213+
return fmt.Errorf("%w", ErrFreePremiumBothSet)
214+
default:
215+
return nil
216+
}
217+
}
218+
219+
func validateFeatureFilters(settings ServerSelection, vpnServiceProvider string) error {
220+
switch {
221+
case *settings.OwnedOnly && vpnServiceProvider != providers.Mullvad:
222+
return fmt.Errorf("%w", ErrOwnedOnlyNotSupported)
223+
case *settings.StreamOnly &&
224+
!helpers.IsOneOf(vpnServiceProvider, providers.Protonvpn, providers.VPNUnlimited):
225+
return fmt.Errorf("%w", ErrStreamOnlyNotSupported)
226+
case *settings.MultiHopOnly && vpnServiceProvider != providers.Surfshark:
227+
return fmt.Errorf("%w", ErrMultiHopOnlyNotSupported)
228+
case *settings.PortForwardOnly && vpnServiceProvider != providers.PrivateInternetAccess:
229+
// ProtonVPN also supports port forwarding, but on all their servers, so these
230+
// don't have the port forwarding boolean field. As a consequence, we only allow
231+
// the use of PortForwardOnly for Private Internet Access.
232+
return fmt.Errorf("%w", ErrPortForwardOnlyNotSupported)
233+
default:
234+
return nil
235+
}
236+
}
237+
245238
func (ss *ServerSelection) copy() (copied ServerSelection) {
246239
return ServerSelection{
247240
VPN: ss.VPN,

0 commit comments

Comments
 (0)