Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.19 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. using System.Text.RegularExpressions;
  8. using System.Collections.Generic;
  9. using System.Threading.Tasks;
  10.  
  11. namespace Server_FTP
  12. {
  13.     public class Client
  14.     {
  15.         public String authorizedEmail;
  16.         public Int32 port;
  17.  
  18.         public Client(String email, Int32 p)
  19.         {
  20.             authorizedEmail = email;
  21.             port = p;
  22.         }
  23.     }
  24.     public class server
  25.     {        
  26.         ManualResetEvent tcpClientConnected = new ManualResetEvent(false);
  27.         List<Client> authorizedEmails = new List<Client>();
  28.         void ProcessIncomingData(object obj)
  29.         {
  30.             TcpClient client = (TcpClient)obj;
  31.             StringBuilder sb = new StringBuilder();
  32.             bool userauth = true;
  33.             bool accessgranted = true;
  34.             Directory.SetCurrentDirectory("D:/FTP");
  35.             using (NetworkStream stream = client.GetStream())
  36.             {
  37.                 int i;                
  38.                 while ((i = stream.ReadByte()) != 0)
  39.                 {
  40.                     sb.Append((char)i);
  41.                 }
  42.                 String[] tab = sb.ToString().Split(' ');
  43.                 switch (tab[0].ToUpper())
  44.                 {
  45.                     case "DELE":
  46.                         {
  47.                             try
  48.                             {
  49.                                 File.Delete(tab[1]);
  50.                             }
  51.                             catch (DirectoryNotFoundException)
  52.                             {
  53.                                 Console.WriteLine("Proba usuniecia pliku zakonczona niepowodzeniem. Podana sciezka jest nieprawidlowa");
  54.                             }
  55.  
  56.                             break;
  57.                         }
  58.                     case "USER":
  59.                         {
  60.                             accessgranted = true;
  61.                             userauth = true;
  62.                             Byte[] bytess = new Byte[256];
  63.  
  64.                             if (tab[1].Equals("anonymous"))
  65.                             {
  66.                                 string rep = "331: Password is required to get access." + '\0';
  67.                                 stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
  68.                             }
  69.                             else
  70.                             {
  71.                                 string rep = "530: Access denied" + '\0';
  72.                                 stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
  73.                             }
  74.                             break;
  75.                         }
  76.                     case "PASS":
  77.                         {
  78.  
  79.                             if (userauth)
  80.                             {
  81.                                 string userpass = tab[1];
  82.                                 if (Regex.IsMatch(tab[1], @"^[a-z0-9][-a-z0-9._]+@([-a-z0-9]+\.)+[a-z]{2,5}$", RegexOptions.IgnoreCase))
  83.                                 {
  84.                                     string rep = "230: Access granted. " + '\0';                                    
  85.                                     authorizedEmails.Add(new Client(userpass,13000));
  86.                                     stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
  87.                                     accessgranted = true;
  88.                                 }
  89.                                 else
  90.                                 {
  91.                                     string rep = "530: Access denied. " + '\0';
  92.                                     stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
  93.  
  94.                                 }
  95.                             }
  96.                             else
  97.                             {
  98.                                 string rep = "User has not been specified yet. " + '\0';
  99.                                 stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
  100.  
  101.                             }
  102.                             break;
  103.                         }
  104.                     case "PASV":
  105.                         {
  106.                             Random rnd = new Random();
  107.                             Int32 portPASV = rnd.Next(1024, 13000);
  108.                             Int32 port1 = portPASV / 256, port2 = portPASV % 256;                            
  109.                             String fn = "127.0.0.1";
  110.                             fn = fn.Replace('.', ',');
  111.                             String message = fn + "," + (port1.ToString()) + "," + (port2.ToString() + '\0');
  112.                             Byte[] bytes = Encoding.ASCII.GetBytes(message);
  113.                             authorizedEmails[authorizedEmails.FindIndex(x => x.authorizedEmail.Equals(tab[1]))].port = portPASV;                            
  114.                             Console.WriteLine(authorizedEmails[authorizedEmails.FindIndex(x => x.authorizedEmail.Equals(tab[1]))].authorizedEmail);
  115.                             Console.WriteLine(authorizedEmails[authorizedEmails.FindIndex(x => x.authorizedEmail.Equals(tab[1]))].port);
  116.                             stream.Write(bytes, 0, bytes.Length);
  117.                             break;
  118.                         }
  119.                     case "RETR":
  120.                         {
  121.                             Client klient = authorizedEmails[authorizedEmails.FindIndex(x => x.authorizedEmail.Equals(tab[2]))];
  122.                             Directory.SetCurrentDirectory("D:\\FTP\\");
  123.                             string path = "D:\\FTP\\" + tab[1];
  124.                             FileStream fs = new FileStream(tab[1], FileMode.Open, FileAccess.Read);
  125.                             Console.WriteLine(klient.authorizedEmail);
  126.                             Console.WriteLine(klient.port);
  127.                             IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), klient.port);
  128.                             TcpListener fileServer = new TcpListener(ep);
  129.                             fileServer.Start();
  130.                             TcpClient files = fileServer.AcceptTcpClient();
  131.                             NetworkStream fileNetworkStream = files.GetStream();                            
  132.                             Byte[] data = new Byte[fs.Length + 1];
  133.                             fs.Read(data, 0, data.Length);
  134.                             fileNetworkStream.WriteAsync(data, 0, data.Length);                                                        
  135.                             fs.Close();
  136.                             fileServer.Stop();                              
  137.  
  138.                             break;
  139.                         }
  140.                     case "PWD":
  141.                         {
  142.                             string dir = Directory.GetCurrentDirectory() + '\0';
  143.                             stream.Write(Encoding.ASCII.GetBytes(dir), 0, dir.Length);
  144.                             break;
  145.                         }              
  146.                     case "MLSD":
  147.                         {
  148.                             string[] filePaths = Directory.GetFiles("D:\\FTP\\", @"*", SearchOption.AllDirectories);
  149.                             string awq = "";
  150.                             foreach (var a in filePaths)
  151.                             {
  152.                                 awq += a;
  153.                                 awq += "\n";
  154.                             }
  155.                             awq += "\0";
  156.                             stream.Write(Encoding.ASCII.GetBytes(awq), 0, awq.Length);
  157.                             break;
  158.                         }
  159.                     case "QUIT":
  160.                         {
  161.                             authorizedEmails.RemoveAt(authorizedEmails.FindIndex(x => x.authorizedEmail.Equals(tab[1])));
  162.                             string rep = "221: Bye. " + '\0';
  163.                             stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
  164.                             stream.Close();
  165.                             client.Close();
  166.                             break;
  167.                         }
  168.                 }
  169.             }
  170.         }
  171.  
  172.  
  173.         byte[] GetFileData(String path)
  174.         {
  175.             using (var sr = new StreamReader(path))
  176.             {
  177.                 return ASCIIEncoding.ASCII.GetBytes(sr.ReadToEnd());
  178.             }
  179.  
  180.         }
  181.         void ProcessIncomingConnection(IAsyncResult ar)
  182.         {
  183.             TcpListener listener = (TcpListener)ar.AsyncState;
  184.             TcpClient client = listener.EndAcceptTcpClient(ar);
  185.             ThreadPool.QueueUserWorkItem(ProcessIncomingData, client);
  186.             tcpClientConnected.Set();
  187.         }
  188.  
  189.         public void start()
  190.         {
  191.             IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 21);
  192.             TcpListener listener = new TcpListener(endpoint);
  193.             listener.Start();
  194.             while (true)
  195.             {
  196.                 tcpClientConnected.Reset();
  197.                 listener.BeginAcceptTcpClient(new AsyncCallback(ProcessIncomingConnection), listener);
  198.                 tcpClientConnected.WaitOne();
  199.             }
  200.         }
  201.     }
  202.  
  203.     class Program
  204.     {
  205.         static void Main(string[] args)
  206.         {
  207.             server s = new server();
  208.             s.start();
  209.         }
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement