Advertisement
Guest User

Untitled

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