Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.IO;
- using System.Text.RegularExpressions;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- namespace Server_FTP
- {
- public class Client
- {
- public String authorizedEmail;
- public Int32 port;
- public Client(String email, Int32 p)
- {
- authorizedEmail = email;
- port = p;
- }
- }
- public class server
- {
- ManualResetEvent tcpClientConnected = new ManualResetEvent(false);
- List<Client> authorizedEmails = new List<Client>();
- void ProcessIncomingData(object obj)
- {
- TcpClient client = (TcpClient)obj;
- StringBuilder sb = new StringBuilder();
- bool userauth = true;
- bool accessgranted = true;
- Directory.SetCurrentDirectory("D:/FTP");
- using (NetworkStream stream = client.GetStream())
- {
- int i;
- while ((i = stream.ReadByte()) != 0)
- {
- sb.Append((char)i);
- }
- String[] tab = sb.ToString().Split(' ');
- switch (tab[0].ToUpper())
- {
- case "DELE":
- {
- try
- {
- File.Delete(tab[1]);
- }
- catch (DirectoryNotFoundException)
- {
- Console.WriteLine("Proba usuniecia pliku zakonczona niepowodzeniem. Podana sciezka jest nieprawidlowa");
- }
- break;
- }
- case "USER":
- {
- accessgranted = true;
- userauth = true;
- Byte[] bytess = new Byte[256];
- if (tab[1].Equals("anonymous"))
- {
- string rep = "331: Password is required to get access." + '\0';
- stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
- }
- else
- {
- string rep = "530: Access denied" + '\0';
- stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
- }
- break;
- }
- case "PASS":
- {
- if (userauth)
- {
- string userpass = tab[1];
- if (Regex.IsMatch(tab[1], @"^[a-z0-9][-a-z0-9._]+@([-a-z0-9]+\.)+[a-z]{2,5}$", RegexOptions.IgnoreCase))
- {
- string rep = "230: Access granted. " + '\0';
- authorizedEmails.Add(new Client(userpass,13000));
- stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
- accessgranted = true;
- }
- else
- {
- string rep = "530: Access denied. " + '\0';
- stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
- }
- }
- else
- {
- string rep = "User has not been specified yet. " + '\0';
- stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
- }
- break;
- }
- case "PASV":
- {
- Random rnd = new Random();
- Int32 portPASV = rnd.Next(1024, 13000);
- Int32 port1 = portPASV / 256, port2 = portPASV % 256;
- String fn = "127.0.0.1";
- fn = fn.Replace('.', ',');
- String message = fn + "," + (port1.ToString()) + "," + (port2.ToString() + '\0');
- Byte[] bytes = Encoding.ASCII.GetBytes(message);
- authorizedEmails[authorizedEmails.FindIndex(x => x.authorizedEmail.Equals(tab[1]))].port = portPASV;
- Console.WriteLine(authorizedEmails[authorizedEmails.FindIndex(x => x.authorizedEmail.Equals(tab[1]))].authorizedEmail);
- Console.WriteLine(authorizedEmails[authorizedEmails.FindIndex(x => x.authorizedEmail.Equals(tab[1]))].port);
- stream.Write(bytes, 0, bytes.Length);
- break;
- }
- case "RETR":
- {
- Client klient = authorizedEmails[authorizedEmails.FindIndex(x => x.authorizedEmail.Equals(tab[2]))];
- Directory.SetCurrentDirectory("D:\\FTP\\");
- string path = "D:\\FTP\\" + tab[1];
- FileStream fs = new FileStream(tab[1], FileMode.Open, FileAccess.Read);
- Console.WriteLine(klient.authorizedEmail);
- Console.WriteLine(klient.port);
- IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), klient.port);
- TcpListener fileServer = new TcpListener(ep);
- fileServer.Start();
- TcpClient files = fileServer.AcceptTcpClient();
- NetworkStream fileNetworkStream = files.GetStream();
- Byte[] data = new Byte[fs.Length + 1];
- fs.Read(data, 0, data.Length);
- fileNetworkStream.WriteAsync(data, 0, data.Length);
- fs.Close();
- fileServer.Stop();
- break;
- }
- case "PWD":
- {
- string dir = Directory.GetCurrentDirectory() + '\0';
- stream.Write(Encoding.ASCII.GetBytes(dir), 0, dir.Length);
- break;
- }
- case "MLSD":
- {
- string[] filePaths = Directory.GetFiles("D:\\FTP\\", @"*", SearchOption.AllDirectories);
- string awq = "";
- foreach (var a in filePaths)
- {
- awq += a;
- awq += "\n";
- }
- awq += "\0";
- stream.Write(Encoding.ASCII.GetBytes(awq), 0, awq.Length);
- break;
- }
- case "QUIT":
- {
- authorizedEmails.RemoveAt(authorizedEmails.FindIndex(x => x.authorizedEmail.Equals(tab[1])));
- string rep = "221: Bye. " + '\0';
- stream.Write(Encoding.ASCII.GetBytes(rep), 0, rep.Length);
- stream.Close();
- client.Close();
- break;
- }
- }
- }
- }
- byte[] GetFileData(String path)
- {
- using (var sr = new StreamReader(path))
- {
- return ASCIIEncoding.ASCII.GetBytes(sr.ReadToEnd());
- }
- }
- void ProcessIncomingConnection(IAsyncResult ar)
- {
- TcpListener listener = (TcpListener)ar.AsyncState;
- TcpClient client = listener.EndAcceptTcpClient(ar);
- ThreadPool.QueueUserWorkItem(ProcessIncomingData, client);
- tcpClientConnected.Set();
- }
- public void start()
- {
- IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 21);
- TcpListener listener = new TcpListener(endpoint);
- listener.Start();
- while (true)
- {
- tcpClientConnected.Reset();
- listener.BeginAcceptTcpClient(new AsyncCallback(ProcessIncomingConnection), listener);
- tcpClientConnected.WaitOne();
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- server s = new server();
- s.start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement