Skip to content
Browse files

add files.move proxyserver to single class. add class deal with PAC

  • Loading branch information...
1 parent be16189 commit f9e24e4701f482930020530f3ea18daa02763f30 @iptton iptton committed Dec 27, 2011
Showing with 234 additions and 0 deletions.
  1. +128 −0 qnetworkproxyfactoryexendforpac.cpp
  2. +51 −0 qnetworkproxyfactoryexendforpac.h
  3. +26 −0 qproxyserver.cpp
  4. +29 −0 qproxyserver.h
View
128 qnetworkproxyfactoryexendforpac.cpp
@@ -0,0 +1,128 @@
+#include "qnetworkproxyfactoryexendforpac.h"
+#include <QScriptEngine>
+#include <QHostAddress>
+#include <QHostInfo>
+#include <QNetworkInterface>
+#include <QRegExp>
+
+
+QNetworkProxyFactoryExendForPAC::QNetworkProxyFactoryExendForPAC()
+{
+ engine = new QScriptEngine(this);
+ install();
+}
+
+QNetworkProxyFactoryExendForPAC::~QNetworkProxyFactoryExendForPAC()
+{
+}
+
+void QNetworkProxyFactoryExendForPAC::setConfig( const QString &config )
+{
+ engine->evaluate( config );
+}
+
+void QNetworkProxyFactoryExendForPAC::install()
+{
+ QScriptValue globalObject = engine->globalObject();
+
+ QScriptValue fun;
+
+ fun = engine->newFunction( debug );
+ globalObject.setProperty( QString("debug"), fun );
+
+ fun = engine->newFunction( myIpAddress );
+ globalObject.setProperty( QString("myIpAddress"), fun );
+
+ fun = engine->newFunction( isInNet );
+ globalObject.setProperty( "isInNet", fun );
+
+ fun = engine->newFunction( shExpMatch );
+ globalObject.setProperty( "shExpMatch", fun );
+
+ fun = engine->newFunction( dnsResolve );
+ globalObject.setProperty( "dnsResolve", fun );
+}
+
+QScriptValue QNetworkProxyFactoryExendForPAC::debug( QScriptContext *context, QScriptEngine *engine )
+{
+ if ( context->argumentCount() != 1 )
+ return context->throwError("Debug takes one argument");
+ qDebug() << context->argument(0).toString();
+ return engine->undefinedValue();
+}
+
+
+/* String myIpAddress */
+QScriptValue QNetworkProxyFactoryExendForPAC::myIpAddress( QScriptContext *context, QScriptEngine *engine )
+{
+ if ( context->argumentCount() != 0 )
+ return context->throwError("myIpAddress takes no arguments");
+
+ foreach( QHostAddress address, QNetworkInterface::allAddresses() ) {
+ if ( address != QHostAddress::LocalHost
+ && address != QHostAddress::LocalHostIPv6 )
+ return QScriptValue( engine, address.toString() );
+ }
+
+ return engine->undefinedValue();
+}
+
+/* bool isInNet ipaddress, netaddress, netmask */
+QScriptValue QNetworkProxyFactoryExendForPAC::isInNet( QScriptContext *context, QScriptEngine *engine )
+{
+ if ( context->argumentCount() != 3 )
+ return context->throwError("isInNet takes three arguments");
+
+ QHostAddress addr( context->argument(0).toString() );
+ QHostAddress netaddr( context->argument(1).toString() );
+ QHostAddress netmask( context->argument(2).toString() );
+
+ if ( (netaddr.toIPv4Address() & netmask.toIPv4Address()) == (addr.toIPv4Address() & netmask.toIPv4Address()) )
+ return QScriptValue( engine, true );
+
+ return QScriptValue( engine, false );
+}
+
+/* bool shExpMatch url, glob */
+QScriptValue QNetworkProxyFactoryExendForPAC::shExpMatch( QScriptContext *context, QScriptEngine *engine )
+{
+ if ( context->argumentCount() != 2 )
+ return context->throwError("shExpMatch takes two arguments");
+
+ QRegExp re( context->argument(1).toString(), Qt::CaseSensitive, QRegExp::Wildcard );
+ if ( re.exactMatch( context->argument(0).toString() ) )
+ return QScriptValue( engine, true );
+
+ return QScriptValue( engine, false );
+}
+
+/* string dnsResolve hostname */
+QScriptValue QNetworkProxyFactoryExendForPAC::dnsResolve( QScriptContext *context, QScriptEngine *engine )
+{
+ if ( context->argumentCount() != 1 )
+ return context->throwError("dnsResolve takes one arguments");
+
+ QHostInfo info = QHostInfo::fromName( context->argument(0).toString() );
+ QList<QHostAddress> addresses = info.addresses();
+ if ( addresses.isEmpty() )
+ return engine->nullValue(); // TODO: Should this be undefined or an exception? check other implementations
+
+ return QScriptValue( engine, addresses.first().toString() );
+}
+
+QString QNetworkProxyFactoryExendForPAC::findProxyForUrl( const QString &url, const QString &host )
+{
+ QScriptValue global = engine->globalObject();
+ QScriptValue fun = global.property("FindProxyForURL");
+ if ( !fun.isFunction() ) {
+ return QString("DIRECT");
+ }
+
+ QScriptValueList args;
+ args << engine->toScriptValue( url ) << engine->toScriptValue( host );
+
+ QScriptValue val = fun.call( global, args );
+
+ return val.toString();
+}
+
View
51 qnetworkproxyfactoryexendforpac.h
@@ -0,0 +1,51 @@
+#ifndef QNETWORKPROXYFACTORYEXENDFORPAC_H
+#define QNETWORKPROXYFACTORYEXENDFORPAC_H
+
+#include <QNetworkProxyFactory>
+#include <QScriptEngine>
+
+class QNetworkProxyFactoryExendForPAC : public QObject
+{
+
+
+ Q_OBJECT
+ Q_PROPERTY( QString config WRITE setConfig )
+
+public:
+ QNetworkProxyFactoryExendForPAC();
+ ~QNetworkProxyFactoryExendForPAC();
+
+ /**
+ * Call this to set the script to be executed. Note that the argument should be
+ * the content of the .pac file to be used, not the URL where it is located.
+ */
+ void setConfig( const QString &config );
+
+ /**
+ * Returns the result
+ */
+ Q_SCRIPTABLE QString findProxyForUrl( const QString &url, const QString &host );
+
+private:
+ void install();
+
+ static QScriptValue debug( QScriptContext *context, QScriptEngine *engine );
+
+ /* String myIpAddress */
+ static QScriptValue myIpAddress( QScriptContext *context, QScriptEngine *engine );
+
+ /* bool isInNet ipaddress, netaddress, netmask */
+ static QScriptValue isInNet( QScriptContext *context, QScriptEngine *engine );
+
+ /* bool shExpMatch url, glob */
+ static QScriptValue shExpMatch( QScriptContext *context, QScriptEngine *engine );
+
+ /* string dnsResolve hostname */
+ static QScriptValue dnsResolve( QScriptContext *context, QScriptEngine *engine );
+
+private:
+ QScriptEngine *engine;
+};
+
+
+#endif // QNETWORKPROXYFACTORYEXENDFORPAC_H
View
26 qproxyserver.cpp
@@ -0,0 +1,26 @@
+#include "qproxyserver.h"
+#include "qpipe.h"
+
+QProxyServer::QProxyServer(QObject *parent) :
+ QTcpServer(parent){
+}
+
+void QProxyServer::incomingConnection(int socketId){
+ QPipe *pipe = new QPipe(nextPendingConnection());
+ pipes.append(pipe);
+
+ connect(pipe,SIGNAL(connected()),SLOT(onPipeConnected()));
+ connect(pipe,SIGNAL(completed()),SLOT(onPipeComplete()));
+ connect(pipe,SIGNAL(error()),SLOT(onPipeError()));
+
+ pipe->start();
+}
+void QProxyServer::onPipeConnected(){
+
+}
+void QProxyServer::onPipeComplete(){
+
+}
+void QProxyServer::onPipeError(){
+
+}
View
29 qproxyserver.h
@@ -0,0 +1,29 @@
+#ifndef QPROXYSERVER_H
+#define QPROXYSERVER_H
+
+#include <QTcpServer>
+#include <pipedata.h>
+#include <QVector>
+class QPipe;
+class PipeData;
+
+class QProxyServer : public QTcpServer{
+ Q_OBJECT
+ public:
+ explicit QProxyServer(QObject *parent = 0);
+
+ signals:
+ void newPipe(int socketId);
+ void pipeUpdate(int socketId,const PipeData pipeData);
+ public slots:
+ void onPipeConnected();
+ void onPipeComplete();
+ void onPipeError();
+ protected:
+ void incomingConnection(int handle);
+
+ QVector<QPipe*> pipes;
+
+};
+
+#endif // QPROXYSERVER_H

0 comments on commit f9e24e4

Please sign in to comment.
Something went wrong with that request. Please try again.