Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.58 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.IO;
  7.  
  8. namespace Klient_FTP
  9. {
  10.  
  11.     class Program
  12.     {
  13.         Int32 portPASV;
  14.         String currentUser=String.Empty;
  15.         static void SaveFile(String fileName, Byte[] Data)
  16.         {
  17.             BinaryWriter Writer = new BinaryWriter(File.OpenWrite(fileName));
  18.             Writer.Write(Data);
  19.             Writer.Flush();
  20.             Writer.Close();
  21.         }
  22.         void Work(object obj)
  23.         {
  24.             Directory.CreateDirectory("C:/FTP");
  25.             Directory.SetCurrentDirectory("C:/FTP");
  26.             var message = ((object[])obj)[0].ToString() + " " + currentUser + '\0';
  27.  
  28.             IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 21);
  29.             TcpClient client = new TcpClient();
  30.             client.Connect(ep);
  31.             StringBuilder sb = new StringBuilder();
  32.             if (message.Contains("RETR"))
  33.             {
  34.                 if (currentUser != String.Empty)
  35.                 {
  36.                     NetworkStream strim = client.GetStream();
  37.                     message = message.Insert(message.LastIndexOf('\0'), " " + currentUser);
  38.                     strim.Write(Encoding.ASCII.GetBytes(message), 0, message.Length);
  39.                     IPEndPoint fileEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), portPASV);
  40.                     StringBuilder msg = new StringBuilder();
  41.                     TcpClient fileClient = new TcpClient();
  42.                     fileClient.Connect(fileEndPoint);
  43.                     StringBuilder sbf = new StringBuilder();
  44.                     using (NetworkStream fileStream = fileClient.GetStream())
  45.                     {
  46.  
  47.                         int _tmp;
  48.                         while ((_tmp = fileStream.ReadByte()) != 0)
  49.                         {
  50.                             sbf.Append((char)_tmp);
  51.                         }
  52.                     }
  53.  
  54.                     String[] splitMessage = message.Split(' ');
  55.                     StringBuilder fileName = new StringBuilder(splitMessage[1]);
  56.                     fileName.Insert(0, '\\');
  57.                     StringBuilder XD = new StringBuilder(fileName.ToString().Substring(fileName.ToString().LastIndexOf("\\")));
  58.                     SaveFile(Directory.GetCurrentDirectory() + XD, System.Text.Encoding.ASCII.GetBytes(sbf.ToString()));
  59.                     fileClient.Close();
  60.                     client.Close();
  61.                     return;
  62.                 }
  63.                 else
  64.                 {
  65.                     Console.WriteLine("530: Access denied. Provide password before using RETR.");
  66.                     return;
  67.                 }
  68.                
  69.  
  70.             }
  71.             using (NetworkStream stream = client.GetStream())
  72.             {
  73.                 Console.WriteLine("sent: " + message);
  74.                 stream.Write(Encoding.ASCII.GetBytes(message), 0, message.Length);
  75.                 int i;
  76.                 while ((i = stream.ReadByte()) != 0)
  77.                 {
  78.                     sb.Append((char)i);
  79.                 }
  80.             }
  81.             if (message.Contains("PASV"))
  82.             {
  83.  
  84.                 String[] PASVtoparse = new String[6];
  85.                 PASVtoparse = sb.ToString().Split(',');
  86.                 IPAddress localAddress = IPAddress.Parse(PASVtoparse[0] + "." + PASVtoparse[1] + "." + PASVtoparse[2] + "." + PASVtoparse[3]);
  87.                 portPASV = Int32.Parse(PASVtoparse[4]) * 256 + Int32.Parse(PASVtoparse[5]);
  88.                 Console.WriteLine("Port: " + portPASV.ToString());
  89.             }            
  90.             else if (message.Contains("PASS"))
  91.             {
  92.                 String[] splitMessage = message.Split(' ');
  93.                 currentUser = splitMessage[1];
  94.                 Console.WriteLine("ID klienta: " + currentUser);
  95.  
  96.             }
  97.             Console.WriteLine("response: \n" + sb.ToString());
  98.             client.Close();
  99.         }
  100.         void start(string m)
  101.         {
  102.             ThreadPool.QueueUserWorkItem(Work, new object[] { m });
  103.             Thread.Sleep(200);
  104.         }
  105.  
  106.         static void Main(string[] args)
  107.         {
  108.             Program p = new Program();
  109.             string message = string.Empty;
  110.             while (true)
  111.             {
  112.                 Console.Write("input: ");
  113.                 message = Console.ReadLine();
  114.                 if (message != "QUIT") p.start(message);
  115.                 else break;
  116.             }
  117.             //press any key to exit
  118.             Console.ReadKey();
  119.             Environment.Exit(0);
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement