public override void DoWork()
        {
            // Input parameters:
            //  - SERVER_NAME
            //  - EXECUTABLE_PATH

            BackgroundTask topTask = TaskManager.TopTask;

            // get input parameters
            string serverName = (string)topTask.GetParamValue("SERVER_NAME");
            string execPath = (string)topTask.GetParamValue("EXECUTABLE_PATH");
            string execParams = (string)topTask.GetParamValue("EXECUTABLE_PARAMS");

            if (execParams == null)
                execParams = "";

            // check input parameters
            if (String.IsNullOrEmpty(serverName))
            {
                TaskManager.WriteWarning("Specify 'Server Name' task parameter");
                return;
            }

            if (String.IsNullOrEmpty(execPath))
            {
                TaskManager.WriteWarning("Specify 'Executable Path' task parameter");
                return;
            }

            // find server by name
            ServerInfo server = ServerController.GetServerByName(serverName);
            if (server == null)
            {
                TaskManager.WriteWarning(String.Format("Server with the name '{0}' was not found", serverName));
                return;
            }

            // execute system command
            WindowsServer winServer = new WindowsServer();
            ServiceProviderProxy.ServerInit(winServer, server.ServerId);
            TaskManager.Write(winServer.ExecuteSystemCommand(execPath, execParams));
        }
 private static WindowsServer GetServerService(int serverId)
 {
     WindowsServer winServer = new WindowsServer();
     ServiceProviderProxy.ServerInit(winServer, serverId);
     return winServer;
 }
