Example #1
0
        public void StartClient()
        {
            socket         = new WebSocket(wsURI);
            socket.Opened += (x, y) => ConnectionOpened?.Invoke();
            socket.Open();
            socket.MessageReceived += InvokeMessageReceived;

            socket.Error += (sender, args) => NewErrorMessage?.Invoke(args.Exception.Message);
        }
Example #2
0
        public static void StartClient()
        {
            string protocol = ConfigurationManager.AppSettings["protocol"];

            switch (protocol)
            {
            case "ws":
                string uri = ConfigurationManager.AppSettings["wsuri"];
                ConnectionClient = new WSClient(uri);
                break;

            default: break;
            }
            ConnectionClient.NewErrorMessage  += (x) => NewErrorMessage?.Invoke(x);
            ConnectionClient.responseReceived += (x) => responseReceived?.Invoke(x);

            ConnectionClient.ConnectionOpened += () => ConnectionOpened?.Invoke();
            ConnectionClient.StartClient();
        }
        public async Task Handle(LoadModule moduleRequest, string replyTo, string correlationId)
        {
            Console.WriteLine($"[i] Got New LoadModule Message.");

            Module module = _taskRepository.GetModule(moduleRequest.Name, moduleRequest.Language);

            string moduleb64  = "";
            string workingDir = Path.Join(Settings.ModulesPath, Settings.LanguageName);
            string exitCode   = "0";
            string output     = "";
            string error      = "";

            if (!String.IsNullOrEmpty(module.BuildCommand))
            {
                Dictionary <string, string> cmdResult = RunCommand(workingDir, module.BuildCommand);
                exitCode = cmdResult["ExitCode"];
                output   = cmdResult["Output"];
                error    = cmdResult["Error"];
            }

            if (exitCode == "0")
            {
                byte[] moduleBytes = File.ReadAllBytes(Path.Join(workingDir, module.BuildLocation));
                moduleb64 = Convert.ToBase64String(moduleBytes);
            }

            if (String.IsNullOrEmpty(moduleb64))
            {
                NewErrorMessage response = new NewErrorMessage();
                response.Source  = ".NET Build Server";
                response.Message = $"Error building {moduleRequest.Name} module.";
                response.Details = $"Stdout: {output}\n Stderr: {error}";
                _eventBus.Publish(response, replyTo = null, correlationId = null);
            }
            else
            {
                ModuleResponse response = new ModuleResponse();
                response.Success  = true;
                response.Contents = $"module {moduleRequest.Name} {moduleb64}";
                _eventBus.Publish(response, replyTo, correlationId);
            }
        }
Example #4
0
        public static void HandleNewError(NewErrorMessage msg)
        {
            Exception exception = msg.Exception;

            string from = WebApplication.Instance.SmtpSettings.Username;
            string to   = WebApplication.Instance.SupportEmail;
            string body = "<b>Message</b>: " + exception.Message +
                          "</br><b>InnerException</b>: " + exception.InnerException +
                          "</br><b>Source</b>: " + exception.Source +
                          "</br><b>StackTrace</b>: " + exception.StackTrace;

            MailMessage message = new MailMessage(from, to);

            message.Subject    = String.Format("ERROR has occurred in Mooketplace");
            message.Body       = body;
            message.IsBodyHtml = true;
            message.Priority   = MailPriority.High;

            PostMailPort.Post(message);
        }
Example #5
0
 static Client()
 {
     ConnectionClient = new WSClient("ws://127.0.0.1:9898");
     ConnectionClient.NewErrorMessage  += (x) => NewErrorMessage?.Invoke(x);
     ConnectionClient.responseReceived += (x) => responseReceived?.Invoke(x);
 }
