Guest User

Untitled

a guest
Feb 13th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using NLog;
  8. using System.IO;
  9.  
  10. namespace RedBad
  11. {
  12.     public class RedServer
  13.     {
  14.         public const int BackLog = 1024;
  15.  
  16.         private ProxyStatePool StatePool;
  17.         private Socket ServerSocket;
  18.        
  19.         public Logger Log { get; private set; }
  20.         public string LocalHost { get; private set; }
  21.         public string RemoteHost { get; private set; }
  22.         public int RemotePort { get; private set; }
  23.  
  24.         public RedServer(string localHost, string remoteHost, int remotePort)
  25.         {
  26.             LocalHost = localHost;
  27.             StatePool = new ProxyStatePool();
  28.             ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  29.             Log = LogManager.GetLogger("server");
  30.  
  31.             RemoteHost = remoteHost;
  32.             RemotePort = remotePort;
  33.         }
  34.         public void Start(IPAddress address, int port)
  35.         {
  36.             ServerSocket.Bind(new IPEndPoint(address, port));
  37.             ServerSocket.Listen(BackLog);
  38.             BeginAccept();
  39.         }
  40.         private void BeginAccept()
  41.         {
  42.             ServerSocket.BeginAccept(EndAccept, null);
  43.         }
  44.         private void EndAccept(IAsyncResult e)
  45.         {
  46.             try
  47.             {
  48.                 var skt = ServerSocket.EndAccept(e);
  49.                 Log.Trace("New connection: {0}", skt.RemoteEndPoint);
  50.                 BeginAccept();
  51.  
  52.                 var remoteskt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  53.                 var state = StatePool.Pop(skt, remoteskt);
  54.  
  55.                 remoteskt.BeginConnect(RemoteHost, RemotePort, EndConnect, state);
  56.             }
  57.             catch (Exception ex)
  58.             {
  59.                 Log.Fatal(ex);
  60.             }
  61.         }
  62.         private void EndConnect(IAsyncResult e)
  63.         {
  64.             var state = (ProxyState)e.AsyncState;
  65.  
  66.             var from = state.From;
  67.             var to = state.To;
  68.  
  69.             try
  70.             {
  71.                 to.EndConnect(e);
  72.  
  73.                 if (!to.Connected || !from.Connected)
  74.                 {
  75.                     DisposeState(state);
  76.                 }
  77.                 else
  78.                 {
  79.                     var c2s = state;
  80.                     var s2c = StatePool.Pop(to, from);
  81.                     Log.Trace("New states: {0} <-> {1}", c2s.Id, s2c.Id);
  82.  
  83.                     BeginReceive(c2s);
  84.                     BeginReceive(s2c);
  85.                 }
  86.             }
  87.             catch
  88.             {
  89.                 DisposeState(state);
  90.             }
  91.         }
  92.         private bool BeginReceive(ProxyState state)
  93.         {
  94.             try
  95.             {
  96.                 state.From.BeginReceive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, EndReceive, state);
  97.                 return true;
  98.             }
  99.             catch
  100.             {
  101.                 DisposeState(state);
  102.                 return false;
  103.             }
  104.         }
  105.         private void EndReceive(IAsyncResult e)
  106.         {
  107.             var state = (ProxyState)e.AsyncState;
  108.             try
  109.             {
  110.                 var length = state.From.EndReceive(e);
  111.                 if (length <= 0)
  112.                 {
  113.                     DisposeState(state);
  114.                     return;
  115.                 }
  116.  
  117.                 var buffer = new byte[length];
  118.                 Buffer.BlockCopy(state.Buffer, 0, buffer, 0, length);
  119.                 state.To.BeginSend(buffer, 0, length, SocketFlags.None, null, null);
  120.  
  121.                 BeginReceive(state);
  122.             }
  123.             catch
  124.             {
  125.                 DisposeState(state);
  126.             }
  127.         }
  128.  
  129.         private void DisposeState(ProxyState state)
  130.         {
  131.             try
  132.             {
  133.                 StatePool.Push(state);
  134.                 state.From.Dispose();
  135.                 state.To.Dispose();
  136.  
  137.                 Log.Trace("Disposed: {0}", state);
  138.             }
  139.             catch(Exception ex)
  140.             {
  141.                 Log.Error("State dispose error: {0}", state);
  142.                 Log.Error(ex);
  143.             }
  144.         }
  145.     }
  146. }
Add Comment
Please, Sign In to add comment