Enhancements

Fix when a request without User-Agent was received, the program will crash.
Fix Emoji matching rules.
Add an interface to serve/convert local/remote rulesets.
This commit is contained in:
Tindy X
2020-02-11 16:43:39 +08:00
parent 7110146873
commit f9bd9cc1ce
5 changed files with 74 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
(流量|时间|应急|过期|Bandwidth|expire),🏳️‍🌈
(?i:流量|时间|应急|过期|Bandwidth|expire),🏳️‍🌈
AC,🇦🇨
(AR|阿根廷),🇦🇷
(奥地利|维也纳),🇦🇹
@@ -7,33 +7,34 @@ BE,🇧🇪
(BR|Brazil|巴西|圣保罗),🇧🇷
(Canada|加拿大|蒙特利尔|温哥华|楓葉|枫叶),🇨🇦
(瑞士|苏黎世),🇨🇭
(DE|Germany|德国|法兰克福|德),🇩🇪
(DE|Germany|法兰克福|德),🇩🇪
丹麦,🇩🇰
ES,🇪🇸
EU,🇪🇺
(Finland|芬兰|赫尔辛基),🇫🇮
(FR|France|法国|巴黎),🇫🇷
(UK|England|United.*?Kingdom|英国|英|伦敦),🇬🇧
(?i)(HK|Hong.*?Kong|香港|深港|沪港|呼港|HKT|HKBN|HGC|WTT|CMI|穗港|京港|港),🇭🇰
(?i:UK|England|United.*?Kingdom|[^-]英|伦敦),🇬🇧
(?i:HK|Hong.*?Kong|HKT|HKBN|HGC|WTT|CMI|[^-]港),🇭🇰
(Indonesia|印尼|印度尼西亚|雅加达),🇮🇩
(Ireland|爱尔兰|都柏林),🇮🇪
(India|印度|孟买),🇮🇳
(Italy|意大利|米兰),🇮🇹
(JP|Japan|日本|东京|大阪|埼玉|沪日|穗日|川日|中日|泉日|杭日),🇯🇵
(JP|Japan|日本|[^-]日),🇯🇵
(KP|朝鲜),🇰🇵
(KR|Korea|KOR|韩国|首尔|韩|韓),🇰🇷
(KR|Korea|KOR|首尔|韩|韓),🇰🇷
(MO|Macao|澳门|CTM),🇲🇴
(MY|Malaysia|马来西亚),🇲🇾
(NL|Netherlands|荷兰|阿姆斯特丹),🇳🇱
(PH|Philippines|菲律宾),🇵🇭
(RO|罗马尼亚),🇷🇴
(RU|Russia|俄罗斯|伯力|莫斯科|圣彼得堡|西伯利亚|新西伯利亚|京俄|杭俄),🇷🇺
(RU|Russia|伯力|莫斯科|圣彼得堡|西伯利亚|新西伯利亚|[^-]俄),🇷🇺
(沙特|迪拜),🇸🇦
(SE|Sweden),🇸🇪
(SG|Singapore|新加坡|狮城|沪新|京新|泉新|穗新|深新|杭新),🇸🇬
(SG|Singapore|新加坡|狮城|[^-]新),🇸🇬
(TH|Thailand|泰国|曼谷),🇹🇭
(TR|Turkey|土耳其|伊斯坦布尔),🇹🇷
(US|America|UnitedStates|美国|美|京美|波特兰|达拉斯|俄勒冈|凤凰城|费利蒙|硅谷|拉斯维加斯|洛杉矶|圣何塞|圣克拉拉|西雅图|芝加哥|沪美),🇺🇲
(?i:US|America|United.*?States|[^-]美|波特兰|达拉斯|俄勒冈|凤凰城|费利蒙|硅谷|拉斯维加斯|洛杉矶|圣何塞|圣克拉拉|西雅图|芝加哥),🇺🇲
(VN|越南),🇻🇳
(ZA|南非),🇿🇦
(CN|China|回国|中国|江苏|北京|上海|广州|深圳|杭州|徐州|青岛|宁波|镇江|back|TW|Taiwan|台湾|台北|台中|新北|彰化|CHT|新北|台|HINET),🇨🇳
(?i:TW|Taiwan|新北|彰化|CHT|新北|[^-]台|HINET),🇨🇳
(?i:CN|China|回国|中国|江苏|北京|上海|广州|深圳|杭州|徐州|青岛|宁波|镇江|back),🇨🇳

View File

@@ -54,6 +54,64 @@ template <typename T> void operator >> (const YAML::Node& node, T& i)
i = node.as<T>();
};
std::string getRuleset(RESPONSE_CALLBACK_ARGS)
{
std::string url = UrlDecode(getUrlArg(argument, "url")), type = getUrlArg(argument, "type"), group = UrlDecode(getUrlArg(argument, "group"));
std::string output_content;
if(!url.size() || !type.size() || !group.size() || (type != "1" && type != "2"))
{
*status_code = 400;
return "Invalid request!";
}
if(fileExist(url))
output_content = fileGet(url, false, true);
else
output_content = webGet(url, "");
if(!output_content.size())
{
*status_code = 400;
return "Invalid request!";
}
if(type == "2")
{
std::string strLine;
std::stringstream ss;
const std::string rule_match_regex = "^(.*?,.*?)(,.*)(,.*)$";
ss << output_content;
char delimiter = count(output_content.begin(), output_content.end(), '\n') < 1 ? '\r' : '\n';
std::string::size_type lineSize;
output_content.clear();
while(getline(ss, strLine, delimiter))
{
if(strLine.find("IP-CIDR6") == 0 || strLine.find("URL-REGEX") == 0 || strLine.find("PROCESS-NAME") == 0 || strLine.find("AND") == 0 || strLine.find("OR") == 0) //remove unsupported types
continue;
lineSize = strLine.size();
if(!strLine.empty() && (strLine[0] != ';' && strLine[0] != '#' && !(lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')))
{
strLine += "," + group;
if(std::count(strLine.begin(), strLine.end(), ',') > 2 && regReplace(strLine, rule_match_regex, "$2") == ",no-resolve")
strLine = regReplace(strLine, rule_match_regex, "$1$3$2");
else
strLine = regReplace(strLine, rule_match_regex, "$1$3");
}
output_content.append(strLine + "\n");
}
}
return output_content;
}
int importItems(string_array &target)
{
string_array result;

View File

@@ -10,6 +10,7 @@
void refreshRulesets(string_array &ruleset_list, std::vector<ruleset_content> &rca);
void readConf();
void generateBase();
std::string getRuleset(RESPONSE_CALLBACK_ARGS);
std::string subconverter(RESPONSE_CALLBACK_ARGS);
std::string simpleToClashR(RESPONSE_CALLBACK_ARGS);
std::string surgeConfToClash(RESPONSE_CALLBACK_ARGS);

View File

@@ -154,6 +154,8 @@ int main(int argc, char *argv[])
append_response("GET", "/surge2clash", "text/plain;charset=utf-8", surgeConfToClash);
append_response("GET", "/getruleset", "text/plain;charset=utf-8", getRuleset);
append_response("GET", "/clash", "text/plain;charset=utf-8", [](RESPONSE_CALLBACK_ARGS) -> std::string
{
return subconverter(argument + "&target=clash", postdata, status_code, extra_headers);

View File

@@ -71,6 +71,7 @@ void OnReq(evhttp_request *req, void *args)
{
const char *req_content_type = evhttp_find_header(req->input_headers, "Content-Type"), *req_ac_method = evhttp_find_header(req->input_headers, "Access-Control-Request-Method");
const char *req_method = req_ac_method == NULL ? EVBUFFER_LENGTH(req->input_buffer) == 0 ? "GET" : "POST" : "OPTIONS", *uri = req->uri;
const char *user_agent = evhttp_find_header(req->input_headers, "User-Agent");
char *client_ip;
u_short client_port;
evhttp_connection_get_peer(evhttp_request_get_connection(req), &client_ip, &client_port);
@@ -78,7 +79,7 @@ void OnReq(evhttp_request *req, void *args)
int retVal;
std::string postdata, content_type, return_data;
if(user_agent_str.compare(evhttp_find_header(req->input_headers, "User-Agent")) == 0)
if(user_agent != NULL && user_agent_str.compare(user_agent) == 0)
{
evhttp_send_error(req, 500, "Loop request detected!");
return;