Exemple #1
0
        public async Task <ActionResult> StopAgent([FromBody] AgentRunnerDto agentRunnerDto, CancellationToken cancellationToken)
        {
            var agent = await agentService.GetByCriteriaAsync(a => a.Name == agentRunnerDto.Agent, cancellationToken);

            if (agent == null)
            {
                return(BadRequest());
            }

            Target target = default;

            if (!string.IsNullOrWhiteSpace(agentRunnerDto.Target))
            {
                target = await this.targetService.GetTargetNotTrackingAsync(t => t.Name == agentRunnerDto.Target, cancellationToken);

                if (target == null)
                {
                    return(BadRequest());
                }
            }

            RootDomain rootDomain = default;

            if (!string.IsNullOrWhiteSpace(agentRunnerDto.RootDomain))
            {
                rootDomain = await this.rootDomainService.GetRootDomainNoTrackingAsync(t => t.Name == agentRunnerDto.RootDomain && t.Target == target, cancellationToken);

                if (rootDomain == null)
                {
                    return(NotFound());
                }
            }

            Subdomain subdomain = default;

            if (!string.IsNullOrWhiteSpace(agentRunnerDto.Subdomain))
            {
                subdomain = await this.subdomainService
                            .GetAllQueryableByCriteria(s => s.RootDomain == rootDomain && s.Name == agentRunnerDto.Subdomain)
                            .AsNoTracking()
                            .SingleOrDefaultAsync(cancellationToken);

                if (subdomain == null)
                {
                    return(NotFound());
                }
            }

            var agentRunner = new AgentRunner
            {
                Agent      = agent,
                Target     = target,
                RootDomain = rootDomain,
                Subdomain  = subdomain
            };

            await this.agentRunnerService.StopAgentAsync(agentRunner, cancellationToken);

            return(NoContent());
        }
Exemple #2
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            logger.LogInformation($"Starting {configuration.Value.CountOfAgents} agents");
            var agentRunner = new AgentRunner(new AgentRunnerConfiguration
            {
                AgentRepositoryUrl = configuration.Value.AgentRepositoryUrl
            }, loggerFactory);

            var info            = FreeTcpPortFinder.GetAvailablePort(10000, configuration.Value.CountOfAgents).Select(x => new StartAgentInformation(x)).ToArray();
            var runAgentsResult = await agentRunner.RunAgentsAsync(info);

            if (!runAgentsResult.IsSuccessful)
            {
                logger.LogError(runAgentsResult.Error);
            }
            else
            {
                foreach (var agentInformation in runAgentsResult.Value)
                {
                    agentsService.AddAgent(agentInformation);
                    logger.LogInformation($"Started agent on {agentInformation.Url}");
                }
            }

            agentsService.Start();
        }
Exemple #3
0
        /// <summary>
        /// Starts this instance.
        /// </summary>
        internal void Start()
        {
            this.AutoLog = true;
            AgentRunner agentRunner = new AgentRunner();

            agentRunner.ToString();
        }
Exemple #4
0
        /// <summary>
        /// Obtain the command to run on bash
        /// </summary>
        /// <param name="agentRunner">The agent</param>
        /// <returns>The command to run on bash</returns>
        public static string GetCommand(AgentRunner agentRunner)
        {
            var command = agentRunner.Command;

            if (string.IsNullOrWhiteSpace(command))
            {
                command = agentRunner.Agent.Command;
            }

            var envUserName = Environment.GetEnvironmentVariable("ReconnessUserName") ??
                              Environment.GetEnvironmentVariable("ReconnessUserName", EnvironmentVariableTarget.User);

            var envPassword = Environment.GetEnvironmentVariable("ReconnessPassword") ??
                              Environment.GetEnvironmentVariable("ReconnessPassword", EnvironmentVariableTarget.User);

            return(command
                   .Replace("{{target}}", agentRunner.Target.Name)
                   .Replace("{{rootDomain}}", agentRunner.RootDomain.Name)
                   .Replace("{{rootdomain}}", agentRunner.RootDomain.Name)
                   .Replace("{{domain}}", agentRunner.Subdomain == null ? agentRunner.RootDomain.Name : agentRunner.Subdomain.Name)
                   .Replace("{{subdomain}}", agentRunner.Subdomain == null ? agentRunner.RootDomain.Name : agentRunner.Subdomain.Name)
                   .Replace("{{userName}}", envUserName)
                   .Replace("{{password}}", envPassword)
                   .Replace("\"", "\\\""));
        }
        /// <summary>
        /// <see cref="IAgentRunnerService.RunningAgentsAsync(AgentRunner, CancellationToken)"/>
        /// </summary>
        public async Task <List <string> > RunningAgentsAsync(AgentRunner agentRunner, CancellationToken cancellationToken = default)
        {
            if ((await this.agentRunnerProvider.RunningCountAsync) == 0)
            {
                return(new List <string>());
            }

            var agentsRunning = new List <string>();

            var channels = await this.agentRunnerProvider.RunningChannelsAsync;

            var agents = await this.agentService.GetAllAsync(cancellationToken);

            foreach (var agent in agents)
            {
                cancellationToken.ThrowIfCancellationRequested();

                agentRunner.Agent = agent;
                var channel = AgentRunnerHelpers.GetChannel(agentRunner);

                if (channels.Any(c => c.Contains(channel)))
                {
                    agentsRunning.Add(agent.Name);
                }
            }

            return(agentsRunning);
        }
        public async Task <ActionResult> StopAgent([FromBody] AgentRunnerDto agentRunnerDto, CancellationToken cancellationToken)
        {
            var agent = await agentService.GetByCriteriaAsync(a => a.Name == agentRunnerDto.Agent, cancellationToken);

            if (agent == null)
            {
                return(BadRequest());
            }

            Target target = default;

            if (!string.IsNullOrWhiteSpace(agentRunnerDto.Target))
            {
                target = await this.targetService.GetByCriteriaAsync(t => t.Name == agentRunnerDto.Target, cancellationToken);

                if (target == null)
                {
                    return(BadRequest());
                }
            }

            RootDomain rootDomain = default;

            if (!string.IsNullOrWhiteSpace(agentRunnerDto.RootDomain))
            {
                rootDomain = await this.rootDomainService.GetByCriteriaAsync(t => t.Name == agentRunnerDto.RootDomain && t.Target == target, cancellationToken);

                if (rootDomain == null)
                {
                    return(NotFound());
                }
            }

            Subdomain subdomain = null;

            if (!string.IsNullOrWhiteSpace(agentRunnerDto.Subdomain))
            {
                subdomain = await this.subdomainService.GetByCriteriaAsync(s => s.RootDomain == rootDomain && s.Name == agentRunnerDto.Subdomain, cancellationToken);

                if (subdomain == null)
                {
                    return(NotFound());
                }
            }

            var agentRunner = new AgentRunner
            {
                Agent      = agent,
                Target     = target,
                RootDomain = rootDomain,
                Subdomain  = subdomain
            };

            var channel = AgentRunnerHelpers.GetChannel(agentRunner);

            await this.agentRunnerService.StopAgentAsync(agentRunner, channel, cancellationToken);

            return(NoContent());
        }
        private ClusteredServiceContainer(Context ctx)
        {
            this.ctx = ctx;
            ctx.Conclude();

            ClusteredServiceAgent agent = new ClusteredServiceAgent(ctx);

            serviceAgentRunner = new AgentRunner(ctx.IdleStrategy(), ctx.ErrorHandler(), ctx.ErrorCounter(), agent);
        }
        /// <summary>
        /// Assign Ip address to the subdomain
        /// </summary>
        /// <param name="subdomain">The subdomain</param>
        /// <param name="agentRunner">The Agent</param>
        /// <param name="scriptOutput">The terminal output one line</param>
        /// <param name="cancellationToken">Notification that operations should be canceled</param>
        /// <returns>A task</returns>
        private async Task UpdateSubdomainIpAddressAsync(Subdomain subdomain, AgentRunner agentRunner, ScriptOutput scriptOutput, CancellationToken cancellationToken = default)
        {
            if (Helpers.Helpers.ValidateIPv4(scriptOutput.Ip) && subdomain.IpAddress != scriptOutput.Ip)
            {
                subdomain.IpAddress = scriptOutput.Ip;
                await this.UpdateAsync(subdomain, cancellationToken);

                if (agentRunner.ActivateNotification)
                {
                    await this.notificationService.SendAsync(NotificationType.IP, new[]
        /// <summary>
        /// <see cref="IAgentRunnerService.StopAgentAsync(AgentRunner,  CancellationToken)"></see>
        /// </summary>
        public async Task StopAgentAsync(AgentRunner agentRunner, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var channel = AgentRunnerHelpers.GetChannel(agentRunner);

            await this.StopAgentAsync(channel, cancellationToken);

            await this.agentRunService.DoneOnScopeAsync(agentRunner, channel, true, false, cancellationToken);
        }
        /// <summary>
        /// <see cref="ISaveTerminalOutputParseService.SaveTerminalOutputParseAsync(agentRunnerner, ScriptOutput, CancellationToken)"/>
        /// </summary>
        public async Task SaveTerminalOutputParseAsync(AgentRunner agentRunner, ScriptOutput terminalOutputParse, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (!string.IsNullOrEmpty(terminalOutputParse.Ip))
            {
                await this.UpdateSubdomainIpAddressAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken);
            }

            if (terminalOutputParse.IsAlive != null)
            {
                await this.UpdateSubdomainIsAliveAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken);
            }

            if (terminalOutputParse.HasHttpOpen != null)
            {
                await this.UpdateSubdomainHasHttpOpenAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken);
            }

            if (terminalOutputParse.Takeover != null)
            {
                await this.UpdateSubdomainTakeoverAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken);
            }

            if (!string.IsNullOrEmpty(terminalOutputParse.HttpDirectory))
            {
                await this.UpdateSubdomainDirectoryAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken);
            }

            if (!string.IsNullOrEmpty(terminalOutputParse.Service))
            {
                await this.UpdateSubdomainServiceAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken);
            }

            if (!string.IsNullOrEmpty(terminalOutputParse.Note))
            {
                await this.UpdateSubdomainNoteAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken);
            }

            if (!string.IsNullOrEmpty(terminalOutputParse.Technology))
            {
                await this.UpdateSubdomainTechnologyAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken);
            }

            if (!string.IsNullOrEmpty(terminalOutputParse.HttpScreenshotFilePath) || !string.IsNullOrEmpty(terminalOutputParse.HttpsScreenshotFilePath))
            {
                await this.UpdateSubdomainScreenshotAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse);
            }

            if (!string.IsNullOrWhiteSpace(terminalOutputParse.Label))
            {
                await this.UpdateSubdomainLabelAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken);
            }
        }
