Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.61 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. class MyTcpListener
  9. {
  10. static async void SendAsync(string msg, NetworkStream stream)
  11. {
  12. try
  13. {
  14. byte[] meesg = System.Text.Encoding.ASCII.GetBytes(msg);
  15. await stream.WriteAsync(meesg, 0, meesg.Length);
  16. }
  17. catch (System.ObjectDisposedException e)
  18. {
  19. //jak zamykam przez QUIT wyskakuje, ale nie ma wtedy znaczenia... chyba?
  20. }
  21. }
  22.  
  23.  
  24. public static async Task ReadFromFileAsync(byte[] buffer, FileStream fs)
  25. {
  26. await fs.ReadAsync(buffer, 0, buffer.Length);
  27. }
  28. public static async Task Server()
  29. {
  30. label:
  31. Console.Clear();
  32. TcpListener server = null;
  33. try
  34. {
  35. Int32 port = 21;
  36. IPAddress localAddr = IPAddress.Parse("127.0.0.1");
  37. server = new TcpListener(localAddr, port);
  38. server.Start();
  39. TcpListener serverdata;
  40. Int32 port1 = 13000;
  41. bool binflag = false;
  42. Byte[] bytes = new Byte[256];
  43. String data = null;
  44. string currentUser = String.Empty;
  45. Console.Write("Waiting for connection... ");
  46. TcpClient klient = await server.AcceptTcpClientAsync();
  47. Console.WriteLine(" Connection has been established.");
  48. NetworkStream stream = klient.GetStream();
  49. NetworkStream stream2;
  50. Directory.SetCurrentDirectory("D:/FTP");
  51. while (true)
  52. {
  53. label2:
  54. int i;
  55. bool userauth = false;
  56. bool accessgranted = false;
  57. Directory.CreateDirectory("D:/FTP");
  58. Directory.SetCurrentDirectory("D:/FTP");
  59. while (true)
  60. {
  61. int m = await stream.ReadAsync(bytes, 0, bytes.Length);
  62. data = System.Text.Encoding.ASCII.GetString(bytes, 0, m);
  63. string[] tab = new string[2];
  64. tab = data.Split(' ');
  65. switch (tab[0].ToUpper())
  66. {
  67. case "USER":
  68. {
  69. accessgranted = false;
  70. userauth = false;
  71. Byte[] bytess = new Byte[256];
  72. string username = tab[1];
  73. if (username.Equals("anonymous"))
  74. {
  75. SendAsync(" 331: Password is required to get access. ", stream);
  76. userauth = true;
  77. currentUser = username;
  78. }
  79. else
  80. {
  81. SendAsync(" 530: Access denied. ", stream);
  82. }
  83. break;
  84. }
  85. case "PASS":
  86. {
  87.  
  88. if (userauth)
  89. {
  90. string userpass = tab[1];
  91. 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)
  92. {
  93. SendAsync(" 230: Access granted. ", stream);
  94. accessgranted = true;
  95. }
  96. else
  97. {
  98. SendAsync(" 530: Access denied. ", stream);
  99. }
  100. }
  101. else
  102. {
  103. SendAsync(" User has not been specified yet. ", stream);
  104. }
  105. userauth = false;
  106. break;
  107. }
  108. case "TYPE":
  109. {
  110. string type = tab[1];
  111. SendAsync(" 200: OK. ", stream);
  112. if (type.ToUpper().Equals("A")) binflag = false;
  113. if (type.ToUpper().Equals("I")) binflag = true;
  114. break;
  115. }
  116. case "QUIT":
  117. {
  118. SendAsync(" 221: Bye. ", stream);
  119. stream.Close();
  120. klient.Close();
  121.  
  122. break;
  123. }
  124. case "PWD":
  125. {
  126. SendAsync((Directory.GetCurrentDirectory()), stream);
  127.  
  128. break;
  129. }
  130. case "RETR":
  131. {
  132. if (accessgranted)
  133. {
  134. FileStream fs = new FileStream(tab[1], FileMode.Open, FileAccess.Read);
  135. byte[] datas = new byte[fs.Length + 1];
  136. await ReadFromFileAsync(datas, fs);
  137. serverdata = new TcpListener(localAddr, port1);
  138. serverdata.Start();
  139. SendAsync("a", stream);
  140. Task<TcpClient> clientAsync = serverdata.AcceptTcpClientAsync();
  141.  
  142. TcpClient client2 = await clientAsync;
  143. stream2 = client2.GetStream();
  144. stream2.WriteAsync(datas, 0, datas.Length).Wait();
  145. Console.WriteLine("Transfer successful... ");
  146. SendAsync("Transfer completed. ", stream);
  147. goto label2;
  148. }
  149. else SendAsync(" Can not access this command", stream);
  150. break;
  151. }
  152. case "PASV":
  153. {
  154. if (accessgranted)
  155. {
  156. string sr = localAddr.ToString();
  157. Random rnd = new Random();
  158. port1 = rnd.Next(1024, 13000);
  159. Int32 p1, p2;
  160. p1 = port1 / 256;
  161. p2 = port1 % 256;
  162. string fn = localAddr.ToString();
  163. string[] tabb = new string[4];
  164. tabb = fn.Split('.');
  165. string message = tabb[0] + "," + tabb[1] + "," + tabb[2] + "," + tabb[3] + ",";
  166. message = message + (p1.ToString());
  167. message += ",";
  168. message += (p2.ToString());
  169. SendAsync(message + "\n", stream);
  170. //Dałoby sie bardziej elegancko stworzyc message z IP/portem, ale... działa. Jak działa, nie ruszać.
  171.  
  172.  
  173. }
  174. else SendAsync(" Can not access this command", stream);
  175. break;
  176. }
  177. case "MKD":
  178. {
  179. Directory.CreateDirectory(tab[1]);
  180. SendAsync(" Directory has been created. ", stream);
  181. break;
  182. }
  183.  
  184. default:
  185. {
  186. SendAsync(" Command not implemented. ", stream);
  187.  
  188. Console.WriteLine("Received {0}", data);
  189. break;
  190. }
  191. }
  192. }
  193.  
  194. }
  195. }
  196. catch (UnauthorizedAccessException e)
  197. {
  198. goto label;
  199. }
  200. catch (ObjectDisposedException e)
  201. {
  202. goto label;
  203.  
  204. }
  205. catch (IndexOutOfRangeException e)
  206. {
  207. Console.WriteLine("Connection terminated. ");
  208. goto label;
  209.  
  210. }
  211. catch (SocketException e)
  212. {
  213. Console.WriteLine("Connection terminated. ");
  214. }
  215. catch (IOException e)
  216. {
  217. goto label;
  218. }
  219. finally
  220. {
  221. server.Stop();
  222. }
  223. }
  224. public static void Main()
  225. {
  226. try
  227. {
  228. Server().Wait();
  229. }
  230. catch (Exception e)
  231. {
  232. Console.WriteLine("Exception: {0}", e);
  233. }
  234. }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement