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
{
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<ushort>(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.")

View File

@ -1,6 +1,7 @@
#pragma once
#include <QObject>
#include <QThread>
#include <memory>
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