fork download
  1. public class TcpProxyConnection
  2. {
  3. TextWriter fileText;
  4. FileStream fileBinary;
  5. string FileName = null;
  6.  
  7. protected TcpConnection _Client;
  8. protected TcpConnection _Server;
  9. protected TcpProxy _Parent;
  10.  
  11. public TcpProxyConnection(TcpProxy Parent, TcpConnection Client, IPEndPoint RemoteEndPoint)
  12. {
  13. try
  14. {
  15. _Parent = Parent;
  16. try
  17. {
  18. if (FormTcpSimpleProxy.LogFlag)
  19. {
  20. FileName = FormTcpSimpleProxy.GetNewFileName();
  21. fileText = File.CreateText(FileName);
  22. fileText.WriteLine("Connected to {0}", RemoteEndPoint);
  23. }
  24.  
  25. TcpConnection cnn = new TcpConnection();
  26. cnn.Connect(RemoteEndPoint.Address.ToString(), RemoteEndPoint.Port);
  27. _Server = cnn;
  28. }
  29. catch (Exception ex)
  30. {
  31. Client.Close();
  32. FormTcpSimpleProxy.Log("TcpProxyConnection() Error: {0}", ex.Message);
  33. return;
  34. }
  35. _Client = Client;
  36.  
  37. _Client.DataReceived += new DataReceivedEventHandler(_Client_DataReceived);
  38. _Client.Disconnected += new DisconnectedEventHandler(_Client_Disconnected);
  39. _Server.DataReceived += new DataReceivedEventHandler(_Server_DataReceived);
  40. _Server.Disconnected += new DisconnectedEventHandler(_Server_Disconnected);
  41. _Client.BeginReceiving();
  42. _Server.BeginReceiving();
  43. }
  44. catch (Exception ex)
  45. {
  46. Console.WriteLine("TcpProxyConnection() - " + ex.Message);
  47. Console.WriteLine(ex.StackTrace);
  48. }
  49. }
  50.  
  51. private void _Client_DataReceived(object Sender, DataReceivedEventArgs e)
  52. {
  53. try
  54. {
  55. if (_Server != null)
  56. {
  57. if (FormTcpSimpleProxy.LogFlag)
  58. {
  59. if (IsBinary(e.Buffer))
  60. {
  61. fileText.WriteLine("\r\nClient Binary Data: {0} bytes (not logged)", e.Buffer.Length);
  62. }
  63. else
  64. {
  65. fileText.WriteLine("\r\nClient Text:\r\n{0}", System.Text.Encoding.ASCII.GetString(e.Buffer));
  66. }
  67. fileText.Flush();
  68. }
  69. try
  70. {
  71. _Server.SendNow(e.Buffer, 0, e.Buffer.Length);
  72. }
  73. catch (Exception ex)
  74. {
  75. FormTcpSimpleProxy.Log("_Client_DataReceived() Error: {0}", ex.Message);
  76. ForceClose();
  77. }
  78. }
  79. }
  80. catch (Exception ex)
  81. {
  82. Console.WriteLine("TcpProxyConnection._Server_Disconnected() - " + ex.Message);
  83. Console.WriteLine(ex.StackTrace);
  84. }
  85. }
  86.  
  87. private void ForceClose()
  88. {
  89. try
  90. {
  91. if (fileText != null)
  92. {
  93. fileText.Close();
  94. fileText = null;
  95. if (fileBinary != null)
  96. {
  97. fileBinary.Close();
  98. fileBinary = null;
  99. }
  100. }
  101. }
  102. catch { }
  103.  
  104. try
  105. {
  106. _Client.Close();
  107. }
  108. catch { }
  109. _Client = null;
  110. try
  111. {
  112. _Server.Close();
  113. }
  114. catch { }
  115. _Server = null;
  116. _Parent.RemoveConnection(this);
  117. }
  118.  
  119. private void _Client_Disconnected(object Sender, DisconnectedEventArgs e)
  120. {
  121. ForceClose();
  122. }
  123.  
  124. private void _Server_DataReceived(object Sender, DataReceivedEventArgs e)
  125. {
  126. try
  127. {
  128. if (_Client != null)
  129. {
  130. if (FormTcpSimpleProxy.LogFlag)
  131. {
  132. if (IsBinary(e.Buffer))
  133. {
  134. fileText.WriteLine("\r\nServer Binary Data: {0} bytes", e.Buffer.Length);
  135. if (fileBinary == null)
  136. {
  137. fileBinary = File.Create(FileName + ".bin");
  138. }
  139. fileBinary.Write(e.Buffer, 0, e.Buffer.Length);
  140. fileBinary.Flush();
  141. }
  142. else
  143. {
  144. fileText.WriteLine("\r\nServer Text:\r\n{0}", System.Text.Encoding.ASCII.GetString(e.Buffer));
  145. }
  146. fileText.Flush();
  147. }
  148. try
  149. {
  150. _Client.SendNow(e.Buffer, 0, e.Buffer.Length);
  151. }
  152. catch (Exception ex)
  153. {
  154. FormTcpSimpleProxy.Log("_Server_DataReceived() Error: {0}", ex.Message);
  155. ForceClose();
  156. }
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. Console.WriteLine("TcpProxyConnection._Server_Disconnected() - " + ex.Message);
  162. Console.WriteLine(ex.StackTrace);
  163. }
  164. }
  165.  
  166. private void _Server_Disconnected(object Sender, DisconnectedEventArgs e)
  167. {
  168. try
  169. {
  170. ForceClose();
  171. }
  172. catch (Exception ex)
  173. {
  174. Console.WriteLine("TcpProxyConnection._Server_Disconnected() - " + ex.Message);
  175. Console.WriteLine(ex.StackTrace);
  176. }
  177. }
  178.  
  179. public void Close()
  180. {
  181. try
  182. {
  183. if (_Server != null)
  184. {
  185. _Server.Close();
  186. _Server = null;
  187. }
  188.  
  189. if (_Client != null)
  190. {
  191. _Client.Close();
  192. _Client = null;
  193. }
  194.  
  195. ForceClose();
  196. }
  197. catch (Exception ex)
  198. {
  199. Console.WriteLine("TcpProxyConnection.Close() - " + ex.Message);
  200. Console.WriteLine(ex.StackTrace);
  201. }
  202. }
  203.  
  204. public bool IsBinary(byte[] buffer)
  205. {
  206. return IsBinary(buffer, 0, buffer.Length);
  207. }
  208.  
  209. public bool IsBinary(byte[] buffer, int Start, int Length)
  210. {
  211. for (int i = 0; i < Length; i++)
  212. {
  213. byte b = buffer[Start + i];
  214. if (b != 10 && b != 13 && b != 9 && b < 32)
  215. {
  216. return true;
  217. }
  218. }
  219. return false;
  220. }
  221. }
  222.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty