/// <summary>
 /// Adds a new site to the configuration file
 /// </summary>
 /// <param name="siteName">The name of the site</param>
 private void AddSite(String siteName = "")
 {
     siteName = siteName == "" ? CommandUtils.Ask("Site Name?") : siteName;
     if (Program.Settings.Sites.Count(x => x.Site.ToLower() == siteName.ToLower()) == 0)
     {
         SiteCredentials credentials = new SiteCredentials();
         if (credentials.UpdateData())
         {
             Program.Settings.Sites = Program.Settings.Sites.Union(new SiteDefinition[] {
                 new SiteDefinition()
                 {
                     Site = siteName, Data = credentials
                 }
             }).ToArray();
             Program.Settings.Save();
             Console.WriteLine(MSG_INF_SITE_ADDED, siteName);
         }
         else
         {
             Console.WriteLine(MSG_ERR_BAD_CRED);
         }
     }
     else
     {
         Console.WriteLine(MSG_ERR_SITE_EXISTS, siteName);
     }
 }
Exemple #2
0
        /// <summary>
        /// Defines a SSH transaction
        /// </summary>
        /// <param name="_cred">The SSH transaction credentials</param>
        /// <param name="task">The Transaction task</param>
        /// <param name="remotePath">Changes how the remote path is transformed before
        /// it is passed to the scp command on the remote server.
        /// DoubleQuote, ShellQuote, None</param>
        /// <returns>The transaction result</returns>
        public static Object SSHTransaction(SiteCredentials _cred, Func <AuraSftpClient, Object> task, IRemotePathTransformation remotePath = null)
        {
            Object result = null;

            using (var client = new AuraSftpClient(_cred))
            {
                try
                {
                    if (remotePath == null)
                    {
                        client.RemotePathTransformation = ShellQuote;
                    }
                    else
                    {
                        client.RemotePathTransformation = remotePath;
                    }
                    client.Connect();
                    result = task(client);
                    client.Disconnect();
                }
                catch (System.Exception exc)
                {
                    Console.WriteLine(exc.Message);
                }
            }
            return(result);
        }
        private void LoadSiteCredentials()
        {
            if (SiteCredentialsCache == null)
            {
                lock (_siteCredentialsCacheLock)
                {
                    if (SiteCredentialsCache != null)
                    {
                        return;
                    }

                    SiteCredentialsCache = new List <SiteCredentials>();
                    DirectoryInfo di   = new DirectoryInfo(HttpContext.Server.MapPath("~"));
                    string        json = System.IO.File.ReadAllText(Path.Combine(di.FullName, ConfigurationManager.AppSettings["CredentialsFile"]));
                    JObject       j    = JObject.Parse(json);
                    foreach (JToken token in j["credentials"].Children())
                    {
                        SiteCredentials credential = new SiteCredentials
                        {
                            Prefix   = token.Value <string>("prefix"),
                            Username = token.Value <string>("username"),
                            Password = token.Value <string>("password")
                        };

                        SiteCredentialsCache.Add(credential);
                    }
                }
            }
        }
        public virtual void Initialize([NotNull] string server, [NotNull] SiteCredentials credentials)
        {
            Assert.ArgumentNotNull(server, nameof(server));
            Assert.ArgumentNotNull(credentials, nameof(credentials));

            Server      = server;
            Credentials = credentials;
        }
Exemple #5
0
 /// <summary>
 /// Validates that the site credentials are valid
 /// </summary>
 /// <param name="credentials">The site credentials</param>
 /// <returns>True if the site credentials are valid</returns>
 public static bool IsValid(this SiteCredentials credentials)
 {
     if (credentials.Port == 0)
     {
         credentials.Port = 22;
     }
     return(credentials.Host.Length > 0 && credentials.User.Length > 0 && credentials.RootDir.Length > 0);
 }
Exemple #6
0
        /// <summary>
        /// Downloads a file
        /// </summary>
        /// <param name="credentials">The ssh site credentials</param>
        /// <param name="fileMap">The mapping file to download</param>
        /// <param name="replace">True if the downloaded file will be replaced</param>
        public static void Download(this SiteCredentials credentials, MappedPath fileMap, Boolean replace)
        {
            FileInfo sC, lC;

            AuraSftpClient.SSHTransactionVoid(credentials, (AuraSftpClient c) => {
                sC = new FileInfo(fileMap.GetFullServerCopy());
                lC = new FileInfo(fileMap.GetFullProjectCopy());
                DownloadFile(c, fileMap.GetFullRemotePath(), sC, lC, replace);
            });
        }
Exemple #7
0
        /// <summary>
        /// Updates the site data via the user request
        /// </summary>
        /// <param name="site">The site to update</param>
        /// <returns>True if the user updated data is valid</returns>
        public static Boolean UpdateData(this SiteCredentials site)
        {
            var variables = typeof(SiteCredentials).GetFields();

            foreach (var v in variables)
            {
                UpdateField(site, v);
            }
            return(SiteUtils.IsValid(site));
        }
        private void SetWebClientAuthentication(WebClient wc, string url)
        {
            SiteCredentials credentials =
                SiteCredentialsCache.FirstOrDefault(
                    sc => url.StartsWith(sc.Prefix, StringComparison.InvariantCultureIgnoreCase));

            if (credentials == null)
            {
                return;
            }

            wc.Credentials = new NetworkCredential(credentials.Username, credentials.Password);
        }
Exemple #9
0
 /// <summary>
 /// Defines a SFTP Client transaction
 /// </summary>
 /// <param name="_cred">The SSH transaction credentials</param>
 /// <param name="task">The Transaction task</param>
 public static void SFTPTransactionVoid(SiteCredentials _cred, Action <Renci.SshNet.SftpClient> task)
 {
     using (var client = new Renci.SshNet.SftpClient(_cred.Host, _cred.Port, _cred.User, _cred.Password))
     {
         try
         {
             client.Connect();
             task(client);
             client.Disconnect();
         }
         catch (System.Exception exc)
         {
             Console.WriteLine(exc.Message);
         }
     }
 }
Exemple #10
0
        private void SetupPanels()
        {
            SiteInformation pSInInfo = new SiteInformation();

            pSInInfo.Info = cSitSite.Config.Info;
            pSInInfo.Dock = DockStyle.Fill;
            scrSite.Panel2.Controls.Add(pSInInfo);
            cDicPanels.Add("info", pSInInfo);

            SiteCredentials pSCsCredentials = new SiteCredentials();

            pSCsCredentials.Credentials = cSitSite.Config.Credentials;
            pSCsCredentials.Dock        = DockStyle.Fill;
            scrSite.Panel2.Controls.Add(pSCsCredentials);
            cDicPanels.Add("credentials", pSCsCredentials);
        }
Exemple #11
0
        /// <summary>
        /// Update the given field
        /// </summary>
        /// <param name="site">The site to update</param>
        /// <param name="field">The field to update</param>
        public static void UpdateField(this SiteCredentials site, FieldInfo field)
        {
            Object value;
            int    port;

            if (field.Name == "Password")
            {
                value = CommandUtils.AskPassword("User " + field.Name).Replace("\r", "");
            }
            else
            {
                value = CommandUtils.Ask("Value of " + field.Name);
            }
            if (field.FieldType == typeof(int))
            {
                value = int.TryParse(value.ToString(), out port) ? port : 22;
            }
            field.SetValue(site, value);
        }
Exemple #12
0
        /// <summary>
        /// Defines a SFTP Client transaction
        /// </summary>
        /// <param name="_cred">The SSH transaction credentials</param>
        /// <param name="task">The Transaction task</param>
        /// <returns>The transaction result</returns>
        public static Object SFTPTransaction(SiteCredentials _cred, Func <Renci.SshNet.SftpClient, Object> task)
        {
            Object result = null;

            using (var client = new Renci.SshNet.SftpClient(_cred.Host, _cred.Port, _cred.User, _cred.Password))
            {
                try
                {
                    client.Connect();
                    result = task(client);
                    client.Disconnect();
                }
                catch (System.Exception exc)
                {
                    Console.WriteLine(exc.Message);
                }
            }
            return(result);
        }
Exemple #13
0
        /// <summary>
        /// Downloads a directory
        /// </summary>
        /// <param name="client">The Sftp client</param>
        /// <param name="file">The directory used to download its files</param>
        /// <param name="filter">The download filter</param>
        /// <param name="silentDownload">Download the files without listing everything</param>
        public static void Download(this RenciSftpClient client, SiteCredentials credentials,
                                    MappedPath dir, SftpFilter filter, Boolean replace, Boolean silentDownload = false)
        {
            var      files = SftpUtils.ListFiles(client, dir.GetFullRemotePath(), filter, silentDownload);
            string   fileName, serverCopy;
            FileInfo cC, wC;

            AuraSftpClient.SSHTransactionVoid(credentials, (Action <AuraSftpClient>)((AuraSftpClient c) => {
                foreach (String remoteFile in files)
                {
                    fileName   = remoteFile.Replace((string)dir.GetFullRemotePath(), "").Substring(1).Replace("/", "\\");
                    serverCopy = Path.Combine((string)dir.GetFullServerCopy(), fileName);
                    //Cache copy
                    cC = new FileInfo(serverCopy);
                    //The path to the working copy
                    wC = new FileInfo(Path.Combine((string)dir.GetFullProjectCopy(), fileName));
                    DownloadFile(c, remoteFile, cC, wC, replace, silentDownload);
                }
            }));
        }
Exemple #14
0
        /// <summary>
        /// Test the credentials connection
        /// </summary>
        /// <param name="_cred">The credential connections</param>
        /// <param name="errMsg">The error message</param>
        /// <returns>True if the credentials are valid to connect to the host</returns>
        public static Boolean TestConnection(SiteCredentials _cred, out string errMsg)
        {
            Boolean result = true;

            errMsg = String.Empty;
            using (var client = new Renci.SshNet.SftpClient(_cred.Host, _cred.Port, _cred.User, _cred.Password))
            {
                try
                {
                    client.Connect();
                    client.Disconnect();
                }
                catch (System.Exception exc)
                {
                    errMsg = Message.MSG_ERR_NO_CONN + ".\n" + exc.Message;
                    result = false;
                }
            }
            return(result);
        }
Exemple #15
0
 /// <summary>
 /// Defines a SSH transaction
 /// </summary>
 /// <param name="_cred">The SSH transaction credentials</param>
 /// <param name="task">The Transaction task</param>
 /// <param name="remotePath">Changes how the remote path is transformed before
 /// it is passed to the scp command on the remote server.
 /// DoubleQuote, ShellQuote, None</param>
 public static void SSHTransactionVoid(SiteCredentials _cred, Action <AuraSftpClient> task, IRemotePathTransformation remotePath = null)
 {
     using (var client = new AuraSftpClient(_cred))
     {
         try
         {
             if (remotePath == null)
             {
                 client.RemotePathTransformation = ShellQuote;
             }
             else
             {
                 client.RemotePathTransformation = remotePath;
             }
             client.Connect();
             task(client);
             client.Disconnect();
         }
         catch (System.Exception exc)
         {
             Console.WriteLine(exc.Message);
         }
     }
 }
Exemple #16
0
 /// <summary>
 /// Defines a SFTP Client transaction
 /// </summary>
 /// <param name="_cred">The SSH transaction credentials</param>
 /// <param name="task">The Transaction task</param>
 /// <returns>The transaction result</returns>
 public static T SFTPTransactionGen <T>(SiteCredentials _cred, Func <Renci.SshNet.SftpClient, T> task)
     where T : class => (T)SFTPTransaction(_cred, task);
Exemple #17
0
 /// <summary>
 /// Defines a SSH transaction
 /// </summary>
 /// <param name="_cred">The SSH transaction credentials</param>
 /// <param name="task">The Transaction task</param>
 /// <param name="remotePath">Changes how the remote path is transformed before
 /// it is passed to the scp command on the remote server.
 /// DoubleQuote, ShellQuote, None</param>
 /// <returns>The transaction result</returns>
 public static T SSHTransactionGen <T>(SiteCredentials _cred, Func <AuraSftpClient, T> task, IRemotePathTransformation remotePath = null)
     where T : class => (T)SSHTransaction(_cred, task);
        public static DataService GetInstance([NotNull] string typeName, [NotNull] string server, [NotNull] SiteCredentials credentials, [NotNull] string webRootPath)
        {
            Assert.ArgumentNotNull(typeName, nameof(typeName));
            Assert.ArgumentNotNull(server, nameof(server));
            Assert.ArgumentNotNull(credentials, nameof(credentials));
            Assert.ArgumentNotNull(webRootPath, nameof(webRootPath));

            var key = typeName + '|' + server + '|' + credentials.UserName + '|' + credentials.Password;

            DataService dataService;

            if (Cache.TryGetValue(key, out dataService))
            {
                return(dataService);
            }

            DataServiceDescriptor descriptor;

            if (!TypeDescriptors.TryGetValue(typeName, out descriptor))
            {
                return(DataService.Empty);
            }

            dataService = Activator.CreateInstance(descriptor.Type) as DataService;
            if (dataService == null)
            {
                dataService = DataService.Empty;
            }
            else
            {
                dataService.Initialize(server, credentials);
            }

            Cache[key] = dataService;

            return(dataService);
        }
Exemple #19
0
 /// <summary>
 /// Start the connection to SFTP client
 /// </summary>
 /// <param name="_cred">The SFTP connection credentials</param>
 public AuraSftpClient(SiteCredentials _cred) : base(_cred.Host, _cred.Port, _cred.User, _cred.Password)
 {
     this.credentials = _cred;
 }