Exemple #1
0
		public static void RunExample(String[] arg)
		{
			try
			{
				JSch jsch=new JSch();

				InputForm inForm = new InputForm();
				inForm.Text = "Enter username@hostname";
				inForm.textBox1.Text = Environment.UserName+"@localhost"; 

				if (!inForm.PromptForInput())
				{
					Console.WriteLine("Cancelled");
					return;
				}
				String host = inForm.textBox1.Text;
				String user=host.Substring(0, host.IndexOf('@'));
				host=host.Substring(host.IndexOf('@')+1);

				Session session=jsch.getSession(user, host, 22);

				// username and password will be given via UserInfo interface.
				UserInfo ui=new MyUserInfo();
				session.setUserInfo(ui);

				session.connect();

				Channel channel=session.openChannel("sftp");
				channel.connect();
				ChannelSftp c=(ChannelSftp)channel;

				Stream ins=Console.OpenStandardInput();
				TextWriter outs=Console.Out;

				ArrayList cmds=new ArrayList();
				byte[] buf=new byte[1024];
				int i;
				String str;
				int level=0;

				while(true)
				{
					outs.Write("sftp> ");
					cmds.Clear();
					i=ins.Read(buf, 0, 1024);
					if(i<=0)break;

					i--;
					if(i>0 && buf[i-1]==0x0d)i--;
					//str=Util.getString(buf, 0, i);
					//Console.WriteLine("|"+str+"|");
					int s=0;
					for(int ii=0; ii<i; ii++)
					{
						if(buf[ii]==' ')
						{
							if(ii-s>0){ cmds.Add(Util.getString(buf, s, ii-s)); }
							while(ii<i){if(buf[ii]!=' ')break; ii++;}
							s=ii;
						}
					}
					if(s<i){ cmds.Add(Util.getString(buf, s, i-s)); }
					if(cmds.Count==0)continue;

					String cmd=(String)cmds[0];
					if(cmd.Equals("quit"))
					{
						c.quit();
						break;
					}
					if(cmd.Equals("exit"))
					{
						c.exit();
						break;
					}
					if(cmd.Equals("rekey"))
					{
						session.rekey();
						continue;
					}
					if(cmd.Equals("compression"))
					{
						if(cmds.Count<2)
						{
							outs.WriteLine("compression level: "+level);
							continue;
						}
						try
						{
							level=int.Parse((String)cmds[1]);
							Hashtable config=new Hashtable();
							if(level==0)
							{
								config.Add("compression.s2c", "none");
								config.Add("compression.c2s", "none");
							}
							else
							{
								config.Add("compression.s2c", "zlib,none");
								config.Add("compression.c2s", "zlib,none");
							}
							session.setConfig(config);
						}
						catch{}//(Exception e){}
						continue;
					}
					if(cmd.Equals("cd") || cmd.Equals("lcd"))
					{
						if(cmds.Count<2) continue;
						String path=(String)cmds[1];
						try
						{
							if(cmd.Equals("cd")) c.cd(path);
							else c.lcd(path);
						}
						catch(SftpException e)
						{
							Console.WriteLine(e.message);
						}
						continue;
					}
					if(cmd.Equals("rm") || cmd.Equals("rmdir") || cmd.Equals("mkdir"))
					{
						if(cmds.Count<2) continue;
						String path=(String)cmds[1];
						try
						{
							if(cmd.Equals("rm")) c.rm(path);
							else if(cmd.Equals("rmdir")) c.rmdir(path);
							else c.mkdir(path);
						}
						catch(SftpException e)
						{
							Console.WriteLine(e.message);
						}
						continue;
					}
					if(cmd.Equals("chgrp") || cmd.Equals("chown") || cmd.Equals("chmod"))
					{
						if(cmds.Count!=3) continue;
						String path=(String)cmds[2];
						int foo=0;
						if(cmd.Equals("chmod"))
						{
							byte[] bar=Util.getBytes((String)cmds[1]);
							int k;
							for(int j=0; j<bar.Length; j++)
							{
								k=bar[j];
								if(k<'0'||k>'7'){foo=-1; break;}
								foo<<=3;
								foo|=(k-'0');
							}
							if(foo==-1)continue;
						}
						else
						{
							try{foo=int.Parse((String)cmds[1]);}
							catch{}//(Exception e){continue;}
						}
						try
						{
							if(cmd.Equals("chgrp")){ c.chgrp(foo, path); }
							else if(cmd.Equals("chown")){ c.chown(foo, path); }
							else if(cmd.Equals("chmod")){ c.chmod(foo, path); }
						}
						catch(SftpException e)
						{
							Console.WriteLine(e.message);
						}
						continue;
					}
					if(cmd.Equals("pwd") || cmd.Equals("lpwd"))
					{
						str=(cmd.Equals("pwd")?"Remote":"Local");
						str+=" working directory: ";
						if(cmd.Equals("pwd")) str+=c.pwd();
						else str+=c.lpwd();
						outs.WriteLine(str);
						continue;
					}
					if(cmd.Equals("ls") || cmd.Equals("dir"))
					{
						String path=".";
						if(cmds.Count==2) path=(String)cmds[1];
						try
						{
							ArrayList vv=c.ls(path);
							if(vv!=null)
							{
								for(int ii=0; ii<vv.Count; ii++)
								{
									object obj = vv[ii];
									if(obj is ChannelSftp.LsEntry)
										outs.WriteLine(vv[ii]);
								}
							}
						}
						catch(SftpException e)
						{
							Console.WriteLine(e.message);
						}
						continue;
					}
					if(cmd.Equals("lls") || cmd.Equals("ldir"))
					{
						String path=".";
						if(cmds.Count==2) path=(String)cmds[1];
						try
						{
							//java.io.File file=new java.io.File(path);
							if(!File.Exists(path))
							{
								outs.WriteLine(path+": No such file or directory");
								continue; 
							}
							if(Directory.Exists(path))
							{
								String[] list=Directory.GetDirectories(path);
								for(int ii=0; ii<list.Length; ii++)
								{
									outs.WriteLine(list[ii]);
								}
								continue;
							}
							outs.WriteLine(path);
						}
						catch(Exception e)
						{
							Console.WriteLine(e);
						}
						continue;
					}
					if(cmd.Equals("get") || 
						cmd.Equals("get-resume") || cmd.Equals("get-append") || 
						cmd.Equals("put") || 
						cmd.Equals("put-resume") || cmd.Equals("put-append")
						)
					{
						if(cmds.Count!=2 && cmds.Count!=3) continue;
						String p1=(String)cmds[1];
						//	  String p2=p1;
						String p2=".";
						if(cmds.Count==3)p2=(String)cmds[2];
						try
						{
							SftpProgressMonitor monitor=new MyProgressMonitor();
							if(cmd.StartsWith("get"))
							{
								int mode=ChannelSftp.OVERWRITE;
								if(cmd.Equals("get-resume")){ mode=ChannelSftp.RESUME; }
								else if(cmd.Equals("get-append")){ mode=ChannelSftp.APPEND; } 
								c.get(p1, p2, monitor, mode);
							}
							else
							{ 
								int mode=ChannelSftp.OVERWRITE;
								if(cmd.Equals("put-resume")){ mode=ChannelSftp.RESUME; }
								else if(cmd.Equals("put-append")){ mode=ChannelSftp.APPEND; } 
								c.put(p1, p2, monitor, mode); 
							}
						}
						catch(SftpException e)
						{
							Console.WriteLine(e.message);
						}
						continue;
					}
					if(cmd.Equals("ln") || cmd.Equals("symlink") || cmd.Equals("rename"))
					{
						if(cmds.Count!=3) continue;
						String p1=(String)cmds[1];
						String p2=(String)cmds[2];
						try
						{
							if(cmd.Equals("rename")) c.rename(p1, p2);
							else c.symlink(p1, p2);
						}
						catch(SftpException e)
						{
							Console.WriteLine(e.message);
						}
						continue;
					}
					if(cmd.Equals("stat") || cmd.Equals("lstat"))
					{
						if(cmds.Count!=2) continue;
						String p1=(String)cmds[1];
						SftpATTRS attrs=null;
						try
						{
							if(cmd.Equals("stat")) attrs=c.stat(p1);
							else attrs=c.lstat(p1);
						}
						catch(SftpException e)
						{
							Console.WriteLine(e.message);
						}
						if(attrs!=null)
						{
							outs.WriteLine(attrs);
						}
						else
						{
						}
						continue;
					}
					if(cmd.Equals("version"))
					{
						outs.WriteLine("SFTP protocol version "+c.version());
						continue;
					}
					if(cmd.Equals("help") || cmd.Equals("?"))
					{
						outs.WriteLine(help);
						continue;
					}
					outs.WriteLine("unimplemented command: "+cmd);
				}
				session.disconnect();
			}
			catch(Exception e)
			{
				Console.WriteLine(e);
			}			
		}
Exemple #2
0
        public void CheckLocal()
        {
            Log.Write("$$$$$$$$$$$$ Checkin Local $$$$$$$$$$$$");
            try
            {
                List<string> alllocal = new List<string>(Directory.GetDirectories(lPath(), "*", SearchOption.AllDirectories));
                alllocal.AddRange(Directory.GetFiles(lPath(), "*", SearchOption.AllDirectories));

                List<string> Namelog = new List<string>(nLog().Split('|', '|'));

                foreach (string s in alllocal)
                {
                    Log.Write("Checking local: {0}", s);
                    bool isDir;
                    FileAttributes attr = File.GetAttributes(s);
                    if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                        isDir = true;
                    else
                        isDir = false;

                    if (!recentlycreated)
                    {
                        string cPath = GetCommonPath(s, false);
                        cPath = cPath.Substring(0, cPath.Length - 1);

                        int i = cPath.LastIndexOf(@"/");
                        string path = cPath.Substring(0, i + 1);
                        string name = cPath.Substring(i + 1, cPath.Length - i - 1);

                        string rPathToCD = path;
                        if (!FTP() && path == "/")
                        {
                            path = "";
                            rPathToCD = ".";
                        }
                        else if (!FTP() && path.StartsWith("/"))
                        {
                            path = path.Substring(1);
                            rPathToCD = rPathToCD.Substring(1);
                        }

                        if (!FTP() && cPath.StartsWith("/"))
                            cPath = cPath.Substring(1);

                        string rempath = rPath();

                        if (rempath.Equals("/"))
                            rempath = "";
                        else if (rempath.StartsWith("/"))
                            rempath = rempath.Substring(1);

                        string compath = noSlashes(rempath) + "/" + cPath;

                        if (isDir)
                            compath = compath + "/";

                        if (name != ".ftpquota")
                        {
                            if (Namelog.Contains(cPath))
                            {
                                Log.Write("++++++++> {0} {1} {2} in {3}", compath, path, name, s);
                                if (FTP())
                                {
                                    if (!FullList.ContainsKey(compath))
                                    {
                                        if (!RenamedList.ContainsValue(compath))
                                        {
                                            Log.Write("Case 1");
                                            Log.Write("FTP > sorry, gotta delete {0}", compath);
                                            if (isDir)
                                            {
                                                Directory.Delete(s, true);

                                                if (ShowNots())
                                                    tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("deleted", false), name), ToolTipIcon.Info);
                                                lasttip = string.Format("Folder {0} was deleted.", name);
                                                link = null;
                                                RemoveFromLog(cPath);
                                            }
                                            else
                                            {
                                                File.Delete(s);

                                                if (ShowNots())
                                                    tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("deleted", true), name), ToolTipIcon.Info);
                                                lasttip = string.Format("File {0} was deleted.", name);
                                                link = null;
                                                RemoveFromLog(cPath);
                                            }
                                        }
                                        else
                                        {
                                            Log.Write("renamedlist contains value: {0}", compath);
                                            foreach (KeyValuePair<string, string> k in RenamedList)
                                            {
                                                if (k.Value == compath)
                                                    RenamedList.Remove(k.Key);
                                            }
                                        }
                                    }
                                    /////////////////////////////////
                                    /*
                                    if (ftp.DirectoryExists(noSlashes(rPath()) + path))
                                    {
                                        ftp.SetCurrentDirectory(noSlashes(rPath()) + path);
                                        if (!ftp.FileExists(name))
                                        {
                                            File.Delete(s);
                                        }
                                    }
                                    else
                                    {
                                        File.Delete(s);
                                    } */
                                }
                                else
                                {
                                    string rpath = rPath();
                                    if (rpath == "/")
                                        rpath = path + name;
                                    else
                                    {
                                        if (rpath.StartsWith("/"))
                                            rpath = rpath.Substring(1);
                                        rpath = noSlashes(rpath) + "/" + path + name;
                                    }
                                    if (isDir && !rpath.EndsWith("/"))
                                        rpath = rpath + "/";

                                    Log.Write("rpath: {0}", rpath);
                                    if (!FullList.ContainsKey(rpath))
                                    {
                                        Log.Write("Case 1");
                                        Log.Write("SFTP > sorry, gotta delete {0}", path + name);
                                        if (isDir)
                                        {
                                            Directory.Delete(s);

                                            if (ShowNots())
                                                tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("deleted", false), name), ToolTipIcon.Info);
                                            lasttip = string.Format("Folder {0} was deleted.", name);
                                            link = null;
                                        }
                                        else
                                        {
                                            File.Delete(s);

                                            if (ShowNots())
                                                tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("deleted", true), name), ToolTipIcon.Info);
                                            lasttip = string.Format("File {0} was deleted.", name);
                                            link = null;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                Log.Write("--------> {0} {1} {2} in {3}", cPath, path, name, s);
                                if (FTP())
                                {
                                    if (!ftp.DirectoryExists(noSlashes(rPath()) + path))
                                    {
                                        ftp.SetCurrentDirectory(rPath());
                                        foreach (string p in path.Split('/', '/'))
                                        {
                                            if (!ftp.DirectoryExists(p))
                                            {
                                                ftp.CreateDirectory(p);

                                                if (ShowNots() && lasttip != string.Format("Folder {0} was created.", name))
                                                    tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("created", false), name), ToolTipIcon.Info);
                                                lasttip = string.Format("Folder {0} was created.", name);
                                                link = null;

                                            }
                                            ftp.SetCurrentDirectory(noSlashes(ftp.GetCurrentDirectory()) + "/" + p);
                                        }
                                    }

                                    ftp.SetCurrentDirectory(noSlashes(rPath()) + path);
                                    if (ftp.FileExists(name))
                                        ftp.RemoveFile(name);

                                    ftp.PutFile(s, name);
                                }
                                else
                                {
                                    if (isDir)
                                    {
                                        Log.Write("comPath: {0}", compath);
                                        if (!FullList.ContainsKey(compath))
                                        {
                                            Log.Write("Gonna make folder {0} to remote server in pwd: {1}", cPath, sftpc.pwd());
                                            sftpc.mkdir(cPath);
                                            Log.Write("success");
                                            if (ShowNots() && lasttip != string.Format("Folder {0} was created.", name))
                                                tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("created", false), name), ToolTipIcon.Info);
                                            lasttip = string.Format("Folder {0} was created.", name);
                                        }
                                    }
                                    else
                                    {
                                        Log.Write("Case 3");
                                        SftpProgressMonitor monitor = new MyProgressMonitor();
                                        sftpc.put(s, rPathToCD, monitor, ChannelSftp.OVERWRITE);
                                        if (ShowNots() && lasttip != string.Format("File {0} was created.", name))
                                            tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("created", true), name), ToolTipIcon.Info);
                                        lasttip = string.Format("File {0} was created.", name);
                                    }

                                }
                                Get_Link(path, name);
                                UpdateTheLog(cPath, DateTime.UtcNow);
                            }
                        }
                    }
                    else
                        Log.Write("recentlycreated is true");
                }
            }
            catch (Exception ex)
            {
                Log.Write("Error::: " + ex.Message);
                failedlisting = true;
            }
            recentlycreated = false;
            Log.Write("$$$$$$$$$$$$ Done Checkin Local $$$$$$$$$$$$");
        }
