예제 #1
0
파일: Base.cs 프로젝트: josemaripl/MBCon
        public static IDriver GetDriver(IniParser ini)
        {
            var driverName = ini.GetSetting("PlayerCheck", "Driver");
            if (driverName == "")
            {
                throw new DriverException("Driver not set");
            }

            try
            {
                var iDriverType = typeof(IDriver);
                var driverType = Type.GetType(String.Format("PlayerCheck.Drivers.{0}", driverName));

                if (driverType.IsInterface || driverType.IsAbstract)
                    throw new DriverException("Driver is interface or abstract");

                if (driverType.GetInterface(iDriverType.FullName) == null)
                    throw new DriverException("Driver doesnt implement IDriver");

                return (IDriver)Activator.CreateInstance(driverType);
            }
            catch (Exception ex)
            {
                throw new DriverException(String.Format("Error loading driver {0}: {1}", driverName, ex.Message));
            }
        }
예제 #2
0
파일: WebRcon.cs 프로젝트: josemaripl/MBCon
        public void Init(IApi api, string dllpath)
        {
            _api = api;
            _dllpath = dllpath;
            try
            {
                _ini = new IniParser(_configPath);
            }
            catch (Exception ex)
            {
                throw new WebRconException(String.Format("Error loading config: {0}", ex.Message));
            }

            _enabled = _ini.GetBoolSetting(Name, "Enabled");
            if (!_enabled)
                throw new WebRconException(String.Format("{0} has been disabled", Name));

            var port = _ini.GetSetting(Name, "Port");
            try
            {
                _port = Convert.ToInt32(port);
            }
            catch (Exception ex)
            {
                throw new WebRconException(String.Format("Invalid port: {0}", ex.Message));
            }

            _password = _ini.GetSetting(Name, "Password");
            if (_password == "")
            {
                _password = Utils.GetRandomString();
            }

#if DEBUG
            _serverurl = String.Format("http://localhost:{0}/", _port);
#else
            _serverurl = String.Format("http://*:{0}/", _port);
#endif
            _webServer = new WebServer(HttpRequest, _serverurl);
            _webServer.Run();
            AppConsole.Log(String.Format("Started HTTP server at {0}. Password: {1}", _serverurl, _password), ConsoleColor.Cyan);

            _port++;
            _socketServer = new WebSocketServer(_port);
            _socketServer.AddWebSocketService("/rcon", () => new SocketBehavior(_api, _password));
            _socketServer.Start();

            LoadHtdocsFiles();

        }
예제 #3
0
        public void Init(IApi api, string dllpath)
        {
            _api = api;
            _dllpath = dllpath;
            try
            {
                _ini = new IniParser(_configPath);
            }
            catch (Exception ex)
            {
                throw new $safeprojectname$Exception(String.Format("Error loading config: {0}", ex.Message));
            }

            _enabled = _ini.GetBoolSetting(Name, "Enabled");
            if (!_enabled)
                throw new $safeprojectname$Exception(String.Format("{0} has been disabled", Name));
            
        }
예제 #4
0
        public void Init(IApi api, string dllpath)
        {
            _api = api;
            _dllpath = dllpath;
            try
            {
                _ini = new IniParser(_configPath);
            }
            catch (Exception ex)
            {
                throw new SimpleMessagesException(String.Format("Error loading config: {0}", ex.Message));
            }

            _enabled = _ini.GetBoolSetting("SimpleMessages", "Enabled");
            if (!_enabled)
                throw new SimpleMessagesException(String.Format("{0} has been disabled", Name));

            Int32 _interval;
            try
            {
                _interval = Convert.ToInt32(_ini.GetSetting("SimpleMessages", "Interval", "60"));
            }
            catch
            {
                _interval = 60;
            }

            _prefix = _ini.GetSetting("SimpleMessages", "Prefix");

            _repeat = _ini.GetBoolSetting("SimpleMessages", "Repeat");
            var _random = _ini.GetBoolSetting("SimpleMessages", "Random");
            _messages = _ini.EnumSection("Messages");
            if (_random)
            {
                var rnd = new Random();
                _messages = _messages.OrderBy(x => rnd.Next()).ToArray();
            }
            _timer = new Timer {Interval = (_interval*1000)};
            _timer.Elapsed += _timer_Elapsed;
            _timer.Enabled = true;
        }
