Exemplo n.º 1
0
        public async new Task <IActionResult> View(string id)
        {
            Device device = deviceRepo.GetDevice(id);
            var    events = eventsRepo.GetEvents(device.Id, 5);

            ViewVM viewModel = new ViewVM();

            viewModel.Device = device;
            viewModel.Events = events;

            AgentApiClient client = new AgentApiClient();

            try
            {
                var response = await client.GetConfiguration(device.Address);

                var hoursWithActivity = eventsRepo.GetEventsForTheLast24Hours(device.Id);
                var last24Hours       = GoogleChartHelpers.To24HourArray(hoursWithActivity);
                ViewData["Last24Hours"] = GoogleChartHelpers.ToGoogleChartString(last24Hours, "Hour", "Count");

                viewModel.Online          = true;
                viewModel.FirewallEnabled = response.FirewallEnabled;
            }
            catch (System.Net.Http.HttpRequestException)
            {
                viewModel.Online = false;
            }

            return(View(viewModel));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Index(string id)
        {
            var device = repo.GetDevice(id);
            var client = new AgentApiClient();

            try
            {
                var dtoRules = await client.GetRules(device.Address);

                List <IRule> rules;

                if (dtoRules.UfwResult != null)
                {
                    rules = dtoRules.UfwResult.Select(rule => (IRule) new LinuxRuleAdapter(rule)).ToList();
                }
                else
                {
                    rules = dtoRules.WindowsResult.Select(entry => (IRule) new WindowsRuleAdapter(entry)).ToList();
                }

                rules = rules.OrderBy(x => x.Direction).ToList();

                HttpContext.Session.SetJson("rules", rules);
                return(View(new IndexVM()
                {
                    Device = device,
                    Rules = rules
                }));
            }
            catch (Exception)
            {
                return(RedirectToAction("View", "Devices", new { Id = id }));
            }
        }
Exemplo n.º 3
0
        private async Task <bool> TryFindInRules(Device device, PacketDroppedEvent ev)
        {
            var client = new AgentApiClient();

            try
            {
                var dtoRules = await client.GetRules(device.Address);

                List <IRule> rules;

                if (dtoRules.UfwResult != null)
                {
                    rules = dtoRules.UfwResult.Select(rule => (IRule) new LinuxRuleAdapter(rule)).ToList();
                }
                else
                {
                    rules = dtoRules.WindowsResult.Select(entry => (IRule) new WindowsRuleAdapter(entry)).ToList();
                }

                rules = rules.OrderBy(x => x.Direction).ToList();
                HttpContext.Session.SetJson("rules", rules);

                var matchingRule = rules.FirstOrDefault(x => x.Name == ev.FilterName);
                if (matchingRule != null)
                {
                    return(true);
                }

                return(false);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> ApplyTo(string id)
        {
            var policy = policyRepo.GetPolicy(id);

            if (policy.Rules.Count <= 0)
            {
                return(RedirectToAction("Index"));
            }

            var f    = Request.Form;
            var devs = f["Device"];

            var rulesRequest = new SetRulesRequest();
            var client       = new AgentApiClient();
            var tasks        = new List <Task>(devs.Count);

            rulesRequest.GenericRequest = Mapper.Map <List <Selly.Agent.Generic.Models.Rule> >(policy.Rules.ToList());

            foreach (var device in devs)
            {
                var d = deviceRepo.GetDevice(device);
                tasks.Add(client.SetRulesRequest(d.Address, rulesRequest));
            }

            await Task.WhenAll(tasks);

            return(RedirectToAction("Index"));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Delete(string id, string name)
        {
            var device = repo.Devices.FirstOrDefault(dev => dev.Id == id);
            var client = new AgentApiClient();
            await client.DeleteRule(device.Address, name);

            return(RedirectToAction("Index"));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Disable(string id)
        {
            var device   = deviceRepo.GetDevice(id);
            var client   = new AgentApiClient();
            var response = await client.Configure(device.Address, new ConfigureRequest()
            {
                FirewallEnabled = false
            });

            return(RedirectToAction("View", new { id = id }));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> CrossDeviceRule(Agent.Generic.Models.Rule rule)
        {
            var f    = Request.Form;
            var devs = f["Device"];

            try
            {
                rule.LocalPort     = rule.LocalPort ?? Agent.Generic.Models.Rule.ANY_PORT;
                rule.RemoteAddress = System.Net.IPAddress.Parse(rule.RemoteAddress).ToString();
            }
            catch (FormatException)
            {
                rule.RemoteAddress = Agent.Generic.Models.Rule.ANY_IP_ADDRESS;
            }

            // TODO: Add to UI
            if (rule.Direction == Agent.Generic.Models.Direction.In)
            {
                rule.RemotePort   = Agent.Generic.Models.Rule.ANY_PORT;
                rule.LocalAddress = Agent.Generic.Models.Rule.ANY_IP_ADDRESS;
            }
            else
            {
                rule.RemotePort = rule.LocalPort;

                rule.LocalPort    = Agent.Generic.Models.Rule.ANY_PORT;
                rule.LocalAddress = Agent.Generic.Models.Rule.ANY_IP_ADDRESS;
            }

            var dtoRule = new SetRuleRequest();

            dtoRule.GenericRequest = rule;

            var client = new AgentApiClient();

            var tasks = new List <Task>(devs.Count);

            foreach (var device in devs)
            {
                var d = deviceRepo.GetDevice(device);
                tasks.Add(client.NewRule(d.Address, dtoRule));
            }

            await Task.WhenAll(tasks);

            return(RedirectToAction("Index"));
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Create(string id, Agent.Generic.Models.Rule rule)
        {
            var device = repo.GetDevice(id);

            try
            {
                rule.LocalPort     = rule.LocalPort ?? Agent.Generic.Models.Rule.ANY_PORT;
                rule.RemoteAddress = System.Net.IPAddress.Parse(rule.RemoteAddress).ToString();
            }
            catch (FormatException)
            {
                rule.RemoteAddress = Agent.Generic.Models.Rule.ANY_IP_ADDRESS;
            }

            // TODO: Add to UI
            if (rule.Direction == Agent.Generic.Models.Direction.In)
            {
                rule.RemotePort   = Agent.Generic.Models.Rule.ANY_PORT;
                rule.LocalAddress = Agent.Generic.Models.Rule.ANY_IP_ADDRESS;
            }
            else
            {
                rule.RemotePort = rule.LocalPort;

                rule.LocalPort    = Agent.Generic.Models.Rule.ANY_PORT;
                rule.LocalAddress = Agent.Generic.Models.Rule.ANY_IP_ADDRESS;
            }

            var dtoRule = new SetRuleRequest();

            dtoRule.GenericRequest = rule;

            var client = new AgentApiClient();
            await client.NewRule(device.Address, dtoRule);

            return(RedirectToAction("Index"));
        }