forked from AlloyTeam/Rythem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
qnetworkproxyfactoryexendforpac.h
105 lines (82 loc) · 3.05 KB
/
qnetworkproxyfactoryexendforpac.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef QNETWORKPROXYFACTORYEXENDFORPAC_H
#define QNETWORKPROXYFACTORYEXENDFORPAC_H
#include <QNetworkProxyFactory>
#include <QJSValue>
#include <QNetworkInterface>
#include <QHostInfo>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QTimer>
#include <QEventLoop>
class QNetworkProxyFactoryExendForPAC : public QObject
{
Q_OBJECT
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(QString config );
/**
* Returns the result
*/
Q_INVOKABLE QString findProxyForUrl( const QString &url, const QString &host ){}
private:
QString getContentByUrl(QString &url) {
qDebug()<<"url"<<url;
if(!QUrl(url).isValid()){
return "";
}
QNetworkAccessManager manager;
QTimer timer;
QEventLoop _loop;
timer.singleShot(10000,&_loop,SLOT(quit()));//5秒内
QNetworkProxyFactory::setUseSystemConfiguration(true);
manager.connect(&manager,SIGNAL(finished(QNetworkReply*)),&_loop,SLOT(quit()));
QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(url)));
_loop.exec();
QString script = QString(reply->readAll());
return script;
}
Q_INVOKABLE QJSValue dnsResolve(QString name){
QHostInfo info = QHostInfo::fromName( name );
QList<QHostAddress> addresses = info.addresses();
if ( addresses.isEmpty() )
return QJSValue::NullValue; // TODO: Should this be undefined or an exception? check other implementations
return addresses.first().toString();
}
Q_INVOKABLE QJSValue debug(QString str){
qDebug()<<str;
return QJSValue::UndefinedValue;
}
Q_INVOKABLE QJSValue myIpAddress(){// myIpAddress 有多个值 ,如何取?
QString result="";
foreach( QHostAddress address, QNetworkInterface::allAddresses() ) {
if ( address != QHostAddress::LocalHost
&& address != QHostAddress::LocalHostIPv6 )
// how to remove fe80::1%lo0 ?
// qDebug()<<address;
result = address.toString();
}
return result;
// return QJSValue::UndefinedValue;
}
Q_INVOKABLE QJSValue isInNet(QString _addr,QString _netAddress,QString _netMask){
QHostAddress addr( _addr );
QHostAddress netaddr( _netAddress );
QHostAddress netmask( _netMask);
if ( (netaddr.toIPv4Address() & netmask.toIPv4Address()) == (addr.toIPv4Address() & netmask.toIPv4Address()) )
return true;
return false;
}
Q_INVOKABLE QJSValue shExpMatch(QString src,QString pattern){
// qDebug()<<src<<pattern;
QRegExp re( pattern, Qt::CaseInsensitive, QRegExp::Wildcard );
return re.exactMatch( src);
}
private:
QJSEngine *engine;
};
#endif // QNETWORKPROXYFACTORYEXENDFORPAC_H