Przeglądaj źródła

refactor: improve api

Mustafa Arici 8 lat temu
rodzic
commit
cb1b32f0de
3 zmienionych plików z 38 dodań i 9 usunięć
  1. 4 0
      const.go
  2. 4 3
      user.go
  3. 30 6
      vpn.go

+ 4 - 0
const.go

@@ -24,3 +24,7 @@ const (
 	_DefaultServerNetwork = "10.9.0.0"
 	_DefaultServerNetMask = "255.255.255.0"
 )
+
+// Testing is used to determine wether we are testing or running normally.
+// Set it to true when testing.
+var Testing = false

+ 4 - 3
user.go

@@ -42,7 +42,8 @@ func (u *DBUser) setPassword(password string) error {
 	return nil
 }
 
-func (u *DBUser) checkPassword(password string) bool {
+// CheckPassword returns wether the given password is correct for the user.
+func (u *DBUser) CheckPassword(password string) bool {
 	return u.Password == password
 }
 
@@ -152,11 +153,11 @@ func (u *DBUser) ResetPassword(password string) error {
 	return nil
 }
 
-// Sign creates a key and a ceritificate signed by the current server's CA.
+// Renew creates a key and a ceritificate signed by the current server's CA.
 //
 // This is often used to sign users when the current CA is changed while there are
 // still  existing users in the database.
-func (u *DBUser) Sign() error {
+func (u *DBUser) Renew() error {
 	if !IsInitialized() {
 		return fmt.Errorf("you first need to create server")
 	}

+ 30 - 6
vpn.go

@@ -124,7 +124,7 @@ func Init(hostname string, port string) error {
 	}
 	// Sign all users in the db with the new server
 	for _, user := range users {
-		err := user.Sign()
+		err := user.Renew()
 		logrus.Infof("user certificate changed for %s, you should run: $ ovpm user export-config --user %s", user.Username, user.Username)
 		if err != nil {
 			logrus.Errorf("can not sign user %s: %v", user.Username, err)
@@ -220,6 +220,23 @@ func GetSystemCA() (*pki.CA, error) {
 // vpnProc represents the OpenVPN process that is managed by the ovpm supervisor globally OpenVPN.
 var vpnProc *supervisor.Process
 
+// StartVPNProc starts the OpenVPN process.
+func StartVPNProc() {
+	if !IsInitialized() {
+		logrus.Error("can not launch OpenVPN because system is not initialized")
+		return
+	}
+	if vpnProc == nil {
+		panic(fmt.Sprintf("vpnProc is not initialized!"))
+	}
+	if vpnProc.IsRunning() {
+		logrus.Error("OpenVPN is already started")
+		return
+	}
+
+	vpnProc.Start()
+}
+
 // RestartVPNProc restarts the OpenVPN process.
 func RestartVPNProc() {
 	if !IsInitialized() {
@@ -234,13 +251,13 @@ func RestartVPNProc() {
 
 // StopVPNProc stops the OpenVPN process.
 func StopVPNProc() {
+	if vpnProc == nil {
+		panic(fmt.Sprintf("vpnProc is not initialized!"))
+	}
 	if !vpnProc.IsRunning() {
 		logrus.Error("OpenVPN is already stopped")
 		return
 	}
-	if vpnProc == nil {
-		panic(fmt.Sprintf("vpnProc is not initialized!"))
-	}
 	vpnProc.Stop()
 }
 
@@ -302,13 +319,20 @@ func Emit() error {
 
 	logrus.Info("configurations emitted to the filesystem")
 
-	RestartVPNProc()
-	logrus.Info("OpenVPN process is restarting")
+	// If the OpenVPN is already running, restart it.
+	if vpnProc.IsRunning() {
+		logrus.Info("OpenVPN process is restarting")
+		RestartVPNProc()
+	}
 
 	return nil
 }
 
 func emitToFile(path, content string, mode uint) error {
+	// When testing don't emit files to the filesystem. Just pretend you did.
+	if Testing {
+		return nil
+	}
 	file, err := os.Create(path)
 	if err != nil {
 		return fmt.Errorf("Cannot create file %s: %v", path, err)