mirror of
https://github.com/asdlokj1qpi233/subconverter.git
synced 2026-11-03 18:24:58 +00:00
Enhancements
Fix rule generation in Surge configs. Refactor log output for curl verbose logs. Optimize codes.
This commit is contained in:
37 files changed
+317
-260
No files matched your search
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <toml.hpp>
|
||||
|
||||
#include "../handler/settings.h"
|
||||
#include "handler/settings.h"
|
||||
#include "crontask.h"
|
||||
#include "proxygroup.h"
|
||||
#include "regmatch.h"
|
||||
|
||||
+2
-2
@@ -6,8 +6,8 @@
|
||||
#include <tuple>
|
||||
#include <cstdint>
|
||||
|
||||
#include "../utils/string.h"
|
||||
#include "../utils/tribool.h"
|
||||
#include "utils/string.h"
|
||||
#include "utils/tribool.h"
|
||||
|
||||
using String = std::string;
|
||||
using Integer = int32_t;
|
||||
|
||||
@@ -3,18 +3,18 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
#include "../../handler/settings.h"
|
||||
#include "../../handler/webget.h"
|
||||
#include "../../parser/config/proxy.h"
|
||||
#include "../../parser/infoparser.h"
|
||||
#include "../../parser/subparser.h"
|
||||
#include "../../script/script_quickjs.h"
|
||||
#include "../../utils/file_extra.h"
|
||||
#include "../../utils/logger.h"
|
||||
#include "../../utils/map_extra.h"
|
||||
#include "../../utils/network.h"
|
||||
#include "../../utils/regexp.h"
|
||||
#include "../../utils/urlencode.h"
|
||||
#include "handler/settings.h"
|
||||
#include "handler/webget.h"
|
||||
#include "parser/config/proxy.h"
|
||||
#include "parser/infoparser.h"
|
||||
#include "parser/subparser.h"
|
||||
#include "script/script_quickjs.h"
|
||||
#include "utils/file_extra.h"
|
||||
#include "utils/logger.h"
|
||||
#include "utils/map_extra.h"
|
||||
#include "utils/network.h"
|
||||
#include "utils/regexp.h"
|
||||
#include "utils/urlencode.h"
|
||||
#include "nodemanip.h"
|
||||
#include "subexport.h"
|
||||
|
||||
|
||||
@@ -9,10 +9,10 @@
|
||||
#include <quickjspp.hpp>
|
||||
#endif // NO_JS_RUNTIME
|
||||
|
||||
#include "../../config/regmatch.h"
|
||||
#include "../../parser/config/proxy.h"
|
||||
#include "../../utils/map_extra.h"
|
||||
#include "../../utils/string.h"
|
||||
#include "config/regmatch.h"
|
||||
#include "parser/config/proxy.h"
|
||||
#include "utils/map_extra.h"
|
||||
#include "utils/string.h"
|
||||
|
||||
struct parse_settings
|
||||
{
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#include <string>
|
||||
|
||||
#include "../../handler/settings.h"
|
||||
#include "../../utils/logger.h"
|
||||
#include "../../utils/network.h"
|
||||
#include "../../utils/regexp.h"
|
||||
#include "../../utils/string.h"
|
||||
#include "../../utils/rapidjson_extra.h"
|
||||
#include "handler/settings.h"
|
||||
#include "utils/logger.h"
|
||||
#include "utils/network.h"
|
||||
#include "utils/regexp.h"
|
||||
#include "utils/string.h"
|
||||
#include "utils/rapidjson_extra.h"
|
||||
#include "subexport.h"
|
||||
|
||||
/// rule type lists
|
||||
@@ -338,7 +338,7 @@ void rulesetToSurge(INIReader &base_rule, std::vector<RulesetContent> &ruleset_c
|
||||
strLine = "RULE-SET," + remote_path_prefix + "/getruleset?type=1&url=" + urlSafeBase64Encode(rule_path_typed) + "," + rule_group;
|
||||
if(x.update_interval)
|
||||
strLine += ",update-interval=" + std::to_string(x.update_interval);
|
||||
allRules.emplace_back(std::move(strLine));
|
||||
allRules.emplace_back(strLine);
|
||||
continue;
|
||||
}
|
||||
else if(surge_ver == -1 && !remote_path_prefix.empty())
|
||||
@@ -372,7 +372,7 @@ void rulesetToSurge(INIReader &base_rule, std::vector<RulesetContent> &ruleset_c
|
||||
if(x.update_interval)
|
||||
strLine += ",update-interval=" + std::to_string(x.update_interval);
|
||||
|
||||
allRules.emplace_back(std::move(strLine));
|
||||
allRules.emplace_back(strLine);
|
||||
continue;
|
||||
}
|
||||
else if(surge_ver == -1 && !remote_path_prefix.empty())
|
||||
@@ -447,7 +447,6 @@ void rulesetToSurge(INIReader &base_rule, std::vector<RulesetContent> &ruleset_c
|
||||
strLine = trimWhitespace(strLine);
|
||||
}
|
||||
|
||||
strLine += "," + rule_group;
|
||||
if(surge_ver == -1 || surge_ver == -2)
|
||||
{
|
||||
if(startsWith(strLine, "IP-CIDR6"))
|
||||
@@ -459,7 +458,7 @@ void rulesetToSurge(INIReader &base_rule, std::vector<RulesetContent> &ruleset_c
|
||||
if(!startsWith(strLine, "AND") && !startsWith(strLine, "OR") && !startsWith(strLine, "NOT"))
|
||||
strLine = transformRuleToCommon(temp, strLine, rule_group);
|
||||
}
|
||||
allRules.emplace_back(std::move(strLine));
|
||||
allRules.emplace_back(strLine);
|
||||
total_rules++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <yaml-cpp/yaml.h>
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
#include "../../utils/ini_reader/ini_reader.h"
|
||||
#include "utils/ini_reader/ini_reader.h"
|
||||
|
||||
enum ruleset_type
|
||||
{
|
||||
|
||||
@@ -4,22 +4,22 @@
|
||||
#include <cmath>
|
||||
#include <climits>
|
||||
|
||||
#include "../../config/regmatch.h"
|
||||
#include "../../generator/config/subexport.h"
|
||||
#include "../../generator/template/templates.h"
|
||||
#include "../../handler/settings.h"
|
||||
#include "../../parser/config/proxy.h"
|
||||
#include "../../script/script_quickjs.h"
|
||||
#include "../../utils/bitwise.h"
|
||||
#include "../../utils/file_extra.h"
|
||||
#include "../../utils/ini_reader/ini_reader.h"
|
||||
#include "../../utils/logger.h"
|
||||
#include "../../utils/network.h"
|
||||
#include "../../utils/rapidjson_extra.h"
|
||||
#include "../../utils/regexp.h"
|
||||
#include "../../utils/stl_extra.h"
|
||||
#include "../../utils/urlencode.h"
|
||||
#include "../../utils/yamlcpp_extra.h"
|
||||
#include "config/regmatch.h"
|
||||
#include "generator/config/subexport.h"
|
||||
#include "generator/template/templates.h"
|
||||
#include "handler/settings.h"
|
||||
#include "parser/config/proxy.h"
|
||||
#include "script/script_quickjs.h"
|
||||
#include "utils/bitwise.h"
|
||||
#include "utils/file_extra.h"
|
||||
#include "utils/ini_reader/ini_reader.h"
|
||||
#include "utils/logger.h"
|
||||
#include "utils/network.h"
|
||||
#include "utils/rapidjson_extra.h"
|
||||
#include "utils/regexp.h"
|
||||
#include "utils/stl_extra.h"
|
||||
#include "utils/urlencode.h"
|
||||
#include "utils/yamlcpp_extra.h"
|
||||
#include "nodemanip.h"
|
||||
#include "ruleconvert.h"
|
||||
|
||||
|
||||
@@ -7,12 +7,12 @@
|
||||
#include <quickjspp.hpp>
|
||||
#endif // NO_JS_RUNTIME
|
||||
|
||||
#include "../../config/proxygroup.h"
|
||||
#include "../../config/regmatch.h"
|
||||
#include "../../parser/config/proxy.h"
|
||||
#include "../../utils/ini_reader/ini_reader.h"
|
||||
#include "../../utils/string.h"
|
||||
#include "../../utils/yamlcpp_extra.h"
|
||||
#include "config/proxygroup.h"
|
||||
#include "config/regmatch.h"
|
||||
#include "parser/config/proxy.h"
|
||||
#include "utils/ini_reader/ini_reader.h"
|
||||
#include "utils/string.h"
|
||||
#include "utils/yamlcpp_extra.h"
|
||||
#include "ruleconvert.h"
|
||||
|
||||
struct extra_settings
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
#include <jinja2cpp/template.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "../../handler/interfaces.h"
|
||||
#include "../../utils/regexp.h"
|
||||
#include "handler/interfaces.h"
|
||||
#include "utils/regexp.h"
|
||||
#include "templates.h"
|
||||
|
||||
static inline void parse_json_pointer(nlohmann::json &json, const std::string &path, const std::string &value)
|
||||
|
||||
@@ -5,14 +5,14 @@
|
||||
#include <inja.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "../../handler/interfaces.h"
|
||||
#include "../../handler/settings.h"
|
||||
#include "../../handler/webget.h"
|
||||
#include "../../utils/logger.h"
|
||||
#include "../../utils/network.h"
|
||||
#include "../../utils/regexp.h"
|
||||
#include "../../utils/urlencode.h"
|
||||
#include "../../utils/yamlcpp_extra.h"
|
||||
#include "handler/interfaces.h"
|
||||
#include "handler/settings.h"
|
||||
#include "handler/webget.h"
|
||||
#include "utils/logger.h"
|
||||
#include "utils/network.h"
|
||||
#include "utils/regexp.h"
|
||||
#include "utils/urlencode.h"
|
||||
#include "utils/yamlcpp_extra.h"
|
||||
#include "templates.h"
|
||||
|
||||
namespace inja
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#include "../../generator/config/subexport.h"
|
||||
#include "../../utils/string.h"
|
||||
#include "generator/config/subexport.h"
|
||||
#include "utils/string.h"
|
||||
|
||||
struct template_args
|
||||
{
|
||||
|
||||
+21
-21
@@ -6,27 +6,27 @@
|
||||
#include <inja.hpp>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include "../config/binding.h"
|
||||
#include "../generator/config/nodemanip.h"
|
||||
#include "../generator/config/ruleconvert.h"
|
||||
#include "../generator/config/subexport.h"
|
||||
#include "../generator/template/templates.h"
|
||||
#include "../script/cron.h"
|
||||
#include "../script/script_quickjs.h"
|
||||
#include "../server/webserver.h"
|
||||
#include "../utils/base64/base64.h"
|
||||
#include "../utils/file_extra.h"
|
||||
#include "../utils/ini_reader/ini_reader.h"
|
||||
#include "../utils/logger.h"
|
||||
#include "../utils/network.h"
|
||||
#include "../utils/regexp.h"
|
||||
#include "../utils/stl_extra.h"
|
||||
#include "../utils/string.h"
|
||||
#include "../utils/string_hash.h"
|
||||
#include "../utils/system.h"
|
||||
#include "../utils/system.h"
|
||||
#include "../utils/urlencode.h"
|
||||
#include "../utils/yamlcpp_extra.h"
|
||||
#include "config/binding.h"
|
||||
#include "generator/config/nodemanip.h"
|
||||
#include "generator/config/ruleconvert.h"
|
||||
#include "generator/config/subexport.h"
|
||||
#include "generator/template/templates.h"
|
||||
#include "script/cron.h"
|
||||
#include "script/script_quickjs.h"
|
||||
#include "server/webserver.h"
|
||||
#include "utils/base64/base64.h"
|
||||
#include "utils/file_extra.h"
|
||||
#include "utils/ini_reader/ini_reader.h"
|
||||
#include "utils/logger.h"
|
||||
#include "utils/network.h"
|
||||
#include "utils/regexp.h"
|
||||
#include "utils/stl_extra.h"
|
||||
#include "utils/string.h"
|
||||
#include "utils/string_hash.h"
|
||||
#include "utils/system.h"
|
||||
#include "utils/system.h"
|
||||
#include "utils/urlencode.h"
|
||||
#include "utils/yamlcpp_extra.h"
|
||||
#include "interfaces.h"
|
||||
#include "multithread.h"
|
||||
#include "settings.h"
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
#include <map>
|
||||
#include <inja.hpp>
|
||||
|
||||
#include "../config/ruleset.h"
|
||||
#include "../generator/config/subexport.h"
|
||||
#include "../server/webserver.h"
|
||||
#include "config/ruleset.h"
|
||||
#include "generator/config/subexport.h"
|
||||
#include "server/webserver.h"
|
||||
|
||||
std::string parseProxy(const std::string &source);
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include <future>
|
||||
#include <thread>
|
||||
|
||||
#include "../handler/settings.h"
|
||||
#include "../utils/network.h"
|
||||
#include "handler/settings.h"
|
||||
#include "utils/network.h"
|
||||
#include "webget.h"
|
||||
#include "multithread.h"
|
||||
//#include "vfs.h"
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include "../config/regmatch.h"
|
||||
#include "../utils/ini_reader/ini_reader.h"
|
||||
#include "../utils/string.h"
|
||||
#include "config/regmatch.h"
|
||||
#include "utils/ini_reader/ini_reader.h"
|
||||
#include "utils/string.h"
|
||||
|
||||
using guarded_mutex = std::lock_guard<std::mutex>;
|
||||
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
#include <mutex>
|
||||
#include <toml.hpp>
|
||||
|
||||
#include "../config/binding.h"
|
||||
#include "../handler/webget.h"
|
||||
#include "../script/cron.h"
|
||||
#include "../server/webserver.h"
|
||||
#include "../utils/logger.h"
|
||||
#include "../utils/network.h"
|
||||
#include "config/binding.h"
|
||||
#include "handler/webget.h"
|
||||
#include "script/cron.h"
|
||||
#include "server/webserver.h"
|
||||
#include "utils/logger.h"
|
||||
#include "utils/network.h"
|
||||
#include "interfaces.h"
|
||||
#include "multithread.h"
|
||||
#include "settings.h"
|
||||
|
||||
+10
-10
@@ -3,16 +3,16 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "../config/crontask.h"
|
||||
#include "../config/regmatch.h"
|
||||
#include "../config/proxygroup.h"
|
||||
#include "../config/ruleset.h"
|
||||
#include "../generator/config/ruleconvert.h"
|
||||
#include "../generator/template/templates.h"
|
||||
#include "../utils/logger.h"
|
||||
#include "../utils/string.h"
|
||||
#include "../utils/stl_extra.h"
|
||||
#include "../utils/tribool.h"
|
||||
#include "config/crontask.h"
|
||||
#include "config/regmatch.h"
|
||||
#include "config/proxygroup.h"
|
||||
#include "config/ruleset.h"
|
||||
#include "generator/config/ruleconvert.h"
|
||||
#include "generator/template/templates.h"
|
||||
#include "utils/logger.h"
|
||||
#include "utils/string.h"
|
||||
#include "utils/stl_extra.h"
|
||||
#include "utils/tribool.h"
|
||||
|
||||
struct Settings
|
||||
{
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#include <string>
|
||||
|
||||
#include "../utils/ini_reader/ini_reader.h"
|
||||
#include "../utils/logger.h"
|
||||
#include "../utils/rapidjson_extra.h"
|
||||
#include "../utils/system.h"
|
||||
#include "utils/ini_reader/ini_reader.h"
|
||||
#include "utils/logger.h"
|
||||
#include "utils/rapidjson_extra.h"
|
||||
#include "utils/system.h"
|
||||
#include "webget.h"
|
||||
|
||||
std::string buildGistData(std::string name, std::string content)
|
||||
|
||||
+34
-83
@@ -7,13 +7,14 @@
|
||||
|
||||
#include <curl/curl.h>
|
||||
|
||||
#include "../handler/settings.h"
|
||||
#include "../utils/base64/base64.h"
|
||||
#include "../utils/defer.h"
|
||||
#include "../utils/file_extra.h"
|
||||
#include "../utils/logger.h"
|
||||
#include "../utils/urlencode.h"
|
||||
#include "../version.h"
|
||||
#include "handler/settings.h"
|
||||
#include "utils/base64/base64.h"
|
||||
#include "utils/defer.h"
|
||||
#include "utils/file_extra.h"
|
||||
#include "utils/lock.h"
|
||||
#include "utils/logger.h"
|
||||
#include "utils/urlencode.h"
|
||||
#include "version.h"
|
||||
#include "webget.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -27,70 +28,6 @@ using guarded_mutex = std::lock_guard<std::mutex>;
|
||||
std::mutex cache_rw_lock;
|
||||
*/
|
||||
|
||||
class RWLock
|
||||
{
|
||||
#define WRITE_LOCK_STATUS (-1)
|
||||
#define FREE_STATUS 0
|
||||
private:
|
||||
const std::thread::id NULL_THREAD;
|
||||
const bool WRITE_FIRST;
|
||||
std::thread::id m_write_thread_id;
|
||||
std::atomic_int m_lockCount;
|
||||
std::atomic_uint m_writeWaitCount;
|
||||
public:
|
||||
RWLock(const RWLock&) = delete;
|
||||
RWLock& operator=(const RWLock&) = delete;
|
||||
RWLock(bool writeFirst = true): WRITE_FIRST(writeFirst), m_write_thread_id(), m_lockCount(0), m_writeWaitCount(0) {}
|
||||
virtual ~RWLock() = default;
|
||||
int readLock()
|
||||
{
|
||||
if (std::this_thread::get_id() != m_write_thread_id)
|
||||
{
|
||||
int count;
|
||||
if (WRITE_FIRST)
|
||||
do {
|
||||
while ((count = m_lockCount) == WRITE_LOCK_STATUS || m_writeWaitCount > 0);
|
||||
} while (!m_lockCount.compare_exchange_weak(count, count + 1));
|
||||
else
|
||||
do {
|
||||
while ((count = m_lockCount) == WRITE_LOCK_STATUS);
|
||||
} while (!m_lockCount.compare_exchange_weak(count, count + 1));
|
||||
}
|
||||
return m_lockCount;
|
||||
}
|
||||
int readUnlock()
|
||||
{
|
||||
if (std::this_thread::get_id() != m_write_thread_id)
|
||||
--m_lockCount;
|
||||
return m_lockCount;
|
||||
}
|
||||
int writeLock()
|
||||
{
|
||||
if (std::this_thread::get_id() != m_write_thread_id)
|
||||
{
|
||||
++m_writeWaitCount;
|
||||
for (int zero = FREE_STATUS; !m_lockCount.compare_exchange_weak(zero, WRITE_LOCK_STATUS); zero = FREE_STATUS);
|
||||
--m_writeWaitCount;
|
||||
m_write_thread_id = std::this_thread::get_id();
|
||||
}
|
||||
return m_lockCount;
|
||||
}
|
||||
int writeUnlock()
|
||||
{
|
||||
if (std::this_thread::get_id() != m_write_thread_id)
|
||||
{
|
||||
throw std::runtime_error("writeLock/Unlock mismatch");
|
||||
}
|
||||
if (WRITE_LOCK_STATUS != m_lockCount)
|
||||
{
|
||||
throw std::runtime_error("RWLock internal error");
|
||||
}
|
||||
m_write_thread_id = NULL_THREAD;
|
||||
m_lockCount.store(FREE_STATUS);
|
||||
return m_lockCount;
|
||||
}
|
||||
};
|
||||
|
||||
RWLock cache_rw_lock;
|
||||
|
||||
//std::string user_agent_str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36";
|
||||
@@ -118,18 +55,17 @@ static int writer(char *data, size_t size, size_t nmemb, std::string *writerData
|
||||
|
||||
writerData->append(data, size*nmemb);
|
||||
|
||||
return size * nmemb;
|
||||
return static_cast<int>(size * nmemb);
|
||||
}
|
||||
|
||||
static int dummy_writer(char *data, size_t size, size_t nmemb, void *writerData)
|
||||
static int dummy_writer(char *, size_t size, size_t nmemb, void *)
|
||||
{
|
||||
/// dummy writer, do not save anything
|
||||
(void)data;
|
||||
(void)writerData;
|
||||
return size * nmemb;
|
||||
return static_cast<int>(size * nmemb);
|
||||
}
|
||||
|
||||
static int size_checker(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
|
||||
//static int size_checker(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
|
||||
static int size_checker(void *clientp, curl_off_t, curl_off_t dlnow, curl_off_t, curl_off_t)
|
||||
{
|
||||
if(clientp)
|
||||
{
|
||||
@@ -163,9 +99,24 @@ static int logger(CURL *handle, curl_infotype type, char *data, size_t size, voi
|
||||
return 0;
|
||||
}
|
||||
std::string content(data, size);
|
||||
if(content.back() == '\n')
|
||||
content.pop_back();
|
||||
writeLog(0, prefix + ": " + content, LOG_LEVEL_VERBOSE);
|
||||
if(content.find("\r\n") != std::string::npos)
|
||||
{
|
||||
string_array lines = split(content, "\r\n");
|
||||
for(auto &x : lines)
|
||||
{
|
||||
std::string log_content = prefix;
|
||||
log_content += ": ";
|
||||
log_content += x;
|
||||
writeLog(0, log_content, LOG_LEVEL_VERBOSE);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string log_content = prefix;
|
||||
log_content += ": ";
|
||||
log_content += trimWhitespace(content);
|
||||
writeLog(0, log_content, LOG_LEVEL_VERBOSE);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -198,7 +149,7 @@ static int curlGet(const FetchArgument &argument, FetchResult &result)
|
||||
std::string *data = result.content, new_url = argument.url;
|
||||
curl_slist *header_list = nullptr;
|
||||
defer(curl_slist_free_all(header_list);)
|
||||
long retVal = 0;
|
||||
long retVal;
|
||||
|
||||
curl_init();
|
||||
|
||||
@@ -361,10 +312,10 @@ std::string webGet(const std::string &url, const std::string &proxy, unsigned in
|
||||
md("cache");
|
||||
const std::string url_md5 = getMD5(url);
|
||||
const std::string path = "cache/" + url_md5, path_header = path + "_header";
|
||||
struct stat result;
|
||||
struct stat result {};
|
||||
if(stat(path.data(), &result) == 0) // cache exist
|
||||
{
|
||||
time_t mtime = result.st_mtime, now = time(NULL); // get cache modified time and current time
|
||||
time_t mtime = result.st_mtime, now = time(nullptr); // get cache modified time and current time
|
||||
if(difftime(now, mtime) <= cache_ttl) // within TTL
|
||||
{
|
||||
writeLog(0, "CACHE HIT: '" + url + "', using local cache.");
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#include "../utils/map_extra.h"
|
||||
#include "../utils/string.h"
|
||||
#include "utils/map_extra.h"
|
||||
#include "utils/string.h"
|
||||
|
||||
enum http_method
|
||||
{
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
#include "../handler/settings.h"
|
||||
#include "handler/settings.h"
|
||||
#include <string>
|
||||
|
||||
Settings global;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "../../utils/tribool.h"
|
||||
#include "utils/tribool.h"
|
||||
|
||||
using String = std::string;
|
||||
using StringArray = std::vector<String>;
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
|
||||
#include "../config/regmatch.h"
|
||||
#include "../parser/config/proxy.h"
|
||||
#include "../utils/base64/base64.h"
|
||||
#include "../utils/rapidjson_extra.h"
|
||||
#include "../utils/regexp.h"
|
||||
#include "../utils/string.h"
|
||||
#include "config/regmatch.h"
|
||||
#include "parser/config/proxy.h"
|
||||
#include "utils/base64/base64.h"
|
||||
#include "utils/rapidjson_extra.h"
|
||||
#include "utils/regexp.h"
|
||||
#include "utils/string.h"
|
||||
|
||||
unsigned long long streamToInt(const std::string &stream)
|
||||
{
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "../utils/string.h"
|
||||
#include "utils/string.h"
|
||||
#include "config/proxy.h"
|
||||
#include "../config/regmatch.h"
|
||||
#include "config/regmatch.h"
|
||||
|
||||
bool getSubInfoFromHeader(const std::string &header, std::string &result);
|
||||
bool getSubInfoFromNodes(const std::vector<Proxy> &nodes, const RegexMatchConfigs &stream_rules, const RegexMatchConfigs &time_rules, std::string &result);
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#include "../utils/base64/base64.h"
|
||||
#include "../utils/ini_reader/ini_reader.h"
|
||||
#include "../utils/network.h"
|
||||
#include "../utils/rapidjson_extra.h"
|
||||
#include "../utils/regexp.h"
|
||||
#include "../utils/string.h"
|
||||
#include "../utils/string_hash.h"
|
||||
#include "../utils/urlencode.h"
|
||||
#include "../utils/yamlcpp_extra.h"
|
||||
#include "utils/base64/base64.h"
|
||||
#include "utils/ini_reader/ini_reader.h"
|
||||
#include "utils/network.h"
|
||||
#include "utils/rapidjson_extra.h"
|
||||
#include "utils/regexp.h"
|
||||
#include "utils/string.h"
|
||||
#include "utils/string_hash.h"
|
||||
#include "utils/urlencode.h"
|
||||
#include "utils/yamlcpp_extra.h"
|
||||
#include "config/proxy.h"
|
||||
#include "subparser.h"
|
||||
|
||||
|
||||
+8
-8
@@ -2,14 +2,14 @@
|
||||
#include <iostream>
|
||||
#include <libcron/Cron.h>
|
||||
|
||||
#include "../config/crontask.h"
|
||||
#include "../handler/interfaces.h"
|
||||
#include "../handler/multithread.h"
|
||||
#include "../handler/settings.h"
|
||||
#include "../server/webserver.h"
|
||||
#include "../utils/logger.h"
|
||||
#include "../utils/rapidjson_extra.h"
|
||||
#include "../utils/system.h"
|
||||
#include "config/crontask.h"
|
||||
#include "handler/interfaces.h"
|
||||
#include "handler/multithread.h"
|
||||
#include "handler/settings.h"
|
||||
#include "server/webserver.h"
|
||||
#include "utils/logger.h"
|
||||
#include "utils/rapidjson_extra.h"
|
||||
#include "utils/system.h"
|
||||
#include "script_quickjs.h"
|
||||
|
||||
libcron::Cron cron;
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
#include <duktape.h>
|
||||
#include <duk_module_node.h>
|
||||
|
||||
#include "../utils/string.h"
|
||||
#include "../utils/string_hash.h"
|
||||
#include "../handler/webget.h"
|
||||
#include "../handler/multithread.h"
|
||||
#include "../utils/base64/base64.h"
|
||||
#include "../utils/network.h"
|
||||
#include "utils/string.h"
|
||||
#include "utils/string_hash.h"
|
||||
#include "handler/webget.h"
|
||||
#include "handler/multithread.h"
|
||||
#include "utils/base64/base64.h"
|
||||
#include "utils/network.h"
|
||||
|
||||
extern int gCacheConfig;
|
||||
extern std::string gProxyConfig;
|
||||
|
||||
@@ -8,12 +8,12 @@
|
||||
#include <windows.h>
|
||||
#endif // _WIN32
|
||||
|
||||
#include "../handler/multithread.h"
|
||||
#include "../handler/webget.h"
|
||||
#include "../handler/settings.h"
|
||||
#include "../parser/config/proxy.h"
|
||||
#include "../utils/map_extra.h"
|
||||
#include "../utils/system.h"
|
||||
#include "handler/multithread.h"
|
||||
#include "handler/webget.h"
|
||||
#include "handler/settings.h"
|
||||
#include "parser/config/proxy.h"
|
||||
#include "utils/map_extra.h"
|
||||
#include "utils/system.h"
|
||||
#include "script_quickjs.h"
|
||||
|
||||
std::string parseProxy(const std::string &source);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef SCRIPT_QUICKJS_H_INCLUDED
|
||||
#define SCRIPT_QUICKJS_H_INCLUDED
|
||||
|
||||
#include "../parser/config/proxy.h"
|
||||
#include "../utils/defer.h"
|
||||
#include "parser/config/proxy.h"
|
||||
#include "utils/defer.h"
|
||||
|
||||
#ifndef NO_JS_RUNTIME
|
||||
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
#include <atomic>
|
||||
#include <curl/curlver.h>
|
||||
|
||||
#include "../utils/map_extra.h"
|
||||
#include "../utils/string.h"
|
||||
#include "../version.h"
|
||||
#include "utils/map_extra.h"
|
||||
#include "utils/string.h"
|
||||
#include "version.h"
|
||||
|
||||
struct Request
|
||||
{
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 16384
|
||||
#include "httplib.h"
|
||||
|
||||
#include "../utils/base64/base64.h"
|
||||
#include "../utils/logger.h"
|
||||
#include "../utils/string_hash.h"
|
||||
#include "../utils/stl_extra.h"
|
||||
#include "../utils/urlencode.h"
|
||||
#include "utils/base64/base64.h"
|
||||
#include "utils/logger.h"
|
||||
#include "utils/string_hash.h"
|
||||
#include "utils/stl_extra.h"
|
||||
#include "utils/urlencode.h"
|
||||
#include "webserver.h"
|
||||
|
||||
static const char *request_header_blacklist[] = {"host", "accept", "accept-encoding"};
|
||||
@@ -73,6 +73,18 @@ static httplib::Server::Handler makeHandler(const responseRoute &rr)
|
||||
};
|
||||
}
|
||||
|
||||
static std::string dump(const httplib::Headers &headers)
|
||||
{
|
||||
std::string s;
|
||||
for (auto &x: headers)
|
||||
{
|
||||
if (startsWith(x.first, "LOCAL_") || startsWith(x.first, "REMOTE_"))
|
||||
continue;
|
||||
s += x.first + ": " + x.second + "|";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
int WebServer::start_web_server_multi(listener_args *args)
|
||||
{
|
||||
httplib::Server server;
|
||||
@@ -125,6 +137,7 @@ int WebServer::start_web_server_multi(listener_args *args)
|
||||
{
|
||||
writeLog(0, "Accept connection from client " + req.remote_addr + ":" + std::to_string(req.remote_port), LOG_LEVEL_DEBUG);
|
||||
writeLog(0, "handle_cmd: " + req.method + " handle_uri: " + req.target, LOG_LEVEL_VERBOSE);
|
||||
writeLog(0, "handle_header: " + dump(req.headers), LOG_LEVEL_VERBOSE);
|
||||
|
||||
if (req.has_header("SubConverter-Request"))
|
||||
{
|
||||
|
||||
@@ -13,12 +13,12 @@
|
||||
#include <pthread.h>
|
||||
#include <thread>
|
||||
|
||||
#include "../utils/base64/base64.h"
|
||||
#include "../utils/file_extra.h"
|
||||
#include "../utils/logger.h"
|
||||
#include "../utils/stl_extra.h"
|
||||
#include "../utils/string.h"
|
||||
#include "../utils/urlencode.h"
|
||||
#include "utils/base64/base64.h"
|
||||
#include "utils/file_extra.h"
|
||||
#include "utils/logger.h"
|
||||
#include "utils/stl_extra.h"
|
||||
#include "utils/string.h"
|
||||
#include "utils/urlencode.h"
|
||||
#include "socket.h"
|
||||
#include "webserver.h"
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <string>
|
||||
|
||||
#include "../string.h"
|
||||
#include "utils/string.h"
|
||||
|
||||
static const std::string base64_chars =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
#include <numeric>
|
||||
#include <algorithm>
|
||||
|
||||
#include "../codepage.h"
|
||||
#include "../file_extra.h"
|
||||
#include "../string.h"
|
||||
#include "utils/codepage.h"
|
||||
#include "utils/file_extra.h"
|
||||
#include "utils/string.h"
|
||||
|
||||
enum
|
||||
{
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
#ifndef LOCK_H_INCLUDED
|
||||
#define LOCK_H_INCLUDED
|
||||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
class RWLock
|
||||
{
|
||||
static constexpr int WRITE_LOCK_STATUS = -1;
|
||||
static constexpr int FREE_STATUS = 0;
|
||||
private:
|
||||
const std::thread::id NULL_THREAD;
|
||||
const bool WRITE_FIRST;
|
||||
std::thread::id m_write_thread_id;
|
||||
std::atomic_int m_lockCount;
|
||||
std::atomic_uint m_writeWaitCount;
|
||||
public:
|
||||
RWLock(const RWLock&) = delete;
|
||||
RWLock& operator=(const RWLock&) = delete;
|
||||
explicit RWLock(bool writeFirst = true): WRITE_FIRST(writeFirst), m_write_thread_id(), m_lockCount(0), m_writeWaitCount(0) {}
|
||||
virtual ~RWLock() = default;
|
||||
int readLock()
|
||||
{
|
||||
if (std::this_thread::get_id() != m_write_thread_id)
|
||||
{
|
||||
int count;
|
||||
if (WRITE_FIRST)
|
||||
{
|
||||
do
|
||||
{
|
||||
while ((count = m_lockCount) == WRITE_LOCK_STATUS || m_writeWaitCount > 0);
|
||||
}
|
||||
while (!m_lockCount.compare_exchange_weak(count, count + 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
do
|
||||
{
|
||||
while ((count = m_lockCount) == WRITE_LOCK_STATUS);
|
||||
}
|
||||
while (!m_lockCount.compare_exchange_weak(count, count + 1));
|
||||
}
|
||||
}
|
||||
return m_lockCount;
|
||||
}
|
||||
int readUnlock()
|
||||
{
|
||||
if (std::this_thread::get_id() != m_write_thread_id)
|
||||
--m_lockCount;
|
||||
return m_lockCount;
|
||||
}
|
||||
int writeLock()
|
||||
{
|
||||
if (std::this_thread::get_id() != m_write_thread_id)
|
||||
{
|
||||
++m_writeWaitCount;
|
||||
for (int zero = FREE_STATUS; !m_lockCount.compare_exchange_weak(zero, WRITE_LOCK_STATUS); zero = FREE_STATUS);
|
||||
--m_writeWaitCount;
|
||||
m_write_thread_id = std::this_thread::get_id();
|
||||
}
|
||||
return m_lockCount;
|
||||
}
|
||||
int writeUnlock()
|
||||
{
|
||||
if (std::this_thread::get_id() != m_write_thread_id)
|
||||
{
|
||||
throw std::runtime_error("writeLock/Unlock mismatch");
|
||||
}
|
||||
if (WRITE_LOCK_STATUS != m_lockCount)
|
||||
{
|
||||
throw std::runtime_error("RWLock internal error");
|
||||
}
|
||||
m_write_thread_id = NULL_THREAD;
|
||||
m_lockCount.store(FREE_STATUS);
|
||||
return m_lockCount;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LOCK_H_INCLUDED
|
||||
+17
-2
@@ -5,7 +5,9 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../handler/settings.h"
|
||||
#include "handler/settings.h"
|
||||
#include "defer.h"
|
||||
#include "lock.h"
|
||||
#include "logger.h"
|
||||
|
||||
std::string getTime(int type)
|
||||
@@ -38,7 +40,20 @@ std::string getTime(int type)
|
||||
static std::string get_thread_name()
|
||||
{
|
||||
static std::atomic_int counter = 0;
|
||||
thread_local static std::string name = "Thread-" + std::to_string(counter++);
|
||||
static std::map<std::thread::id, std::string> thread_names;
|
||||
static RWLock lock;
|
||||
std::thread::id id = std::this_thread::get_id();
|
||||
lock.readLock();
|
||||
if (thread_names.find(id) != thread_names.end())
|
||||
{
|
||||
defer(lock.readUnlock();)
|
||||
return thread_names[id];
|
||||
}
|
||||
lock.readUnlock();
|
||||
lock.writeLock();
|
||||
std::string name = "Thread-" + std::to_string(++counter);
|
||||
thread_names[id] = name;
|
||||
lock.writeUnlock();
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
|
||||
#include "../server/socket.h"
|
||||
#include "server/socket.h"
|
||||
#include "string.h"
|
||||
#include "regexp.h"
|
||||
|
||||
|
||||
Reference in new issue
Block a user