Skip to content

Commit

Permalink
Optimize codes
Browse files Browse the repository at this point in the history
  • Loading branch information
tindy2013 committed Nov 15, 2023
1 parent 434a775 commit c578ea6
Show file tree
Hide file tree
Showing 9 changed files with 814 additions and 299 deletions.
833 changes: 690 additions & 143 deletions include/quickjspp.hpp

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/generator/config/nodemanip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,20 @@ int addNodes(std::string link, std::vector<Proxy> &allNodes, int groupID, parse_
for(Proxy &x : nodes)
{
x.GroupId = groupID;
if(custom_group.size())
if(!custom_group.empty())
x.Group = custom_group;
}
copyNodes(nodes, allNodes);
break;
default:
explode(link, node);
if(node.Type == -1)
if(node.Type == ProxyType::Unknown)
{
writeLog(LOG_TYPE_ERROR, "No valid link found.");
return -1;
}
node.GroupId = groupID;
if(custom_group.size())
if(!custom_group.empty())
node.Group = custom_group;
allNodes.emplace_back(std::move(node));
}
Expand Down Expand Up @@ -494,9 +494,9 @@ void preprocessNodes(std::vector<Proxy> &nodes, extra_settings &ext)
auto compare = (std::function<int(const Proxy&, const Proxy&)>) ctx.eval("compare");
auto comparer = [&](const Proxy &a, const Proxy &b)
{
if(a.Type == ProxyType::Unknow)
if(a.Type == ProxyType::Unknown)
return 1;
if(b.Type == ProxyType::Unknow)
if(b.Type == ProxyType::Unknown)
return 0;
return compare(a, b);
};
Expand Down
14 changes: 11 additions & 3 deletions src/generator/config/subexport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,15 @@ bool applyMatcher(const std::string &rule, std::string &real_rule, const Proxy &
std::string target, ret_real_rule;
static const std::string groupid_regex = R"(^!!(?:GROUPID|INSERT)=([\d\-+!,]+)(?:!!(.*))?$)", group_regex = R"(^!!(?:GROUP)=(.+?)(?:!!(.*))?$)";
static const std::string type_regex = R"(^!!(?:TYPE)=(.+?)(?:!!(.*))?$)", port_regex = R"(^!!(?:PORT)=(.+?)(?:!!(.*))?$)", server_regex = R"(^!!(?:SERVER)=(.+?)(?:!!(.*))?$)";
static const string_array types = {"", "SS", "SSR", "VMESS", "TROJAN", "SNELL", "HTTP", "HTTPS", "SOCKS5", "WIREGUARD"};
static const std::map<ProxyType, const char *> types = {{ProxyType::Shadowsocks, "SS"},
{ProxyType::ShadowsocksR, "SSR"},
{ProxyType::VMess, "VMESS"},
{ProxyType::Trojan, "TROJAN"},
{ProxyType::Snell, "SNELL"},
{ProxyType::HTTP, "HTTP"},
{ProxyType::HTTPS, "HTTPS"},
{ProxyType::SOCKS5, "SOCKS5"},
{ProxyType::WireGuard, "WIREGUARD"}};
if(startsWith(rule, "!!GROUP="))
{
regGetMatch(rule, group_regex, 3, 0, &target, &ret_real_rule);
Expand All @@ -134,9 +142,9 @@ bool applyMatcher(const std::string &rule, std::string &real_rule, const Proxy &
{
regGetMatch(rule, type_regex, 3, 0, &target, &ret_real_rule);
real_rule = ret_real_rule;
if(node.Type == ProxyType::Unknow)
if(node.Type == ProxyType::Unknown)
return false;
return regMatch(types[node.Type], target);
return regMatch(types.at(node.Type), target);
}
else if(startsWith(rule, "!!PORT="))
{
Expand Down
14 changes: 7 additions & 7 deletions src/generator/template/templates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ int render_template(const std::string &content, const template_args &vars, std::
for(auto &x : vars.request_params)
{
all_args += x.first;
if(x.second.size())
if(!x.second.empty())
{
parse_json_pointer(data["request"], x.first, x.second);
all_args += "=" + x.second;
Expand Down Expand Up @@ -295,19 +295,19 @@ const std::string clash_script_keyword_template = R"( keywords = [{{ rule.keywo
std::string findFileName(const std::string &path)
{
string_size pos = path.rfind('/');
if(pos == path.npos)
if(pos == std::string::npos)
{
pos = path.rfind('\\');
if(pos == path.npos)
if(pos == std::string::npos)
pos = 0;
}
string_size pos2 = path.rfind('.');
if(pos2 < pos || pos2 == path.npos)
if(pos2 < pos || pos2 == std::string::npos)
pos2 = path.size();
return path.substr(pos + 1, pos2 - pos - 1);
}

int renderClashScript(YAML::Node &base_rule, std::vector<RulesetContent> &ruleset_content_array, std::string remote_path_prefix, bool script, bool overwrite_original_rules, bool clash_classical_ruleset)
int renderClashScript(YAML::Node &base_rule, std::vector<RulesetContent> &ruleset_content_array, const std::string &remote_path_prefix, bool script, bool overwrite_original_rules, bool clash_classical_ruleset)
{
nlohmann::json data;
std::string match_group, geoips, retrieved_rules;
Expand Down Expand Up @@ -381,7 +381,7 @@ int renderClashScript(YAML::Node &base_rule, std::vector<RulesetContent> &rulese
groups.emplace_back(std::move(rule_name));
continue;
}
if(remote_path_prefix.size())
if(!remote_path_prefix.empty())
{
if(fileExist(rule_path, true) || isLink(rule_path))
{
Expand Down Expand Up @@ -524,7 +524,7 @@ int renderClashScript(YAML::Node &base_rule, std::vector<RulesetContent> &rulese
}
if(script)
{
if(geoips.size())
if(!geoips.empty())
parse_json_pointer(data, "geoips", geoips.erase(geoips.size() - 1));

parse_json_pointer(data, "match_group", match_group);
Expand Down
2 changes: 1 addition & 1 deletion src/generator/template/templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ struct template_args
};

int render_template(const std::string &content, const template_args &vars, std::string &output, const std::string &include_scope = "templates");
int renderClashScript(YAML::Node &base_rule, std::vector<RulesetContent> &ruleset_content_array, std::string remote_path_prefix, bool script, bool overwrite_original_rules, bool clash_classic_ruleset);
int renderClashScript(YAML::Node &base_rule, std::vector<RulesetContent> &ruleset_content_array, const std::string &remote_path_prefix, bool script, bool overwrite_original_rules, bool clash_classic_ruleset);

#endif // TEMPLATES_H_INCLUDED
8 changes: 4 additions & 4 deletions src/parser/config/proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
using String = std::string;
using StringArray = std::vector<String>;

enum ProxyType
enum class ProxyType
{
Unknow,
Unknown,
Shadowsocks,
ShadowsocksR,
VMess,
Expand All @@ -23,7 +23,7 @@ enum ProxyType
WireGuard
};

inline String getProxyTypeName(int type)
inline String getProxyTypeName(ProxyType type)
{
switch(type)
{
Expand All @@ -50,7 +50,7 @@ inline String getProxyTypeName(int type)

struct Proxy
{
int Type = ProxyType::Unknow;
ProxyType Type = ProxyType::Unknown;
uint32_t Id = 0;
uint32_t GroupId = 0;
String Group;
Expand Down
12 changes: 6 additions & 6 deletions src/parser/subparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,15 +743,15 @@ void explodeTrojan(std::string trojan, Proxy &node)
std::string server, port, psk, addition, group, remark, host, path, network;
tribool tfo, scv;
trojan.erase(0, 9);
string_size pos = trojan.rfind("#");
string_size pos = trojan.rfind('#');

if(pos != trojan.npos)
if(pos != std::string::npos)
{
remark = urlDecode(trojan.substr(pos + 1));
trojan.erase(pos);
}
pos = trojan.find("?");
if(pos != trojan.npos)
pos = trojan.find('?');
if(pos != std::string::npos)
{
addition = trojan.substr(pos + 1);
trojan.erase(pos);
Expand Down Expand Up @@ -1242,7 +1242,7 @@ void explodeShadowrocket(std::string rocket, Proxy &node)
std::string addition;
rocket = rocket.substr(8);

string_size pos = rocket.find("?");
string_size pos = rocket.find('?');
addition = rocket.substr(pos + 1);
rocket.erase(pos);

Expand Down Expand Up @@ -2274,7 +2274,7 @@ void explodeSub(std::string sub, std::vector<Proxy> &nodes)
if(strLink.rfind("\r") != strLink.npos)
strLink.erase(strLink.size() - 1);
explode(strLink, node);
if(strLink.empty() || node.Type == ProxyType::Unknow)
if(strLink.empty() || node.Type == ProxyType::Unknown)
{
continue;
}
Expand Down
49 changes: 23 additions & 26 deletions src/script/script_quickjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <map>
#include <iostream>
#include <quickjspp.hpp>
#include <quickjs/quickjs-libc.h>

#ifdef _WIN32
#include <windows.h>
Expand Down Expand Up @@ -291,9 +292,7 @@ namespace qjs
static qjs_fetch_Headers unwrap(JSContext *ctx, JSValueConst v)
{
qjs_fetch_Headers result;
auto headers = JS_GetPropertyStr(ctx, v, "headers");
result.headers = js_traits<detail::string_icase_map>::unwrap(ctx, headers);
JS_FreeValue(ctx, headers);
result.headers = unwrap_free<detail::string_icase_map>(ctx, v, "headers");
return result;
}
static JSValue wrap(JSContext *ctx, const qjs_fetch_Headers &h)
Expand All @@ -310,14 +309,12 @@ namespace qjs
static qjs_fetch_Request unwrap(JSContext *ctx, JSValueConst v)
{
qjs_fetch_Request request;
auto headers = JS_GetPropertyStr(ctx, v, "headers");
request.method = JS_GetPropertyToString(ctx, v, "method");
request.url = JS_GetPropertyToString(ctx, v, "url");
request.postdata = JS_GetPropertyToString(ctx, v, "data");
request.proxy = JS_GetPropertyToString(ctx, v, "proxy");
request.cookies = JS_GetPropertyToString(ctx, v, "cookies");
request.headers = js_traits<qjs_fetch_Headers>::unwrap(ctx, headers);
JS_FreeValue(ctx, headers);
request.method = unwrap_free<std::string>(ctx, v, "method");
request.url = unwrap_free<std::string>(ctx, v, "url");
request.postdata = unwrap_free<std::string>(ctx, v, "data");
request.proxy = unwrap_free<std::string>(ctx, v, "proxy");
request.cookies = unwrap_free<std::string>(ctx, v, "cookies");
request.headers = unwrap_free<qjs_fetch_Headers>(ctx, v, "headers");
return request;
}
};
Expand Down Expand Up @@ -390,7 +387,6 @@ std::string getGeoIP(const std::string &address, const std::string &proxy)
void script_runtime_init(qjs::Runtime &runtime)
{
js_std_init_handlers(runtime.rt);
JS_SetModuleLoaderFunc(runtime.rt, nullptr, js_module_loader, nullptr);
}

int ShowMsgbox(const std::string &title, std::string content, uint16_t type = 0)
Expand Down Expand Up @@ -459,18 +455,6 @@ int script_context_init(qjs::Context &context)
.fun<&qjs_fetch_Response::content>("data")
.fun<&qjs_fetch_Response::cookies>("cookies")
.fun<&qjs_fetch_Response::headers>("headers");
/*
module.class_<nodeInfo>("NodeInfo")
.constructor<>()
.fun<&nodeInfo::linkType>("LinkType")
.fun<&nodeInfo::id>("ID")
.fun<&nodeInfo::groupID>("GroupID")
.fun<&nodeInfo::group>("Group")
.fun<&nodeInfo::remarks>("Remark")
.fun<&nodeInfo::server>("Hostname")
.fun<&nodeInfo::port>("Port")
.fun<&nodeInfo::proxyStr>("ProxyInfo");
*/
module.class_<Proxy>("Proxy")
.constructor<>()
.fun<&Proxy::Type>("Type")
Expand Down Expand Up @@ -502,7 +486,20 @@ int script_context_init(qjs::Context &context)
.fun<&Proxy::UDP>("UDP")
.fun<&Proxy::TCPFastOpen>("TCPFastOpen")
.fun<&Proxy::AllowInsecure>("AllowInsecure")
.fun<&Proxy::TLS13>("TLS13");
.fun<&Proxy::TLS13>("TLS13")
.fun<&Proxy::SnellVersion>("SnellVersion")
.fun<&Proxy::ServerName>("ServerName")
.fun<&Proxy::SelfIP>("SelfIP")
.fun<&Proxy::SelfIPv6>("SelfIPv6")
.fun<&Proxy::PublicKey>("PublicKey")
.fun<&Proxy::PrivateKey>("PrivateKey")
.fun<&Proxy::PreSharedKey>("PreSharedKey")
.fun<&Proxy::DnsServers>("DnsServers")
.fun<&Proxy::Mtu>("Mtu")
.fun<&Proxy::AllowedIPs>("AllowedIPs")
.fun<&Proxy::KeepAlive>("KeepAlive")
.fun<&Proxy::TestUrl>("TestUrl")
.fun<&Proxy::ClientId>("ClientId");
context.global().add<&makeDataURI>("makeDataURI")
.add<&qjs_fetch>("fetch")
.add<&base64Encode>("atob")
Expand All @@ -518,7 +515,7 @@ int script_context_init(qjs::Context &context)
globalThis.Request = interUtils.Request
globalThis.Response = interUtils.Response
globalThis.Headers = interUtils.Headers
globalThis.NodeInfo = interUtils.NodeInfo
globalThis.Proxy = interUtils.Proxy
import * as std from 'std'
import * as os from 'os'
globalThis.std = std
Expand Down
Loading

0 comments on commit c578ea6

Please sign in to comment.