예제 #5
0
        public void Init(IApi api, string dllpath)
        {
            _api = api;
            _dllpath = dllpath;
            try
            {
                _ini = new IniParser(_configPath);
            }
            catch (Exception ex)
            {
                throw new BEFilterMonitorException(String.Format("Error loading config: {0}", ex.Message));
            }

            _enabled = _ini.GetBoolSetting(Name, "Enabled");
            if (!_enabled)
                throw new BEFilterMonitorException(String.Format("{0} has been disabled", Name));


            _worker = new Thread(FilterMonitor);
            _worker.Start();
        }
예제 #6
0
        public void Init(IApi api, string dllpath)
        {
            _api = api;
            _dllpath = dllpath;
            IniParser _ini;
            try
            {
                _ini = new IniParser(_configPath);
            }
            catch (Exception ex)
            {
                throw new PlayerCheckException(String.Format("Error loading config: {0}", ex.Message));
            }

            _enabled = _ini.GetBoolSetting(Name, "Enabled");
            if (!_enabled)
                throw new PlayerCheckException(String.Format("{0} has been disabled", Name));

            _checkip = _ini.GetBoolSetting(Name, "CheckIP");
            _kickMessage = _ini.GetSetting(Name, "KickMessage");

            if (_ini.GetSetting(Name, "Mode") == "white")
            {
                _mode = Mode.Whitelist;
            }

            var settings = new DriverSettings()
            {
                Api = api,
                Ini = _ini,
                PluginPath = dllpath
            };

            _driver = Base.GetDriver(_ini);
            _driver.SetConfig(settings);
            api.OnBeMessageReceivedEvent += onBEMessageReceivedEvent;
        }
예제 #7
0
파일: Logger.cs 프로젝트: josemaripl/MBCon
        public void Init(IApi api, string dllpath)
        {
            _api = api;
            _dllpath = dllpath;
            try
            {
                _ini = new IniParser(_configPath);
            }
            catch (Exception ex)
            {
                throw new LoggerException(String.Format("Error loading config: {0}", ex.Message));
            }

            _enabled = _ini.GetBoolSetting("Logger", "Enabled");
            if (!_enabled)
            {
                throw new LoggerException(String.Format("{0} has been disabled", Name));
            }
            _path = _ini.GetSetting("Logger", "Path");
            if (_path == "")
                _path = _defaultLogPath;

            if (!Directory.Exists(_path))
            {
                try
                {
                    Directory.CreateDirectory(_path);
                }
                catch
                {
                    throw new LoggerException(String.Format("Log path \"{0}\" does not exist.", _path));
                }
            }

            OpenLogs();
            _api.OnBeMessageReceivedEvent += _be_MessageEventHandler;
        }
예제 #8
0
파일: Core.cs 프로젝트: josemaripl/MBCon
        public void Run(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Console.Title = "MBCon - Connecting...";
#if DEBUG
            AppConsole.Log(String.Format("MBCon - WARNING THIS IS A DEBUG APP!!!"), ConsoleColor.Red);
            AppConsole.Log(String.Format("MBCon - WARNING THIS IS A DEBUG APP!!!"), ConsoleColor.Red);
            AppConsole.Log(String.Format("MBCon - WARNING THIS IS A DEBUG APP!!!"), ConsoleColor.Red);
            AppConsole.Log(String.Format("MBCon - WARNING THIS IS A DEBUG APP!!!"), ConsoleColor.Red);
            AppConsole.Log(String.Format("MBCon - WARNING THIS IS A DEBUG APP!!!"), ConsoleColor.Red);
#else
            AppConsole.Log(String.Format("=========================="), ConsoleColor.DarkCyan);
            AppConsole.Log(String.Format("MBCon - by maca134"), ConsoleColor.Cyan);
            AppConsole.Log(String.Format("*****@*****.**"), ConsoleColor.Gray);
            AppConsole.Log(String.Format("=========================="), ConsoleColor.DarkCyan);
            AppConsole.Log("");
            Thread.Sleep(4000);
#endif

            _args.Setup<string>("config")
                .Callback(val => _configPath = val)
                .SetDefault(Path.Combine(BasePath, "config.ini"));

            _args.Setup<int>("delay")
                .Callback(val => _startupDelay = val)
                .SetDefault(0);

            _args.Parse(args);

            if (!File.Exists(_configPath))
            {
                throw new CoreException(String.Format("Config file \"{0}\" was not found.", _configPath));
            }
            AppConsole.Log("Config file found, continuing to load...");
            var ini = new IniParser(_configPath);

            try
            {
                _settings = new Settings(ini);
            }
            catch (SettingsException ex)
            {
                throw new CoreException(String.Format("Error Loading Settings: {0}", ex.Message));
            }

            if (_startupDelay > 0)
            {
                AppConsole.Log(string.Format("Waiting for {0} seconds", _startupDelay));
                Thread.Sleep(_startupDelay * 1000);
            }

            _beclient = BEClient();
            _pluginManager = new PluginManager(PluginPath);

            _api = new Api(_beclient, _settings);
            _pluginManager.Init(_api);

            AppConsole.Log("Connecting to server...");

            Connect();
        }
예제 #9
0
        public Settings(IniParser ini)
        {
            string beconfig;

            if (!IPAddress.TryParse(ini.GetSetting("General", "IP").Trim(), out _address))
            {
                throw new SettingsException("Port is not set.");
            }

            try
            {
                _port = Convert.ToInt32(ini.GetSetting("General", "Port").Trim());
                if (_port > 65535)
                    throw new SettingsException("Port is too high.");
            }
            catch (Exception ex)
            {
                throw new SettingsException(String.Format("Port is not set or invalid: {0}", ex.Message));
            }

            try
            {
                _bepath = ini.GetSetting("General", "BEPath").Trim();
                if (!Directory.Exists(_bepath))
                    throw new SettingsException("BEPath does not exist.");
                beconfig = Path.Combine(_bepath, "BEServer.cfg");
                if (!File.Exists(beconfig))
                {
                    var files = Directory.GetFiles(_bepath, "*.cfg")
                     .Where(path => path.Contains("BEServer_active_"))
                     .ToList();
                    if (files.Count == 0)
                        throw new SettingsException("Cound not find BEServer.cfg.");
                    beconfig = Path.Combine(_bepath, files[0]);
                }
            }
            catch (Exception ex)
            {
                throw new SettingsException(String.Format("BEPath is invalid: {0}", ex.Message));
            }

            try
            {
                var file = new StreamReader(beconfig);
                string line;
                while ((line = file.ReadLine()) != null)
                {
                    if (line.StartsWith("RConPassword "))
                    {
                        _rconpass = line.Replace("RConPassword ", "");
                        break;
                    }
                }
                if (_rconpass == null)
                    throw new SettingsException("Could not find rcon password in BEServer.cfg.");
            }
            catch (Exception ex)
            {
                throw new SettingsException(String.Format("Error reading BEServer.cfg: {0}", ex.Message));
            }

            try
            {
                _pluginpath = ini.GetSetting("General", "PluginPath").Trim();
                if (_pluginpath == "")
                {
                    _pluginpath = Path.Combine(Core.BasePath, "plugins");
                }
                if (!Directory.Exists(_pluginpath))
                    Directory.CreateDirectory(_pluginpath);
            }
            catch (Exception ex)
            {
                throw new SettingsException(String.Format("Error reading plugin directory: {0}", ex.Message));
            }

            try
            {
                _admins = new Dictionary<string, string>();
                var adminsList = ini.EnumSection("Admins");
                foreach (var guid in adminsList)
                {
                    var name = ini.GetSetting("Admins", guid);
                    _admins.Add(guid.Trim(), name.Trim());
                }
            }
            catch (Exception ex)
            {
                throw new SettingsException(String.Format("Error reading settings: {0}", ex.Message));
            }

        }
예제 #10
0
 public static void Forge(IniParser ini)
 {
     Instance = new Settings(ini);
 }
예제 #11
0
        public void Init(IApi api, string dllpath)
        {
            _api = api;
            _dllpath = dllpath;
            try
            {
                _ini = new IniParser(_configPath);
            }
            catch (Exception ex)
            {
                throw new ScheduledTasksException(String.Format("Error loading config: {0}", ex.Message));
            }

            _enabled = _ini.GetBoolSetting("ScheduledTasks", "Enabled");
            if (!_enabled)
                throw new ScheduledTasksException(String.Format("{0} has been disabled", Name));

            _debug = _ini.GetBoolSetting("ScheduledTasks", "Debug");
            _schedule = _ini.GetSetting("ScheduledTasks", "Schedule");
            if (_schedule == "")
                throw new ScheduledTasksException(String.Format("{0} has been disabled. No schedule defined.", Name));

            if (!Path.IsPathRooted(_schedule))
            {
                _schedule = Path.Combine(_dllpath, _schedule);
            }

            var ext = Path.GetExtension(_schedule);
            if (ext == ".json")
            {
                Task.LoadFromJSON(_schedule);
            }
            
            if (_debug)
            {
                var i = 0;
                AppConsole.Log("---------------", ConsoleColor.DarkMagenta);
                AppConsole.Log("Scheduled Tasks", ConsoleColor.DarkMagenta);
                AppConsole.Log("---------------", ConsoleColor.DarkMagenta);

                foreach (var t in Task.Tasks)
                {
                    i++;
                    AppConsole.Log(String.Format("Task {0}", i), ConsoleColor.DarkMagenta);
                    AppConsole.Log(String.Format("Running in about {0}", t.TimeSpan.Humanize(2, true)), ConsoleColor.DarkMagenta);
                    if (t.Loop != 1)
                    {
                        AppConsole.Log(String.Format("Repeating every {0}, {1} times", t.Interval.Humanize(2, true), t.Loop), ConsoleColor.DarkMagenta);
                    }
                    AppConsole.Log(String.Format("Command: {0} {1}", t.Command.Type, t.Command.Parameters), ConsoleColor.DarkMagenta);
                    AppConsole.Log("");
                }
            }
            if (Task.Tasks.Count == 0)
            {
                throw new ScheduledTasksException(String.Format("{0} has been disabled. No tasks found.", Name));
            }

            _worker = new Thread(TaskMonitor);
            _worker.Start();
        }
예제 #12
0
파일: Task.cs 프로젝트: josemaripl/MBCon
        public static void LoadTasks(IniParser ini)
        {
            var _i = -1;
            while (true)
            {
                var now = DateTime.Now;
                _i++;
                var command = ini.GetSetting("Tasks", String.Format("Command{0}", _i));
                if (String.IsNullOrWhiteSpace(command))
                {
                    if (_i == 0)
                        continue;
                    else
                    {
                        AppConsole.Log("Finished loading tasks", ConsoleColor.DarkMagenta);
                        break;
                    }
                }

                Command cmd;
                DateTime nextRun;
                var interval = TimeSpan.FromDays(1);
                var loop = 1;

                try
                {
                    cmd = new Command(command);
                }
                catch (CommandException ex)
                {
                    AppConsole.Log(String.Format("Error loading task {0}: {1}", _i, ex.Message), ConsoleColor.Red);
                    continue;
                }

                var time = ini.GetSetting("Tasks", String.Format("Time{0}", _i));
                if (!String.IsNullOrWhiteSpace(time))
                {
                    try
                    {
                        var h = Convert.ToDouble(time.Substring(0, 2));
                        var m = Convert.ToDouble(time.Substring(2, 2));

                        nextRun = DateTime.Today;
                        nextRun = nextRun.AddHours(h);
                        nextRun = nextRun.AddMinutes(m);

                        if (now > nextRun)
                        {
                            nextRun = nextRun.Add(interval);
                        }
                    }
                    catch (Exception ex)
                    {
                        AppConsole.Log(String.Format("Error loading task: Bad String Time {0}", ex.Message), ConsoleColor.Red);
                        continue;
                    }
                }
                else
                {
                    try
                    {
                        interval = TimeSpan.FromSeconds(Convert.ToInt32(ini.GetSetting("Tasks", String.Format("Interval{0}", _i))));
                    }
                    catch (Exception ex)
                    {
                        AppConsole.Log(String.Format("Error loading task: Bad String Interval {0}", ex.Message), ConsoleColor.Red);
                        continue;
                    }

                    TimeSpan delay;
                    try
                    {
                        delay = TimeSpan.FromSeconds(Convert.ToDouble(ini.GetSetting("Tasks", String.Format("Delay{0}", _i), "0")));
                    }
                    catch
                    {
                        delay = new TimeSpan();
                    }
                    loop = (ini.GetBoolSetting("Tasks", String.Format("Repeat{0}", _i))) ? -1 : 1;

                    nextRun = now.Add(delay);
                    nextRun = nextRun.Add(interval);
                }

                var task = new Task()
                {
                    _command = cmd,
                    _nextRun = nextRun,
                    _loop = loop,
                    _interval = interval
                };
                _tasks.Add(task);
            }
            AppConsole.Log(String.Format("Added {0} tasks", _tasks.Count), ConsoleColor.DarkMagenta);
        }