Exemple #11
0
        /// <summary>
        /// <see cref="IAgentBackgroundService.SaveOutputParseOnScopeAsync(AgentRunner, ScriptOutput, CancellationToken)"/>
        /// </summary>
        public async Task SaveOutputParseOnScopeAsync(AgentRunner agentRun, ScriptOutput terminalOutputParse, CancellationToken cancellationToken = default)
        {
            using (var scope = this.serviceProvider.CreateScope())
            {
                var targetService =
                    scope.ServiceProvider
                    .GetRequiredService <ITargetService>();

                await targetService.SaveTerminalOutputParseAsync(agentRun, terminalOutputParse, cancellationToken);
            }
        }
        /// <inheritdoc/>
        public async Task TerminalOutputScopeAsync(AgentRunner agentRunner, string channel, string terminalOutput, bool includeTime = true, CancellationToken cancellationToken = default)
        {
            if (!terminalOuputs.ContainsKey(channel))
            {
                terminalOuputs.TryAdd(channel, string.Empty);
            }

            terminalOuputs[channel] += terminalOutput;

            await connectorService.SendAsync(channel, terminalOutput, includeTime, cancellationToken);
        }
        /// <inheritdoc/>
        public async Task DoneOnScopeAsync(AgentRunner agentRunner, string channel, bool stoppedManually, bool fromException, CancellationToken cancellationToken)
        {
            if (agentRunner.ActivateNotification)
            {
                await this.SendNotificationOnScopeAsync($"Agent {agentRunner.Agent.Name} is done!", cancellationToken);
            }

            await this.UpdateLastRunAgentOnScopeAsync(agentRunner.Agent, cancellationToken);


            using (var scope = this.serviceProvider.CreateScope())
            {
                var unitOfWork =
                    scope.ServiceProvider
                    .GetRequiredService <IUnitOfWork>();

                var agentRun = await unitOfWork.Repository <AgentRun>()
                               .GetAllQueryableByCriteria(ar => ar.Agent == agentRunner.Agent && ar.Channel == channel)
                               .OrderByDescending(o => o.CreatedAt)
                               .FirstOrDefaultAsync(cancellationToken);

                if (agentRun != null)
                {
                    if (fromException)
                    {
                        agentRun.Stage       = Entities.Enum.AgentRunStage.FAILED;
                        agentRun.Description = $"The agent {agentRunner.Agent.Name} failed";
                    }
                    else if (stoppedManually)
                    {
                        agentRun.Stage       = Entities.Enum.AgentRunStage.STOPPED;
                        agentRun.Description = $"The agent {agentRunner.Agent.Name} was stopped manually";
                    }
                    else
                    {
                        agentRun.Stage       = Entities.Enum.AgentRunStage.SUCCESS;
                        agentRun.Description = $"The agent {agentRunner.Agent.Name} ran successfully";
                    }

                    if (terminalOuputs.ContainsKey(channel))
                    {
                        agentRun.TerminalOutput = terminalOuputs[channel];
                        terminalOuputs[channel] = string.Empty;
                    }

                    agentRun.TerminalOutput += "\nAgent Done";

                    unitOfWork.Repository <AgentRun>().Update(agentRun);
                    await unitOfWork.CommitAsync();
                }
            }

            await this.connectorService.SendAsync(channel, "Agent Done!", false, cancellationToken);
        }
