Răsfoiți Sursa

refactor: ensure single OpenVPN process

Mustafa Arici 8 ani în urmă
părinte
comite
871dd1fced
2 a modificat fișierele cu 19 adăugiri și 4 ștergeri
  1. 1 0
      cmd/ovpmd/main.go
  2. 18 4
      vpn.go

+ 1 - 0
cmd/ovpmd/main.go

@@ -58,6 +58,7 @@ func main() {
 		pb.RegisterUserServiceServer(s, &api.UserService{})
 		pb.RegisterVPNServiceServer(s, &api.VPNService{})
 		logrus.Infof("OVPM is running :%s ...", port)
+		ovpm.RestartVPNProc()
 		s.Serve(lis)
 		return nil
 	}

+ 18 - 4
vpn.go

@@ -208,13 +208,19 @@ func GetSystemCA() (*pki.CA, error) {
 
 }
 
+// vpnProc represents the OpenVPN process that is managed by the ovpm supervisor globally OpenVPN.
+var vpnProc *supervisor.Process
+
 // RestartVPNProc restarts the OpenVPN process.
 func RestartVPNProc() {
-	p, err := supervisor.NewProcess(getOpenVPNExecutable(), varBasePath, []string{"--config", _DefaultVPNConfPath})
-	if err != nil {
-		logrus.Errorf("can not create process: %v", err)
+	if !IsInitialized() {
+		logrus.Error("can not launch OpenVPN because system is not initialized")
+		return
+	}
+	if vpnProc == nil {
+		panic(fmt.Sprintf("vpnProc is not initialized!"))
 	}
-	p.Restart()
+	vpnProc.Restart()
 }
 
 // Emit generates all needed files for the OpenVPN server and dumps them to their corresponding paths defined in the config.
@@ -533,3 +539,11 @@ func checkIptablesExecutable() bool {
 	logrus.Infof("iptables executable detected: %s  ✔", strings.TrimSpace(string(output[:])))
 	return true
 }
+
+func init() {
+	var err error
+	vpnProc, err = supervisor.NewProcess(getOpenVPNExecutable(), varBasePath, []string{"--config", _DefaultVPNConfPath})
+	if err != nil {
+		logrus.Errorf("can not create process: %v", err)
+	}
+}