Add FrmTunnelTCP
This commit is contained in:
21
VAR.Toolbox/UI/FrmToolbox.Designer.cs
generated
21
VAR.Toolbox/UI/FrmToolbox.Designer.cs
generated
@@ -32,6 +32,7 @@
|
||||
this.btnBase64 = new System.Windows.Forms.Button();
|
||||
this.btnProxyCmd = new System.Windows.Forms.Button();
|
||||
this.btnWebcam = new System.Windows.Forms.Button();
|
||||
this.btnTunnelTCP = new System.Windows.Forms.Button();
|
||||
this.pnlSuspension1 = new VAR.Toolbox.UI.PnlSuspension();
|
||||
this.pnlCover1 = new VAR.Toolbox.UI.PnlCover();
|
||||
this.SuspendLayout();
|
||||
@@ -75,11 +76,23 @@
|
||||
this.btnWebcam.UseVisualStyleBackColor = true;
|
||||
this.btnWebcam.Click += new System.EventHandler(this.btnWebcam_Click);
|
||||
//
|
||||
// btnTunnelTCP
|
||||
//
|
||||
this.btnTunnelTCP.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.btnTunnelTCP.Location = new System.Drawing.Point(18, 208);
|
||||
this.btnTunnelTCP.Name = "btnTunnelTCP";
|
||||
this.btnTunnelTCP.Size = new System.Drawing.Size(278, 55);
|
||||
this.btnTunnelTCP.TabIndex = 5;
|
||||
this.btnTunnelTCP.Text = "TunnelTCP";
|
||||
this.btnTunnelTCP.UseVisualStyleBackColor = true;
|
||||
this.btnTunnelTCP.Click += new System.EventHandler(this.btnTunnelTCP_Click);
|
||||
//
|
||||
// pnlSuspension1
|
||||
//
|
||||
this.pnlSuspension1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.pnlSuspension1.Location = new System.Drawing.Point(18, 373);
|
||||
this.pnlSuspension1.Location = new System.Drawing.Point(18, 432);
|
||||
this.pnlSuspension1.Name = "pnlSuspension1";
|
||||
this.pnlSuspension1.Size = new System.Drawing.Size(278, 175);
|
||||
this.pnlSuspension1.TabIndex = 4;
|
||||
@@ -88,7 +101,7 @@
|
||||
//
|
||||
this.pnlCover1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.pnlCover1.Location = new System.Drawing.Point(18, 219);
|
||||
this.pnlCover1.Location = new System.Drawing.Point(18, 278);
|
||||
this.pnlCover1.Name = "pnlCover1";
|
||||
this.pnlCover1.Size = new System.Drawing.Size(278, 148);
|
||||
this.pnlCover1.TabIndex = 3;
|
||||
@@ -97,7 +110,8 @@
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(309, 560);
|
||||
this.ClientSize = new System.Drawing.Size(309, 619);
|
||||
this.Controls.Add(this.btnTunnelTCP);
|
||||
this.Controls.Add(this.pnlSuspension1);
|
||||
this.Controls.Add(this.pnlCover1);
|
||||
this.Controls.Add(this.btnWebcam);
|
||||
@@ -122,6 +136,7 @@
|
||||
private System.Windows.Forms.Button btnWebcam;
|
||||
private PnlCover pnlCover1;
|
||||
private PnlSuspension pnlSuspension1;
|
||||
private System.Windows.Forms.Button btnTunnelTCP;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,11 @@ namespace VAR.Toolbox.UI
|
||||
CreateWindow(typeof(FrmWebcam));
|
||||
}
|
||||
|
||||
private void btnTunnelTCP_Click(object sender, EventArgs e)
|
||||
{
|
||||
CreateWindow(typeof(FrmTunnelTCP));
|
||||
}
|
||||
|
||||
#region Window handling
|
||||
|
||||
private Form CreateWindow(Type type)
|
||||
@@ -85,6 +90,5 @@ namespace VAR.Toolbox.UI
|
||||
}
|
||||
|
||||
#endregion Window handling
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
161
VAR.Toolbox/UI/FrmTunnelTCP.Designer.cs
generated
Normal file
161
VAR.Toolbox/UI/FrmTunnelTCP.Designer.cs
generated
Normal file
@@ -0,0 +1,161 @@
|
||||
namespace VAR.Toolbox.UI
|
||||
{
|
||||
partial class FrmTunnelTCP
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.btnRun = new System.Windows.Forms.Button();
|
||||
this.btnStop = new System.Windows.Forms.Button();
|
||||
this.lblRemoteHost = new System.Windows.Forms.Label();
|
||||
this.lblRemotePort = new System.Windows.Forms.Label();
|
||||
this.lblLocalPort = new System.Windows.Forms.Label();
|
||||
this.txtRemoteHost = new System.Windows.Forms.TextBox();
|
||||
this.txtRemotePort = new System.Windows.Forms.TextBox();
|
||||
this.txtLocalPort = new System.Windows.Forms.TextBox();
|
||||
this.lsbOutput = new System.Windows.Forms.ListBox();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// btnRun
|
||||
//
|
||||
this.btnRun.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.btnRun.Location = new System.Drawing.Point(356, 368);
|
||||
this.btnRun.Name = "btnRun";
|
||||
this.btnRun.Size = new System.Drawing.Size(83, 45);
|
||||
this.btnRun.TabIndex = 0;
|
||||
this.btnRun.Text = "Run";
|
||||
this.btnRun.UseVisualStyleBackColor = true;
|
||||
this.btnRun.Click += new System.EventHandler(this.btnRun_Click);
|
||||
//
|
||||
// btnStop
|
||||
//
|
||||
this.btnStop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.btnStop.Location = new System.Drawing.Point(275, 368);
|
||||
this.btnStop.Name = "btnStop";
|
||||
this.btnStop.Size = new System.Drawing.Size(75, 45);
|
||||
this.btnStop.TabIndex = 1;
|
||||
this.btnStop.Text = "Stop";
|
||||
this.btnStop.UseVisualStyleBackColor = true;
|
||||
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
|
||||
//
|
||||
// lblRemoteHost
|
||||
//
|
||||
this.lblRemoteHost.AutoSize = true;
|
||||
this.lblRemoteHost.Location = new System.Drawing.Point(12, 15);
|
||||
this.lblRemoteHost.Name = "lblRemoteHost";
|
||||
this.lblRemoteHost.Size = new System.Drawing.Size(100, 20);
|
||||
this.lblRemoteHost.TabIndex = 2;
|
||||
this.lblRemoteHost.Text = "RemoteHost";
|
||||
//
|
||||
// lblRemotePort
|
||||
//
|
||||
this.lblRemotePort.AutoSize = true;
|
||||
this.lblRemotePort.Location = new System.Drawing.Point(12, 47);
|
||||
this.lblRemotePort.Name = "lblRemotePort";
|
||||
this.lblRemotePort.Size = new System.Drawing.Size(95, 20);
|
||||
this.lblRemotePort.TabIndex = 3;
|
||||
this.lblRemotePort.Text = "RemotePort";
|
||||
//
|
||||
// lblLocalPort
|
||||
//
|
||||
this.lblLocalPort.AutoSize = true;
|
||||
this.lblLocalPort.Location = new System.Drawing.Point(12, 79);
|
||||
this.lblLocalPort.Name = "lblLocalPort";
|
||||
this.lblLocalPort.Size = new System.Drawing.Size(76, 20);
|
||||
this.lblLocalPort.TabIndex = 4;
|
||||
this.lblLocalPort.Text = "LocalPort";
|
||||
//
|
||||
// txtRemoteHost
|
||||
//
|
||||
this.txtRemoteHost.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.txtRemoteHost.Location = new System.Drawing.Point(118, 12);
|
||||
this.txtRemoteHost.Name = "txtRemoteHost";
|
||||
this.txtRemoteHost.Size = new System.Drawing.Size(321, 26);
|
||||
this.txtRemoteHost.TabIndex = 5;
|
||||
//
|
||||
// txtRemotePort
|
||||
//
|
||||
this.txtRemotePort.Location = new System.Drawing.Point(118, 44);
|
||||
this.txtRemotePort.Name = "txtRemotePort";
|
||||
this.txtRemotePort.Size = new System.Drawing.Size(100, 26);
|
||||
this.txtRemotePort.TabIndex = 6;
|
||||
//
|
||||
// txtLocalPort
|
||||
//
|
||||
this.txtLocalPort.Location = new System.Drawing.Point(118, 76);
|
||||
this.txtLocalPort.Name = "txtLocalPort";
|
||||
this.txtLocalPort.Size = new System.Drawing.Size(100, 26);
|
||||
this.txtLocalPort.TabIndex = 7;
|
||||
//
|
||||
// lsbOutput
|
||||
//
|
||||
this.lsbOutput.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|
||||
| System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.lsbOutput.FormattingEnabled = true;
|
||||
this.lsbOutput.ItemHeight = 20;
|
||||
this.lsbOutput.Location = new System.Drawing.Point(12, 108);
|
||||
this.lsbOutput.Name = "lsbOutput";
|
||||
this.lsbOutput.Size = new System.Drawing.Size(427, 244);
|
||||
this.lsbOutput.TabIndex = 8;
|
||||
//
|
||||
// FrmTunnelTCP
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(451, 425);
|
||||
this.Controls.Add(this.lsbOutput);
|
||||
this.Controls.Add(this.txtLocalPort);
|
||||
this.Controls.Add(this.txtRemotePort);
|
||||
this.Controls.Add(this.txtRemoteHost);
|
||||
this.Controls.Add(this.lblLocalPort);
|
||||
this.Controls.Add(this.lblRemotePort);
|
||||
this.Controls.Add(this.lblRemoteHost);
|
||||
this.Controls.Add(this.btnStop);
|
||||
this.Controls.Add(this.btnRun);
|
||||
this.MinimumSize = new System.Drawing.Size(473, 372);
|
||||
this.Name = "FrmTunnelTCP";
|
||||
this.Text = "TunnelTCP";
|
||||
this.Load += new System.EventHandler(this.FrmTunnelTCP_Load);
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private System.Windows.Forms.Button btnRun;
|
||||
private System.Windows.Forms.Button btnStop;
|
||||
private System.Windows.Forms.Label lblRemoteHost;
|
||||
private System.Windows.Forms.Label lblRemotePort;
|
||||
private System.Windows.Forms.Label lblLocalPort;
|
||||
private System.Windows.Forms.TextBox txtRemoteHost;
|
||||
private System.Windows.Forms.TextBox txtRemotePort;
|
||||
private System.Windows.Forms.TextBox txtLocalPort;
|
||||
private System.Windows.Forms.ListBox lsbOutput;
|
||||
}
|
||||
}
|
||||
266
VAR.Toolbox/UI/FrmTunnelTCP.cs
Normal file
266
VAR.Toolbox/UI/FrmTunnelTCP.cs
Normal file
@@ -0,0 +1,266 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace VAR.Toolbox.UI
|
||||
{
|
||||
public partial class FrmTunnelTCP : Form
|
||||
{
|
||||
private bool _running = false;
|
||||
|
||||
private class ConnectedClient
|
||||
{
|
||||
public string hostRemoto = null;
|
||||
public int puertoRemoto = 0;
|
||||
public Socket socketCliente = null;
|
||||
}
|
||||
|
||||
public FrmTunnelTCP()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void FrmTunnelTCP_Load(object sender, EventArgs e)
|
||||
{
|
||||
btnStop.Enabled = false;
|
||||
btnRun.Enabled = true;
|
||||
}
|
||||
|
||||
private void btnStop_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_running == false) { return; }
|
||||
|
||||
_running = false;
|
||||
btnStop.Enabled = false;
|
||||
btnRun.Enabled = true;
|
||||
}
|
||||
|
||||
private void btnRun_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_running == true) { return; }
|
||||
|
||||
_running = true;
|
||||
btnStop.Enabled = true;
|
||||
btnRun.Enabled = false;
|
||||
lsbOutput.Items.Clear();
|
||||
|
||||
string remoteHost = txtRemoteHost.Text;
|
||||
int remotePort = Convert.ToInt32(txtRemotePort.Text);
|
||||
int localPort = Convert.ToInt32(txtLocalPort.Text);
|
||||
|
||||
Thread thread = new Thread(() =>
|
||||
{
|
||||
TunnelTCP(remoteHost, remotePort, localPort);
|
||||
});
|
||||
thread.Start();
|
||||
}
|
||||
|
||||
private void lblOutput_AddText(string text)
|
||||
{
|
||||
if (lsbOutput.InvokeRequired)
|
||||
{
|
||||
lsbOutput.Invoke((MethodInvoker)(() => { lsbOutput.Items.Add(text); }));
|
||||
}
|
||||
else
|
||||
{
|
||||
lsbOutput.Items.Add(text);
|
||||
}
|
||||
}
|
||||
|
||||
private void TunnelTCP(string remoteHost, int remotePort, int localPort)
|
||||
{
|
||||
try
|
||||
{
|
||||
Socket sock;
|
||||
|
||||
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
sock.Bind(new IPEndPoint(IPAddress.Any, localPort));
|
||||
sock.Listen(1000);
|
||||
|
||||
while (_running)
|
||||
{
|
||||
if (sock.Poll(100, SelectMode.SelectRead))
|
||||
{
|
||||
Socket sockCliente = sock.Accept();
|
||||
sockCliente.Blocking = false;
|
||||
sockCliente.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
|
||||
|
||||
ConnectedClient client = new ConnectedClient
|
||||
{
|
||||
hostRemoto = remoteHost,
|
||||
puertoRemoto = remotePort,
|
||||
socketCliente = sockCliente,
|
||||
};
|
||||
Thread thread = new Thread(() =>
|
||||
{
|
||||
ProcessClient(client);
|
||||
});
|
||||
thread.Start();
|
||||
}
|
||||
}
|
||||
|
||||
sock.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
lblOutput_AddText("Excepcion: " + ex.Message);
|
||||
lblOutput_AddText("Backtrace:");
|
||||
lblOutput_AddText(ex.StackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessClient(ConnectedClient client)
|
||||
{
|
||||
try
|
||||
{
|
||||
Socket remoteSock = null;
|
||||
Socket clientSock = client.socketCliente;
|
||||
bool threadRunning;
|
||||
byte[] buffer = new byte[4096];
|
||||
int len;
|
||||
long totalSent = 0;
|
||||
long totalRecv = 0;
|
||||
|
||||
// Info
|
||||
lblOutput_AddText(
|
||||
DateTime.Now.ToString() +
|
||||
" Nuevo Cliente: " +
|
||||
((IPEndPoint)clientSock.RemoteEndPoint).Address);
|
||||
|
||||
// Conectar al host remoto
|
||||
IPHostEntry EntryHostRemoto = Dns.GetHostEntry(client.hostRemoto);
|
||||
IPAddress ipHostRemoto = null;
|
||||
foreach (IPAddress addr in EntryHostRemoto.AddressList)
|
||||
{
|
||||
if (addr.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
ipHostRemoto = addr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
IPEndPoint endpHostRemoto = new IPEndPoint(ipHostRemoto, client.puertoRemoto);
|
||||
remoteSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
remoteSock.Connect(endpHostRemoto);
|
||||
|
||||
// Bucle de recepcion y envio entre el cliente y el servidor remoto
|
||||
threadRunning = true;
|
||||
while (threadRunning && _running)
|
||||
{
|
||||
// Comprobar recepcion del cliente
|
||||
if (clientSock.Poll(100, SelectMode.SelectRead))
|
||||
{
|
||||
if (clientSock.Available == 0)
|
||||
{
|
||||
// Cliente cierra conexion
|
||||
threadRunning = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Obtener y enviar al remoto
|
||||
len = clientSock.Receive(buffer, 0, buffer.Length, 0);
|
||||
int enviado = 0;
|
||||
do
|
||||
{
|
||||
try
|
||||
{
|
||||
enviado = remoteSock.Send(buffer, 0, len, 0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
lblOutput_AddText("No se pudo enviar... (" + ex.Message + ")");
|
||||
}
|
||||
totalRecv += enviado;
|
||||
} while (enviado <= 0 && remoteSock.Connected);
|
||||
}
|
||||
}
|
||||
|
||||
// Comprobar recepcion del remoto
|
||||
if (remoteSock.Poll(100, SelectMode.SelectRead))
|
||||
{
|
||||
if (remoteSock.Available == 0)
|
||||
{
|
||||
// Remoto cierra conexion
|
||||
threadRunning = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Obtener y enviar al cliente
|
||||
len = remoteSock.Receive(buffer, 0, buffer.Length, 0);
|
||||
int enviado = 0;
|
||||
do
|
||||
{
|
||||
try
|
||||
{
|
||||
enviado = clientSock.Send(buffer, 0, len, 0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
lblOutput_AddText("No se pudo enviar... (" + ex.Message + ")");
|
||||
Thread.Sleep(10);
|
||||
}
|
||||
totalSent += enviado;
|
||||
} while (enviado <= 0 && clientSock.Connected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Info
|
||||
lblOutput_AddText(
|
||||
DateTime.Now.ToString() +
|
||||
" Desconexion de cliente: " +
|
||||
((IPEndPoint)clientSock.RemoteEndPoint).Address +
|
||||
" IN/OUT: " + UnidadesBytes(totalRecv) + "/" + UnidadesBytes(totalSent));
|
||||
|
||||
// Cerrar conexiones
|
||||
remoteSock.Close();
|
||||
clientSock.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
lblOutput_AddText("Excepcion: " + ex.Message);
|
||||
lblOutput_AddText("Backtrace:");
|
||||
lblOutput_AddText(ex.StackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] StrToByteArray(string str)
|
||||
{
|
||||
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
|
||||
return encoding.GetBytes(str);
|
||||
}
|
||||
|
||||
private static string UnidadesBytes(long n)
|
||||
{
|
||||
double number = n;
|
||||
if (number < 1024)
|
||||
{
|
||||
return string.Format("{0} B", number);
|
||||
}
|
||||
|
||||
number = number / 1024.0;
|
||||
if (number < 1024)
|
||||
{
|
||||
return string.Format("{0:#.00} KiB", number);
|
||||
}
|
||||
|
||||
number = number / 1024.0;
|
||||
if (number < 1024)
|
||||
{
|
||||
return string.Format("{0:#.00} MiB", number);
|
||||
}
|
||||
|
||||
number = number / 1024.0;
|
||||
if (number < 1024)
|
||||
{
|
||||
return string.Format("{0:#.00} GiB", number);
|
||||
}
|
||||
|
||||
number = number / 1024.0;
|
||||
|
||||
return string.Format("{0:#.00} TiB", number);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -106,6 +106,12 @@
|
||||
<Compile Include="UI\FrmProxyCmd.Designer.cs">
|
||||
<DependentUpon>FrmProxyCmd.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="UI\FrmTunnelTCP.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
<Compile Include="UI\FrmTunnelTCP.Designer.cs">
|
||||
<DependentUpon>FrmTunnelTCP.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="UI\FrmToolbox.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
|
||||
Reference in New Issue
Block a user