Support for username and password changes
package openvpn
import (
+ "strings"
+
"github.com/qdm12/golibs/files"
"github.com/qdm12/private-internet-access-docker/internal/constants"
)
// WriteAuthFile writes the OpenVPN auth file to disk with the right permissions
func (c *configurator) WriteAuthFile(user, password string, uid, gid int) error {
- authExists, err := c.fileManager.FileExists(string(constants.OpenVPNAuthConf))
+ exists, err := c.fileManager.FileExists(string(constants.OpenVPNAuthConf))
+ if err != nil {
+ return err
+ } else if exists {
+ data, err := c.fileManager.ReadFile(string(constants.OpenVPNAuthConf))
if err != nil {
return err
- } else if authExists { // in case of container stop/start
- c.logger.Info("%s already exists", constants.OpenVPNAuthConf)
+ }
+ lines := strings.Split(string(data), "\n")
+ if len(lines) > 1 && lines[0] == user && lines[1] == password {
return nil
}
- c.logger.Info("writing auth file %s", constants.OpenVPNAuthConf)
+ c.logger.Info("username and password changed", constants.OpenVPNAuthConf)
+ }
return c.fileManager.WriteLinesToFile(
string(constants.OpenVPNAuthConf),
[]string{user, password},