Exemple #14
0
        /// <summary>
        /// <see cref="ISaveTerminalOutputParseService.SaveTerminalOutputParseAsync(AgentRunner, ScriptOutput, CancellationToken)"/>
        /// </summary>
        public async Task SaveTerminalOutputParseAsync(AgentRunner agentRunner, ScriptOutput terminalOutputParse, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (await this.NeedAddNewRootDomain(agentRunner, terminalOutputParse.RootDomain, cancellationToken))
            {
                agentRunner.RootDomain = await this.AddTargetNewRootDomainAsync(agentRunner.Target, terminalOutputParse.RootDomain, cancellationToken);

                if (agentRunner.ActivateNotification)
                {
                    await this.notificationService.SendAsync(NotificationType.SUBDOMAIN, new[]
        /// <summary>
        /// If we need to run the Agent in each subdomain
        /// </summary>
        /// <param name="agentRunner">The agent run parameters</param>
        /// <returns>If we need to run the Agent in each subdomain</returns>
        private string GetAgentRunnerType(AgentRunner agentRunner)
        {
            var type = agentRunner.Agent.AgentType;

            return(type switch
            {
                AgentTypes.TARGET => agentRunner.Target == null ? AgentRunnerTypes.ALL_TARGETS : AgentRunnerTypes.CURRENT_TARGET,
                AgentTypes.ROOTDOMAIN => agentRunner.RootDomain == null ? AgentRunnerTypes.ALL_ROOTDOMAINS : AgentRunnerTypes.CURRENT_ROOTDOMAIN,
                AgentTypes.SUBDOMAIN => agentRunner.Subdomain == null ? AgentRunnerTypes.ALL_SUBDOMAINS : AgentRunnerTypes.CURRENT_SUBDOMAIN,
                _ => throw new ArgumentException("The Agent need to have valid Type")
            });
        /// <summary>
        /// If ran before (target, rootdomain, subdomain)
        /// </summary>
        /// <param name="agentRunner">The agent runner</param>
        /// <param name="agentTypeTarget">if is the Target the agent type</param>
        /// <param name="agentTypeRootDomain">if is the RootDomain the agent type</param>
        /// <param name="agentTypeSubdomain">if is the Subdomain the agent type</param>
        /// <returns>If ran before (target, rootdomain, subdomain)></returns>
        private static bool RanBefore(AgentRunner agentRunner, bool agentTypeTarget, bool agentTypeRootDomain, bool agentTypeSubdomain)
        {
            var agentRanBeforeInThisTarget = agentTypeTarget && agentRunner.Target != null &&
                                             !string.IsNullOrEmpty(agentRunner.Target.AgentsRanBefore) &&
                                             agentRunner.Target.AgentsRanBefore.Contains(agentRunner.Agent.Name);

            var agentRanBeforeInThisRootDomain = agentTypeRootDomain && agentRunner.RootDomain != null &&
                                                 !string.IsNullOrEmpty(agentRunner.RootDomain.AgentsRanBefore) &&
                                                 agentRunner.RootDomain.AgentsRanBefore.Contains(agentRunner.Agent.Name);

            var agentRanBeforeInThisSubdomain = agentTypeSubdomain && agentRunner.Subdomain != null &&
                                                !string.IsNullOrEmpty(agentRunner.Subdomain.AgentsRanBefore) &&
                                                agentRunner.Subdomain.AgentsRanBefore.Contains(agentRunner.Agent.Name);

            return(agentRanBeforeInThisTarget || agentRanBeforeInThisRootDomain || agentRanBeforeInThisSubdomain);
        }
        /// <summary>
        /// <see cref="ISaveTerminalOutputParseService.UpdateAgentRanAsync(AgentRunner, CancellationToken)"/>
        /// </summary>
        public async Task UpdateAgentRanAsync(AgentRunner agentRunner, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var subdomain = agentRunner.Subdomain;
            var agentName = agentRunner.Agent.Name;

            if (string.IsNullOrWhiteSpace(subdomain.AgentsRanBefore))
            {
                subdomain.AgentsRanBefore = agentName;
                await this.UpdateAsync(subdomain, cancellationToken);
            }
            else if (!subdomain.AgentsRanBefore.Contains(agentName))
            {
                subdomain.AgentsRanBefore = string.Join(", ", subdomain.AgentsRanBefore, agentName);
                await this.UpdateAsync(subdomain, cancellationToken);
            }
        }
Exemple #18
0
        /// <summary>
        /// <see cref="ISaveTerminalOutputParseService.UpdateAgentRanAsync(AgentRunner, CancellationToken)"/>
        /// </summary>
        public async Task UpdateAgentRanAsync(AgentRunner agentRunner, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var target    = agentRunner.Target;
            var agentName = agentRunner.Agent.Name;

            if (string.IsNullOrWhiteSpace(target.AgentsRanBefore))
            {
                target.AgentsRanBefore = agentName;
                await this.UpdateAsync(target, cancellationToken);
            }
            else if (!target.AgentsRanBefore.Contains(agentName))
            {
                target.AgentsRanBefore = string.Join(", ", target.AgentsRanBefore, agentName);
                await this.UpdateAsync(target, cancellationToken);
            }
        }
        /// <summary>
        /// Check if we need to skip the subdomain and does not the agent in that subdomain
        /// </summary>
        /// <param name="agentRunner">The agent runner</param>
        /// <param name="agentRunnerType">The agent runner type</param>
        public static bool NeedToSkipRun(AgentRunner agentRunner, string agentRunnerType)
        {
            var agentTrigger = agentRunner.Agent.AgentTrigger;

            if (agentTrigger == null)
            {
                return(false);
            }

            var agentTypeTarget     = AgentRunnerTypes.CURRENT_TARGET.Equals(agentRunnerType) || AgentRunnerTypes.ALL_DIRECTORIES.Equals(agentRunnerType);
            var agentTypeRootDomain = AgentRunnerTypes.CURRENT_ROOTDOMAIN.Equals(agentRunnerType) || AgentRunnerTypes.ALL_ROOTDOMAINS.Equals(agentRunnerType);
            var agentTypeSubdomain  = AgentRunnerTypes.CURRENT_SUBDOMAIN.Equals(agentRunnerType) || AgentRunnerTypes.ALL_SUBDOMAINS.Equals(agentRunnerType);


            return((agentTrigger.SkipIfRunBefore && RanBefore(agentRunner, agentTypeTarget, agentTypeRootDomain, agentTypeSubdomain)) ||
                   (agentTypeTarget && SkipTarget(agentRunner.Target, agentTrigger)) ||
                   (agentTypeRootDomain && SkipRootDomain(agentRunner.RootDomain, agentTrigger)) ||
                   (agentTypeSubdomain && SkipSubdomain(agentRunner.Subdomain, agentTrigger)));
        }
        /// <summary>
        /// Obtain the channel, we use the channel to send notification to the frontend (tarminal and logs)
        /// and to register the Runners process
        /// </summary>
        /// <param name="agent">The agent</param>
        /// <param name="rootDomain">The domain</param>
        /// <param name="subdomain">The subdomain</param>
        /// <returns>The channel to send the menssage</returns>
        public static string GetChannel(AgentRunner agentRunner)
        {
            if (agentRunner.Target == null)
            {
                return($"{agentRunner.Agent.Name}");
            }

            if (agentRunner.RootDomain == null)
            {
                return($"{agentRunner.Agent.Name}_{agentRunner.Target.Name}");
            }

            if (agentRunner.Subdomain == null)
            {
                return($"{agentRunner.Agent.Name}_{agentRunner.Target.Name}_{agentRunner.RootDomain.Name}");
            }

            return($"{agentRunner.Agent.Name}_{agentRunner.Target.Name}_{agentRunner.RootDomain.Name}_{agentRunner.Subdomain.Name}");
        }
Exemple #21
0
        /// <summary>
        /// <see cref="IAgentRunnerService.StopAgentAsync(AgentRunner, string, CancellationToken)"></see>
        /// </summary>
        public async Task StopAgentAsync(AgentRunner agentRunner, string channel, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            try
            {
                if (!(await this.agentRunnerProvider.IsStoppedAsync(channel)))
                {
                    await this.agentRunnerProvider.StopAsync(channel);

                    await this.SendAgentDoneNotificationAsync(agentRunner, channel, cancellationToken);
                }
            }
            catch (Exception ex)
            {
                await this.connectorService.SendAsync(channel, ex.Message, true, cancellationToken);

                await this.SendAgentDoneNotificationAsync(agentRunner, channel, cancellationToken);
            }
        }
        /// <inheritdoc/>
        public async Task StartOnScopeAsync(AgentRunner agentRunner, string channel, CancellationToken cancellationToken = default)
        {
            var agentRun = new AgentRun
            {
                Agent       = agentRunner.Agent,
                Channel     = channel,
                Description = $"Start running the agent {agentRunner.Agent.Name}",
                Stage       = Entities.Enum.AgentRunStage.RUNNING
            };

            using (var scope = this.serviceProvider.CreateScope())
            {
                var unitOfWork =
                    scope.ServiceProvider
                    .GetRequiredService <IUnitOfWork>();

                unitOfWork.Repository <AgentRun>().Update(agentRun);
                await unitOfWork.CommitAsync();
            }
        }
Exemple #23
0
        /// <summary>
        /// <see cref="IAgentRunnerService.RunAgentAsync(AgentRunner, CancellationToken)"></see>
        /// </summary>
        public async Task RunAgentAsync(AgentRunner agentRunner, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            Thread.Sleep(1000);

            var channel = AgentRunnerHelpers.GetChannel(agentRunner);

            await this.agentRunnerProvider.InitializesAsync(channel);

            var agentRunnerType = this.GetAgentRunnerType(agentRunner);

            if (agentRunnerType.Contains("Current"))
            {
                await this.RunAgentAsync(agentRunner, channel, agentRunnerType, last : true);
            }
            else
            {
                await this.RunAgenthInEachSubConceptAsync(agentRunner, channel, agentRunnerType, cancellationToken);
            }
        }
        private ClusteredServiceContainer(Context ctx)
        {
            this.ctx = ctx;

            try
            {
                ctx.Conclude();
            }
            catch (Exception)
            {
                if (null != ctx.MarkFile())
                {
                    ctx.MarkFile().SignalFailedStart();
                }

                throw;
            }

            ClusteredServiceAgent agent = new ClusteredServiceAgent(ctx);

            serviceAgentRunner = new AgentRunner(ctx.IdleStrategy(), ctx.ErrorHandler(), ctx.ErrorCounter(), agent);
        }
        /// <summary>
        /// <see cref="IAgentRunnerService.RunAgentAsync(AgentRunner, CancellationToken)"></see>
        /// </summary>
        public async Task RunAgentAsync(AgentRunner agentRunner, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var channel = AgentRunnerHelpers.GetChannel(agentRunner);

            await this.agentRunnerProvider.InitializesAsync(channel);

            _logger.Info($"Start channel {channel}");

            await this.agentRunService.StartOnScopeAsync(agentRunner, channel, cancellationToken);

            var agentRunnerType = this.GetAgentRunnerType(agentRunner);

            if (agentRunnerType.StartsWith("Current"))
            {
                await this.RunAgentAsync(agentRunner, channel, agentRunnerType, last : true, allowSkip : false);
            }
            else
            {
                await this.RunAgenthInEachSubConceptAsync(agentRunner, channel, agentRunnerType, cancellationToken);
            }
        }
Exemple #26
0
        /// <inheritdoc/>
        public async Task UpdateAgentOnScopeAsync(AgentRunner agentRun, string agentRunType, CancellationToken cancellationToken = default)
        {
            using var scope = this.serviceProvider.CreateScope();

            if (AgentRunnerTypes.ALL_TARGETS.Equals(agentRunType, StringComparison.CurrentCultureIgnoreCase) ||
                AgentRunnerTypes.CURRENT_TARGET.Equals(agentRunType, StringComparison.CurrentCultureIgnoreCase))
            {
                var targetService = scope.ServiceProvider.GetRequiredService <ITargetService>();
                await targetService.UpdateAgentRanAsync(agentRun.Target, agentRun.Agent.Name, cancellationToken);
            }
            else if (AgentRunnerTypes.ALL_ROOTDOMAINS.Equals(agentRunType, StringComparison.CurrentCultureIgnoreCase) ||
                     AgentRunnerTypes.CURRENT_ROOTDOMAIN.Equals(agentRunType, StringComparison.CurrentCultureIgnoreCase))
            {
                var rootDomainService = scope.ServiceProvider.GetRequiredService <IRootDomainService>();
                await rootDomainService.UpdateAgentRanAsync(agentRun.RootDomain, agentRun.Agent.Name, cancellationToken);
            }
            else if (AgentRunnerTypes.ALL_SUBDOMAINS.Equals(agentRunType, StringComparison.CurrentCultureIgnoreCase) ||
                     AgentRunnerTypes.CURRENT_SUBDOMAIN.Equals(agentRunType, StringComparison.CurrentCultureIgnoreCase))
            {
                var subdomainService = scope.ServiceProvider.GetRequiredService <ISubdomainService>();
                await subdomainService.UpdateAgentRanAsync(agentRun.Subdomain, agentRun.Agent.Name, cancellationToken);
            }
        }
Exemple #27
0
 /// <summary>
 /// <see cref="IAgentBackgroundService.SaveOutputParseOnScopeAsync(AgentRunner, string, ScriptOutput, CancellationToken)"/>
 /// </summary>
 public async Task SaveOutputParseOnScopeAsync(AgentRunner agentRun, string agentRunType, ScriptOutput terminalOutputParse, CancellationToken cancellationToken = default)
 {
     using (var scope = this.serviceProvider.CreateScope())
     {
         if (AgentRunnerTypes.ALL_TARGETS.Equals(agentRunType, StringComparison.CurrentCultureIgnoreCase) ||
             AgentRunnerTypes.CURRENT_TARGET.Equals(agentRunType, StringComparison.CurrentCultureIgnoreCase))
         {
             var targetService = scope.ServiceProvider.GetRequiredService <ITargetService>();
             await targetService.SaveTerminalOutputParseAsync(agentRun.Target, agentRun.Agent.Name, agentRun.ActivateNotification, terminalOutputParse, cancellationToken);
         }
         else if (AgentRunnerTypes.ALL_ROOTDOMAINS.Equals(agentRunType, StringComparison.CurrentCultureIgnoreCase) ||
                  AgentRunnerTypes.CURRENT_ROOTDOMAIN.Equals(agentRunType, StringComparison.CurrentCultureIgnoreCase))
         {
             var rootDomainService = scope.ServiceProvider.GetRequiredService <IRootDomainService>();
             await rootDomainService.SaveTerminalOutputParseAsync(agentRun.RootDomain, agentRun.Agent.Name, agentRun.ActivateNotification, terminalOutputParse, cancellationToken);
         }
         else if (AgentRunnerTypes.ALL_SUBDOMAINS.Equals(agentRunType, StringComparison.CurrentCultureIgnoreCase) ||
                  AgentRunnerTypes.CURRENT_SUBDOMAIN.Equals(agentRunType, StringComparison.CurrentCultureIgnoreCase))
         {
             var subdomainService = scope.ServiceProvider.GetRequiredService <ISubdomainService>();
             await subdomainService.SaveTerminalOutputParseAsync(agentRun.Subdomain, agentRun.Agent.Name, agentRun.ActivateNotification, terminalOutputParse, cancellationToken);
         }
     }
 }
 private ClusteredServiceContainer Start()
 {
     AgentRunner.StartOnThread(serviceAgentRunner, ctx.ThreadFactory());
     return(this);
 }
Exemple #29
0
 private Aeron Start()
 {
     AgentRunner.StartOnThread(_conductorRunner, _ctx.ThreadFactory());
     return(this);
 }
 /// <summary>
 /// Starts this instance.
 /// </summary>
 internal void Start()
 {
     this.AutoLog = true;
     AgentRunner agentRunner = new AgentRunner();
     agentRunner.ToString();
 }
Exemple #31
0
 internal Aeron(Context ctx)
 {
     _ctx             = ctx.Conclude();
     _conductor       = ctx.CreateClientConductor();
     _conductorRunner = ctx.CreateConductorRunner(_conductor);
 }