示例#3
0
        public override void DoWork()
        {
            // Input parameters:
            //  - FILE_PATH
            //  - FTP_SERVER
            //  - FTP_USERNAME
            //  - FTP_PASSWORD
            //  - FTP_FOLDER

            BackgroundTask topTask = TaskManager.TopTask;

            // get input parameters
            string filePath = (string)topTask.GetParamValue("FILE_PATH");
            string ftpServer = (string)topTask.GetParamValue("FTP_SERVER");
            string ftpUsername = (string)topTask.GetParamValue("FTP_USERNAME");
            string ftpPassword = (string)topTask.GetParamValue("FTP_PASSWORD");
            string ftpFolder = (string)topTask.GetParamValue("FTP_FOLDER");

            // check input parameters
            if (String.IsNullOrEmpty(filePath))
            {
                TaskManager.WriteWarning("Specify 'File' task parameter");
                return;
            }

            if (String.IsNullOrEmpty(ftpServer))
            {
                TaskManager.WriteWarning("Specify 'FTP Server' task parameter");
                return;
            }

            // substitute parameters
            DateTime d = DateTime.Now;
            string date = d.ToString("yyyyMMdd");
            string time = d.ToString("HHmm");

            filePath = Utils.ReplaceStringVariable(filePath, "date", date);
            filePath = Utils.ReplaceStringVariable(filePath, "time", time);

            // build FTP command file
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);

            // FTP server
            writer.WriteLine("open " + ftpServer);

            // check if anonymous mode
            if (String.IsNullOrEmpty(ftpUsername))
            {
                ftpUsername = "******";
                ftpPassword = "******";
            }

            // FTP username/password
            writer.WriteLine(ftpUsername);
            writer.WriteLine(ftpPassword);

            // check if we need to change remote folder
            if (!String.IsNullOrEmpty(ftpFolder))
            {
                writer.WriteLine("cd " + ftpFolder.Replace("\\", "/"));
            }

            // file to send
            writer.WriteLine("binary");
            writer.WriteLine("put " + FilesController.GetFullPackagePath(topTask.PackageId, filePath));

            // bye
            writer.WriteLine("bye");

            string cmdBatch = sb.ToString();

            // create temp file in user space
            string cmdPath = Utils.GetRandomString(10) + ".txt";
            string fullCmdPath = FilesController.GetFullPackagePath(topTask.PackageId, cmdPath);

            // upload batch
            FilesController.UpdateFileBinaryContent(topTask.PackageId, cmdPath, Encoding.UTF8.GetBytes(cmdBatch));

            // execute system command
            // load OS service
            int serviceId = PackageController.GetPackageServiceId(topTask.PackageId, ResourceGroups.Os);

            // load service
            ServiceInfo service = ServerController.GetServiceInfo(serviceId);
            if (service == null)
                return;

            WindowsServer winServer = new WindowsServer();
            ServiceProviderProxy.ServerInit(winServer, service.ServerId);
            TaskManager.Write(winServer.ExecuteSystemCommand("ftp.exe", "-s:" + fullCmdPath));

            // delete batch file
            FilesController.DeleteFiles(topTask.PackageId, new string[] { cmdPath });
        }
        public override void DoWork()
        {
            BackgroundTask topTask = TaskManager.TopTask;

            List<DomainDnsChanges> domainsChanges = new List<DomainDnsChanges>();
            var domainUsers = new Dictionary<int, UserInfo>();

            // get input parameters
            string dnsServersString = (string)topTask.GetParamValue(DnsServersParameter);
            string serverName = (string)topTask.GetParamValue(ServerNameParameter);

            int pause;

                        // check input parameters
            if (String.IsNullOrEmpty(dnsServersString))
            {
                TaskManager.WriteWarning("Specify 'DNS' task parameter.");
                return;
            }

            if (String.IsNullOrEmpty((string)topTask.GetParamValue("MAIL_TO")))
            {
                TaskManager.WriteWarning("The e-mail message has not been sent because 'Mail To' is empty.");
                return;
            }


            if (!int.TryParse((string)topTask.GetParamValue(PauseBetweenQueriesParameter), out pause))
            {
                TaskManager.WriteWarning("The 'pause between queries' parameter is not valid.");
                return;
            }

            // find server by name
            ServerInfo server = ServerController.GetServerByName(serverName);
            if (server == null)
            {
                TaskManager.WriteWarning(String.Format("Server with the name '{0}' was not found", serverName));
                return;
            }

            WindowsServer winServer = new WindowsServer();
            ServiceProviderProxy.ServerInit(winServer, server.ServerId);

            var user = UserController.GetUser(topTask.UserId);

            var dnsServers = dnsServersString.Split(';');

            var packages = ObjectUtils.CreateListFromDataReader<PackageInfo>(DataProvider.GetAllPackages());


            foreach (var package in packages)
            {
                var domains = ServerController.GetDomains(package.PackageId);

                domains = domains.Where(x => !x.IsSubDomain && !x.IsDomainPointer).ToList(); //Selecting top-level domains

                //domains = domains.Where(x => x.ZoneItemId > 0).ToList(); //Selecting only dns enabled domains

                foreach (var domain in domains)
                {
                    if (domainsChanges.Any(x => x.DomainName == domain.DomainName))
                    {
                        continue;
                    }

                    if (!domainUsers.ContainsKey(domain.PackageId))
                    {
                        var domainUser = UserController.GetUser(packages.First(x=>x.PackageId == domain.PackageId).UserId);

                        domainUsers.Add(domain.PackageId, domainUser);
                    }

                    DomainDnsChanges domainChanges = new DomainDnsChanges();
                    domainChanges.DomainName = domain.DomainName;
                    domainChanges.PackageId = domain.PackageId;
                    domainChanges.Registrar = domain.RegistrarName;
                    domainChanges.ExpirationDate = domain.ExpirationDate;

                    var dbDnsRecords = ObjectUtils.CreateListFromDataReader<DnsRecordInfo>(DataProvider.GetDomainAllDnsRecords(domain.DomainId));

                    //execute server
                    foreach (var dnsServer in dnsServers)
                    {
                        var dnsMxRecords = GetDomainDnsRecords(winServer, domain.DomainName, dnsServer, DnsRecordType.MX, pause) ?? dbDnsRecords.Where(x => x.RecordType == DnsRecordType.MX).ToList();
                        var dnsNsRecords = GetDomainDnsRecords(winServer, domain.DomainName, dnsServer, DnsRecordType.NS, pause) ?? dbDnsRecords.Where(x => x.RecordType == DnsRecordType.NS).ToList();

                        FillRecordData(dnsMxRecords, domain, dnsServer);
                        FillRecordData(dnsNsRecords, domain, dnsServer);

                        domainChanges.DnsChanges.AddRange(ApplyDomainRecordsChanges(dbDnsRecords.Where(x => x.RecordType == DnsRecordType.MX), dnsMxRecords, dnsServer));
                        domainChanges.DnsChanges.AddRange(ApplyDomainRecordsChanges(dbDnsRecords.Where(x => x.RecordType == DnsRecordType.NS), dnsNsRecords, dnsServer));

                        domainChanges.DnsChanges = CombineDnsRecordChanges(domainChanges.DnsChanges, dnsServer).ToList();
                    }

                    domainsChanges.Add(domainChanges);
                }
            }

            var changedDomains = FindDomainsWithChangedRecords(domainsChanges);

            SendMailMessage(user, changedDomains, domainUsers);
        }
        public List<DnsRecordInfo> GetDomainDnsRecords(WindowsServer winServer, string domain, string dnsServer, DnsRecordType recordType, int pause)
        {
            Thread.Sleep(pause);

            //nslookup -type=mx google.com 195.46.39.39
            var command = "nslookup";
            var args = string.Format("-type={0} {1} {2}", recordType, domain, dnsServer);

            // execute system command
            var raw  = string.Empty;
            int triesCount = 0;

            do
            {
                raw = winServer.ExecuteSystemCommand(command, args);
            } 
            while (raw.ToLowerInvariant().Contains(DnsTimeOutMessage) && ++triesCount < DnsTimeOutRetryCount);

            //timeout check 
            if (raw.ToLowerInvariant().Contains(DnsTimeOutMessage))
            {
                return null;
            }

            var records = ParseNsLookupResult(raw, dnsServer, recordType);

            return records.ToList();
        }