Esempio n. 1
0
        //Initial Set-up for first run only
        public static void FirstRun()
        {
            //Grab latest server jar
            YAMS.AutoUpdate.UpdateIfNeeded(YAMS.AutoUpdate.strMCServerURL, YAMS.Core.RootFolder + @"\lib\minecraft_server.jar.UPDATE");

            //Set our MC Defaults in the DB
            var NewServer = new List <KeyValuePair <string, string> >();

            NewServer.Add(new KeyValuePair <string, string>("admin-slot", "true"));
            NewServer.Add(new KeyValuePair <string, string>("enable-health", "true"));
            NewServer.Add(new KeyValuePair <string, string>("hellworld", "false"));
            NewServer.Add(new KeyValuePair <string, string>("level-name", @"world"));
            NewServer.Add(new KeyValuePair <string, string>("max-players", "20"));
            NewServer.Add(new KeyValuePair <string, string>("motd", "Welcome to a YAMS server!"));
            NewServer.Add(new KeyValuePair <string, string>("online-mode", "true"));
            NewServer.Add(new KeyValuePair <string, string>("public", "false"));
            NewServer.Add(new KeyValuePair <string, string>("pvp", "true"));
            NewServer.Add(new KeyValuePair <string, string>("server-ip", ""));
            NewServer.Add(new KeyValuePair <string, string>("server-name", "My YAMS MC Server"));
            NewServer.Add(new KeyValuePair <string, string>("server-port", "25565"));
            NewServer.Add(new KeyValuePair <string, string>("spawn-animals", "true"));
            NewServer.Add(new KeyValuePair <string, string>("spawn-monsters", "true"));
            NewServer.Add(new KeyValuePair <string, string>("verify-names", "true"));
            NewServer.Add(new KeyValuePair <string, string>("white-list", "false"));
            Database.NewServer(NewServer, "My First YAMS Server");

            //Set our YAMS Defaults
            Database.SaveSetting("UpdateJAR", "true");
            Database.SaveSetting("UpdateSVC", "true");
            Database.SaveSetting("UpdateGUI", "true");
            Database.SaveSetting("UpdateWeb", "true");
            Database.SaveSetting("UpdateAddons", "true");
            Database.SaveSetting("RestartOnJarUpdate", "true");
            Database.SaveSetting("RestartOnSVCUpdate", "true");
            Database.SaveSetting("Memory", "1024");
            Database.SaveSetting("EnableJavaOptimisations", "true");
            Database.SaveSetting("AdminListenPort", "56552");                                                         //Use an IANA legal internal port 49152 - 65535
            Database.SaveSetting("PublicListenPort", Convert.ToString(Networking.TcpPort.FindNextAvailablePort(80))); //Find nearest open port to 80 for public site
            Database.SaveSetting("ExternalIP", Networking.GetExternalIP().ToString());
            Database.SaveSetting("ListenIP", Networking.GetListenIP().ToString());
            Database.SaveSetting("UpdateBranch", "live");

            //Run an update now
            AutoUpdate.CheckUpdates();

            //Tell the DB that we've run this
            Database.SaveSetting("FirstRun", "true");
        }
Esempio n. 2
0
        private void ServerError(object sender, DataReceivedEventArgs e)
        {
            DateTime datTimeStamp = DateTime.Now;

            //Catch null messages (usually as server is going down)
            if (e.Data == null || e.Data == ">")
            {
                return;
            }

            //MC's server seems to use stderr for things that aren't really errors, so we need some logic to catch that.
            string strLevel   = "info";
            string strMessage = e.Data;

            //Strip out date and time info
            strMessage = this.regRemoveDateStamp.Replace(strMessage, "");
            strMessage = this.regRemoveTimeStamp.Replace(strMessage, "");

            //Work out the error level then remove it from the string
            Match regMatch = this.regErrorLevel.Match(strMessage);

            strMessage = this.regErrorLevel.Replace(strMessage, "").Trim();

            if (regMatch.Success)
            {
                switch (regMatch.Groups[1].Value)
                {
                case "INFO":
                    //Check if it's player chat
                    if (regPlayerChat.Match(strMessage).Success || regPlayerPM.Match(strMessage).Success || regConsoleChat.Match(strMessage).Success)
                    {
                        strLevel = "chat";
                    }
                    else
                    {
                        strLevel = "info";
                    }

                    //See if it's a log in or log out event
                    if (regPlayerLoggedIn.Match(strMessage).Success)
                    {
                        this.PlayerLogin(regPlayerLoggedIn.Match(strMessage).Groups[1].Value);                                                  //is a login event
                    }
                    if (regPlayerLoggedOut.Match(strMessage).Success)
                    {
                        this.PlayerLogout(regPlayerLoggedOut.Match(strMessage).Groups[1].Value);                                                   //logout event
                    }
                    //See if it's the server version tag
                    if (regServerVersion.Match(strMessage).Success)
                    {
                        this.ServerVersion = strMessage.Replace("Starting minecraft server version ", "");
                    }

                    //Detect game type
                    if (regGameMode.Match(strMessage).Success)
                    {
                        this.GameMode = Convert.ToInt32(regGameMode.Match(strMessage).Groups[1].Value);
                    }
                    break;

                case "WARNING":
                    strLevel = "warn";
                    break;

                default:
                    strLevel = "error";
                    break;
                }
            }
            else
            {
                strLevel = "error";
            }

            if (strMessage.IndexOf("Invalid or corrupt jarfile ") > -1)
            {
                //We have downloaded a corrupt jar, clear the download cache and force a re-download now
                this.SafeStop = true;
                AutoUpdate.CheckUpdates(true);
                this.Start();
            }

            Database.AddLog(datTimeStamp, strMessage, "server", strLevel, false, this.ServerID);
            if (Database.GetSetting("EnableTelnet", "YAMS") == "true")
            {
                TelnetServer.SendMessage(e.Data, this.ServerID);
            }
        }
Esempio n. 3
0
        //Control
        public static void Init()
        {
            //See if there is a new version of the web files waiting before we start the server
            if (File.Exists(Core.RootFolder + @"\web.zip"))
            {
                if (Directory.Exists(Core.RootFolder + @"\web\"))
                {
                    Directory.Delete(Core.RootFolder + @"\web\", true);
                }
                Directory.CreateDirectory(YAMS.Core.RootFolder + @"\web\");
                AutoUpdate.ExtractZip(YAMS.Core.RootFolder + @"\web.zip", YAMS.Core.RootFolder + @"\web\");
                File.Delete(Core.RootFolder + @"\web.zip");
            }

            adminServer  = new Server();
            publicServer = new Server();

            //Handle the requests for static files
            var adminModule = new FileModule();

            adminModule.Resources.Add(new FileResources("/assets/", YAMS.Core.RootFolder + "\\web\\assets\\"));
            adminServer.Add(adminModule);

            //Add any server specific folders
            var publicModule = new FileModule();

            publicModule.Resources.Add(new FileResources("/assets/", YAMS.Core.RootFolder + "\\web\\assets\\"));
            SqlCeDataReader readerServers = YAMS.Database.GetServers();

            while (readerServers.Read())
            {
                var intServerID = readerServers["ServerID"].ToString();
                if (!Directory.Exists(Core.StoragePath + intServerID + "\\renders\\"))
                {
                    Directory.CreateDirectory(Core.StoragePath + intServerID + "\\renders\\");
                }
                publicModule.Resources.Add(new FileResources("/servers/" + intServerID + "/renders/", Core.StoragePath + intServerID + "\\renders\\"));
                if (!Directory.Exists(Core.StoragePath + intServerID + "\\backups\\"))
                {
                    Directory.CreateDirectory(Core.StoragePath + intServerID + "\\backups\\");
                }
                publicModule.Resources.Add(new FileResources("/servers/" + intServerID + "/backups/", Core.StoragePath + intServerID + "\\backups\\"));
            }
            publicServer.Add(publicModule);

            //Handle requests to API
            adminServer.Add(new Web.AdminAPI());
            publicServer.Add(new Web.PublicAPI());

            adminServer.Add(HttpListener.Create(IPAddress.Any, Convert.ToInt32(YAMS.Database.GetSetting("AdminListenPort", "YAMS"))));
            publicServer.Add(HttpListener.Create(IPAddress.Any, Convert.ToInt32(YAMS.Database.GetSetting("PublicListenPort", "YAMS"))));

            adminServer.ErrorPageRequested  += new EventHandler <ErrorPageEventArgs>(myServer_ErrorPageRequested);
            publicServer.ErrorPageRequested += new EventHandler <ErrorPageEventArgs>(myServer_ErrorPageRequested);

            adminServerThread  = new Thread(new ThreadStart(StartAdmin));
            publicServerThread = new Thread(new ThreadStart(StartPublic));
            adminServerThread.Start();
            publicServerThread.Start();

            //Open firewall ports
            Networking.OpenFirewallPort(Convert.ToInt32(YAMS.Database.GetSetting("AdminListenPort", "YAMS")), "Admin website");
            Networking.OpenFirewallPort(Convert.ToInt32(YAMS.Database.GetSetting("PublicListenPort", "YAMS")), "Public website");

            Networking.OpenUPnP(Convert.ToInt32(YAMS.Database.GetSetting("AdminListenPort", "YAMS")), "Admin website");
            Networking.OpenUPnP(Convert.ToInt32(YAMS.Database.GetSetting("PublicListenPort", "YAMS")), "Public website");
        }
Esempio n. 4
0
 public static void timUpdate_Tick(object t)
 {
     AutoUpdate.CheckUpdates();
 }
Esempio n. 5
0
        public static void StartUp()
        {
            //Clear out old files if they exist, if it doesn't work we'll just do it on next startup.
            try { if (File.Exists(RootFolder + @"\YAMS-Library.dll.OLD"))
                  {
                      File.Delete(RootFolder + @"\YAMS-Library.dll.OLD");
                  }
            }
            catch { };
            try { if (File.Exists(RootFolder + @"\YAMS-Service.exe.OLD"))
                  {
                      File.Delete(RootFolder + @"\YAMS-Service.exe.OLD");
                  }
            }
            catch { };
            try { if (File.Exists(RootFolder + @"\YAMS-Service.exe.config.OLD"))
                  {
                      File.Delete(RootFolder + @"\YAMS-Service.exe.config.OLD");
                  }
            }
            catch { };

            //Start DB Connection
            Database.init();
            Database.AddLog("Starting Up");

            //Is this the first run?
            if (Database.GetSetting("FirstRun", "YAMS") != "true")
            {
                YAMS.Util.FirstRun();
            }

            //Fill up some vars
            AutoUpdate.bolUpdateAddons = Convert.ToBoolean(Database.GetSetting("UpdateAddons", "YAMS"));
            AutoUpdate.bolUpdateGUI    = Convert.ToBoolean(Database.GetSetting("UpdateGUI", "YAMS"));
            AutoUpdate.bolUpdateJAR    = Convert.ToBoolean(Database.GetSetting("UpdateJAR", "YAMS"));
            AutoUpdate.bolUpdateSVC    = Convert.ToBoolean(Database.GetSetting("UpdateSVC", "YAMS"));
            AutoUpdate.bolUpdateWeb    = Convert.ToBoolean(Database.GetSetting("UpdateWeb", "YAMS"));
            StoragePath = Database.GetSetting("StoragePath", "YAMS");

            //Are there any PIDs we previously started still running?
            if (File.Exists(Core.RootFolder + "\\pids.txt"))
            {
                try
                {
                    StreamReader trPids = new StreamReader(Core.RootFolder + "\\pids.txt");
                    string       line;
                    while ((line = trPids.ReadLine()) != null)
                    {
                        try
                        {
                            Process.GetProcessById(Convert.ToInt32(line)).Kill();
                        }
                        catch (Exception e)
                        {
                            Database.AddLog("Process " + line + " not killed: " + e.Message);
                        }
                    }

                    trPids.Close();
                }
                catch (Exception e)
                {
                    Database.AddLog("Not all processes killed: " + e.Message);
                }
                try
                {
                    File.Delete(Core.RootFolder + "\\pids.txt");
                }
                catch (Exception e)
                {
                    Database.AddLog("Unable to delete the pids.txt file: " + e.Message);
                }
            }
            ;

            //Check for updates
            AutoUpdate.CheckUpdates();

            //Load any servers
            SqlCeDataReader readerServers = YAMS.Database.GetServers();

            while (readerServers.Read())
            {
                Database.AddLog("Starting Server " + readerServers["ServerID"]);
                MCServer myServer = new MCServer(Convert.ToInt32(readerServers["ServerID"]));
                if (Convert.ToBoolean(readerServers["ServerAutostart"]))
                {
                    myServer.Start();
                }
                Servers.Add(Convert.ToInt32(readerServers["ServerID"]), myServer);
            }

            //Start job engine
            JobEngine.Init();

            //Start Webserver
            WebServer.Init();

            //Start Telnet Server
            if (Database.GetSetting("EnableTelnet", "YAMS") == "true")
            {
                TelnetServer.Init();
            }
        }
Esempio n. 6
0
        public static void Tick(object t)
        {
            DateTime datNow     = DateTime.Now;
            int      intMinutes = datNow.Minute;
            int      intHour    = datNow.Hour;

            //is it time to phone home?
            if (Database.GetSetting("UsageData", "YAMS") == "true" && intMinutes == 0)
            {
                Util.PhoneHome();
            }

            //Should we check DNS?
            if (intMinutes % 5 == 0 && Database.GetSetting("DNSName", "YAMS") != "")
            {
                Util.UpdateDNS();
            }

            //Get jobs for current minute
            SqlCeDataReader rdJobs = Database.GetJobs(intHour, intMinutes);

            MCServer s;

            while (rdJobs.Read())
            {
                try
                {
                    //Split up the parameters
                    string strParams = rdJobs["JobParams"].ToString();
                    Dictionary <string, string> jobParams = new Dictionary <string, string> {
                    };

                    if (strParams != "")
                    {
                        string[] arrKeys = strParams.Split('&');
                        foreach (string strKey in arrKeys)
                        {
                            string[] arrValues = strKey.Split('=');
                            if (arrValues.Length == 2)
                            {
                                jobParams.Add(arrValues[0], arrValues[1]);
                            }
                            else
                            {
                                Database.AddLog("Params failed on job. String was " + strParams, "job", "warn");
                            }
                        }
                    }

                    switch (rdJobs["JobAction"].ToString())
                    {
                    case "overviewer":
                        s = Core.Servers[Convert.ToInt32(rdJobs["JobServer"])];
                        AddOns.Overviewer gmap = new AddOns.Overviewer(s, rdJobs["JobParams"].ToString());
                        gmap.Start();
                        break;

                    case "c10t":
                        s = Core.Servers[Convert.ToInt32(rdJobs["JobServer"])];
                        AddOns.c10t c10t = new AddOns.c10t(s, rdJobs["JobParams"].ToString());
                        c10t.Start();
                        break;

                    case "biome-extractor":
                        s = Core.Servers[Convert.ToInt32(rdJobs["JobServer"])];
                        AddOns.BiomeExtractor extractor = new AddOns.BiomeExtractor(s, rdJobs["JobParams"].ToString());
                        extractor.Start();
                        break;

                    case "backup":
                        s = Core.Servers[Convert.ToInt32(rdJobs["JobServer"])];
                        Backup.BackupIfNeeded(s);
                        break;

                    case "update":
                        AutoUpdate.CheckUpdates();
                        break;

                    case "delayedrestart":
                        s = Core.Servers[Convert.ToInt32(rdJobs["JobServer"])];
                        s.DelayedRestart(Convert.ToInt32(rdJobs["JobParams"]));
                        break;

                    case "restartwhenfree":
                        s = Core.Servers[Convert.ToInt32(rdJobs["JobServer"])];
                        s.RestartIfEmpty();
                        break;

                    case "start":
                        s = Core.Servers[Convert.ToInt32(rdJobs["JobServer"])];
                        s.Start();
                        break;

                    case "stop":
                        s = Core.Servers[Convert.ToInt32(rdJobs["JobServer"])];
                        s.Stop();
                        break;

                    case "clearlogs":
                        Database.ClearLogs(jobParams["period"], Convert.ToInt32(jobParams["amount"]));
                        break;

                    case "clearbackups":
                        Backup.ClearBackups(Core.Servers[Convert.ToInt32(rdJobs["JobServer"])], jobParams["period"], Convert.ToInt32(jobParams["amount"]));
                        break;

                    case "command":
                        s = Core.Servers[Convert.ToInt32(rdJobs["JobServer"])];
                        s.Send(jobParams["command"]);
                        break;

                    default:
                        Database.AddLog("Invalid entry in Job database", "job", "warn");
                        break;
                    }
                } catch (Exception e) {
                    YAMS.Database.AddLog("Error running job: " + e.Message, "job", "error");
                }
            }
        }