Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using NLog;
- using System.IO;
- namespace RedBad
- {
- public class RedServer
- {
- public const int BackLog = 1024;
- private ProxyStatePool StatePool;
- private Socket ServerSocket;
- public Logger Log { get; private set; }
- public string LocalHost { get; private set; }
- public string RemoteHost { get; private set; }
- public int RemotePort { get; private set; }
- public RedServer(string localHost, string remoteHost, int remotePort)
- {
- LocalHost = localHost;
- StatePool = new ProxyStatePool();
- ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- Log = LogManager.GetLogger("server");
- RemoteHost = remoteHost;
- RemotePort = remotePort;
- }
- public void Start(IPAddress address, int port)
- {
- ServerSocket.Bind(new IPEndPoint(address, port));
- ServerSocket.Listen(BackLog);
- BeginAccept();
- }
- private void BeginAccept()
- {
- ServerSocket.BeginAccept(EndAccept, null);
- }
- private void EndAccept(IAsyncResult e)
- {
- try
- {
- var skt = ServerSocket.EndAccept(e);
- Log.Trace("New connection: {0}", skt.RemoteEndPoint);
- BeginAccept();
- var remoteskt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- var state = StatePool.Pop(skt, remoteskt);
- remoteskt.BeginConnect(RemoteHost, RemotePort, EndConnect, state);
- }
- catch (Exception ex)
- {
- Log.Fatal(ex);
- }
- }
- private void EndConnect(IAsyncResult e)
- {
- var state = (ProxyState)e.AsyncState;
- var from = state.From;
- var to = state.To;
- try
- {
- to.EndConnect(e);
- if (!to.Connected || !from.Connected)
- {
- DisposeState(state);
- }
- else
- {
- var c2s = state;
- var s2c = StatePool.Pop(to, from);
- Log.Trace("New states: {0} <-> {1}", c2s.Id, s2c.Id);
- BeginReceive(c2s);
- BeginReceive(s2c);
- }
- }
- catch
- {
- DisposeState(state);
- }
- }
- private bool BeginReceive(ProxyState state)
- {
- try
- {
- state.From.BeginReceive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, EndReceive, state);
- return true;
- }
- catch
- {
- DisposeState(state);
- return false;
- }
- }
- private void EndReceive(IAsyncResult e)
- {
- var state = (ProxyState)e.AsyncState;
- try
- {
- var length = state.From.EndReceive(e);
- if (length <= 0)
- {
- DisposeState(state);
- return;
- }
- var buffer = new byte[length];
- Buffer.BlockCopy(state.Buffer, 0, buffer, 0, length);
- state.To.BeginSend(buffer, 0, length, SocketFlags.None, null, null);
- BeginReceive(state);
- }
- catch
- {
- DisposeState(state);
- }
- }
- private void DisposeState(ProxyState state)
- {
- try
- {
- StatePool.Push(state);
- state.From.Dispose();
- state.To.Dispose();
- Log.Trace("Disposed: {0}", state);
- }
- catch(Exception ex)
- {
- Log.Error("State dispose error: {0}", state);
- Log.Error(ex);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment