Beispiel #1
0
        public async Task <IActionResult> StopAndTakeAway(string apiKey, int sshServerId, int pavlovServerId)
        {
            if (!HasAccess(apiKey))
            {
                return(BadRequest("No AuthKey set or wrong auth key!"));
            }
            var sshServer = await _sshServerSerivce.FindOne(sshServerId);

            var pavlovServer = await _pavlovServerService.FindOne(pavlovServerId);

            if (sshServer == null)
            {
                return(BadRequest("The ssh server does not exist!"));
            }
            if (!sshServer.IsForHosting)
            {
                return(BadRequest("The ssh server ist not for hosting!"));
            }
            await RconStatic.SystemDStop(pavlovServer, _pavlovServerService);

            pavlovServer.OldOwner = pavlovServer.Owner;
            pavlovServer.Owner    = null;
            await _pavlovServerService.Upsert(pavlovServer);

            return(Ok());
        }
        private async Task <PavlovServer> HasToStop(PavlovServer pavlovServer, bool hasToStop, bool root)
        {
            if (hasToStop)
            {
                DataBaseLogger.LogToDatabaseAndResultPlusNotify("stop server again!", LogEventLevel.Verbose,
                                                                _notifyService);
                await RconStatic.SystemDStop(pavlovServer, this);

                pavlovServer = await GetServerServiceState(pavlovServer);
            }

            return(pavlovServer);
        }
        public async Task <IActionResult> StopSystemdService(int serverId)
        {
            if (!await RightsHandler.HasRightsToThisPavlovServer(HttpContext.User,
                                                                 await _userservice.getUserFromCp(HttpContext.User), serverId, _service, _pavlovServerService))
            {
                return(Forbid());
            }
            var server = await _pavlovServerService.FindOne(serverId);

            try
            {
                await RconStatic.SystemDStop(server, _pavlovServerService);
            }
            catch (CommandException e)
            {
                return(BadRequest(e.Message));
            }

            return(RedirectToAction("Index", "SshServer"));
        }
        public async Task RemovePavlovServerFromDisk(
            PavlovServerViewModel server, bool ignoreMostExceptions)
        {
            //Todo AuthError??
            DataBaseLogger.LogToDatabaseAndResultPlusNotify("Start remove server!", LogEventLevel.Verbose,
                                                            _notifyService);
            string result = "";

            //start server and stop server to get Saved folder etc.
            if (ignoreMostExceptions)
            {
                try
                {
                    await RconStatic.SystemDStop(server, _pavlovServerService);
                }
                catch (CommandException)
                {
                    //ignore
                }
            }
            else
            {
                await RconStatic.SystemDStop(server, _pavlovServerService);
            }

            if (ignoreMostExceptions)
            {
                try
                {
                    await RconStatic.RemovePath(server, server.ServerFolderPath, _pavlovServerService);
                }
                catch (CommandException)
                {
                    //ignore
                }
            }
            else
            {
                await RconStatic.RemovePath(server, server.ServerFolderPath, _pavlovServerService);
            }


            server.SshServer = await FindOne(server.sshServerId);

            if (server.SshServer == null)
            {
                throw new CommandException("Could not get the sshServer!");
            }
            var oldSSHcrid = new SshServer
            {
                SshPassphrase  = server.SshServer.SshPassphrase,
                SshUsername    = server.SshServer.SshUsername,
                SshPassword    = server.SshServer.SshPassword,
                SshKeyFileName = server.SshServer.SshKeyFileName
            };

            server.SshServer.SshPassphrase = server.SshPassphraseRoot;
            server.SshServer.SshUsername   = server.SshUsernameRoot;
            server.SshServer.SshPassword   = server.SshPasswordRoot;
            if (server.SshKeyFileNameForm != null)
            {
                await using var ms = new MemoryStream();
                await server.SshKeyFileNameForm.CopyToAsync(ms);

                var fileBytes = ms.ToArray();
                server.SshKeyFileNameRoot = fileBytes;
                // act on the Base64 data
            }
            server.SshServer.SshKeyFileName     = server.SshKeyFileNameRoot;
            server.SshServer.NotRootSshUsername = oldSSHcrid.SshUsername;


            if (ignoreMostExceptions)
            {
                try
                {
                    await RconStatic.RemovePath(server,
                                                "/etc/systemd/system/" + server.ServerSystemdServiceName + ".service", _pavlovServerService);
                }
                catch (CommandException)
                {
                    //ignore
                }
            }
            else
            {
                await RconStatic.RemovePath(server,
                                            "/etc/systemd/system/" + server.ServerSystemdServiceName + ".service", _pavlovServerService);
            }

            //Remove the server from the sudoers file
            var sudoersPathParent = "/etc/sudoers.d";
            var sudoersPath       = sudoersPathParent + "/pavlovRconWebserverManagement";
            var removed           = true;


            if (ignoreMostExceptions)
            {
                try
                {
                    removed = RconStatic.RemoveServerLineToSudoersFile(server, _notifyService, sudoersPath, _pavlovServerService);
                }
                catch (CommandException)
                {
                    //ignore
                }
            }
            else
            {
                removed = RconStatic.RemoveServerLineToSudoersFile(server, _notifyService, sudoersPath, _pavlovServerService);
            }
            if (removed)
            {
                DataBaseLogger.LogToDatabaseAndResultPlusNotify("server line removed from sudoers file!",
                                                                LogEventLevel.Verbose, _notifyService);
            }
            else
            {
                //means that the lines still is in the file!!!
                var error =
                    "Could not remove the server line from sudoers file!";
                DataBaseLogger.LogToDatabaseAndResultPlusNotify(error, LogEventLevel.Fatal, _notifyService);
                if (!ignoreMostExceptions)
                {
                    throw new CommandException(error);
                }
            }

            //Handle the presets an newer systemd
            if (ignoreMostExceptions)
            {
                try
                {
                    RconStatic.AddLineToNewerSystemDsIfNeeded(server, _notifyService, true);
                }
                catch (CommandException)
                {
                    //ignore
                }
            }
            else
            {
                RconStatic.AddLineToNewerSystemDsIfNeeded(server, _notifyService, true);
            }



            DataBaseLogger.LogToDatabaseAndResultPlusNotify(result, LogEventLevel.Verbose, _notifyService);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="server"></param>
        /// <param name="reservedFor"></param>
        /// <exception cref="CommandExceptionCreateServerDuplicate"></exception>

        public async Task CreatePavlovServer(PavlovServerViewModel server, string reservedFor = "")
        {
            DataBaseLogger.LogToDatabaseAndResultPlusNotify("Start creting server", LogEventLevel.Verbose,
                                                            _notifyService);
            //Check stuff
            var exist = RconStatic.DoesPathExist(server, server.ServerFolderPath, _notifyService);

            if (exist)
            {
                throw new CommandExceptionCreateServerDuplicate("ServerFolderPath already exist!");
            }

            exist = RconStatic.DoesPathExist(server,
                                             "/etc/systemd/system/" + server.ServerSystemdServiceName + ".service", _notifyService);
            if (exist)
            {
                throw new CommandExceptionCreateServerDuplicate("Systemd Service already exist!");
            }

            var portsUsed = (await FindAll()).Where(x => x.SshServer.Id == server.SshServer.Id)
                            .FirstOrDefault(x => x.ServerPort == server.ServerPort || x.TelnetPort == server.TelnetPort);

            if (portsUsed != null)
            {
                if (portsUsed.ServerPort == server.ServerPort)
                {
                    throw new CommandExceptionCreateServerDuplicate("The server port is already used!");
                }

                if (portsUsed.TelnetPort == server.TelnetPort)
                {
                    throw new CommandExceptionCreateServerDuplicate("The telnet port is already used!");
                }
            }


            DataBaseLogger.LogToDatabaseAndResultPlusNotify("Start Install pavlovserver", LogEventLevel.Verbose,
                                                            _notifyService);

            DataBaseLogger.LogToDatabaseAndResultPlusNotify(
                "Username used befor changed to root: " + server.SshServer.SshUsername, LogEventLevel.Verbose,
                _notifyService);

            await RconStatic.UpdateInstallPavlovServer(server, this);


            DataBaseLogger.LogToDatabaseAndResultPlusNotify("Start Install pavlovserver service",
                                                            LogEventLevel.Verbose, _notifyService);

            var oldSSHcrid = new SshServer
            {
                SshPassphrase  = server.SshServer.SshPassphrase,
                SshUsername    = server.SshServer.SshUsername,
                SshPassword    = server.SshServer.SshPassword,
                SshKeyFileName = server.SshServer.SshKeyFileName
            };

            server.SshServer.SshPassphrase      = server.SshPassphraseRoot;
            server.SshServer.SshUsername        = server.SshUsernameRoot;
            server.SshServer.SshPassword        = server.SshPasswordRoot;
            server.SshServer.SshKeyFileName     = server.SshKeyFileNameRoot;
            server.SshServer.NotRootSshUsername = oldSSHcrid.SshUsername;

            try
            {
                await RconStatic.InstallPavlovServerService(server, _notifyService);
            }
            catch (CommandException e)
            {
                //If crash inside here the user login is still root. If the root login is bad this will fail to remove the server afterwards
                OverwrideTheNormalSSHLoginData(server, oldSSHcrid);
                DataBaseLogger.LogToDatabaseAndResultPlusNotify(
                    "Could not install service while creating the pavlov server -> " + e.Message, LogEventLevel.Verbose, _notifyService);
                throw;
            }
            OverwrideTheNormalSSHLoginData(server, oldSSHcrid);
            DataBaseLogger.LogToDatabaseAndResultPlusNotify(
                "Username used after override old infos: " + server.SshServer.SshUsername, LogEventLevel.Verbose,
                _notifyService);
            //start server and stop server to get Saved folder etc.

            DataBaseLogger.LogToDatabaseAndResultPlusNotify("Start after install", LogEventLevel.Verbose,
                                                            _notifyService);

            await RconStatic.SystemDStart(server, this);

            DataBaseLogger.LogToDatabaseAndResultPlusNotify("stop after install", LogEventLevel.Verbose,
                                                            _notifyService);

            await RconStatic.SystemDStop(server, this);

            DataBaseLogger.LogToDatabaseAndResultPlusNotify("Try to save game ini", LogEventLevel.Verbose,
                                                            _notifyService);



            var pavlovServerGameIni = new PavlovServerGameIni();

            DataBaseLogger.LogToDatabaseAndResultPlusNotify("created Ini", LogEventLevel.Verbose, _notifyService);
            var selectedMaps = await _serverSelectedMapService.FindAllFrom(server);

            DataBaseLogger.LogToDatabaseAndResultPlusNotify("found maps", LogEventLevel.Verbose, _notifyService);
            pavlovServerGameIni.ServerName = server.Name;


            pavlovServerGameIni.SaveToFile(server, selectedMaps, _notifyService);

            //also create rcon settings


            DataBaseLogger.LogToDatabaseAndResultPlusNotify("write rcon file", LogEventLevel.Verbose,
                                                            _notifyService);

            var lines = new List <string>
            {
                "Password="******"Port=" + server.TelnetPort
            };

            RconStatic.WriteFile(server.SshServer, server.ServerFolderPath + FilePaths.RconSettings,
                                 lines.ToArray(), _notifyService);
        }