Linux下获取系统代理信息

本文介绍了如何在Linux环境中利用Qt库来检测和获取系统的网络代理信息,包括从PAC文件处理到直接获取Windows互联网代理设置的方法。通过参考QT论坛、StackOverflow等资源,可以实现对不同代理配置的处理。

https://forum.qt.io/topic/17497/how-to-detect-network-proxies-using-qt

http://stackoverflow.com/questions/932824/how-do-i-get-the-system-proxy-using-qt

QNetworkProxyQuery npq(QUrl("http://www.google.com"));
QList<QNetworkProxy> listOfProxies = QNetworkProxyFactory::systemProxyForQuery(npq);

http://www.qtcentre.org/threads/21668-Getting-windows-internet-proxy-settings

http://ivandemarino.me/2011/03/21/How-to-handle-Proxy-PAC-configuration-with-Qt/

QtScript to the rescue

Fortunately, Qt provides a lot of help. The QtNetwork and QtScript (now based on WebKit’s JavascriptCore) have what it takes to solve the problem. What I show here is a usage of the QtScript Engine, that takes the PAC file and executes it to extract the right configuration.

#include <QtCore/QCoreApplication>
#include <QScriptEngine>
#include <QIODevice>
#include <QFile>
#include <QDebug>
#include <QScriptValue>
#include <QHostInfo>
#include <QHostAddress>
#include <QNetworkInterface>

// Functions to implement are here:
// - http://linuxmafia.com/faq/Web/autoproxy.html
// - http://www.returnproxy.com/proxypac/static/netscape-proxy-format.html

QScriptValue dnsResolve(QScriptContext *context, QScriptEngine *engine) {
   Q_UNUSED(engine)
   QString toResolve = context->argument(0).toString();
   qDebug() << "dnsResolve - toResolve: " << toResolve;

   QHostInfo info = QHostInfo::fromName(toResolve);
   if ( info.error() == QHostInfo::NoError ) {
      return info.addresses().first().toString(); //< Pick the first one
   } else {
      return QString("Host not found");
   }
}

QScriptValue isPlainHostName(QScriptContext *context, QScriptEngine *engine) {
   Q_UNUSED(engine)
   QString hostname = context->argument(0).toString();
   qDebug() << "isPlainHostName - hostname: " << hostname;

   return hostname.contains('.') ? false : true;
}

QScriptValue isInNet(QScriptContext *context, QScriptEngine *engine) {
   Q_UNUSED(engine)
   QString host = context->argument(0).toString();
   QHostAddress hostAddress;
   QPair<QHostAddress, int> subnet;
   QString pattern = context->argument(1).toString();
   QString mask = context->argument(2).toString();
   qDebug() << "isInNet - host: " << host;
   qDebug() << "isInNet - pattern: " << pattern;
   qDebug() << "isInNet - mask: " << mask;

   QHostInfo info = QHostInfo::fromName(host);
   if ( info.error() == QHostInfo::NoError ) {
      hostAddress = info.addresses().first();
      subnet = QHostAddress::parseSubnet(QString("%1/%2").arg(pattern,mask));
      qDebug() << "Host "<< host << " is in Subnet " << QString("%1/%2").arg(pattern,mask) << "? " << hostAddress.isInSubnet(subnet);
      return hostAddress.isInSubnet(subnet);
   } else {
      return false;
   }
}

QScriptValue myIpAddress(QScriptContext *context, QScriptEngine *engine) {
   Q_UNUSED(context)
   Q_UNUSED(engine)

   foreach ( QHostAddress address, QNetworkInterface::allAddresses() ) {
      if( !address.isNull()
         && address != QHostAddress::Null
         && address != QHostAddress::LocalHost
         && address != QHostAddress::LocalHostIPv6
         && address != QHostAddress::Broadcast
         && address != QHostAddress::Any
         && address != QHostAddress::AnyIPv6
         ) {
         qDebug() << "myIpAddress - " << address.toString();
         return address.toString(); //< Pick the First one
      }
   }
}

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);

   QScriptEngine engine;
   QString str;

   // Read the PAC file in a String
   QFile scriptFile(":/proxy-config.pac");
   if ( !scriptFile.open(QIODevice::ReadOnly|QIODevice::Text) ) {
      return EXIT_FAILURE;
   }
   str = scriptFile.readAll();
   scriptFile.close();

   // Inject the extra JS function as explained above
   QScriptValue dnsResolveFunction = engine.newFunction(dnsResolve);
   engine.globalObject().setProperty("dnsResolve", dnsResolveFunction);
   QScriptValue isPlainHostNameFunction = engine.newFunction(isPlainHostName);
   engine.globalObject().setProperty("isPlainHostName", isPlainHostNameFunction);
   QScriptValue isInNetFunction = engine.newFunction(isInNet);
   engine.globalObject().setProperty("isInNet", isInNetFunction);
   QScriptValue myIpAddressFunction = engine.newFunction(myIpAddress);
   engine.globalObject().setProperty("myIpAddress", myIpAddressFunction);

   // Evalute the PAC file
   if ( engine.canEvaluate(str) ) {
      engine.evaluate(str);
      QScriptValue ctor = engine.evaluate("FindProxyForURL(\"http://blog.orangelabsuk.com\", \"blog.orangelabsuk.com\")");
      if ( !ctor.isError() ) {
         qDebug() << ctor.toString();
         return EXIT_SUCCESS;
      }
   }

   qDebug() << "Error Occurred";
   return EXIT_FAILURE;
}

http://advancedlinuxprogramming.com/alp-folder/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值