Fix cache system not working properly

Clean up codes.
This commit is contained in:
Tindy X committed 2021-10-10 21:32:54 +08:00
1 parent b972dfaa56
commit b4201b24f7
18 files changed
+1661 -1667

No files matched your search

+114 -1471
View File
File diff suppressed because it is too large. Load diff
+3 -1
View File
@@ -9,7 +9,9 @@
#include "../generator/config/subexport.h"
#include "../server/webserver.h"
void refreshRulesets(RulesetConfigs &ruleset_list, std::vector<ruleset_content> &rca);
std::string parseProxy(const std::string &source);
void refreshRulesets(RulesetConfigs &ruleset_list, std::vector<RulesetContent> &rca);
void readConf();
int simpleGenerator();
std::string convertRuleset(const std::string &content, int type);
+11 -13
View File
@@ -1,63 +1,61 @@
#include <future>
#include <thread>
#include "../handler/settings.h"
#include "../utils/network.h"
#include "webget.h"
#include "multithread.h"
//#include "vfs.h"
#include "../utils/network.h"
//safety lock for multi-thread
std::mutex on_emoji, on_rename, on_stream, on_time;
extern RegexMatchConfigs gEmojis, gRenames;
extern RegexMatchConfigs gStreamNodeRules, gTimeNodeRules;
RegexMatchConfigs safe_get_emojis()
{
guarded_mutex guard(on_emoji);
return gEmojis;
return global.emojis;
}
RegexMatchConfigs safe_get_renames()
{
guarded_mutex guard(on_rename);
return gRenames;
return global.renames;
}
RegexMatchConfigs safe_get_streams()
{
guarded_mutex guard(on_stream);
return gStreamNodeRules;
return global.streamNodeRules;
}
RegexMatchConfigs safe_get_times()
{
guarded_mutex guard(on_time);
return gTimeNodeRules;
return global.timeNodeRules;
}
void safe_set_emojis(RegexMatchConfigs data)
{
guarded_mutex guard(on_emoji);
gEmojis.swap(data);
global.emojis.swap(data);
}
void safe_set_renames(RegexMatchConfigs data)
{
guarded_mutex guard(on_rename);
gRenames.swap(data);
global.renames.swap(data);
}
void safe_set_streams(RegexMatchConfigs data)
{
guarded_mutex guard(on_stream);
gStreamNodeRules.swap(data);
global.streamNodeRules.swap(data);
}
void safe_set_times(RegexMatchConfigs data)
{
guarded_mutex guard(on_time);
gTimeNodeRules.swap(data);
global.timeNodeRules.swap(data);
}
std::shared_future<std::string> fetchFileAsync(const std::string &path, const std::string &proxy, int cache_ttl, bool async)
File diff suppressed because it is too large. Load diff
+102
View File
@@ -0,0 +1,102 @@
#ifndef SETTINGS_H_INCLUDED
#define SETTINGS_H_INCLUDED
#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/string.h"
#include "../utils/stl_extra.h"
#include "../utils/tribool.h"
struct Settings
{
//common settings
std::string prefPath = "pref.ini", defaultExtConfig;
string_array excludeRemarks, includeRemarks;
RulesetConfigs customRulesets;
RegexMatchConfigs streamNodeRules, timeNodeRules;
std::vector<RulesetContent> rulesetsContent;
std::string listenAddress = "127.0.0.1", defaultUrls, insertUrls, managedConfigPrefix;
int listenPort = 25500, maxPendingConns = 10, maxConcurThreads = 4;
bool prependInsert = true, skipFailedLinks = false;
bool APIMode = true, writeManagedConfig = false, enableRuleGen = true, updateRulesetOnRequest = false, overwriteOriginalRules = true;
bool printDbgInfo = false, CFWChildProcess = false, appendUserinfo = true, asyncFetchRuleset = false, surgeResolveHostname = true;
std::string accessToken, basePath = "base";
std::string custom_group;
int logLevel;
long maxAllowedDownloadSize;
string_map aliases;
//global variables for template
std::string templatePath = "templates";
string_map templateVars;
//generator settings
bool generatorMode = false;
std::string generateProfiles;
//preferences
RegexMatchConfigs renames, emojis;
bool addEmoji = false, removeEmoji = false, appendType = false, filterDeprecated = true;
tribool UDPFlag, TFOFlag, skipCertVerify, TLS13Flag, enableInsert;
bool enableSort = false, updateStrict = false;
bool clashUseNewField = false;
std::string clashProxiesStyle = "flow";
std::string proxyConfig, proxyRuleset, proxySubscription;
int updateInterval = 0;
std::string sortScript, filterScript;
std::string clashBase;
ProxyGroupConfigs customProxyGroups;
std::string surgeBase, surfboardBase, mellowBase, quanBase, quanXBase, loonBase, SSSubBase;
std::string surgeSSRPath, quanXDevID;
//cache system
bool serveCacheOnFetchFail = false;
int cacheSubscription = 60, cacheConfig = 300, cacheRuleset = 21600;
//limits
size_t maxAllowedRulesets = 64, maxAllowedRules = 32768;
bool scriptCleanContext = false;
//cron system
bool enableCron = false;
CronTaskConfigs cronTasks;
};
struct ExternalConfig
{
ProxyGroupConfigs custom_proxy_group;
RulesetConfigs surge_ruleset;
std::string clash_rule_base;
std::string surge_rule_base;
std::string surfboard_rule_base;
std::string mellow_rule_base;
std::string quan_rule_base;
std::string quanx_rule_base;
std::string loon_rule_base;
std::string sssub_rule_base;
RegexMatchConfigs rename;
RegexMatchConfigs emoji;
string_array include;
string_array exclude;
template_args *tpl_args = nullptr;
bool overwrite_original_rules = false;
bool enable_rule_generator = true;
tribool add_emoji;
tribool remove_old_emoji;
};
extern Settings global;
int importItems(string_array &target, bool scope_limit = true);
int loadExternalConfig(std::string &path, ExternalConfig &ext);
void parseGroupTimes(const std::string &src, int *interval, int *tolerance, int *timeout);
#endif // SETTINGS_H_INCLUDED
+4 -6
View File
@@ -7,6 +7,7 @@
#include <curl/curl.h>
#include "../handler/settings.h"
#include "../utils/base64/base64.h"
#include "../utils/defer.h"
#include "../utils/file_extra.h"
@@ -21,9 +22,6 @@
#endif // _stat
#endif // _WIN32
extern bool gPrintDbgInfo, gServeCacheOnFetchFail;
extern int gLogLevel;
/*
using guarded_mutex = std::lock_guard<std::mutex>;
std::mutex cache_rw_lock;
@@ -150,7 +148,7 @@ static int size_checker(void *clientp, curl_off_t dltotal, curl_off_t dlnow, cur
static inline void curl_set_common_options(CURL *curl_handle, const char *url, curl_progress_data *data)
{
curl_easy_setopt(curl_handle, CURLOPT_URL, url);
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, gLogLevel == LOG_LEVEL_VERBOSE ? 1L : 0L);
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, global.logLevel == LOG_LEVEL_VERBOSE ? 1L : 0L);
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
@@ -355,7 +353,7 @@ std::string webGet(const std::string &url, const std::string &proxy, unsigned in
writeLog(0, "CACHE NOT EXIST: '" + url + "', creating new cache.");
//content = curlGet(url, proxy, response_headers, return_code); // try to fetch data
curlGet(argument, fetch_res);
if(return_code == CURLE_OK) // success, save new cache
if(return_code == 200) // success, save new cache
{
//guarded_mutex guard(cache_rw_lock);
cache_rw_lock.writeLock();
@@ -366,7 +364,7 @@ std::string webGet(const std::string &url, const std::string &proxy, unsigned in
}
else
{
if(fileExist(path) && gServeCacheOnFetchFail) // failed, check if cache exist
if(fileExist(path) && global.serveCacheOnFetchFail) // failed, check if cache exist
{
writeLog(0, "Fetch failed. Serving cached content."); // cache exist, serving cache
//guarded_mutex guard(cache_rw_lock);