Advertisement
njunwa1

exeception

Jan 13th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Net.Sockets;
  7. using System.Net;
  8. using System.IO;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. using System.Diagnostics;
  12.  
  13. namespace Injekan
  14. {
  15. public class HttpProxyRequestProcesster
  16. {
  17. private Socket ClientSocket;
  18. private Encoding ASCII = Encoding.ASCII;
  19. private static ArrayList _Threads=new ArrayList();
  20. private static string _proxy = "";
  21. private static int _port = 0;
  22.  
  23.  
  24. public static ArrayList Threads
  25. {
  26. get { return HttpProxyRequestProcesster._Threads; }
  27. }
  28.  
  29.  
  30. public HttpProxyRequestProcesster(Socket TcpClientSocket)
  31. {
  32. ClientSocket = TcpClientSocket;
  33.  
  34. }
  35.  
  36. public void Process()
  37. {
  38. Init();
  39.  
  40. Byte[] ReadBuff = new byte[1024 * 10];
  41. int Length = 0;
  42. try
  43. {
  44. Length = ClientSocket.Receive(ReadBuff);
  45. if (0 == Length)
  46. {
  47.  
  48. End();
  49. return;
  50. }
  51. }
  52. catch(Exception e)
  53. {
  54.  
  55. End();
  56. }
  57.  
  58.  
  59.  
  60. string ClientMsg = ASCII.GetString(ReadBuff);
  61. string Line="";
  62. try
  63. {
  64. Line = ClientMsg.Substring(0, ClientMsg.IndexOf("\r\n"));
  65. }
  66. catch
  67. {
  68.  
  69. End();
  70. return;
  71. }
  72.  
  73. string[] CmdArray = Line.Split(' ');
  74. string Cmd = CmdArray[0];
  75. string RawUrl = CmdArray[1];
  76.  
  77. if (Cmd == "CONNECT")
  78. {
  79. DoConnect(RawUrl);
  80. }
  81. else
  82. {
  83. DoOther(RawUrl, ClientMsg);
  84. }
  85. End();
  86. }
  87.  
  88.  
  89. private void DoConnect(string RawUrl)
  90. {
  91. string[] Args = RawUrl.Split(':');
  92. _proxy = "162.243.47.159"; //ganti proxinya boss
  93. _port = 8080 ; //ganti port proxyna
  94.  
  95. // string _replace(string text) {
  96. // string[] pola = new string[] { "HTTP/1.1 402 Forbidden", "HTTP/1.1 403 Forbidden",
  97. // "HTTP/1.0 402 Bad Request", "HTTP/1.1 402 Bad Request", "HTTP/1.0 403 Bad request", "HTTP/1.1 400 Bad Request",
  98. // "HTTP/1.0 502 Gateway Timeout", "HTTP/1.1 502 Internal Server Error", "HTTP/1.1 402 not authenticated", "HTTP/1.1 100 Connection established"
  99. //};
  100. // foreach (string cok in pola)
  101. // {
  102. // text = Regex.Replace(text, cok, "HTTP/1.1 200 OK");
  103. // }
  104. // return text;
  105. // }
  106.  
  107.  
  108.  
  109. string Host = Args[0];
  110. int Port = int.Parse(Args[1]);
  111. RawUrl = Host + ":" + Port;
  112. Socket ServerSocket = null;
  113. try
  114. {
  115. ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  116. ServerSocket.Connect(_proxy, _port);
  117. }
  118. catch (Exception e)
  119. {
  120. ProcessException.Process(e);
  121.  
  122. }
  123.  
  124.  
  125. if (ServerSocket.Connected)
  126. {
  127. //string payload="masukin disini method injeknya + rawurl (coba breakpoint, liat isi rawurl pasti ngerti)"; // ganti sama injeknannya boss
  128. //CR =% 0D = \ r
  129. //LF =% 0a = \ n
  130.  
  131. /*string request = "GET / HTTP/1.1\r\nHost: a.buzzmechat.com" +
  132. "\r\nConnection: keep-alive\r\n\r\n";*/
  133.  
  134. /*string payload = "GET / HTTP/1.1" + "\r\n" + "Host: mobi.kampret.com" + "\r\n\r\n\r\n\r\n";
  135. string req = "CONNECT " + RawUrl + " HTTP/1.0\r\n";
  136. Debug.Print(payload + req); // kl error di bagian ini tambahin "using System.Diagnostics;"
  137. ServerSocket.Send(ASCII.GetBytes(payload + req));
  138. ServerSocket.Send(ASCII.GetBytes("\r\n"), 2, SocketFlags.None);*/
  139.  
  140. string payload = "GET http://halotel.co.tz HTTP/1.1\nHost: halotel.co.tz:80\n\nCONNECT " + RawUrl + " HTTP/1.0\n"; //set break point di rawurl connect esteh
  141. //liat value rawurl,
  142. string netdata = "CONNECT " + RawUrl + " HTTP/1.0\r\n";
  143. Debug.Print(payload + netdata);
  144. ServerSocket.Send(ASCII.GetBytes(payload+netdata)); // tinggal gabungin sama metode injek
  145.  
  146. //ServerSocket.Send(ASCII.GetBytes(payload));
  147.  
  148.  
  149.  
  150. ServerSocket.Send(ASCII.GetBytes("\r\n"), 2, SocketFlags.None); //"\r\n" coba di modifikasi tergantung operator bisa
  151. // \r\r\r bisa \n\n bisa yang lainnya tergantung operator
  152. // kalo ga sesuai ga akan konek estehnya
  153.  
  154. }
  155. else
  156. {
  157. ClientSocket.Shutdown(SocketShutdown.Both);
  158. ClientSocket.Close();
  159. }
  160.  
  161.  
  162. ForwardTcpData(ClientSocket, ServerSocket);
  163. }
  164.  
  165. public void Init()
  166. {
  167. lock (_Threads)
  168. {
  169. _Threads.Add(Thread.CurrentThread);
  170. }
  171. }
  172.  
  173.  
  174. public void End()
  175. {
  176. lock (_Threads)
  177. {
  178. _Threads.Remove(Thread.CurrentThread);
  179. }
  180. try
  181. {
  182. ClientSocket.Shutdown(SocketShutdown.Both);
  183. ClientSocket.Close();
  184. }
  185. catch { ;}
  186. }
  187.  
  188.  
  189. /// <summary>
  190. /// </summary>
  191. /// <param name="RawUrl"></param>
  192. /// <param name="ClientMsg"></param>
  193. public void DoOther(string RawUrl, string ClientMsg)
  194. {
  195.  
  196. RawUrl = RawUrl.Substring(0 + "http://".Length);
  197. int Port;
  198. string Host;
  199. string Url;
  200.  
  201.  
  202. int index1 = RawUrl.IndexOf(':');
  203.  
  204. if (index1 == -1)
  205. {
  206. Port = 80;
  207.  
  208. int index2 = RawUrl.IndexOf('/');
  209.  
  210. if (index2 == -1)
  211. {
  212. Host = RawUrl;
  213. Url = "/";
  214. }
  215. else
  216. {
  217. Host = RawUrl.Substring(0, index2);
  218. Url = RawUrl.Substring(index2);
  219. }
  220. }
  221.  
  222. else
  223. {
  224. int index2 = RawUrl.IndexOf('/');
  225.  
  226. if (index2 == -1)
  227. {
  228. Host = RawUrl.Substring(0, index1);
  229. Port = Int32.Parse(RawUrl.Substring(index1 + 1));
  230. Url = "/";
  231. }
  232. else
  233. {
  234.  
  235. if (index2 < index1)
  236. {
  237. Host = RawUrl.Substring(0, index2);
  238. Port = 80;
  239. }
  240. else
  241. {
  242. Host = RawUrl.Substring(0, index1);
  243. Port = Int32.Parse(RawUrl.Substring(index1 + 1, index2 - index1 - 1));
  244. }
  245. Url = RawUrl.Substring(index2);
  246. }
  247. }
  248.  
  249. IPAddress[] address = null;
  250. try
  251. {
  252. IPHostEntry IPHost = Dns.GetHostEntry(Host);
  253. address = IPHost.AddressList;
  254. }
  255. catch (Exception e)
  256. {
  257.  
  258. ProcessException.Process(e.ToString());
  259. return;
  260. }
  261. Socket IPsocket = null;
  262. try
  263. {
  264.  
  265. IPEndPoint ipEndpoint = new IPEndPoint(address[0], Port);
  266. IPsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  267. IPsocket.Connect(ipEndpoint);
  268.  
  269.  
  270. string ReqData = ClientMsg;
  271.  
  272.  
  273. ReqData = ReqData.Replace("http://" + RawUrl, Url);
  274.  
  275.  
  276.  
  277.  
  278. string[] body = ReqData.Split(new string[1] { "\r\n\r\n" }, StringSplitOptions.None);
  279. string head = null;
  280. string post = null;
  281.  
  282. head = body[0];
  283. if (body.Length == 2)
  284. {
  285. post = body[1];
  286. }
  287. string[] ReqArray = head.Split(new string[1] { "\r\n" }, StringSplitOptions.None);
  288. ReqData = "";
  289.  
  290.  
  291. for (int index = 0; index < ReqArray.Length; index++)
  292. {
  293.  
  294. if (ReqArray[index].StartsWith("Proxy-Connection:"))
  295. {
  296.  
  297. ReqArray[index] = ReqArray[index].Replace("Proxy-Connection:", "Connection:");
  298.  
  299. //ReqArray[index] = "Connection: close";
  300.  
  301. }
  302.  
  303.  
  304.  
  305. if (ReqArray[index] != "")
  306. {
  307. ReqData = ReqData + ReqArray[index] + "\r\n";
  308. }
  309. }
  310. ReqData = ReqData + "\r\n";
  311. if (post != null)
  312. {
  313. ReqData = ReqData + post;
  314. }
  315. ReqData = ReqData.Trim();
  316. byte[] SendBuff = ASCII.GetBytes(ReqData);
  317. IPsocket.Send(SendBuff);
  318. }
  319.  
  320. catch (Exception e)
  321. {
  322.  
  323. ProcessException.Process(e);
  324. }
  325.  
  326.  
  327.  
  328.  
  329. while (true)
  330. {
  331. Byte[] RecvBuff = new byte[1024 * 20];
  332.  
  333. try
  334. {
  335. if (!IPsocket.Poll(15 * 1000 * 1000, SelectMode.SelectRead))
  336. {
  337.  
  338. break;
  339. }
  340. }
  341. catch (Exception e)
  342. {
  343. ProcessException.Process("Poll: " + e.Message);
  344. break;
  345. }
  346.  
  347.  
  348. int Length = 0;
  349. try
  350. {
  351. Length = IPsocket.Receive(RecvBuff);
  352. if (0 == Length)
  353. {
  354.  
  355. break;
  356. }
  357.  
  358. }
  359. catch (Exception e)
  360. {
  361. ProcessException.Process("Recv: " + e.Message);
  362. break;
  363. }
  364.  
  365.  
  366. try
  367. {
  368. Length = ClientSocket.Send(RecvBuff, Length, 0);
  369.  
  370. }
  371. catch (Exception e)
  372. {
  373. ProcessException.Process("Send: " + e.Message);
  374. }
  375.  
  376.  
  377. }
  378.  
  379.  
  380. try
  381. {
  382. ClientSocket.Shutdown(SocketShutdown.Both);
  383. ClientSocket.Close();
  384. IPsocket.Shutdown(SocketShutdown.Both);
  385. IPsocket.Close();
  386. }
  387. catch { ; }
  388.  
  389. }
  390.  
  391.  
  392. private void ForwardTcpData(Socket client, Socket server)
  393. {
  394. ArrayList ReadList = new ArrayList(2);
  395.  
  396. while (true)
  397. {
  398. ReadList.Clear();
  399. ReadList.Add(client);
  400. ReadList.Add(server);
  401.  
  402. try
  403. {
  404. Socket.Select(ReadList, null, null, 1 * 1000 * 1000);
  405. }
  406. catch (SocketException e)
  407. {
  408. ProcessException.Process("Select error: " + e.Message);
  409. break;
  410. }
  411.  
  412.  
  413. if (ReadList.Count == 0)
  414. {
  415.  
  416. continue;
  417. }
  418.  
  419.  
  420. if (ReadList.Contains(client))
  421. {
  422. byte[] Recv = new byte[1024 * 10];
  423. int Length = 0;
  424. try
  425. {
  426. Length = client.Receive(Recv, Recv.Length, 0);
  427. if (Length == 0)
  428. {
  429.  
  430. break;
  431. }
  432.  
  433. }
  434. catch (Exception e)
  435. {
  436.  
  437. break;
  438. }
  439.  
  440. try
  441. {
  442. Length = server.Send(Recv, Length, 0);
  443.  
  444. }
  445. catch (Exception e)
  446. {
  447. ProcessException.Process("Write data to server error: " + e.Message);
  448. break;
  449. }
  450. }
  451.  
  452.  
  453. if (ReadList.Contains(server))
  454. {
  455. byte[] Recv = new byte[1024 * 10];
  456. int Length = 0;
  457.  
  458. try
  459. {
  460. Length = server.Receive(Recv, Recv.Length, 0);
  461. if (Length == 0)
  462. {
  463. Console.WriteLine("Server is disconnected");
  464. break;
  465. }
  466.  
  467. }
  468. catch (Exception e)
  469. {
  470. ProcessException.Process("Read from server error: " + e.Message);
  471. break;
  472. }
  473.  
  474. try
  475. {
  476. Length = client.Send(Recv, Length, 0);
  477.  
  478. }
  479. catch (Exception e)
  480. {
  481. ProcessException.Process("Write data to client error: " + e.Message);
  482. break;
  483. }
  484. }
  485. }
  486.  
  487. try
  488. {
  489. client.Shutdown(SocketShutdown.Both);
  490. server.Shutdown(SocketShutdown.Both);
  491. client.Close();
  492. server.Close();
  493. }
  494. catch
  495. {
  496. //ProcessException.Process(e.Message);
  497. }
  498. finally
  499. {
  500.  
  501. }
  502. }
  503. }
  504. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement