/// <summary>Checks if an admin user present.</summary> /// <param name="authTables">The authentication tables.</param> public static User CheckAdminPresent(this AuthTables authTables) { var users = authTables.Users.ToArray(); var admin = users.Where(u => ((MDBUserLevel)u.AuthLevel).HasFlag(MDBUserLevel.Admin)); bool adminPresent = admin.Any(); if (adminPresent) { authTables.LogNotice("Admin Users: {0}", admin.Join(", ")); return(admin.First()); } //promote existing user ? if (users.Length > 0) { //yes User user = users.First(); if (user.ID > 0) { user.AuthLevel = (int)MDBUserLevel.Admin; authTables.Users.Replace(user); authTables.LogNotice("Promoted user {0} to admin!", user); return(user); } } //create admin { string pass = "******"; authTables.CreateUser("admin", "admin", pass, UserState.Confirmed, MDBUserLevel.Admin, out User user, out EmailAddress email); authTables.LogNotice("Added admin {0} password {1}.", user, pass); if (authTables.Database == null) { authTables.Save(); } return(user); } }
protected override void Worker() { if (LogConsole != null) { LogConsole.ExceptionMode = LogExceptionMode.Full; LogConsole.Flags |= LogConsoleFlags.DisplayTimeStamp; LogConsole.Mode = LogReceiverMode.Opportune; } Logger.DebugReceiver?.Close(); //init the async mysql connection class we want to use. //new MySql.Data.MySqlClient.MySqlConnection().Dispose(); WebServer webServer = null; FtpServer ftpServer = null; MusicDataBase mdb = null; MDBBroadcaster broadcaster = null; DesktopPlayer player = null; try { var rootFolder = FileSystem.ProgramDirectory; webServer = new WebServer(); mdb = new MusicDataBase(rootFolder); if (LogSystem != null) { //allow user to override loglevel by commandline if (LogSystem.Level == LogLevel.Information) { LogSystem.Level = mdb.Config.ReadEnum <LogLevel>("MusicDataBase", "LogLevel", LogLevel.Information); } } this.LogInfo("Loading Database..."); mdb.Load(); MemoryStorage.Default.LogVerboseMessages = LogSystem?.Level == LogLevel.Verbose; if (null != mdb.Database) { mdb.Database.Storage.LogVerboseMessages = MemoryStorage.Default.LogVerboseMessages; } if (mdb.Config.ReadBool("MusicDataBase", "ClearDatabase", false)) { foreach (ITable t in mdb.Tables) { t.Clear(); } } mdb.Save(); this.LogInfo("Loading auth tables..."); if (mdb.Database != null) { webServer.AuthTables.Connect(TableConnectorMode.Direct, mdb.Database); } else { try { webServer.AuthTables.Load(mdb.DataBasePath); } catch (Exception ex) { this.LogWarning(ex, "Load auth tables failed. Recreating..."); } } authTables = webServer.AuthTables; var authInterface = new AuthInterface <MDBUserLevel>(webServer); { //auth interface User admin = webServer.AuthTables.CheckAdminPresent(); if (mdb.Config.ReadBool("MusicDataBase", "LocalhostIsAdmin", false)) { authInterface.DefaultLocalhostUser = admin; } } this.LogInfo("Initializing FtpServer..."); ftpServer = new FtpServer(); ftpServer.CheckLogin += this.FtpServerCheckLogin; //add music dirs to ftp server SetFtpMusicFolders(mdb, ftpServer); int ftpPort = mdb.Config.ReadInt32("FtpServer", "Port", 8021); ftpServer.Listen(ftpPort); player = new DesktopPlayer(mdb, (long)MDBStreamType.JukeBob); player.Start(); this.LogInfo("Initializing WebServer..."); webServer.SessionMode = WebServerSessionMode.Cookie; webServer.SessionTimeout = TimeSpan.FromDays(1); webServer.PerformanceChecks = mdb.Config.ReadBool("WebServer", "PerformanceChecks", false); webServer.EnableExplain = mdb.Config.ReadBool("WebServer", "Explain", false); webServer.TransmitLayout = false; webServer.EnableTemplates = true; webServer.CheckAccess += WebServerCheckAccess; webServer.StaticFilesPath = mdb.WebFolder; //prepare rpc var avatarFolder = mdb.GetFolderConfig("MusicDataBase", "AvatarFolder", "avatar"); webServer.Register(new AvatarInterface(avatarFolder)); webServer.Register(authInterface); var webInterface = new WebInterface(mdb, webServer.AuthTables, player); webInterface.LogCollector.Level = LogLevel.Debug; webInterface.LogCollector.MaximumItemCount = 1000; webInterface.LogCollector.ExceptionMode = LogExceptionMode.Full; webServer.Register(webInterface); webServer.Register(new HostInterface(this)); //start server int webPort = mdb.Config.ReadInt32("WebServer", "Port", 8080); webServer.Listen(webPort); mdb.SetHostConfiguration(Environment.UserName + "." + Environment.MachineName, webPort, ftpPort, mdb.GetLocalAddresses()); //using (Streamer streamer = new Streamer(mdb, MDBStreamType.JukeBox)) broadcaster = new MDBBroadcaster(mdb); broadcaster.Start(); if (mdb.Config.ReadBool("Crawler", "RunAtStartup", false)) { webInterface.FileCrawler.Start(); } //main loop this.LogNotice("Entering main loop..."); while (!ServiceParameters.Shutdown) { ServiceParameters.WaitForShutdown(1000); //SetConsoleTitle(); } } finally { //cleanup broadcaster?.Dispose(); ftpServer?.Close(); player?.Stop(); player?.Dispose(); authTables?.Save(); authTables = null; mdb?.Save(); webServer?.Close(); } this.LogInfo("Shutdown completed."); Logger.Flush(); }