pac: fixing PAC server

This commit is contained in:
Qv2ray-dev 2020-03-09 15:36:55 +08:00
parent c0735cf704
commit 541b38aee0
2 changed files with 16 additions and 17 deletions

View File

@ -7,7 +7,7 @@
namespace Qv2ray::components::pac namespace Qv2ray::components::pac
{ {
PACServer::PACServer() : QObject() PACServer::PACServer() : QThread()
{ {
pacServer = new httplib::Server(); pacServer = new httplib::Server();
} }
@ -23,7 +23,7 @@ namespace Qv2ray::components::pac
DEBUG(MODULE_PROXY, "Setting new PAC proxy string: " + proxyString) DEBUG(MODULE_PROXY, "Setting new PAC proxy string: " + proxyString)
this->proxyString = proxyString; this->proxyString = proxyString;
} }
void PACServer::StartListen() void PACServer::run()
{ {
LOG(MODULE_PROXY, "Starting PAC listener") LOG(MODULE_PROXY, "Starting PAC listener")
// //
@ -35,11 +35,11 @@ namespace Qv2ray::components::pac
QString gfwContent = StringFromFile(QV2RAY_RULES_GFWLIST_PATH); QString gfwContent = StringFromFile(QV2RAY_RULES_GFWLIST_PATH);
pacContent = ConvertGFWToPAC(gfwContent, proxyString); pacContent = ConvertGFWToPAC(gfwContent, proxyString);
// //
pacServer->Get("", onNewRequest);
auto result = pacServer->listen(address.toStdString().c_str(), static_cast<ushort>(port)); auto result = pacServer->listen(address.toStdString().c_str(), static_cast<ushort>(port));
if (result) if (result)
{ {
isStarted = true; DEBUG(MODULE_PROXY, "PAC handler stopped.")
DEBUG(MODULE_PROXY, "Started PAC handler")
} }
else else
{ {
@ -50,22 +50,14 @@ namespace Qv2ray::components::pac
void PACServer::StopServer() void PACServer::StopServer()
{ {
if (isStarted) pacServer->stop();
{
pacServer->stop();
DEBUG(MODULE_PROXY, "PAC Handler stopped.")
isStarted = false;
}
} }
void PACServer::onNewRequest(const httplib::Request &req, httplib::Response &rsp) void PACServer::onNewRequest(const httplib::Request &req, httplib::Response &rsp)
{ {
rsp.set_header("Server", "Qv2ray/" QV2RAY_VERSION_STRING " PAC_Handler"); rsp.set_header("Server", "Qv2ray/" QV2RAY_VERSION_STRING " PAC_Handler");
if (req.method == "GET") if (req.method == "GET")
{ {
//
if (req.path == "/pac") if (req.path == "/pac")
{ {
DEBUG(MODULE_PROXY, "Serving PAC file request.") DEBUG(MODULE_PROXY, "Serving PAC file request.")

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <QObject> #include <QObject>
#include <QThread>
#include <memory> #include <memory>
namespace httplib namespace httplib
@ -13,24 +14,30 @@ namespace httplib
namespace Qv2ray::components::pac namespace Qv2ray::components::pac
{ {
QString ConvertGFWToPAC(const QString &rawContent, const QString &customProxyString); QString ConvertGFWToPAC(const QString &rawContent, const QString &customProxyString);
class PACServer : public QObject class PACServer : public QThread
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit PACServer(); explicit PACServer();
~PACServer(); ~PACServer();
void SetProxyString(const QString &proxyString); void SetProxyString(const QString &proxyString);
void StartListen(); void StartListen()
{
start();
}
void StopServer(); void StopServer();
QString gfwFilePath; QString gfwFilePath;
private: private:
void onNewRequest(const httplib::Request &req, httplib::Response &rsp); void run() override;
bool isStarted; bool isStarted;
httplib::Server *pacServer; httplib::Server *pacServer;
QString pacContent;
QString proxyString; QString proxyString;
private:
static void onNewRequest(const httplib::Request &req, httplib::Response &rsp);
static inline QString pacContent;
}; };
} // namespace Qv2ray::components::pac } // namespace Qv2ray::components::pac