Example #6
0
        public async Task Handle(NewPayload newPayload, string replyTo, string correlationId)
        {
            Console.WriteLine($"[i] Got New Payload Message.");
            Payload payload = new Payload();

            payload.AgentType                  = _taskRepository.GetAgentType(newPayload.AgentTypeId);
            payload.AgentTransportType         = _taskRepository.GetAgentTransportType(newPayload.AgentTransportTypeId);
            payload.Transport                  = _taskRepository.GetTransport(newPayload.TransportId);
            payload.AgentTypeArchitectureId    = newPayload.ArchitectureId;
            payload.AgentTypeFormatId          = newPayload.FormatId;
            payload.AgentTypeVersionId         = newPayload.VersionId;
            payload.AgentTypeConfigurationId   = newPayload.AgentTypeConfigurationId;
            payload.AgentTypeOperatingSystemId = newPayload.OperatingSystemId;
            payload.Debug = newPayload.Debug;

            payload.Name           = newPayload.Name;
            payload.Description    = newPayload.Description;
            payload.Jitter         = newPayload.Jitter;
            payload.BeaconInterval = newPayload.BeaconInterval;
            payload.ExpirationDate = newPayload.ExpirationDate;
            payload.BuildToken     = newPayload.BuildToken;

            payload.Created    = DateTime.UtcNow;
            payload.Enabled    = true;
            payload.Visible    = true;
            payload.Built      = false;
            payload.Key        = Utility.GenerateSecureString(32);
            payload.LanguageId = Settings.LanguageId;
            _taskRepository.Add(payload);
            _eventBus.Publish(payload, replyTo, correlationId);

            string workingDir = Path.Join(Settings.AgentsPath, payload.AgentType.Name);

            // Create build config file
            BuildConfig buildConfig = CreateBuildConfig(payload);

            string buildConfigFile = Path.GetTempFileName();

            File.AppendAllText(buildConfigFile, JsonConvert.SerializeObject(buildConfig, Formatting.Indented));

            // Build transport first
            File.Delete(Path.Join(workingDir, payload.AgentTransportType.BuildLocation));

            string transportBuildCommand = $"{payload.AgentTransportType.BuildCommand} {buildConfigFile}";

            Dictionary <string, string> cmdResult = RunCommand(workingDir, transportBuildCommand);
            string transportB64 = "";

            if (cmdResult["ExitCode"] == "0")
            {
                byte[] transportBytes = File.ReadAllBytes(Path.Join(workingDir, payload.AgentTransportType.BuildLocation));
                transportB64 = Convert.ToBase64String(transportBytes);
                File.Delete(buildConfigFile);
            }
            else
            {
                Console.WriteLine($"ERROR DURING TRANSPORT BUILD: \nStdout: {cmdResult["Output"]}\n Stderr: {cmdResult["Error"]}");
                NewErrorMessage response = new NewErrorMessage();
                response.Source  = ".NET Build Server";
                response.Message = $"Error building {payload.AgentType.Name}";
                response.Details = $"Stdout: {cmdResult["Output"]}\n Stderr: {cmdResult["Error"]}";
                _eventBus.Publish(response, replyTo = null, correlationId = null);
            }

            // Build the agent
            if (!String.IsNullOrEmpty(transportB64))
            {
                buildConfig.TransportModule = transportB64;
                File.AppendAllText(buildConfigFile, JsonConvert.SerializeObject(buildConfig, Formatting.Indented));

                File.Delete(Path.Join(workingDir, payload.AgentType.BuildLocation));
                string buildCommand = $"{payload.AgentType.BuildCommand} {buildConfigFile}";
                cmdResult = RunCommand(workingDir, buildCommand);

                if (cmdResult["ExitCode"] == "0")
                {
                    try {
                        Console.WriteLine($"[PayloadBuildService] Build Successful!");
                        string originalPath  = Path.Join(workingDir, payload.AgentType.BuildLocation);
                        string fileExtension = Path.GetExtension(originalPath);
                        string payloadPath   = Path.Join(Settings.AgentsPath, "/build/", $"{payload.AgentType.Name}_{payload.AgentTypeConfiguration.Name}_{payload.Name}_{DateTime.Now.ToString("yyyyMMddHHmmss")}{fileExtension}");
                        Console.WriteLine($"[PayloadBuildService] Moving from {originalPath} to {payloadPath}");
                        File.Move(originalPath, payloadPath);
                        string    uploadUlr = $"{apiUrl}/{payload.Id}/file/";
                        WebClient wc        = new WebClient();
                        wc.Headers.Add("build-token", payload.BuildToken);
                        Console.WriteLine($"[PayloadBuildService] Uploading to {uploadUlr} with token {payload.BuildToken}");
                        byte[] resp = wc.UploadFile(uploadUlr, payloadPath);
                        Console.WriteLine($"[PayloadBuildService] Response: {wc.Encoding.GetString(resp)}");
                        //File.Delete(buildConfigFile);
                    }
                    catch (Exception e) {
                        Console.WriteLine($"ERROR UPLOADING PAYLOAD TO API: \n{e.Message}");
                        NewErrorMessage response = new NewErrorMessage();
                        response.Source  = ".NET Build Server";
                        response.Message = $"Error uploading {payload.AgentType.Name} payload to API";
                        response.Details = $"{e.Message}";
                        _eventBus.Publish(response, replyTo = null, correlationId = null);
                    }
                }
                else
                {
                    Console.WriteLine($"ERROR DURING AGENT BUILD: \nStdout: {cmdResult["Output"]}\n Stderr: {cmdResult["Error"]}");
                    NewErrorMessage response = new NewErrorMessage();
                    response.Source  = ".NET Build Server";
                    response.Message = $"Error building {payload.AgentType.Name}";
                    response.Details = $"Stdout: {cmdResult["Output"]}\n Stderr: {cmdResult["Error"]}";
                    _eventBus.Publish(response, replyTo = null, correlationId = null);
                }
            }
            else
            {
                Console.WriteLine($"ERROR DURING AGENT BUILD: \nStdout: {cmdResult["Output"]}\n Stderr: {cmdResult["Error"]}");
                NewErrorMessage response = new NewErrorMessage();
                response.Source  = ".NET Build Server";
                response.Message = $"Error building {payload.AgentType.Name}";
                response.Details = $"Tried to build an agent without a Base64 encoded transport string. Transport build must have failed.";
                _eventBus.Publish(response, replyTo = null, correlationId = null);
            }
        }