예제 #13
0
        public void Init(IApi api, string dllpath)
        {
            _api = api;
            _dllpath = dllpath;
            try
            {
                _ini = new IniParser(_configPath);
            }
            catch (Exception ex)
            {
                throw new WebLoggerException(String.Format("Error loading config: {0}", ex.Message));
            }

            _enabled = _ini.GetBoolSetting("WebLogger", "Enabled");
            if (!_enabled)
                throw new WebLoggerException(String.Format("{0} has been disabled", Name));

            var url = _ini.GetSetting("WebLogger", "URL");
            if (!Uri.TryCreate(url, UriKind.Absolute, out _uri))
            {
                throw new WebLoggerException(String.Format("Error parsing url: {0}", url));
            }

            _worker = new Thread(ProcessQueue);
            _worker.Start();
            _api.OnBeMessageReceivedEvent += MessageEventHandler;
        }
예제 #14
0
        public void Init(IApi api, string dllpath)
        {
            _api = api;
            _dllpath = dllpath;
            try
            {
                _ini = new IniParser(_configPath);
            }
            catch (Exception ex)
            {
                throw new RestartMessagesException(String.Format("Error loading config: {0}", ex.Message));
            }

            _enabled = _ini.GetBoolSetting(Name, "Enabled");
            if (!_enabled)
                throw new RestartMessagesException(String.Format("{0} has been disabled", Name));

            _debug = _ini.GetBoolSetting("RestartMessages", "Debug");

            try
            {
                _runtime = DateTime.Now.AddHours(Convert.ToInt16(_ini.GetSetting("RestartMessages", "ServerRuntime").Trim()));
            }
            catch (Exception ex)
            {
                throw new RestartMessagesException(String.Format("Could not parse ServerRuntime: {0}", ex.Message));
            }

            try
            {
                var intervals = _ini.GetSetting("RestartMessages", "Interval");
                foreach (var i in intervals.Split(','))
                {
                    _intervals.Add(_runtime.AddMinutes(-1 * Convert.ToInt16(i)));
                }
                _intervals.Sort();
                if (_intervals.Count == 0)
                {
                    throw new RestartMessagesException("Not intervals have been set");
                }
            }
            catch (Exception ex)
            {
                throw new RestartMessagesException(String.Format("Could not parse Interval: {0}", ex.Message));
            }

            try
            {
                _repeat = Convert.ToInt16(_ini.GetSetting("RestartMessages", "Repeat").Trim());
            }
            catch (Exception ex)
            {
                throw new RestartMessagesException(String.Format("Could not parse Repeat: {0}", ex.Message));
            }

            try
            {
                _message = _ini.GetSetting("RestartMessages", "Message").Trim();
            }
            catch (Exception ex)
            {
                throw new RestartMessagesException(String.Format("Could not parse Message: {0}", ex.Message));
            }

            _worker = new Thread(TaskMonitor);
            _worker.Start();
        }