Пример #1
0
 public static void Exit()
 {
     if (VPN.openvpnProcess != null && IsProcessRunning(VPN.openvpnProcess))
     {
         VPN.openvpnProcess.Kill();
     }
     VPN.InstallDrivers(false, null);
     Environment.Exit(1);
 }
Пример #2
0
 static void StartHosting(bool repair = false)
 {
     conf.gitLocalDir = Program.GetLocalDir(conf.gitLocalPath, false);
     Git.GitInit(conf.gitLocalDir, repair);
     conf.serverStartFile = GetLocalFile(conf.serverStartFilePath, false);
     VPN.Connect();
     Console.ForegroundColor = ConsoleColor.Cyan;
     Program.serverIp        = VPN.localIp;
     Console.WriteLine("The Server is running on: " + serverIp);
     Console.ResetColor();
     updateDatabaseTask = true;
     new Task(() => {
         while (updateDatabaseTask)
         {
             Database.Response dbResponse = Database.Response.unexpectedError;
             string responseStr           = Database.GetResponse(out dbResponse);
             Thread.Sleep(30000);
         }
     }).Start();
     RunProgram(conf.serverStartFile, "", null, true, null, true);
     updateDatabaseTask = false;
     if (Git.CommitAllChanges($"Update by {conf.gitUsername} at {DateTime.Now.ToString()}"))
     {
         Git.PushCommits(conf.gitRemotePath);
         Console.WriteLine("Project updated successfully");
     }
     {
         Database.Response dbResponse  = Database.Response.unexpectedError;
         string            responseStr = Database.GetResponse(out dbResponse, true);
         if (dbResponse == Database.Response.closedSuccessfully)
         {
             Console.WriteLine("Session closed successfully.");
         }
         else
         {
             ErrorExit("Could not close session. Please run Squadcraft again later.", false);
         }
     }
     Exit();
 }
Пример #3
0
        static void Main(string[] args)
        {
            mainDir  = GetLocalDir(AppDomain.CurrentDomain.BaseDirectory, false);
            confFile = new FileInfo(mainDir.FullName + @"conf\conf.json");
            if (confFile.Exists == false)
            {
                Configuration.CreateConfigFile(confFile.FullName);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Generated conf.json file in \\conf folder. Please configure it and run Squadcraft again.");
                Console.ReadLine();
                Exit();
            }
            conf = Configuration.LoadConfig(confFile.FullName);
            conf.Init();

            VPN.openVpnExeFile = GetLocalFile(mainDir.FullName + @"openvpn\openvpn.exe", false);

            Database.Response dbResponse         = Database.Response.unexpectedError;
            string            responseStr        = Database.GetResponse(out dbResponse);
            Action            postResponseAction = null;

            switch (dbResponse)
            {
            case Database.Response.hostedReadyToJoin:
                Console.WriteLine(responseStr.Split(':')[1].Substring(1) + responseStr.Split(':')[2]);
                serverIp            = responseStr.Split(':')[2];
                postResponseAction += () => {
                    VPN.Connect();
                    bool run = true;
                    Console.CancelKeyPress += (x, d) => run = false;
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine($"Server is hosted at: {serverIp}");
                    Console.ResetColor();
                    Console.WriteLine("Press escape to exit.");
                    while (Console.ReadKey(true).Key != ConsoleKey.Escape)
                    {
                    }
                    Console.WriteLine("Exitting...");
                    Exit();
                };
                break;

            case Database.Response.hostedInvalidKey:
                ErrorExit($"Your join key {conf.serverJoinSecretKey} is invalid!");
                break;

            case Database.Response.invalidHostKey:
                ErrorExit($"Your host key {conf.serverHostSecretKey} is invalid!\nMake sure you are allowed to host this server.");
                break;

            case Database.Response.readyToHost:
                Console.WriteLine("Server is ready to host...");
                postResponseAction += () => {
                    StartHosting();
                };
                break;

            case Database.Response.hostUnexpectedError:
                ErrorExit("Unexpected response from database.");
                break;

            case Database.Response.hostUpdate:
                Console.WriteLine("Server is ready to host...");
                postResponseAction += () => {
                    StartHosting();
                };
                break;

            case Database.Response.serverDoesNotExist:
                ErrorExit($"Server {conf.serverName} does not exist.");
                break;

            case Database.Response.unexpectedError:
                ErrorExit("Unexpected response from database.");
                break;

            case Database.Response.sessionNotClosedProperly:
                string previousHoster = responseStr.Split('*')[1].Split('*')[0];
                if (previousHoster == conf.gitUsername)
                {
                    ErrorExit("Previous session was not closed properly. Trying to fix that...", false);
                    postResponseAction += () => {
                        StartHosting(true);
                    };
                }
                else
                {
                    ErrorExit($"Previous session was not closed properly by {previousHoster}.\nHe must run Squadcraft again before you can continue.");
                }
                break;

            default:
                ErrorExit("Unexpected response from database.");
                break;
            }

            Thread.Sleep(1500);
            postResponseAction();
        }