fix(portforward): different validation when vpn is up or not
This commit is contained in:
@@ -85,7 +85,7 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{},
|
|||||||
// Stop call takes care of stopping the service
|
// Stop call takes care of stopping the service
|
||||||
return
|
return
|
||||||
case partialUpdate := <-updateTrigger:
|
case partialUpdate := <-updateTrigger:
|
||||||
updatedSettings, err := l.settings.updateWith(partialUpdate)
|
updatedSettings, err := l.settings.updateWith(partialUpdate, *l.settings.VPNIsUp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
updateResult <- err
|
updateResult <- err
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -34,17 +34,28 @@ func (s *Settings) OverrideWith(update Settings) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrServerNameNotSet = errors.New("server name not set")
|
ErrPortForwarderNotSet = errors.New("port forwarder not set")
|
||||||
ErrFilepathNotSet = errors.New("file path not set")
|
ErrServerNameNotSet = errors.New("server name not set")
|
||||||
ErrInterfaceNotSet = errors.New("interface not set")
|
ErrFilepathNotSet = errors.New("file path not set")
|
||||||
|
ErrInterfaceNotSet = errors.New("interface not set")
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Settings) Validate() (err error) {
|
func (s *Settings) Validate(forStartup bool) (err error) {
|
||||||
switch {
|
// Minimal validation
|
||||||
// Port forwarder can be nil when the loop updates
|
if s.Filepath == "" {
|
||||||
// to stop the service.
|
|
||||||
case s.Filepath == "":
|
|
||||||
return fmt.Errorf("%w", ErrFilepathNotSet)
|
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 == "":
|
case s.Interface == "":
|
||||||
return fmt.Errorf("%w", ErrInterfaceNotSet)
|
return fmt.Errorf("%w", ErrInterfaceNotSet)
|
||||||
case s.PortForwarder.Name() == providers.PrivateInternetAccess && s.ServerName == "":
|
case s.PortForwarder.Name() == providers.PrivateInternetAccess && s.ServerName == "":
|
||||||
|
|||||||
@@ -17,10 +17,11 @@ type Settings struct {
|
|||||||
// fields set in the partialUpdate argument, validates the new settings
|
// fields set in the partialUpdate argument, validates the new settings
|
||||||
// and returns them if they are valid, or returns an error otherwise.
|
// and returns them if they are valid, or returns an error otherwise.
|
||||||
// In all cases, the receiving settings are unmodified.
|
// 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 = s.copy()
|
||||||
updated.overrideWith(partialUpdate)
|
updated.overrideWith(partialUpdate)
|
||||||
err = updated.validate()
|
err = updated.validate(forStartup)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return updated, err
|
return updated, err
|
||||||
}
|
}
|
||||||
@@ -38,6 +39,6 @@ func (s *Settings) overrideWith(update Settings) {
|
|||||||
s.Service.OverrideWith(update.Service)
|
s.Service.OverrideWith(update.Service)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Settings) validate() (err error) {
|
func (s Settings) validate(forStartup bool) (err error) {
|
||||||
return s.Service.Validate()
|
return s.Service.Validate(forStartup)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user