コード例 #1
0
        public ActionResult CreateWeb([Bind(Prefix = "Web")] Web web, string solution = "core", string lang = "")
        {
            try
            {
                var app = App.Get();
                //set default locale
                if (!string.IsNullOrEmpty(lang))
                {
                    DnaConfig.UpdateWebConfig("configuration/appSettings/add[@key=\"DefaultLocale\"]", "value", lang);
                }

                app.Widgets.RegisterAll();
                var topWeb = app.Solutions.Install(solution, "home", web.Owner, web.Title, web.Description, "", lang);

                if (topWeb.Pages.Count() == 0)
                {
                    topWeb.CreatePage("Default");
                }

                PermissionLoader.Load();

                DnaConfig.UpdateWebConfig("configuration/appSettings/add[@key=\"Initialized\"]", "value", "True");
            }
            catch (Exception e)
            {
                //var msg = e.Message;
                Exception innerExpt = e.InnerException;
                var       errors    = new StringBuilder();

                App.Get().DataContext.Delete <Web>(w => w.Name.Equals("home"));
                App.Get().DataContext.SaveChanges();
                errors.AppendLine(e.Message);

                while (innerExpt != null)
                {
                    errors.AppendLine(innerExpt.Message);
                    //msg = innerExpt.Message;
                    innerExpt = innerExpt.InnerException;
                }
                errors.AppendLine(e.StackTrace);
                //if (innerExpt != null)
                // errors.Append(e.Message);
                //msg = innerExpt.Message;

                return(Json(new { error = errors.ToString() }, JsonRequestBehavior.AllowGet));
            }
            return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
        }
コード例 #2
0
ファイル: Server.cs プロジェクト: dretax/SquadRconTool
        public Server()
        {
            bool success = LoadSettings();

            if (!success)
            {
                Logger.Log("Config failure, shutting down...");
                Thread.Sleep(1000);
                return;
            }

            GenerateSelfSignedCertificate();

            SquadServerLoader.LoadServers();
            PermissionLoader.LoadPermissions();
            ValidateServers();

            IPAddress listenip = IPAddress.Any;

            if (ListenIPAddress.ToLower() != "any")
            {
                if (!IPAddress.TryParse(ListenIPAddress, out listenip))
                {
                    Logger.Log("Failed to convert listen ip to an actual IP Address. Listening on all.");
                }
            }

            // Remove insecure protocols (SSL3, TLS 1.0, TLS 1.1)
            ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Ssl3;
            ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Tls;
            ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Tls11;
            // Add TLS 1.2, and 1.3
            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls13;

            TCPServer = new TcpListener(listenip, ListenPort);
            TCPServer.Start();
            Logger.Log("[TCPServer] Listening for incoming connections. IP: " + listenip + " Port: " + ListenPort);
            Thread t = new Thread(ListenForIncomingConnections);

            t.IsBackground = true;
            t.Start();
        }
コード例 #3
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            ModelBinders.Binders.Add(typeof(DateTime), new CustomDateModelBinder());
            ICreative.Services.AutoMapperBootStrapper.ServiceMap();
            ICreative.Controllers.AutoMapperBootStrapper.ControllerMap();
            ICreative.Services.AutoMapperBootStrapper.Init();
            ApplicationSettingsFactory.InitializeApplicationSettingsFactory(new WebConfigApplicationSettings());
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            IUnityContainer unity = Bootstrapper.Initialise();

            GlobalFilters.Filters.Add(new AuthorizeUserAttribute());

            //Clear ViewEngine increase application performance
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new RazorViewEngine());


            PermissionLoader.LoadRights(unity.Resolve <IPermissionService>());
            // SingletonRestrictionCollection.Restrictions.LoadRestriction(unity.Resolve<IRestrictionService>());
        }
コード例 #4
0
        public static void Main(string[] args)
        {
            Logger.Init();
            Logger.Log("Squad Rcon Bridge Server Created by " + Author + " v" + Version);
            Logger.Log("Respository Link: " + Github);
            Logger.Log("More Contact: " + WebSite + " " + Discord);
            Logger.Log("Initializing TCP...");
            _srv = new Server();

            while (_isrunning)
            {
                string input = Console.ReadLine();
                switch (input)
                {
                case "quit":
                    _srv.StopConnections();
                    _isrunning = false;
                    Logger.Log("Shutting down TCP Server.");
                    break;

                case "reloadperms":
                    PermissionLoader.LoadPermissions();
                    Logger.Log("Permissions reloaded.");
                    break;

                case "adduser":
                    Console.WriteLine("Please enter a username");
                    string username = Console.ReadLine();
                    Console.WriteLine("Please enter a password. I suggest generating one.");
                    string password = Console.ReadLine();
                    Console.WriteLine("Username: "******"Password: "******"Add user? (Y / N) Default permissions will be applied.");
                    string ok = Console.ReadLine();
                    if (ok != null && ok.ToLower() == "y")
                    {
                        PermissionLoader.AddUser(username, password);
                        Console.WriteLine(username + " added. Edit permissions in the ini file, and reload.");
                    }
                    break;

                case "changepassword":
                    break;

                case "removeuser":
                    break;

                case "cleartokens":
                    TokenHandler.ClearTokens();
                    Logger.Log("Tokens cleared!");
                    break;

                case "help":
                    break;

                default:
                    Logger.Log("Unknown command. Type 'help' to display all of the commands.");
                    break;
                }
            }
            Logger.Log("Press something to exit...");
            Console.ReadKey();
        }
