Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- class MyTcpListener
- {
- static async void SendAsync(string msg, NetworkStream stream)
- {
- try
- {
- byte[] meesg = System.Text.Encoding.ASCII.GetBytes(msg);
- await stream.WriteAsync(meesg, 0, meesg.Length);
- }
- catch (System.ObjectDisposedException e)
- {
- //jak zamykam przez QUIT wyskakuje, ale nie ma wtedy znaczenia... chyba?
- }
- }
- public static async Task ReadFromFileAsync(byte[] buffer, FileStream fs)
- {
- await fs.ReadAsync(buffer, 0, buffer.Length);
- }
- public static async Task Server()
- {
- label:
- Console.Clear();
- TcpListener server = null;
- try
- {
- Int32 port = 21;
- IPAddress localAddr = IPAddress.Parse("127.0.0.1");
- server = new TcpListener(localAddr, port);
- server.Start();
- TcpListener serverdata;
- Int32 port1 = 13000;
- bool binflag = false;
- Byte[] bytes = new Byte[256];
- String data = null;
- string currentUser = String.Empty;
- Console.Write("Waiting for connection... ");
- TcpClient klient = await server.AcceptTcpClientAsync();
- Console.WriteLine(" Connection has been established.");
- NetworkStream stream = klient.GetStream();
- NetworkStream stream2;
- Directory.SetCurrentDirectory("D:/FTP");
- while (true)
- {
- label2:
- int i;
- bool userauth = false;
- bool accessgranted = false;
- Directory.CreateDirectory("D:/FTP");
- Directory.SetCurrentDirectory("D:/FTP");
- while (true)
- {
- int m = await stream.ReadAsync(bytes, 0, bytes.Length);
- data = System.Text.Encoding.ASCII.GetString(bytes, 0, m);
- string[] tab = new string[2];
- tab = data.Split(' ');
- switch (tab[0].ToUpper())
- {
- case "USER":
- {
- accessgranted = false;
- userauth = false;
- Byte[] bytess = new Byte[256];
- string username = tab[1];
- if (username.Equals("anonymous"))
- {
- SendAsync(" 331: Password is required to get access. ", stream);
- userauth = true;
- currentUser = username;
- }
- else
- {
- SendAsync(" 530: Access denied. ", stream);
- }
- break;
- }
- case "PASS":
- {
- if (userauth)
- {
- string userpass = tab[1];
- if (userpass.Contains("@") && !(userpass.StartsWith("@")) && !(userpass.EndsWith("@")) && (userpass.EndsWith(".com") || userpass.EndsWith(".pl")))//Jeśli hasło zawiera ale nie zaczyna i nie kończy się @, i kończy się na .com lub .pl (w skrócie jakiś tam e-mail, można regexem, ale na potrzeby protezy starczy)
- {
- SendAsync(" 230: Access granted. ", stream);
- accessgranted = true;
- }
- else
- {
- SendAsync(" 530: Access denied. ", stream);
- }
- }
- else
- {
- SendAsync(" User has not been specified yet. ", stream);
- }
- userauth = false;
- break;
- }
- case "TYPE":
- {
- string type = tab[1];
- SendAsync(" 200: OK. ", stream);
- if (type.ToUpper().Equals("A")) binflag = false;
- if (type.ToUpper().Equals("I")) binflag = true;
- break;
- }
- case "QUIT":
- {
- SendAsync(" 221: Bye. ", stream);
- stream.Close();
- klient.Close();
- break;
- }
- case "PWD":
- {
- SendAsync((Directory.GetCurrentDirectory()), stream);
- break;
- }
- case "RETR":
- {
- if (accessgranted)
- {
- FileStream fs = new FileStream(tab[1], FileMode.Open, FileAccess.Read);
- byte[] datas = new byte[fs.Length + 1];
- await ReadFromFileAsync(datas, fs);
- serverdata = new TcpListener(localAddr, port1);
- serverdata.Start();
- SendAsync("a", stream);
- Task<TcpClient> clientAsync = serverdata.AcceptTcpClientAsync();
- TcpClient client2 = await clientAsync;
- stream2 = client2.GetStream();
- stream2.WriteAsync(datas, 0, datas.Length).Wait();
- Console.WriteLine("Transfer successful... ");
- SendAsync("Transfer completed. ", stream);
- goto label2;
- }
- else SendAsync(" Can not access this command", stream);
- break;
- }
- case "PASV":
- {
- if (accessgranted)
- {
- string sr = localAddr.ToString();
- Random rnd = new Random();
- port1 = rnd.Next(1024, 13000);
- Int32 p1, p2;
- p1 = port1 / 256;
- p2 = port1 % 256;
- string fn = localAddr.ToString();
- string[] tabb = new string[4];
- tabb = fn.Split('.');
- string message = tabb[0] + "," + tabb[1] + "," + tabb[2] + "," + tabb[3] + ",";
- message = message + (p1.ToString());
- message += ",";
- message += (p2.ToString());
- SendAsync(message + "\n", stream);
- //Dałoby sie bardziej elegancko stworzyc message z IP/portem, ale... działa. Jak działa, nie ruszać.
- }
- else SendAsync(" Can not access this command", stream);
- break;
- }
- case "MKD":
- {
- Directory.CreateDirectory(tab[1]);
- SendAsync(" Directory has been created. ", stream);
- break;
- }
- default:
- {
- SendAsync(" Command not implemented. ", stream);
- Console.WriteLine("Received {0}", data);
- break;
- }
- }
- }
- }
- }
- catch (UnauthorizedAccessException e)
- {
- goto label;
- }
- catch (ObjectDisposedException e)
- {
- goto label;
- }
- catch (IndexOutOfRangeException e)
- {
- Console.WriteLine("Connection terminated. ");
- goto label;
- }
- catch (SocketException e)
- {
- Console.WriteLine("Connection terminated. ");
- }
- catch (IOException e)
- {
- goto label;
- }
- finally
- {
- server.Stop();
- }
- }
- public static void Main()
- {
- try
- {
- Server().Wait();
- }
- catch (Exception e)
- {
- Console.WriteLine("Exception: {0}", e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement