Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Xml.Linq;
- using System.Linq;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace mirror
- {
- class Ftp
- {
- public String server;
- private int port;
- private TcpClient client;
- private NetworkStream stream;
- public Ftp(String server, int port)
- {
- this.server = server;
- this.port = port;
- }
- public void Connect()
- {
- client = new TcpClient();
- client.Connect(this.server, this.port);
- stream = client.GetStream();
- byte[] buffer = new byte[1024];
- int bytes = stream.Read(buffer, 0, buffer.Length);
- String response = Encoding.ASCII.GetString(buffer, 0, bytes);
- int responseState = Convert.ToInt32(response.Substring(0, 3));
- }
- public String SendText(String message, int responseExpected)
- {
- byte[] text = Encoding.ASCII.GetBytes(message);
- stream.Write(text, 0, text.Length);
- byte[] buffer = new byte[1024];
- int bytes = stream.Read(buffer, 0, buffer.Length);
- String response = Encoding.ASCII.GetString(buffer, 0, bytes);
- int responseState = -1;
- if (response != null)
- {
- responseState = Convert.ToInt32(response.Substring(0, 3));
- }
- return response;
- }
- public int CountPort(String serverResponse)
- {
- String response = serverResponse.Substring(serverResponse.IndexOf("(") + 1);
- response = response.Remove(response.Length - 4);
- String[] nums = response.Split(',');
- int first = Int32.Parse(nums[nums.Length - 2]);
- int second = Int32.Parse(nums[nums.Length - 1]);
- return (first * 256 + second);
- }
- public void Disconnect()
- {
- SendText("QUIT\r\n", 221);
- stream.Close();
- client.Close();
- }
- public void Receive()
- {
- byte[] buffer = new byte[1024];
- int bytes = stream.Read(buffer, 0, buffer.Length);
- String response = Encoding.ASCII.GetString(buffer, 0, bytes);
- //Console.WriteLine("Odpowiedz serwera:\n" + response);
- }
- public String List()
- {
- this.SendText("TYPE A\r\n", 200);
- String passiveModeResponse = this.SendText("PASV\r\n", 227);
- int downloadPort = this.CountPort(passiveModeResponse);
- FtpData ftpDataConnection = new FtpData();
- ftpDataConnection.Connect(server, downloadPort);
- this.SendText("LIST\r\n", 150);
- String list = ftpDataConnection.ReceiveText(225);
- ftpDataConnection.Disconnect();
- this.Receive();
- this.SendText("TYPE I\r\n", 200);
- return list;
- }
- public void SetWriteTime(String filename, String localDirectory)
- {
- String passiveModeResponse = this.SendText("PASV\r\n", 227);
- int newPort = this.CountPort(passiveModeResponse);
- FtpData ftpDataConnection = new FtpData();
- ftpDataConnection.Connect(server, newPort);
- //pobranie daty modyfikacji
- String mdtm = this.SendText("MDTM " + filename + "\r\n", 213);
- ftpDataConnection.Disconnect();
- int year = Int32.Parse(mdtm.Substring(4, 4));
- int month = Int32.Parse(mdtm.Substring(8, 2));
- int day = Int32.Parse(mdtm.Substring(10, 2));
- int hours = Int32.Parse(mdtm.Substring(12, 2));
- int minutes = Int32.Parse(mdtm.Substring(14, 2));
- int seconds = Int32.Parse(mdtm.Substring(16, 2));
- File.SetLastWriteTime(localDirectory + "/" + filename, new DateTime(year, month, day, hours, minutes, seconds));
- }
- }
- class FtpData
- {
- private TcpClient client;
- private NetworkStream stream;
- public void Connect(String server, int port)
- {
- client = new TcpClient();
- client.Connect(server, port);
- stream = client.GetStream();
- }
- public void Disconnect()
- {
- byte[] text = Encoding.ASCII.GetBytes("QUIT\r\n");
- stream.Write(text, 0, text.Length);
- stream.Close();
- client.Close();
- }
- public String ReceiveText(int expectedResponse)
- {
- byte[] buffer = new byte[900000];
- String response = "";
- int bytesRead = 0;
- do
- {
- bytesRead = stream.Read(buffer, 0, buffer.Length);
- response += Encoding.ASCII.GetString(buffer, 0, bytesRead);
- }
- while (stream.DataAvailable);
- if ((response != "") && (response.Substring(0, 3) == expectedResponse.ToString()))
- {
- Console.WriteLine("Blad! Jedno polaczenie danych jest juz otwarte!\n");
- Console.WriteLine("Odpowiedz serwera:\n" + response);
- this.Disconnect();
- Console.ReadLine();
- Environment.Exit(0);
- }
- if (response != "")
- {
- response = response.Remove(response.Length - 1);
- Console.WriteLine(response);
- }
- return response;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- XDocument xdoc = XDocument.Load("app.xml");
- String server = xdoc.Descendants("server").First().Value;
- String user = xdoc.Descendants("user").First().Value;
- String serverDirectory = xdoc.Descendants("serverDirectory").First().Value;
- String localDirectory = xdoc.Descendants("localDirectory").First().Value;
- int port;
- try
- {
- server = args[0].Substring(args[0].IndexOf('@') + 1, args[0].IndexOf("/") - args[0].IndexOf('@') - 1);
- port = 21;
- user = args[0].Substring(0, args[0].IndexOf('@'));
- serverDirectory = args[0].Substring(args[0].IndexOf('/') + 1);
- localDirectory = args[1];
- Console.WriteLine();
- }
- catch (Exception e)
- {
- Console.WriteLine("Blad! Niepoprawne argumenty.");
- Console.Write(e.ToString());
- return;
- }
- int maxLevel = -1;
- if (args.Length == 3)
- {
- maxLevel = Int32.Parse(args[2].Substring(1, args[2].Length - 1));
- }
- //połączenie kontrolne
- Ftp ftp = new Ftp(server, port);
- ftp.Connect();
- Console.WriteLine("Połączono");
- //autentykacja
- ftp.SendText("USER " + user + "\r\n", 331);
- Console.WriteLine("Prosze wprowadzic haslo:");
- String password = ReadPassword();
- Console.WriteLine("\n");
- ftp.SendText("PASS " + new String('*', password.Length) + "\r\n", 230);
- //przejście do interesującego nas katalogu
- ftp.SendText("CWD " + serverDirectory + "\r\n", 250);
- //utworzenie lokalnego katalogu (jeśli nie istnieje)
- if (!Directory.Exists(localDirectory))
- {
- Directory.CreateDirectory(localDirectory);
- }
- //tryb tekstowy
- ftp.SendText("TYPE A\r\n", 200);
- String passiveModeResponse = ftp.SendText("PASV\r\n", 227);
- int downloadPort = ftp.CountPort(passiveModeResponse);
- //połączenie danych
- FtpData ftpDataConnection = new FtpData();
- ftpDataConnection.Connect(ftp.server, downloadPort);
- ftp.SendText("LIST\r\n", 150);
- //odbieranie listy
- String list = ftpDataConnection.ReceiveText(225);
- //zamknięcie połączenia danych
- ftpDataConnection.Disconnect();
- //info czy transfer zakończył się sukcesem
- ftp.Receive();
- //tryb binarny (odbiór plików)
- ftp.SendText("TYPE I\r\n", 200);
- String[] elements = list.Split('\n');
- String[] info;
- foreach(String element in elements)
- {
- info = element.Split(' ');
- //jeśli element jest plikiem
- if (info.Length >= 20 && element.ElementAt(0) == '-')
- {
- String filename = info[info.Length - 1].Remove(info[info.Length - 1].Length - 1);
- //jeśli plik nie istnieje w katalogu lokalnym to go pobieramy
- if (!File.Exists(localDirectory + "/" + filename))
- {
- }
- else //jeśli istnieje to porównujemy daty modyfikacji
- {
- String mdtm = ftp.SendText("MDTM " + filename + "\r\n", 213);
- int year = Int32.Parse(mdtm.Substring(4, 4));
- int month = Int32.Parse(mdtm.Substring(8, 2));
- int day = Int32.Parse(mdtm.Substring(10, 2));
- int hours = Int32.Parse(mdtm.Substring(12, 2));
- int minutes = Int32.Parse(mdtm.Substring(14, 2));
- int seconds = Int32.Parse(mdtm.Substring(16, 2));
- DateTime newTime = new DateTime(year, month, day, hours, minutes, seconds);
- DateTime oldTime = File.GetLastWriteTime(localDirectory + "/" + filename);
- }
- }
- else if (info.Length >= 20 && element.ElementAt(0) == 'd') //jeśli element jest katalogiem
- {
- String directoryName = info[info.Length - 1].Remove(info[info.Length - 1].Length - 1);
- if (!Directory.Exists(localDirectory + "/" + directoryName))
- {
- Directory.CreateDirectory(localDirectory + "/" + directoryName);
- }
- if (maxLevel != 0) // limit nieustawiony lub 0 to pobieramy zawartość
- {
- String serverDirectoryPath = serverDirectory + directoryName;
- ftp.SendText("CWD " + directoryName + "/\r\n", 250);
- ftp.SendText("CDUP\r\n", 250);
- }
- }
- }
- ftp.Disconnect();
- Console.ReadLine();
- }
- public static String ReadPassword()
- {
- String password = "";
- char key;
- while ((key = Console.ReadKey(true).KeyChar) != '\r')
- {
- if (key == '\b' && password.Length > 0)
- {
- Console.Write("\b \b");
- password = password.Remove(password.Length - 1, 1);
- }
- else if (Char.IsLetterOrDigit(key))
- {
- Console.Write("*");
- password = password + key;
- }
- }
- return password;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement