From 6aefa6632023c99c5b91bae0e099df94fa69d890 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 17 May 2011 04:18:13 +0000 Subject: GatsCon now supports proxying, and way better than qtbenc ever did. Not only does it proxy, but you can inject your own messages going to the client or host. Pretty slick, really. Next up, reading and creating files. --- src/gatscon/clientthread.cpp | 2 +- src/gatscon/clientwidget.cpp | 17 ++-- src/gatscon/clientwidget.h | 2 +- src/gatscon/mainwnd.cpp | 25 +++++- src/gatscon/proxythread.cpp | 108 +++++++++++++++++++++++++ src/gatscon/proxythread.h | 60 ++++++++++++++ src/gatscon/proxywidget.cpp | 115 +++++++++++++++++++++++++++ src/gatscon/proxywidget.h | 30 +++++++ src/gatscon/proxywidget.ui | 181 ++++++++++++++++++++++++++++++++++++++++++ src/gatscon/setupproxydlg.cpp | 27 +++++++ src/gatscon/setupproxydlg.h | 18 +++++ src/gatscon/setupproxydlg.ui | 112 ++++++++++++++++++++++++++ 12 files changed, 682 insertions(+), 15 deletions(-) create mode 100644 src/gatscon/proxythread.cpp create mode 100644 src/gatscon/proxythread.h create mode 100644 src/gatscon/proxywidget.cpp create mode 100644 src/gatscon/proxywidget.h create mode 100644 src/gatscon/proxywidget.ui create mode 100644 src/gatscon/setupproxydlg.cpp create mode 100644 src/gatscon/setupproxydlg.h create mode 100644 src/gatscon/setupproxydlg.ui (limited to 'src') diff --git a/src/gatscon/clientthread.cpp b/src/gatscon/clientthread.cpp index b1dc4d0..4c7b72a 100644 --- a/src/gatscon/clientthread.cpp +++ b/src/gatscon/clientthread.cpp @@ -30,7 +30,7 @@ void ClientThread::run() { Gats::Object *pObj = gsCli.readObject(); if( pObj == NULL ) - break; + continue; emit recv( pObj ); } diff --git a/src/gatscon/clientwidget.cpp b/src/gatscon/clientwidget.cpp index 6039035..0ea04a3 100644 --- a/src/gatscon/clientwidget.cpp +++ b/src/gatscon/clientwidget.cpp @@ -1,5 +1,4 @@ #include "clientwidget.h" -#include "connectdlg.h" #include "clientthread.h" #include "gatstotree.h" @@ -11,23 +10,17 @@ using namespace Bu; -ClientWidget::ClientWidget( QWidget *pParent ) : +ClientWidget::ClientWidget( QWidget *pParent, const QByteArray &baHost, + int iPort ) : QWidget( pParent ) { setupUi( this ); - { - ConnectDlg dlg( this ); - dlg.exec(); - - sio << "Connect: " << dlg.getHostname().constData() << ":" - << dlg.getPort() << sio.nl; - - pCli = new ClientThread( this, dlg.getHostname(), dlg.getPort() ); - } - pCli->start(); + pCli = new ClientThread( this, baHost, iPort ); connect( pCli, SIGNAL(recv( Gats::Object *)), this, SLOT(recv(Gats::Object *)), Qt::QueuedConnection ); + + pCli->start(); } ClientWidget::~ClientWidget() diff --git a/src/gatscon/clientwidget.h b/src/gatscon/clientwidget.h index 21fb1b7..9ce2798 100644 --- a/src/gatscon/clientwidget.h +++ b/src/gatscon/clientwidget.h @@ -12,7 +12,7 @@ class ClientWidget : public QWidget, protected Ui::ClientWidget { Q_OBJECT; public: - ClientWidget( QWidget *pParent=NULL ); + ClientWidget( QWidget *pParent, const QByteArray &baHost, int iPort ); virtual ~ClientWidget(); public slots: diff --git a/src/gatscon/mainwnd.cpp b/src/gatscon/mainwnd.cpp index c86e1c2..f20a531 100644 --- a/src/gatscon/mainwnd.cpp +++ b/src/gatscon/mainwnd.cpp @@ -1,5 +1,10 @@ #include "mainwnd.h" + #include "clientwidget.h" +#include "proxywidget.h" + +#include "connectdlg.h" +#include "setupproxydlg.h" MainWnd::MainWnd() { @@ -12,11 +17,29 @@ MainWnd::~MainWnd() void MainWnd::connect() { - setCentralWidget( new ClientWidget( this ) ); + ConnectDlg dlg( this ); + if( dlg.exec() == QDialog::Accepted ) + { + setCentralWidget( + new ClientWidget( + this, dlg.getHostname(), dlg.getPort() + ) + ); + } } void MainWnd::proxy() { + SetupProxyDlg dlg( this ); + + if( dlg.exec() == QDialog::Accepted ) + { + setCentralWidget( + new ProxyWidget( + this, dlg.getPortIn(), dlg.getHostOut(), dlg.getPortOut() + ) + ); + } } void MainWnd::open() diff --git a/src/gatscon/proxythread.cpp b/src/gatscon/proxythread.cpp new file mode 100644 index 0000000..574b56b --- /dev/null +++ b/src/gatscon/proxythread.cpp @@ -0,0 +1,108 @@ +#include "proxythread.h" + +#include + +#include +#include +#include +#include + +using namespace Bu; + +ProxyThread::ProxyThread( QObject *pParent, int iPortIn, + const QByteArray &baHostOut, int iPortOut ) : + QThread( pParent ), + pHost( NULL ), + iPortIn( iPortIn ), + baHostOut( baHostOut ), + iPortOut( iPortOut ), + gsCli( ssCli ) +{ + pHost = new ProxyHostThread( pParent, this ); +} + +ProxyThread::~ProxyThread() +{ +} + +void ProxyThread::send( Gats::Object *pObj ) +{ + MemBuf bg; + Gats::GatsStream gs( bg ); + gs.writeObject( pObj ); + ssCli.write( bg.getString().getStr(), bg.getString().getSize() ); +} + +void ProxyThread::run() +{ + int iSockIn; + + { + TcpServerSocket tsIn( iPortIn ); + do + { + iSockIn = tsIn.accept( 5 ); + } while( iSockIn < 0 ); + } + + emit gotConnection(); + + ssCli.setStream( new TcpSocket( iSockIn ) ); + ssCli.setBlocking( true ); + + pHost->setStream( + new TcpSocket( baHostOut.constData(), iPortOut ) + ); + + pHost->start(); + + while( !ssCli.isEos() ) + { + Gats::Object *pObj = gsCli.readObject(); + if( pObj == NULL ) + continue; + + pHost->send( pObj ); + emit recv( pObj ); + } + +} + +ProxyHostThread::ProxyHostThread( QObject *pParent, ProxyThread *pClient ) : + QThread( pParent ), + pClient( pClient ), + ssHst(), + gsHst( ssHst ) +{ +} + +ProxyHostThread::~ProxyHostThread() +{ +} + +void ProxyHostThread::send( Gats::Object *pObj ) +{ + MemBuf bg; + Gats::GatsStream gs( bg ); + gs.writeObject( pObj ); + ssHst.write( bg.getString().getStr(), bg.getString().getSize() ); +} + +void ProxyHostThread::setStream( Bu::Stream *pStr ) +{ + ssHst.setStream( pStr ); +} + +void ProxyHostThread::run() +{ + while( !ssHst.isEos() ) + { + Gats::Object *pObj = gsHst.readObject(); + if( pObj == NULL ) + continue; + + pClient->send( pObj ); + emit recv( pObj ); + } +} + diff --git a/src/gatscon/proxythread.h b/src/gatscon/proxythread.h new file mode 100644 index 0000000..df75046 --- /dev/null +++ b/src/gatscon/proxythread.h @@ -0,0 +1,60 @@ +#ifndef PROXY_THREAD_H +#define PROXY_THREAD_H + +#include + +#include +#include + +class ProxyThread : public QThread +{ + Q_OBJECT; +public: + ProxyThread( QObject *pParent, int iPortIn, const QByteArray &baHostOut, + int iPortOut ); + virtual ~ProxyThread(); + + class ProxyHostThread *pHost; + + void send( Gats::Object *pObj ); + +signals: + void recv( Gats::Object *pObj ); + void gotConnection(); + +protected: + virtual void run(); + +private: + int iPortIn; + QByteArray baHostOut; + int iPortOut; + + Bu::StreamStack ssCli; + Gats::GatsStream gsCli; +}; + +class ProxyHostThread : public QThread +{ + Q_OBJECT; +public: + ProxyHostThread( QObject *pParent, ProxyThread *pClient ); + virtual ~ProxyHostThread(); + + void send( Gats::Object *pObj ); + + void setStream( Bu::Stream *pStr ); + +signals: + void recv( Gats::Object *pObj ); + +protected: + virtual void run(); + +private: + ProxyThread *pClient; + Bu::StreamStack ssHst; + Gats::GatsStream gsHst; +}; + +#endif diff --git a/src/gatscon/proxywidget.cpp b/src/gatscon/proxywidget.cpp new file mode 100644 index 0000000..28bd1fc --- /dev/null +++ b/src/gatscon/proxywidget.cpp @@ -0,0 +1,115 @@ +#include "proxywidget.h" +#include "proxythread.h" + +#include "gatstotree.h" + +#include + +#include +#include + +using namespace Bu; + +ProxyWidget::ProxyWidget( QWidget *pParent, int iPortIn, + const QByteArray baHost, int iPortOut ) : + QWidget( pParent ), + pPrx( NULL ) +{ + setupUi( this ); + + pPrx = new ProxyThread( this, iPortIn, baHost, iPortOut ); + + connect( pPrx, SIGNAL(gotConnection()), + this, SLOT(gotConnection()), Qt::QueuedConnection ); + connect( pPrx, SIGNAL(recv( Gats::Object *)), + this, SLOT(clientRecv(Gats::Object *)), Qt::QueuedConnection ); + connect( pPrx->pHost, SIGNAL(recv( Gats::Object *)), + this, SLOT(hostRecv(Gats::Object *)), Qt::QueuedConnection ); + + pPrx->start(); +} + +ProxyWidget::~ProxyWidget() +{ +} + +void ProxyWidget::sendToClient() +{ + try + { + Gats::Object *pObj = Gats::Object::strToGats( + leGats->text().toAscii().constData() + ); + sio << "Send: " << *pObj << sio.nl; + QTreeWidgetItem *pIt = new QTreeWidgetItem( + twHistory->invisibleRootItem() + ); + pIt->setText( 0, "proxy -> client" ); + gatsToTree( pIt, pObj ); + pPrx->send( pObj ); + delete pObj; + + leGats->setText(""); + leGats->setFocus(); + } + catch( Bu::ExceptionBase &e ) + { + QMessageBox::critical( this, "GatsCon - Error", e.what() ); + } +} + +void ProxyWidget::sendToServer() +{ + try + { + Gats::Object *pObj = Gats::Object::strToGats( + leGats->text().toAscii().constData() + ); + sio << "Send: " << *pObj << sio.nl; + QTreeWidgetItem *pIt = new QTreeWidgetItem( + twHistory->invisibleRootItem() + ); + pIt->setText( 0, "proxy -> host" ); + gatsToTree( pIt, pObj ); + pPrx->pHost->send( pObj ); + delete pObj; + + leGats->setText(""); + leGats->setFocus(); + } + catch( Bu::ExceptionBase &e ) + { + QMessageBox::critical( this, "GatsCon - Error", e.what() ); + } +} + +void ProxyWidget::clientRecv( Gats::Object *pObj ) +{ + sio << "Recv: " << *pObj << sio.nl; + + QTreeWidgetItem *pIt = new QTreeWidgetItem( + twHistory->invisibleRootItem() + ); + pIt->setText( 0, "client -> host" ); + gatsToTree( pIt, pObj ); + delete pObj; +} + +void ProxyWidget::hostRecv( Gats::Object *pObj ) +{ + sio << "Recv: " << *pObj << sio.nl; + + QTreeWidgetItem *pIt = new QTreeWidgetItem( + twHistory->invisibleRootItem() + ); + pIt->setText( 0, "host -> client" ); + gatsToTree( pIt, pObj ); + delete pObj; +} + +void ProxyWidget::gotConnection() +{ + lwConnect->stop(); + swRoot->setCurrentIndex( 1 ); +} + diff --git a/src/gatscon/proxywidget.h b/src/gatscon/proxywidget.h new file mode 100644 index 0000000..0ef1fd4 --- /dev/null +++ b/src/gatscon/proxywidget.h @@ -0,0 +1,30 @@ +#ifndef PROXY_WIDGET_H +#define PROXY_WIDGET_H + +#include "ui_proxywidget.h" + +namespace Gats +{ + class Object; +}; + +class ProxyWidget : public QWidget, protected Ui::ProxyWidget +{ + Q_OBJECT; +public: + ProxyWidget( QWidget *pParent, int iPortIn, const QByteArray baHost, + int iPortOut ); + virtual ~ProxyWidget(); + +public slots: + void sendToClient(); + void sendToServer(); + void clientRecv( Gats::Object *pObj ); + void hostRecv( Gats::Object *pObj ); + void gotConnection(); + +private: + class ProxyThread *pPrx; +}; + +#endif diff --git a/src/gatscon/proxywidget.ui b/src/gatscon/proxywidget.ui new file mode 100644 index 0000000..995fc73 --- /dev/null +++ b/src/gatscon/proxywidget.ui @@ -0,0 +1,181 @@ + + + ProxyWidget + + + + 0 + 0 + 338 + 300 + + + + Form + + + + 0 + + + + + 0 + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Listening for connections... + + + Qt::AlignCenter + + + + + + + + 0 + 50 + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + + Name + + + + + Type + + + + + Value + + + + + + + + + + Gats: + + + + + + + + + + + + Send to Client + + + + + + + Send to Server + + + + + + + + + + + + + + + + LoadingWidget + QWidget +
loadingwidget.h
+ 1 +
+
+ + + + pushButton + clicked() + ProxyWidget + sendToClient() + + + 280 + 258 + + + 392 + 223 + + + + + pushButton_2 + clicked() + ProxyWidget + sendToServer() + + + 306 + 284 + + + 199 + 340 + + + + + + sendToClient() + sendToServer() + +
diff --git a/src/gatscon/setupproxydlg.cpp b/src/gatscon/setupproxydlg.cpp new file mode 100644 index 0000000..7c7a873 --- /dev/null +++ b/src/gatscon/setupproxydlg.cpp @@ -0,0 +1,27 @@ +#include "setupproxydlg.h" + +SetupProxyDlg::SetupProxyDlg( QWidget *pParent ) : + QDialog( pParent ) +{ + setupUi( this ); +} + +SetupProxyDlg::~SetupProxyDlg() +{ +} + +int SetupProxyDlg::getPortIn() const +{ + return sbPortIn->value(); +} + +QByteArray SetupProxyDlg::getHostOut() const +{ + return leHostOut->text().toAscii(); +} + +int SetupProxyDlg::getPortOut() const +{ + return sbPortOut->value(); +} + diff --git a/src/gatscon/setupproxydlg.h b/src/gatscon/setupproxydlg.h new file mode 100644 index 0000000..6cc31bd --- /dev/null +++ b/src/gatscon/setupproxydlg.h @@ -0,0 +1,18 @@ +#ifndef SETUP_PROXY_DLG_H +#define SETUP_PROXY_DLG_H + +#include "ui_setupproxydlg.h" + +class SetupProxyDlg : public QDialog, protected Ui::SetupProxyDlg +{ + Q_OBJECT; +public: + SetupProxyDlg( QWidget *pParent=NULL ); + virtual ~SetupProxyDlg(); + + int getPortIn() const; + QByteArray getHostOut() const; + int getPortOut() const; +}; + +#endif diff --git a/src/gatscon/setupproxydlg.ui b/src/gatscon/setupproxydlg.ui new file mode 100644 index 0000000..8482193 --- /dev/null +++ b/src/gatscon/setupproxydlg.ui @@ -0,0 +1,112 @@ + + + SetupProxyDlg + + + + 0 + 0 + 359 + 122 + + + + GatsCon - Setup Proxy + + + + + + + + Listening port: + + + + + + + 1 + + + 32767 + + + + + + + Target host: + + + + + + + + + + Target port: + + + + + + + 1 + + + 32767 + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + SetupProxyDlg + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + SetupProxyDlg + reject() + + + 316 + 260 + + + 286 + 274 + + + + + -- cgit v1.2.3