fix(portforward): different validation when vpn is up or not

This commit is contained in:
Quentin McGaw
2023-10-07 12:43:36 +00:00
parent ee413f59a2
commit ec1f252528
3 changed files with 25 additions and 13 deletions

View File

@@ -85,7 +85,7 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{},
// Stop call takes care of stopping the service
return
case partialUpdate := <-updateTrigger:
updatedSettings, err := l.settings.updateWith(partialUpdate)
updatedSettings, err := l.settings.updateWith(partialUpdate, *l.settings.VPNIsUp)
if err != nil {
updateResult <- err
continue

View File

@@ -34,17 +34,28 @@ func (s *Settings) OverrideWith(update Settings) {
}
var (
ErrServerNameNotSet = errors.New("server name not set")
ErrFilepathNotSet = errors.New("file path not set")
ErrInterfaceNotSet = errors.New("interface not set")
ErrPortForwarderNotSet = errors.New("port forwarder not set")
ErrServerNameNotSet = errors.New("server name not set")
ErrFilepathNotSet = errors.New("file path not set")
ErrInterfaceNotSet = errors.New("interface not set")
)
func (s *Settings) Validate() (err error) {
switch {
// Port forwarder can be nil when the loop updates
// to stop the service.
case s.Filepath == "":
func (s *Settings) Validate(forStartup bool) (err error) {
// Minimal validation
if s.Filepath == "" {
return fmt.Errorf("%w", ErrFilepathNotSet)
}
if !forStartup {
// No additional validation needed if the service
// is not to be started with the given settings.
return nil
}
// Startup validation requires additional fields set.
switch {
case s.PortForwarder == nil:
return fmt.Errorf("%w", ErrPortForwarderNotSet)
case s.Interface == "":
return fmt.Errorf("%w", ErrInterfaceNotSet)
case s.PortForwarder.Name() == providers.PrivateInternetAccess && s.ServerName == "":

View File

@@ -17,10 +17,11 @@ type Settings struct {
// fields set in the partialUpdate argument, validates the new settings
// and returns them if they are valid, or returns an error otherwise.
// In all cases, the receiving settings are unmodified.
func (s Settings) updateWith(partialUpdate Settings) (updated Settings, err error) {
func (s Settings) updateWith(partialUpdate Settings,
forStartup bool) (updated Settings, err error) {
updated = s.copy()
updated.overrideWith(partialUpdate)
err = updated.validate()
err = updated.validate(forStartup)
if err != nil {
return updated, err
}
@@ -38,6 +39,6 @@ func (s *Settings) overrideWith(update Settings) {
s.Service.OverrideWith(update.Service)
}
func (s Settings) validate() (err error) {
return s.Service.Validate()
func (s Settings) validate(forStartup bool) (err error) {
return s.Service.Validate(forStartup)
}