Exemple #3
0
        public static void RunExample(String[] arg)
        {
            try
            {
                JSch jsch = new JSch();

                InputForm inForm = new InputForm();
                inForm.Text          = "Enter username@hostname";
                inForm.textBox1.Text = Environment.UserName + "@localhost";

                if (!inForm.PromptForInput())
                {
                    Console.WriteLine("Cancelled");
                    return;
                }
                String host = inForm.textBox1.Text;
                String user = host.Substring(0, host.IndexOf('@'));
                host = host.Substring(host.IndexOf('@') + 1);

                Session session = jsch.getSession(user, host, 22);

                // username and password will be given via UserInfo interface.
                UserInfo ui = new MyUserInfo();
                session.setUserInfo(ui);

                session.connect();

                Channel channel = session.openChannel("sftp");
                channel.connect();
                ChannelSftp c = (ChannelSftp)channel;

                Stream     ins  = Console.OpenStandardInput();
                TextWriter outs = Console.Out;

                ArrayList cmds = new ArrayList();
                byte[]    buf  = new byte[1024];
                int       i;
                String    str;
                int       level = 0;

                while (true)
                {
                    outs.Write("sftp> ");
                    cmds.Clear();
                    i = ins.Read(buf, 0, 1024);
                    if (i <= 0)
                    {
                        break;
                    }

                    i--;
                    if (i > 0 && buf[i - 1] == 0x0d)
                    {
                        i--;
                    }
                    //str=Util.getString(buf, 0, i);
                    //Console.WriteLine("|"+str+"|");
                    int s = 0;
                    for (int ii = 0; ii < i; ii++)
                    {
                        if (buf[ii] == ' ')
                        {
                            if (ii - s > 0)
                            {
                                cmds.Add(Util.getString(buf, s, ii - s));
                            }
                            while (ii < i)
                            {
                                if (buf[ii] != ' ')
                                {
                                    break;
                                }
                                ii++;
                            }
                            s = ii;
                        }
                    }
                    if (s < i)
                    {
                        cmds.Add(Util.getString(buf, s, i - s));
                    }
                    if (cmds.Count == 0)
                    {
                        continue;
                    }

                    String cmd = (String)cmds[0];
                    if (cmd.Equals("quit"))
                    {
                        c.quit();
                        break;
                    }
                    if (cmd.Equals("exit"))
                    {
                        c.exit();
                        break;
                    }
                    if (cmd.Equals("rekey"))
                    {
                        session.rekey();
                        continue;
                    }
                    if (cmd.Equals("compression"))
                    {
                        if (cmds.Count < 2)
                        {
                            outs.WriteLine("compression level: " + level);
                            continue;
                        }
                        try
                        {
                            level = int.Parse((String)cmds[1]);
                            Hashtable config = new Hashtable();
                            if (level == 0)
                            {
                                config.Add("compression.s2c", "none");
                                config.Add("compression.c2s", "none");
                            }
                            else
                            {
                                config.Add("compression.s2c", "zlib,none");
                                config.Add("compression.c2s", "zlib,none");
                            }
                            session.setConfig(config);
                        }
                        catch {}                       //(Exception e){}
                        continue;
                    }
                    if (cmd.Equals("cd") || cmd.Equals("lcd"))
                    {
                        if (cmds.Count < 2)
                        {
                            continue;
                        }
                        String path = (String)cmds[1];
                        try
                        {
                            if (cmd.Equals("cd"))
                            {
                                c.cd(path);
                            }
                            else
                            {
                                c.lcd(path);
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if (cmd.Equals("rm") || cmd.Equals("rmdir") || cmd.Equals("mkdir"))
                    {
                        if (cmds.Count < 2)
                        {
                            continue;
                        }
                        String path = (String)cmds[1];
                        try
                        {
                            if (cmd.Equals("rm"))
                            {
                                c.rm(path);
                            }
                            else if (cmd.Equals("rmdir"))
                            {
                                c.rmdir(path);
                            }
                            else
                            {
                                c.mkdir(path);
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if (cmd.Equals("chgrp") || cmd.Equals("chown") || cmd.Equals("chmod"))
                    {
                        if (cmds.Count != 3)
                        {
                            continue;
                        }
                        String path = (String)cmds[2];
                        int    foo  = 0;
                        if (cmd.Equals("chmod"))
                        {
                            byte[] bar = Util.getBytes((String)cmds[1]);
                            int    k;
                            for (int j = 0; j < bar.Length; j++)
                            {
                                k = bar[j];
                                if (k < '0' || k > '7')
                                {
                                    foo = -1; break;
                                }
                                foo <<= 3;
                                foo  |= (k - '0');
                            }
                            if (foo == -1)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            try{ foo = int.Parse((String)cmds[1]); }
                            catch {}                           //(Exception e){continue;}
                        }
                        try
                        {
                            if (cmd.Equals("chgrp"))
                            {
                                c.chgrp(foo, path);
                            }
                            else if (cmd.Equals("chown"))
                            {
                                c.chown(foo, path);
                            }
                            else if (cmd.Equals("chmod"))
                            {
                                c.chmod(foo, path);
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if (cmd.Equals("pwd") || cmd.Equals("lpwd"))
                    {
                        str  = (cmd.Equals("pwd")?"Remote":"Local");
                        str += " working directory: ";
                        if (cmd.Equals("pwd"))
                        {
                            str += c.pwd();
                        }
                        else
                        {
                            str += c.lpwd();
                        }
                        outs.WriteLine(str);
                        continue;
                    }
                    if (cmd.Equals("ls") || cmd.Equals("dir"))
                    {
                        String path = ".";
                        if (cmds.Count == 2)
                        {
                            path = (String)cmds[1];
                        }
                        try
                        {
                            ArrayList vv = c.ls(path);
                            if (vv != null)
                            {
                                for (int ii = 0; ii < vv.Count; ii++)
                                {
                                    object obj = vv[ii];
                                    if (obj is ChannelSftp.LsEntry)
                                    {
                                        outs.WriteLine(vv[ii]);
                                    }
                                }
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if (cmd.Equals("lls") || cmd.Equals("ldir"))
                    {
                        String path = ".";
                        if (cmds.Count == 2)
                        {
                            path = (String)cmds[1];
                        }
                        try
                        {
                            //java.io.File file=new java.io.File(path);
                            if (!File.Exists(path))
                            {
                                outs.WriteLine(path + ": No such file or directory");
                                continue;
                            }
                            if (Directory.Exists(path))
                            {
                                String[] list = Directory.GetDirectories(path);
                                for (int ii = 0; ii < list.Length; ii++)
                                {
                                    outs.WriteLine(list[ii]);
                                }
                                continue;
                            }
                            outs.WriteLine(path);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                        continue;
                    }
                    if (cmd.Equals("get") ||
                        cmd.Equals("get-resume") || cmd.Equals("get-append") ||
                        cmd.Equals("put") ||
                        cmd.Equals("put-resume") || cmd.Equals("put-append")
                        )
                    {
                        if (cmds.Count != 2 && cmds.Count != 3)
                        {
                            continue;
                        }
                        String p1 = (String)cmds[1];
                        //	  String p2=p1;
                        String p2 = ".";
                        if (cmds.Count == 3)
                        {
                            p2 = (String)cmds[2];
                        }
                        try
                        {
                            SftpProgressMonitor monitor = new MyProgressMonitor();
                            if (cmd.StartsWith("get"))
                            {
                                int mode = ChannelSftp.OVERWRITE;
                                if (cmd.Equals("get-resume"))
                                {
                                    mode = ChannelSftp.RESUME;
                                }
                                else if (cmd.Equals("get-append"))
                                {
                                    mode = ChannelSftp.APPEND;
                                }
                                c.get(p1, p2, monitor, mode);
                            }
                            else
                            {
                                int mode = ChannelSftp.OVERWRITE;
                                if (cmd.Equals("put-resume"))
                                {
                                    mode = ChannelSftp.RESUME;
                                }
                                else if (cmd.Equals("put-append"))
                                {
                                    mode = ChannelSftp.APPEND;
                                }
                                c.put(p1, p2, monitor, mode);
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if (cmd.Equals("ln") || cmd.Equals("symlink") || cmd.Equals("rename"))
                    {
                        if (cmds.Count != 3)
                        {
                            continue;
                        }
                        String p1 = (String)cmds[1];
                        String p2 = (String)cmds[2];
                        try
                        {
                            if (cmd.Equals("rename"))
                            {
                                c.rename(p1, p2);
                            }
                            else
                            {
                                c.symlink(p1, p2);
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        continue;
                    }
                    if (cmd.Equals("stat") || cmd.Equals("lstat"))
                    {
                        if (cmds.Count != 2)
                        {
                            continue;
                        }
                        String    p1    = (String)cmds[1];
                        SftpATTRS attrs = null;
                        try
                        {
                            if (cmd.Equals("stat"))
                            {
                                attrs = c.stat(p1);
                            }
                            else
                            {
                                attrs = c.lstat(p1);
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.message);
                        }
                        if (attrs != null)
                        {
                            outs.WriteLine(attrs);
                        }
                        else
                        {
                        }
                        continue;
                    }
                    if (cmd.Equals("version"))
                    {
                        outs.WriteLine("SFTP protocol version " + c.version());
                        continue;
                    }
                    if (cmd.Equals("help") || cmd.Equals("?"))
                    {
                        outs.WriteLine(help);
                        continue;
                    }
                    outs.WriteLine("unimplemented command: " + cmd);
                }
                session.disconnect();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemple #4
0
        private void SftpCreate(string path, string cPath, string name, bool isDir)
        {
            try
            {
                Syncing();
                downloading = true;
                SftpCDtoRoot();
                string rPathToCD = cPath;
                string rFullPath = noSlashes(cPath) + "/";
                if (rFullPath == "/")
                {
                    rFullPath = "";
                    rPathToCD = ".";
                }
                else if (rFullPath.StartsWith("/"))
                {
                    rFullPath = rFullPath.Substring(1);
                    rPathToCD = rPathToCD.Substring(1);
                }
                //sftpc.cd(rFullPath);
                Log.Write("&&&&&&&&&& {0} {1} {2}", rFullPath, cPath, name, rPathToCD);

                if (isDir)
                {
                    //DirectoryInfo di = new DirectoryInfo(path);
                    sftpc.mkdir(rFullPath + name);
                    Log.Write("????> Created Directory: {0} (remote)", path);

                    if (ShowNots() && lasttip != string.Format("Folder {0} was created.", name))
                        tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("created", false), name), ToolTipIcon.Info);
                    lasttip = string.Format("Folder {0} was created.", name);
                    link = null;
                }
                else
                {
                    FileInfo fi = new FileInfo(path);
                    string comPath = rFullPath + fi.Name;
                    Log.Write("comPath: {0}", comPath);

                    Log.Write("DirectoryName: {0} from path: {1}", fi.DirectoryName, path);
                    //sftpc.lcd(fi.DirectoryName);
                    Log.Write("LCD: check");
                    SftpProgressMonitor monitor = new MyProgressMonitor();
                    sftpc.put(path, rPathToCD, monitor, ChannelSftp.OVERWRITE);

                    Log.Write("********** {0} **********", fi.DirectoryName);

                    if (ShowNots() && lasttip != string.Format("File {0} was created.", fi.Name))
                        tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("created", true), fi.Name), ToolTipIcon.Info);
                    lasttip = string.Format("File {0} was created.", fi.Name);
                    Get_Link(cPath, fi.Name);

                    //ftp.SetCurrentDirectory(rPath());

                    Log.Write("~~~~~~~~~~~~~> comPath: {0}", comPath);
                    UpdateTheLog(comPath, SftpGetLastWriteTime(comPath));
                }

                DoneSyncing();
            }
            catch (SftpException e)
            {
                Log.Write(e.message);
            }
        }
Exemple #5
0
 private void SftpDownloadFile(string path)
 {
     Syncing();
     downloading = true;
     Log.Write("GONNA DOWNLOAD: {0} to path {1}", path, sftpc.lpwd());
     SftpProgressMonitor monitor = new MyProgressMonitor();
     int mode = ChannelSftp.OVERWRITE;
     sftpc.get(path, ".", monitor, mode);
     DoneSyncing();
 }
Exemple #6
0
        public void UploadWebInt()
        {
            Log.Write("Gonna upload webint");

            Syncing();
            string path = Application.StartupPath + @"\WebInterface";

            foreach (string d in Directory.GetDirectories(path, "*", SearchOption.AllDirectories))
            {
                Log.Write("dir: {0}", d);
                string fname = d.Substring(path.Length, d.Length - path.Length);
                fname = noSlashes(fname);
                fname = fname.Replace(@"\", @"/");
                Log.Write("fname: {0}", fname);

                if (FTP())
                {
                    ftpbg.CreateDirectory(noSlashes(rPath()) + "/" + fname);
                }
                else
                {
                    SftpCDtoRoot();
                    try
                    {
                        sftpc.mkdir(fname);
                    }
                    catch { }
                }
            }

            foreach (string f in Directory.GetFiles(path, "*", SearchOption.AllDirectories))
            {
                Log.Write("file: {0}", f);

                FileAttributes attr = File.GetAttributes(f);
                if ((attr & FileAttributes.Directory) != FileAttributes.Directory)
                {
                    FileInfo fi = new FileInfo(f);
                    string fname = f.Substring(path.Length, f.Length - path.Length);
                    fname = noSlashes(fname);
                    fname = fname.Replace(@"\", @"/");
                    string cpath = fname.Substring(0, fname.Length - fi.Name.Length);

                    Log.Write("fname: {0} | cpath: {1}", fname, cpath);

                    if (FTP())
                    {
                        ftpbg.SetCurrentDirectory(noSlashes(rPath()) + "/" + cpath);
                        ftpbg.PutFile(f);
                    }
                    else
                    {
                        SftpProgressMonitor monitor = new MyProgressMonitor();
                        sftpc.put(f, cpath, monitor, ChannelSftp.OVERWRITE);
                    }
                }
            }

            link = ftpParent();
            if (!link.StartsWith("http://") || !link.StartsWith("https://"))
                link = "http://" + link;
            if (link.EndsWith("/"))
                link = link + "webint";
            else
                link = link + "/webint";
            //open link in browser

            if (ShowNots())
                tray.ShowBalloonTip(50, "FTPbox", get_webint_message("updated"), ToolTipIcon.Info);

            Directory.Delete(Application.StartupPath + @"\WebInterface", true);
            File.Delete(Application.StartupPath + @"\webint.zip");
            try
            {
                Directory.Delete(Application.StartupPath + @"\webint", true);
            }
            catch { }

            updatewebintpending = false;
            DoneSyncing();
            if (!FTP())
            {
                //sftpc.quit();
                //sftp_login();
            }
            ListAllFiles();
        }
Exemple #7
0
        public void webintwb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            string lpath = Application.StartupPath;
            if (FTP())
            {
                ftpbg.SetCurrentDirectory(noSlashes(rPath()) + "/webint");
                ftpbg.SetLocalDirectory(lpath);
                ftpbg.GetFile("version.ini", false);
            }
            else
            {
                SftpCDtoRoot();
                sftpc.lcd(lpath);

                SftpProgressMonitor monitor = new MyProgressMonitor();
                int mode = ChannelSftp.OVERWRITE;

                sftpc.get("webint/version.ini", ".", monitor, mode);
            }

            string inipath = lpath + @"\version.ini";
            IniFile ini = new IniFile(inipath);
            string currentversion = ini.ReadValue("Version", "latest");
            Log.Write("currentversion is: {0} when newest is: {1}", currentversion, webintwb.Document.Body.InnerText);

            if (currentversion != webintwb.Document.Body.InnerText)
            {
                Syncing();
                string data = @"|\webint\layout\css|\webint\layout\images\fancybox|\webint\layout\templates|\webint\system\classes|\webint\system\config|\webint\system\js|\webint\system\logs|\webint\system\savant\Savant3\resources|";
                MakeWebIntFolders(data);
                DeleteWebInt(true);
                GetWebInt();
            }
            else
            {
                DoneSyncing();
                ListAllFiles();
            }
            File.Delete(inipath);
        }
Exemple #8
0
        /// <summary>
        /// check a file that exists both locally and on remote server, by comparing LastWriteTime values to the ones logged
        /// </summary>
        /// <param name="name">name of file</param>
        /// <param name="cPath">common path to file</param>
        /// <param name="FullRemPath">Full remote path to file's folder</param>
        /// <param name="FullLocalPath">full local path to file</param>
        /// <param name="LocalFileParentFolder">full local path to file's folder</param>
        /// <param name="lDT">current LastWriteTime of local file</param>
        /// <param name="rDT">current LastWriteTime of remote file</param>
        public void CheckExistingFile(string name, string cPath, string FullRemPath, string FullLocalPath, string LocalFileParentFolder, DateTime lDT, DateTime rDT, string comPath)
        {
            DateTime lDTlog = GetlDateTime(cPath);
            DateTime rDTlog = GetrDateTime(cPath);
            int rResult = DateTime.Compare(rDT, rDTlog);    //compare remote file's lastwritetime with the value saved in log
            int lResult = DateTime.Compare(lDT, lDTlog);    //compare local file's lastwritetime with the value saved in log
            int bResult = DateTime. Compare(rDT, lDT);       //compare remote file's lastwritetime with local's lastwritetime value
            string rPathToCD = cPath;
            if (rPathToCD == "/")
            {
                rPathToCD = ".";
            }
            else if (rPathToCD.StartsWith("/"))
            {
                rPathToCD = rPathToCD.Substring(1);
            }

            TimeSpan dif = rDT - rDTlog;
            if (rResult > 0 && dif.TotalSeconds > 1)
            {
                if (dif.TotalSeconds > 1)
                {
                    Log.Write("Total Milliseconds of difference: {0} Seconds: {1}", dif.TotalMilliseconds.ToString(), dif.TotalSeconds.ToString());
                    try
                    {
                        Syncing();
                        Log.Write("rResult > 0");
                        Log.Write("fRP: {0} -- lfPF: {1} lDT: {2} -- lDTlog: {3} -- rDT: {4} -- rDTlog {5}", FullRemPath, LocalFileParentFolder, lDT.ToString(),
                            lDTlog.ToString(), rDT.ToString(), rDTlog.ToString());
                        if (FTP())
                        {
                            ftpbg.SetCurrentDirectory(FullRemPath);
                            ftpbg.SetLocalDirectory(LocalFileParentFolder);
                            downloading = true;
                            fswFiles.EnableRaisingEvents = false;
                            ftpbg.GetFile(name, false);
                        }
                        else
                        {
                            string dlPath = noSlashes(FullRemPath) + "/" + name;
                            if (dlPath.StartsWith("/"))
                                dlPath = dlPath.Substring(1);
                            SftpCDtoRoot();
                            //sftpc.cd(FullRemPath);
                            sftpc.lcd(LocalFileParentFolder);
                            downloading = true;
                            fswFiles.EnableRaisingEvents = false;
                            SftpDownloadFile(dlPath);
                        }

                        if (RenamedList.ContainsKey(comPath))
                        {
                            string nameOld = comPath;
                            string nameNew;
                            RenamedList.TryGetValue(comPath, out nameNew);

                            if (ShowNots())
                                tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("renamed", true), nameOld, nameNew), ToolTipIcon.Info);
                            Get_Link("", nameNew);

                            RemoveFromLog(nameOld);
                            UpdateTheLog(nameNew, DateTime.UtcNow);

                            DoneSyncing();
                            RenamedList.Remove(nameOld);
                            RemoveFromLog(nameOld);
                        }
                        else
                        {
                            if (ShowNots())
                                tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("updated", true), name), ToolTipIcon.Info);

                            fswFiles.EnableRaisingEvents = true;
                            if (FTP())
                                Get_Link(comPath, name);
                            else
                                Get_Link("", comPath);
                            UpdateTheLog(cPath, rDT);
                            downloading = false;
                            DoneSyncing();
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Write("[ERROR] -> " + ex.Message);
                        DoneSyncing();
                    }
                }
            }
            else
            {
                if ((lDT.ToString() != lDTlog.ToString()) && bResult < 0)
                {
                    try
                    {
                        Syncing();
                        Log.Write("(lDT.ToString() != lDTlog.ToString()) && bResult < 0");
                        Log.Write("lDT: {0} -- lDTlog: {1} -- rDT: {2} -- rDTlog: {3}", lDT.ToString(), lDTlog.ToString(), rDT.ToString(), rDTlog.ToString());
                        if (FTP())
                        {
                            ftpbg.SetCurrentDirectory(FullRemPath);
                            ftpbg.PutFile(FullLocalPath, name);
                        }
                        else
                        {
                            SftpCDtoRoot();
                            string ulPath = noSlashes(FullRemPath) + "/" + name;
                            if (ulPath.StartsWith("/"))
                                ulPath = ulPath.Substring(1);
                            //sftpc.cd(FullRemPath);
                            //SftpAppend(FullLocalPath, ulPath);
                            SftpProgressMonitor monitor = new MyProgressMonitor();
                            sftpc.put(FullLocalPath, rPathToCD, monitor, ChannelSftp.OVERWRITE);
                        }

                        if (RenamedList.ContainsKey(comPath))
                        {
                            string nameOld = comPath;
                            string nameNew;
                            RenamedList.TryGetValue(comPath, out nameNew);

                            if (ShowNots())
                                tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("renamed", true), nameOld, nameNew), ToolTipIcon.Info);
                            Get_Link("", nameNew);

                            RemoveFromLog(nameOld);
                            UpdateTheLog(nameNew, DateTime.UtcNow);

                            DoneSyncing();
                            RenamedList.Remove(nameOld);
                            RemoveFromLog(nameOld);
                        }
                        else
                        {
                            if (ShowNots() && lasttip != string.Format("File {0} was updated.", name))
                                tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("updated", true), name), ToolTipIcon.Info);
                            lasttip = string.Format("File {0} was updated.", name);
                            UpdateTheLog(cPath, GetLWTof(FullRemPath, name));
                            DoneSyncing();
                        }

                    }
                    catch (Exception ex)
                    {
                        Log.Write("[ERROR] -> " + ex.Message);
                        DoneSyncing();
                    }
                }
                else if ((lDT.ToString() != lDTlog.ToString()) && bResult > 0)
                {
                    try
                    {
                        Syncing();
                        Log.Write("(lDT.ToString() != lDTlog.ToString()) && bResult > 0");
                        Log.Write("lDT: {0} -- lDTlog: {1} -- rDT: {2} -- rDTlog: {3}", lDT.ToString(), lDTlog.ToString(), rDT.ToString(), rDTlog.ToString());
                        Log.Write("Timedif: {0}", timedif.Hours.ToString());

                        if (FTP())
                        {
                            ftpbg.SetCurrentDirectory(FullRemPath);
                            ftpbg.SetLocalDirectory(LocalFileParentFolder);
                            downloading = true;
                            fswFiles.EnableRaisingEvents = false;
                            ftpbg.GetFile(name, false);
                            fswFiles.EnableRaisingEvents = true;
                        }
                        else
                        {
                            SftpCDtoRoot();

                            string dlPath = noSlashes(FullRemPath) + "/" + name;
                            if (dlPath.StartsWith("/"))
                                dlPath = dlPath.Substring(1);

                            //sftpc.cd(FullRemPath);
                            sftpc.lcd(LocalFileParentFolder);
                            downloading = true;
                            fswFiles.EnableRaisingEvents = true;
                            SftpDownloadFile(dlPath);
                        }

                        Get_Link(comPath, name);

                        if (RenamedList.ContainsKey(comPath))
                        {
                            string nameOld = comPath;
                            string nameNew;
                            RenamedList.TryGetValue(comPath, out nameNew);

                            if (ShowNots())
                                tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("renamed", true), nameOld, nameNew), ToolTipIcon.Info);
                            Get_Link("", nameNew);

                            RemoveFromLog(nameOld);
                            UpdateTheLog(nameNew, DateTime.UtcNow);

                            DoneSyncing();
                            RenamedList.Remove(nameOld);
                            RemoveFromLog(nameOld);
                        }
                        else
                        {
                            if (ShowNots())
                                tray.ShowBalloonTip(50, "FTPbox", string.Format(Get_Message("updated", true), name), ToolTipIcon.Info);

                            fswFiles.EnableRaisingEvents = true;
                            UpdateTheLog(cPath, SftpGetLastWriteTime(cPath));// DateTime.UtcNow);
                            downloading = false;
                            DoneSyncing();
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Write("[ERROR] -> " + ex.Message);
                        DoneSyncing();
                    }
                }
                else
                {
                    Log.Write("***********");
                    Log.Write(cPath);
                    Log.Write("rDT {0} rDTlog {1} result {2}", rDT.ToString(), rDTlog.ToString(), rResult.ToString());
                    Log.Write("lDT {0} lDTlog {1} result {2}", lDT.ToString(), lDTlog.ToString(), lResult.ToString());
                    Log.Write(bResult.ToString());
                    Log.Write("***********");
                }
            }
        }
Exemple #9
0
 private void Init()
 {
     m_monitor = new MyProgressMonitor(this);
 }
Exemple #10
0
 private void Init()
 {
     m_monitor = new MyProgressMonitor(this);
 }
Exemple #11
0
        public void Get(string partida, string destino)
        {
            if (partida == null)
            {
                throw new ArgumentNullException("partida");
            }

            if (partida.Equals(string.Empty))
            {
                throw new ArgumentException("partida invalida");
            }

            if (destino == null)
            {
                throw new ArgumentNullException("destino");
            }

            if (destino.Equals(string.Empty))
            {
                throw new ArgumentException("destino invalido");
            }

            try
            {
                monitor = new MyProgressMonitor();
                this.Conectar();
                this.canalsftp.get(partida, destino, monitor);
            }
            finally{
                this.Desconectar();
            }
        }
Exemple #12
0
        public void Put(ArrayList lista_Partida, string destino)
        {
            if (lista_Partida == null)
            {
                throw new ArgumentNullException("lista_partida");
            }

            if (destino == null)
            {
                throw new ArgumentNullException("destino");
            }

            if (destino.Equals(string.Empty))
            {
                throw new ArgumentException("destino invalido");
            }

            try
            {
                this.Conectar();
                foreach (string partidas in lista_Partida)
                {
                    if (partidas != null)
                    {
                        if (!partidas.Equals(string.Empty))
                        {
                            monitor = new MyProgressMonitor();
                            this.canalsftp.put(partidas, destino, monitor, ChannelSftp.OVERWRITE);
                        }
                        else
                        {
                            throw new ArgumentException("partida invalida");
                        }
                    }
                    else
                    {
                        throw new ArgumentNullException("partida");
                    }
                }
            }
            finally
            {
                this.Desconectar();
            }
        }