public static bool EnsurePortsOpen(string exeName, int[] ports, string ruleName)
        {
            string rulesCommand = String.Format(@"
netsh advfirewall firewall delete rule all program = ""{0}""
netsh advfirewall firewall add rule name = ""{1} TCP"" action = allow program = ""{0}"" enable = yes localport={2} remoteport = any protocol = tcp dir = in
IF ERRORLEVEL 1 EXIT 1
netsh advfirewall firewall add rule name = ""{1} UDP"" action = allow program = ""{0}"" enable = yes localport={2} remoteport = any protocol = udp dir = in
IF ERRORLEVEL 1 EXIT 1
", exeName, ruleName, String.Join(",", ports));

            return(ScriptUtils.RunElevatedShellScript(nameof(EnsurePortsOpen), rulesCommand));
        }
Пример #2
0
        public static bool ScheduleUpdates(string updateKey, int autoUpdatePeriod, string cacheDir, string installDir, string rconIP, int rconPort, string rconPass, int graceMinutes, TimeSpan?forceRestartTime)
        {
            var schedulerKey      = $"ArkServerManager\\AutoUpgrade_{updateKey}";
            var forceSchedulerKey = $"ArkServerManager\\AutoUpgrade_Force_{updateKey}";

            var rootSrcPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var scriptPath  = Path.Combine(rootSrcPath, "Lib", "ServerUpdater", "AutoUpdateFromCache.ps1");

            var serverUpdateCmdPath = Path.Combine(installDir, "ShooterGame", "Saved", "Config", "WindowsServer", "UpdateServerFromCache.cmd");
            var logPath             = Path.Combine(installDir, "ShooterGame", "Saved", "Config", "WindowsServer", "UpdateServerFromCache.log");

            ScriptUtils.WriteCommandScript(serverUpdateCmdPath, $"powershell -ExecutionPolicy Bypass -File \"{scriptPath}\"  \"{cacheDir}\" \"{installDir}\" \"{rconIP}\" \"{rconPort}\" \"{rconPass}\" \"{graceMinutes}\" > \"{logPath}\"");

            string forceServerUpdateCmdPath = null;

            if (forceRestartTime.HasValue)
            {
                forceServerUpdateCmdPath = Path.Combine(installDir, "ShooterGame", "Saved", "Config", "WindowsServer", "ForceUpdateServerFromCache.cmd");
                var cmdBuilder = new StringBuilder();
                cmdBuilder.AppendLine($"echo force_update > {(installDir + @"\ForceUpdate.txt").AsQuoted()} ");
                cmdBuilder.AppendLine($"schTasks /Run /TN {schedulerKey}");
                ScriptUtils.WriteCommandScript(forceServerUpdateCmdPath, cmdBuilder.ToString());
            }

            PlaceMCRcon(cacheDir);

            var builder = new StringBuilder();

            builder.AppendLine($"schtasks /Delete /TN {schedulerKey} /F");
            builder.AppendLine($"schtasks /Delete /TN {forceSchedulerKey} /F");
            if (autoUpdatePeriod != 0)
            {
                builder.AppendLine($"schTasks /Create /TN {schedulerKey} /TR \"'{serverUpdateCmdPath}'\" /SC MINUTE /MO {autoUpdatePeriod} /NP /RL LIMITED ");
                builder.AppendLine("IF ERRORLEVEL 1 EXIT 1");

                if (forceRestartTime.HasValue)
                {
                    builder.AppendLine($"schTasks /Create /TN {forceSchedulerKey} /TR \"'{forceServerUpdateCmdPath}'\" /SC DAILY /ST {forceRestartTime.Value.Hours:D2}:{forceRestartTime.Value.Minutes:D2} /NP");
                    builder.AppendLine("IF ERRORLEVEL 1 EXIT 1");
                }
                builder.AppendLine($"schtasks /Run /TN {schedulerKey}");
                builder.AppendLine("IF ERRORLEVEL 1 EXIT 1");
            }

            builder.AppendLine("EXIT 0");
            var script = builder.ToString();

            return(ScriptUtils.RunElevatedShellScript(nameof(ScheduleUpdates), script));
        }
Пример #3
0
        public static bool EnsurePortsOpen(string exeName, int[] ports, string ruleName)
        {
            StringBuilder rulesCommand = new StringBuilder();

            rulesCommand.AppendFormat("netsh advfirewall firewall delete rule all program = \"{0}\"", exeName);
            rulesCommand.AppendLine();
            rulesCommand.AppendFormat("netsh advfirewall firewall add rule name = \"{0} TCP\" action = allow program = \"{1}\" enable = yes localport={2} remoteport = any protocol = tcp dir = in", ruleName, exeName, String.Join(",", ports));
            rulesCommand.AppendLine();
            rulesCommand.AppendFormat("IF ERRORLEVEL 1 EXIT 1");
            rulesCommand.AppendLine();
            rulesCommand.AppendFormat("netsh advfirewall firewall add rule name = \"{0} UDP\" action = allow program = \"{1}\" enable = yes localport={2} remoteport = any protocol = udp dir = in", ruleName, exeName, String.Join(",", ports));
            rulesCommand.AppendLine();
            rulesCommand.AppendFormat("IF ERRORLEVEL 1 EXIT 1");
            rulesCommand.AppendLine();

            return(ScriptUtils.RunElevatedShellScript(nameof(EnsurePortsOpen), rulesCommand.ToString()));
        }
Пример #4
0
        public static bool ScheduleAutoStart(string updateKey, bool enableAutoStart, string command, string args)
        {
            var schedulerKey = $"ArkServerManager\\AutoStart_{updateKey}";
            var builder      = new StringBuilder();

            builder.AppendLine($"schtasks /Delete /TN {schedulerKey} /F");
            if (enableAutoStart)
            {
                builder.AppendLine($"schtasks /Create /TN {schedulerKey} /TR \"'{command}' '{args}'\" /SC ONSTART /DELAY 0001:00 /NP /RL LIMITED");
                builder.AppendLine("IF ERRORLEVEL 1 EXIT 1");
            }

            builder.AppendLine("EXIT 0");
            var  script = builder.ToString();
            bool result = ScriptUtils.RunElevatedShellScript(nameof(ScheduleAutoStart), script);

            return(result);
        }
Пример #5
0
        public static void UpdateASM()
        {
            var applicationZip     = Path.Combine(Path.GetTempPath(), "ASMLatest.zip");
            var extractPath        = Path.Combine(Path.GetTempPath(), "ASMLatest");
            var updateFilePath     = Path.Combine(Path.GetTempPath(), "ASMUpdate.cmd");
            var currentInstallPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var backupPath         = currentInstallPath + "_bak";

            // Grab the latest bits
            using (var client = new WebClient())
            {
                if (App.Instance.BetaVersion)
                {
                    client.DownloadFile(Config.Default.LatestASMBetaDownloadUrl, applicationZip);
                }
                else
                {
                    client.DownloadFile(Config.Default.LatestASMDownloadUrl, applicationZip);
                }
                Unblock(applicationZip);
            }

            // Extract them
            try { Directory.Delete(extractPath, true); } catch { }
            ZipFile.ExtractToDirectory(applicationZip, extractPath);

            // Replace the current installation
            var script = new StringBuilder();

            script.AppendLine("timeout 2");
            script.AppendLine($"rmdir /s /q {backupPath.AsQuoted()}");
            script.AppendLine($"rename {currentInstallPath.AsQuoted()} {Path.GetFileName(backupPath).AsQuoted()}");
            script.AppendLine($"xcopy /e /y {(extractPath + "\\*.*").AsQuoted()} {(currentInstallPath + "\\").AsQuoted()}");
            script.AppendLine($"start \"\" {Assembly.GetExecutingAssembly().Location.AsQuoted()} {App.Instance.Args}");
            script.AppendLine("exit");

            ScriptUtils.RunShellScript(nameof(UpdateASM), script.ToString(), withElevation: false, waitForExit: false);

            Application.Current.Shutdown(0);
        }