Advertisement
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.Threading.Tasks;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- using System.Windows.Forms;
- using System.IO;
- namespace WouldRevealProjectName.Proxy
- {
- public class ProxyServer
- {
- #region Header Variables
- private Thread listenerThread;
- private TcpClient mcServer;
- private NetworkStream stream = null;
- private List<ProxyClient> clients = new List<ProxyClient>();
- private string host;
- private string remoteHost;
- private int port;
- private int serverport;
- private bool acceptClients = false;
- #endregion
- public ProxyServer()
- {
- remoteHost = Program.GetConfigProperty("remote-address", "127.0.0.1");
- host = Program.GetServerProperty("server-ip", "127.0.0.1");
- port = Convert.ToInt32(Program.GetConfigProperty("listening-port", "25565"));
- serverport = Convert.ToInt32(Program.GetServerProperty("server-port", "25566"));
- listenerThread = new Thread(__ListenerThread);
- mcServer = new TcpClient();
- }
- public List<ProxyClient> GetClientList()
- {
- return clients;
- }
- public bool Start()
- {
- try
- {
- listenerThread.Start();
- mcServer.Connect(IPAddress.Parse("127.0.0.1"), serverport);
- acceptClients = true;
- return true;
- }
- catch (Exception ex)
- {
- this.Stop();
- MessageBox.Show("Exception caught inside of ProxyServer::Start\r\n" + ex.Message);
- return false;
- }
- }
- public bool Stop()
- {
- try
- {
- listenerThread.Abort();
- acceptClients = false;
- return true;
- }
- catch (Exception ex)
- {
- MessageBox.Show("Exception caught inside of ProxyServer::Stop\r\n" + ex.Message);
- return false;
- }
- }
- public bool Dispose()
- {
- try
- {
- listenerThread.Abort();
- while (listenerThread.IsAlive)
- {
- listenerThread.Abort();
- Thread.Sleep(10);
- }
- return true;
- }
- catch (Exception ex)
- {
- MessageBox.Show("Exception caught inside of ProxyServer::Dispose\r\n" + ex.Message);
- return false;
- }
- }
- #region Threads
- private void __ListenerThread()
- {
- TcpListener listener;
- try
- {
- listener = new TcpListener(IPAddress.Parse(host), port);
- listener.Start();
- if (listener.Server.Connected)
- {
- SetStream(new NetworkStream(listener.Server));
- }
- }
- catch (Exception ex) { SimpleLogger.log(System.Drawing.Color.Crimson, "Exception::ListenerThread: " + ex.Message); return; }
- while (acceptClients)
- {
- if (listener.Pending())
- {
- try
- {
- TcpClient acceptTcpClient = listener.AcceptTcpClient();
- ProxyClient client = new ProxyClient(acceptTcpClient, this);
- client.Start(host, serverport);
- clients.Add(client);
- SimpleLogger.log(System.Drawing.Color.Green, "(" + acceptTcpClient.Client.RemoteEndPoint.ToString() + ") connected!");
- }
- catch (Exception ex) { SimpleLogger.log(System.Drawing.Color.Crimson, "Exception::ListenerThread: " + ex.Message); }
- }
- }
- }
- private void __PlayerListRefreshThread()
- {
- while (true)
- {
- foreach (ProxyClient pc in clients)
- {
- Program.mainFrm.lbPlayers.Items.Clear();
- Program.mainFrm.lbPlayers.Items.Add(pc);
- }
- Thread.Sleep(1000);
- }
- }
- #endregion
- #region Networking Methods
- public NetworkStream GetStream()
- {
- return stream;
- }
- public void SetStream(NetworkStream stream)
- {
- this.stream = stream;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement