chore(settings): split openvpn validation in functions
This commit is contained in:
@@ -91,51 +91,21 @@ func (o OpenVPN) validate(vpnProvider string) (err error) {
|
|||||||
return ErrOpenVPNPasswordIsEmpty
|
return ErrOpenVPNPasswordIsEmpty
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate ConfFile
|
err = validateOpenVPNConfigFilepath(isCustom, *o.ConfFile)
|
||||||
if isCustom {
|
|
||||||
if *o.ConfFile == "" {
|
|
||||||
return fmt.Errorf("%w: no file path specified", ErrOpenVPNConfigFile)
|
|
||||||
}
|
|
||||||
err := helpers.FileExists(*o.ConfFile)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%w: %s", ErrOpenVPNConfigFile, err)
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check client certificate
|
err = validateOpenVPNClientCertificate(vpnProvider, *o.ClientCrt)
|
||||||
switch vpnProvider {
|
|
||||||
case
|
|
||||||
constants.Cyberghost,
|
|
||||||
constants.VPNUnlimited:
|
|
||||||
if *o.ClientCrt == "" {
|
|
||||||
return ErrOpenVPNClientCertMissing
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if *o.ClientCrt != "" {
|
|
||||||
_, err = parse.ExtractCert([]byte(*o.ClientCrt))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%w: %s", ErrOpenVPNClientCertNotValid, err)
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check client key
|
err = validateOpenVPNClientKey(vpnProvider, *o.ClientKey)
|
||||||
switch vpnProvider {
|
|
||||||
case
|
|
||||||
constants.Cyberghost,
|
|
||||||
constants.VPNUnlimited,
|
|
||||||
constants.Wevpn:
|
|
||||||
if *o.ClientKey == "" {
|
|
||||||
return ErrOpenVPNClientKeyMissing
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if *o.ClientKey != "" {
|
|
||||||
_, err = parse.ExtractPrivateKey([]byte(*o.ClientKey))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%w: %s", ErrOpenVPNClientKeyNotValid, err)
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate MSSFix
|
|
||||||
const maxMSSFix = 10000
|
const maxMSSFix = 10000
|
||||||
if *o.MSSFix > maxMSSFix {
|
if *o.MSSFix > maxMSSFix {
|
||||||
return fmt.Errorf("%w: %d is over the maximum value of %d",
|
return fmt.Errorf("%w: %d is over the maximum value of %d",
|
||||||
@@ -147,7 +117,6 @@ func (o OpenVPN) validate(vpnProvider string) (err error) {
|
|||||||
ErrOpenVPNInterfaceNotValid, o.Interface, regexpInterfaceName)
|
ErrOpenVPNInterfaceNotValid, o.Interface, regexpInterfaceName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate Verbosity
|
|
||||||
if *o.Verbosity < 0 || *o.Verbosity > 6 {
|
if *o.Verbosity < 0 || *o.Verbosity > 6 {
|
||||||
return fmt.Errorf("%w: %d can only be between 0 and 5",
|
return fmt.Errorf("%w: %d can only be between 0 and 5",
|
||||||
ErrOpenVPNVerbosityIsOutOfBounds, o.Verbosity)
|
ErrOpenVPNVerbosityIsOutOfBounds, o.Verbosity)
|
||||||
@@ -156,6 +125,68 @@ func (o OpenVPN) validate(vpnProvider string) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateOpenVPNConfigFilepath(isCustom bool,
|
||||||
|
confFile string) (err error) {
|
||||||
|
if !isCustom {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if confFile == "" {
|
||||||
|
return fmt.Errorf("%w: no file path specified", ErrOpenVPNConfigFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = helpers.FileExists(confFile)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%w: %s", ErrOpenVPNConfigFile, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateOpenVPNClientCertificate(vpnProvider,
|
||||||
|
clientCert string) (err error) {
|
||||||
|
switch vpnProvider {
|
||||||
|
case
|
||||||
|
constants.Cyberghost,
|
||||||
|
constants.VPNUnlimited:
|
||||||
|
if clientCert == "" {
|
||||||
|
return ErrOpenVPNClientCertMissing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if clientCert == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = parse.ExtractCert([]byte(clientCert))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%w: %s", ErrOpenVPNClientCertNotValid, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateOpenVPNClientKey(vpnProvider, clientKey string) (err error) {
|
||||||
|
switch vpnProvider {
|
||||||
|
case
|
||||||
|
constants.Cyberghost,
|
||||||
|
constants.VPNUnlimited,
|
||||||
|
constants.Wevpn:
|
||||||
|
if clientKey == "" {
|
||||||
|
return ErrOpenVPNClientKeyMissing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if clientKey == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = parse.ExtractPrivateKey([]byte(clientKey))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%w: %s", ErrOpenVPNClientKeyNotValid, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (o *OpenVPN) copy() (copied OpenVPN) {
|
func (o *OpenVPN) copy() (copied OpenVPN) {
|
||||||
return OpenVPN{
|
return OpenVPN{
|
||||||
Version: o.Version,
|
Version: o.Version,
|
||||||
|
|||||||
Reference in New Issue
Block a user