コード例 #5
0
 public ActionResult RefreshPerms()
 {
     PermissionLoader.Load();
     return(new HttpStatusCodeResult(200));
     //return Redirect("~/host/roles");
 }
コード例 #6
0
ファイル: Server.cs プロジェクト: dretax/SquadRconTool
        private void HandleConnection(System.Net.Sockets.Socket s)
        {
            User currentuser = null;

            try
            {
                s.ReceiveTimeout = 0;
                s.SendTimeout    = 0;
                s.NoDelay        = true;
                string ipr = s.RemoteEndPoint.ToString().Split(':')[0];

                NetworkStream stream = new NetworkStream(s);
                SslStream     ssl    = new SslStream(stream, false);

                ssl.AuthenticateAsServer(Certificate, false, SslProtocols.Tls12 | SslProtocols.Tls13, true);
                while (s.Connected)
                {
                    if (!ssl.CanRead && !stream.DataAvailable)
                    {
                        Thread.Sleep(100);
                        continue;
                    }

                    byte[] leng = new byte[4];
                    int    k2   = 0;
                    try
                    {
                        k2 = ssl.Read(leng, 0, leng.Length);
                    }
                    catch (Exception ex)
                    {
                        if (ex.ToString()
                            .Contains("An established connection was aborted by the software in your host machine"))
                        {
                            if (s.Connected)
                            {
                                s.Close();
                            }

                            return;
                        }

                        if (ex.ToString().Contains("System.NullReferenceException"))
                        {
                            if (s.Connected)
                            {
                                s.Close();
                            }

                            return;
                        }

                        Logger.Log("[Communication Error] General error. " + ipr + " " + ex);
                    }

                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(leng);
                    }

                    int upcominglength = (BitConverter.ToInt32(leng, 0));
                    if (upcominglength > 15000000 || upcominglength <= 0)
                    {
                        ssl.Flush();
                        s.Close();
                        return;
                    }

                    byte[] b = ByteReader(upcominglength, ssl, s);
                    if (b == null || k2 == 0)
                    {
                        ssl.Flush();
                        s.Close();
                        return;
                    }

                    b = LZ4Compresser.Decompress(b);

                    string message = Encoding.UTF8.GetString(b, 0, b.Length);
                    if (string.IsNullOrEmpty(message) || !message.Contains(Constants.MainSeparator))
                    {
                        ssl.Flush();
                        s.Close();
                        return;
                    }

                    string[] split = message.Split(Constants.MainSeparator);
                    if (split.Length != 2 || string.IsNullOrEmpty(split[1]) || string.IsNullOrEmpty(split[0]))
                    {
                        s.Close();
                        return;
                    }

                    Codes code = Codes.Unknown;
                    int   intp = -1;
                    bool  bbbb = int.TryParse(split[0], out intp);
                    if (!bbbb || intp == -1)
                    {
                        s.Close();
                        return;
                    }

                    if (!Enum.IsDefined(typeof(Codes), intp))
                    {
                        s.Close();
                        return;
                    }

                    code = (Codes)intp;

                    string[] otherdata = split[1].Split(Constants.AssistantSeparator);
                    string   bmsg      = "";
                    switch (code)
                    {
                    case Codes.Login:
                    {
                        try
                        {
                            if (otherdata.Length != 3)
                            {
                                if (s.Connected)
                                {
                                    s.Close();
                                }

                                return;
                            }

                            string name     = otherdata[0].Substring(0, Math.Min(otherdata[0].Length, 50));
                            string password = otherdata[1].Substring(0, Math.Min(otherdata[1].Length, 50));
                            string version  = otherdata[2].Substring(0, Math.Min(otherdata[2].Length, 10));

                            if (string.IsNullOrEmpty(name) ||
                                string.IsNullOrEmpty(password) ||
                                string.IsNullOrEmpty(version))
                            {
                                if (s.Connected)
                                {
                                    s.Close();
                                }

                                return;
                            }

                            bmsg = MessageConnector.FormMessage(Codes.Login, "InvalidNameOrPassword");

                            currentuser = PermissionLoader.GetUser(name);
                            if (currentuser != null && !currentuser.IsLoggedIn &&
                                currentuser.PasswordCheck(password) && version == Program.Version)
                            {
                                currentuser.Token      = TokenHandler.AddNewToken(currentuser.UserName);
                                currentuser.IsLoggedIn = true;
                                // TODO: Only send authorized servers.
                                bmsg = (int)Codes.Login + Constants.MainSeparator + "Success" + Constants.AssistantSeparator + currentuser.Token + Constants.AssistantSeparator +
                                       string.Join(Constants.AssistantSeparator, SquadServerLoader.AllServers.Keys);
                                Logger.Log("[TCPServer] Authentication from " + ipr + " Name: " + name);
                            }
                            else
                            {
                                Logger.Log("[TCPServer] Authentication failure from " + ipr + " Name: " + name);
                            }

                            if (s.Connected)
                            {
                                byte[] messagebyte = asen.GetBytes(bmsg);
                                byte[] intBytes    = BitConverter.GetBytes(messagebyte.Length);
                                if (BitConverter.IsLittleEndian)
                                {
                                    Array.Reverse(intBytes);
                                }
                                ssl.Write(intBytes);
                                ssl.Write(messagebyte);
                                if (bmsg.Contains("InvalidNameOrPassword"))
                                {
                                    s.Close();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError("[Authentication] Error: " + ex);
                        }

                        break;
                    }

                    case Codes.RequestPlayers:
                    {
                        if (otherdata.Length != 2)
                        {
                            if (s.Connected)
                            {
                                s.Close();
                            }

                            return;
                        }

                        string token      = otherdata[0].Substring(0, Math.Min(otherdata[0].Length, 50));
                        string servername = otherdata[1].Substring(0, Math.Min(otherdata[1].Length, 50));

                        if (string.IsNullOrEmpty(token) ||
                            string.IsNullOrEmpty(servername))
                        {
                            if (s.Connected)
                            {
                                s.Close();
                            }

                            return;
                        }

                        if (currentuser != null)
                        {
                            // If token is no longer valid, or doesn't equal with the current one something is wrong.
                            if (currentuser.Token != token || !TokenHandler.HasValidToken(currentuser.UserName) || !currentuser.IsLoggedIn)
                            {
                                if (s.Connected)
                                {
                                    s.Close();
                                }
                                return;
                            }

                            bmsg = MessageConnector.FormMessage(Codes.RequestPlayers, "Unknown");
                            if (ValidServers.ContainsKey(servername))
                            {
                                string response = ValidServers[servername].GetPlayerList();
                                try
                                {
                                    PlayerListProcesser x      = new PlayerListProcesser(response);
                                    string players             = JsonParser.Serialize(x.Players);
                                    string disconnectedplayers = JsonParser.Serialize(x.DisconnectedPlayers);
                                    bmsg = MessageConnector.FormMessage(Codes.RequestPlayers, players,
                                                                        disconnectedplayers);
                                }
                                catch (InvalidSquadPlayerListException ex)
                                {
                                    Logger.LogError("[PlayerListRequest Error] " + ex.Message);
                                }
                            }

                            if (s.Connected)
                            {
                                byte[] messagebyte = asen.GetBytes(bmsg);
                                byte[] intBytes    = BitConverter.GetBytes(messagebyte.Length);
                                if (BitConverter.IsLittleEndian)
                                {
                                    Array.Reverse(intBytes);
                                }
                                ssl.Write(intBytes);
                                ssl.Write(messagebyte);
                            }
                        }
                        else
                        {
                            if (s.Connected)
                            {
                                s.Close();
                            }

                            return;
                        }

                        break;
                    }

                    case Codes.Disconnect:
                    {
                        if (otherdata.Length != 1)
                        {
                            if (s.Connected)
                            {
                                s.Close();
                            }

                            return;
                        }

                        string token = otherdata[0].Substring(0, Math.Min(otherdata[0].Length, 50));

                        if (string.IsNullOrEmpty(token))
                        {
                            if (s.Connected)
                            {
                                s.Close();
                            }

                            return;
                        }

                        if (currentuser != null)
                        {
                            // If token is no longer valid, or doesn't equal with the current one something is wrong.
                            if (currentuser.Token != token || !TokenHandler.HasValidToken(currentuser.UserName) || !currentuser.IsLoggedIn)
                            {
                                if (s.Connected)
                                {
                                    s.Close();
                                }
                                return;
                            }

                            if (currentuser.Token != null)
                            {
                                TokenHandler.RemoveToken(currentuser.Token);
                            }

                            currentuser.IsLoggedIn = false;
                            currentuser.Token      = null;

                            bmsg = (int)Codes.RequestPlayers + Constants.MainSeparator + "Ok";

                            if (s.Connected)
                            {
                                byte[] messagebyte = asen.GetBytes(bmsg);
                                byte[] intBytes    = BitConverter.GetBytes(messagebyte.Length);
                                if (BitConverter.IsLittleEndian)
                                {
                                    Array.Reverse(intBytes);
                                }
                                ssl.Write(intBytes);
                                ssl.Write(messagebyte);
                                s.Close();
                            }
                        }
                        else
                        {
                            if (s.Connected)
                            {
                                s.Close();
                            }

                            return;
                        }

                        break;
                    }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is ObjectDisposedException)
                {
                    return;
                }

                Logger.LogError("[HandleConnection] General Error: " + ex);
            }
            finally
            {
                if (currentuser != null)
                {
                    if (currentuser.Token != null)
                    {
                        TokenHandler.RemoveToken(currentuser.Token);
                    }

                    currentuser.IsLoggedIn = false;
                    currentuser.Token      = null;
                }
            }
        }