diff --git a/src/components/pac/QvPACHandler.cpp b/src/components/pac/QvPACHandler.cpp index 8e5c19ad..265ed6be 100644 --- a/src/components/pac/QvPACHandler.cpp +++ b/src/components/pac/QvPACHandler.cpp @@ -7,7 +7,7 @@ namespace Qv2ray::components::pac { - PACServer::PACServer() : QObject() + PACServer::PACServer() : QThread() { pacServer = new httplib::Server(); } @@ -23,7 +23,7 @@ namespace Qv2ray::components::pac DEBUG(MODULE_PROXY, "Setting new PAC proxy string: " + proxyString) this->proxyString = proxyString; } - void PACServer::StartListen() + void PACServer::run() { LOG(MODULE_PROXY, "Starting PAC listener") // @@ -35,11 +35,11 @@ namespace Qv2ray::components::pac QString gfwContent = StringFromFile(QV2RAY_RULES_GFWLIST_PATH); pacContent = ConvertGFWToPAC(gfwContent, proxyString); // + pacServer->Get("", onNewRequest); auto result = pacServer->listen(address.toStdString().c_str(), static_cast(port)); if (result) { - isStarted = true; - DEBUG(MODULE_PROXY, "Started PAC handler") + DEBUG(MODULE_PROXY, "PAC handler stopped.") } else { @@ -50,22 +50,14 @@ namespace Qv2ray::components::pac void PACServer::StopServer() { - if (isStarted) - { - pacServer->stop(); - DEBUG(MODULE_PROXY, "PAC Handler stopped.") - isStarted = false; - } + pacServer->stop(); } void PACServer::onNewRequest(const httplib::Request &req, httplib::Response &rsp) { - rsp.set_header("Server", "Qv2ray/" QV2RAY_VERSION_STRING " PAC_Handler"); - if (req.method == "GET") { - // if (req.path == "/pac") { DEBUG(MODULE_PROXY, "Serving PAC file request.") diff --git a/src/components/pac/QvPACHandler.hpp b/src/components/pac/QvPACHandler.hpp index 762b15d5..b6e052dd 100644 --- a/src/components/pac/QvPACHandler.hpp +++ b/src/components/pac/QvPACHandler.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include namespace httplib @@ -13,24 +14,30 @@ namespace httplib namespace Qv2ray::components::pac { QString ConvertGFWToPAC(const QString &rawContent, const QString &customProxyString); - class PACServer : public QObject + class PACServer : public QThread { Q_OBJECT public: explicit PACServer(); ~PACServer(); void SetProxyString(const QString &proxyString); - void StartListen(); + void StartListen() + { + start(); + } void StopServer(); QString gfwFilePath; private: - void onNewRequest(const httplib::Request &req, httplib::Response &rsp); + void run() override; bool isStarted; httplib::Server *pacServer; - QString pacContent; QString proxyString; + + private: + static void onNewRequest(const httplib::Request &req, httplib::Response &rsp); + static inline QString pacContent; }; } // namespace Qv2ray::components::pac