Skip to content

Commit

Permalink
Enhancements
Browse files Browse the repository at this point in the history
Add support for parsing authentication info in SOCKS5 links. (tindy2013#652)
Optimize codes.
  • Loading branch information
tindy2013 authored and littleneko committed Feb 26, 2024
1 parent 7547d91 commit e2236ed
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/generator/config/nodemanip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ int addNodes(std::string link, std::vector<Proxy> &allNodes, int groupID, parse_
writeLog(LOG_TYPE_WARN, "No system proxy is set. Skipping.");
}
*/
if(strSub.size())
if(!strSub.empty())
{
writeLog(LOG_TYPE_INFO, "Parsing subscription data...");
if(explodeConfContent(strSub, nodes) == 0)
Expand Down
2 changes: 1 addition & 1 deletion src/handler/webget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ std::mutex cache_rw_lock;

class RWLock
{
#define WRITE_LOCK_STATUS -1
#define WRITE_LOCK_STATUS (-1)
#define FREE_STATUS 0
private:
const std::thread::id NULL_THREAD;
Expand Down
26 changes: 14 additions & 12 deletions src/parser/infoparser.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <string>
#include <vector>
#include <cmath>
#include <time.h>
#include <ctime>

#include "../config/regmatch.h"
#include "../parser/config/proxy.h"
Expand All @@ -12,7 +12,7 @@

unsigned long long streamToInt(const std::string &stream)
{
if(!stream.size())
if(stream.empty())
return 0;
double streamval = 1.0;
std::vector<std::string> units = {"B", "KB", "MB", "GB", "TB", "PB", "EB"};
Expand Down Expand Up @@ -74,7 +74,7 @@ bool getSubInfoFromHeader(const std::string &header, std::string &result)
if(regFind(header, pattern))
{
regGetMatch(header, pattern, 2, 0, &retStr);
if(retStr.size())
if(!retStr.empty())
{
result = retStr;
return true;
Expand All @@ -90,7 +90,7 @@ bool getSubInfoFromNodes(const std::vector<Proxy> &nodes, const RegexMatchConfig
for(const Proxy &x : nodes)
{
remarks = x.Remark;
if(!stream_info.size())
if(stream_info.empty())
{
for(const RegexMatchConfig &y : stream_rules)
{
Expand All @@ -109,7 +109,7 @@ bool getSubInfoFromNodes(const std::vector<Proxy> &nodes, const RegexMatchConfig
}

remarks = x.Remark;
if(!time_info.size())
if(time_info.empty())
{
for(const RegexMatchConfig &y : time_rules)
{
Expand All @@ -127,40 +127,42 @@ bool getSubInfoFromNodes(const std::vector<Proxy> &nodes, const RegexMatchConfig
}
}

if(stream_info.size() && time_info.size())
if(!stream_info.empty() && !time_info.empty())
break;
}

if(!stream_info.size() && !time_info.size())
if(stream_info.empty() && time_info.empty())
return false;

//calculate how much stream left
unsigned long long total = 0, left, used = 0, expire = 0;
std::string total_str = getUrlArg(stream_info, "total"), left_str = getUrlArg(stream_info, "left"), used_str = getUrlArg(stream_info, "used");
if(strFind(total_str, "%"))
{
if(used_str.size())
if(!used_str.empty())
{
used = streamToInt(used_str);
total = used / (1 - percentToDouble(total_str));
}
else if(left_str.size())
else if(!left_str.empty())
{
left = streamToInt(left_str);
total = left / percentToDouble(total_str);
if (left > total) left = 0;
used = total - left;
}
}
else
{
total = streamToInt(total_str);
if(used_str.size())
if(!used_str.empty())
{
used = streamToInt(used_str);
}
else if(left_str.size())
else if(!left_str.empty())
{
left = streamToInt(left_str);
if (left > total) left = 0;
used = total - left;
}
}
Expand All @@ -183,7 +185,7 @@ bool getSubInfoFromSSD(const std::string &sub, std::string &result)
return false;

std::string used_str = GetMember(json, "traffic_used"), total_str = GetMember(json, "traffic_total"), expire_str = GetMember(json, "expiry");
if(!used_str.size() || !total_str.size())
if(used_str.empty() || total_str.empty())
return false;
unsigned long long used = stod(used_str) * std::pow(1024, 3), total = stod(total_str) * std::pow(1024, 3), expire;
result = "upload=0; download=" + std::to_string(used) + "; total=" + std::to_string(total) + ";";
Expand Down
91 changes: 49 additions & 42 deletions src/parser/subparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ void explodeVmessConf(std::string content, std::vector<Proxy> &nodes)
nodes.emplace_back(std::move(node));
index++;
}
return;
}

void explodeSS(std::string ss, Proxy &node)
Expand All @@ -356,20 +355,22 @@ void explodeSS(std::string ss, Proxy &node)
ss = replaceAllDistinct(ss.substr(5), "/?", "?");
if(strFind(ss, "#"))
{
ps = urlDecode(ss.substr(ss.find("#") + 1));
ss.erase(ss.find("#"));
auto sspos = ss.find('#');
ps = urlDecode(ss.substr(sspos + 1));
ss.erase(sspos);
}

if(strFind(ss, "?"))
{
addition = ss.substr(ss.find("?") + 1);
addition = ss.substr(ss.find('?') + 1);
plugins = urlDecode(getUrlArg(addition, "plugin"));
plugin = plugins.substr(0, plugins.find(";"));
pluginopts = plugins.substr(plugins.find(";") + 1);
auto pluginpos = plugins.find(';');
plugin = plugins.substr(0, pluginpos);
pluginopts = plugins.substr(pluginpos + 1);
group = getUrlArg(addition, "group");
if(!group.empty())
group = urlSafeBase64Decode(group);
ss.erase(ss.find("?"));
ss.erase(ss.find('?'));
}
if(strFind(ss, "@"))
{
Expand Down Expand Up @@ -465,7 +466,6 @@ void explodeSSD(std::string link, std::vector<Proxy> &nodes)
nodes.emplace_back(std::move(node));
index++;
}
return;
}

void explodeSSAndroid(std::string ss, std::vector<Proxy> &nodes)
Expand All @@ -474,7 +474,7 @@ void explodeSSAndroid(std::string ss, std::vector<Proxy> &nodes)
std::string plugin, pluginopts;

Document json;
int index = nodes.size();
auto index = nodes.size();
//first add some extra data before parsing
ss = "{\"nodes\":" + ss + "}";
json.Parse(ss.data());
Expand Down Expand Up @@ -509,7 +509,7 @@ void explodeSSConf(std::string content, std::vector<Proxy> &nodes)
{
Document json;
std::string ps, password, method, server, port, plugin, pluginopts, group = SS_DEFAULT_GROUP;
int index = nodes.size();
auto index = nodes.size();

json.Parse(content.data());
if(json.HasParseError() || !json.IsObject())
Expand Down Expand Up @@ -540,7 +540,6 @@ void explodeSSConf(std::string content, std::vector<Proxy> &nodes)
nodes.emplace_back(std::move(node));
index++;
}
return;
}

void explodeSSR(std::string ssr, Proxy &node)
Expand Down Expand Up @@ -584,7 +583,7 @@ void explodeSSRConf(std::string content, std::vector<Proxy> &nodes)
{
Document json;
std::string remarks, group, server, port, method, password, protocol, protoparam, obfs, obfsparam, plugin, pluginopts;
int index = nodes.size();
auto index = nodes.size();

json.Parse(content.data());
if(json.HasParseError() || !json.IsObject())
Expand Down Expand Up @@ -642,22 +641,33 @@ void explodeSSRConf(std::string content, std::vector<Proxy> &nodes)
nodes.emplace_back(std::move(node));
index++;
}
return;
}

void explodeSocks(std::string link, Proxy &node)
{
std::string group, remarks, server, port, username, password;
if(strFind(link, "socks://")) //v2rayn socks link
{
std::vector<std::string> arguments;
if(strFind(link, "#"))
{
remarks = urlDecode(link.substr(link.find("#") + 1));
link.erase(link.find("#"));
auto pos = link.find('#');
remarks = urlDecode(link.substr(pos + 1));
link.erase(pos);
}
link = urlSafeBase64Decode(link.substr(8));
arguments = split(link, ":");
if(strFind(link, "@"))
{
auto userinfo = split(link, '@');
if(userinfo.size() < 2)
return;
link = userinfo[1];
userinfo = split(userinfo[0], ':');
if(userinfo.size() < 2)
return;
username = userinfo[0];
password = userinfo[1];
}
auto arguments = split(link, ':');
if(arguments.size() < 2)
return;
server = arguments[0];
Expand Down Expand Up @@ -707,8 +717,8 @@ void explodeHTTPSub(std::string link, Proxy &node)
std::string group, remarks, server, port, username, password;
std::string addition;
bool tls = strFind(link, "https://");
string_size pos = link.find("?");
if(pos != link.npos)
auto pos = link.find('?');
if(pos != std::string::npos)
{
addition = link.substr(pos + 1);
link.erase(pos);
Expand Down Expand Up @@ -1188,7 +1198,6 @@ void explodeClash(Node yamlnode, std::vector<Proxy> &nodes)
nodes.emplace_back(std::move(node));
index++;
}
return;
}

void explodeStdVMess(std::string vmess, Proxy &node)
Expand All @@ -1198,8 +1207,8 @@ void explodeStdVMess(std::string vmess, Proxy &node)
vmess = vmess.substr(8);
string_size pos;

pos = vmess.rfind("#");
if(pos != vmess.npos)
pos = vmess.rfind('#');
if(pos != std::string::npos)
{
remarks = urlDecode(vmess.substr(pos + 1));
vmess.erase(pos);
Expand Down Expand Up @@ -1232,7 +1241,6 @@ void explodeStdVMess(std::string vmess, Proxy &node)
remarks = add + ":" + port;

vmessConstruct(node, V2RAY_DEFAULT_GROUP, remarks, add, port, type, id, aid, net, "auto", path, host, "", tls, "");
return;
}

void explodeShadowrocket(std::string rocket, Proxy &node)
Expand Down Expand Up @@ -1286,21 +1294,21 @@ void explodeKitsunebi(std::string kit, Proxy &node)
string_size pos;
kit = kit.substr(9);

pos = kit.find("#");
if(pos != kit.npos)
pos = kit.find('#');
if(pos != std::string::npos)
{
remarks = kit.substr(pos + 1);
kit = kit.substr(0, pos);
}

pos = kit.find("?");
pos = kit.find('?');
addition = kit.substr(pos + 1);
kit = kit.substr(0, pos);

if(regGetMatch(kit, "(.*?)@(.*):(.*)", 4, 0, &id, &add, &port))
return;
pos = port.find("/");
if(pos != port.npos)
pos = port.find('/');
if(pos != std::string::npos)
{
path = port.substr(pos);
port.erase(pos);
Expand Down Expand Up @@ -1958,8 +1966,8 @@ bool explodeSurge(std::string surge, std::vector<Proxy> &nodes)
vmessConstruct(node, V2RAY_DEFAULT_GROUP, remarks, server, port, "", id, aead, net, method, path, host, "", tls, "", udp, tfo, scv, tls13);
break;
case "trojan"_hash: //quantumult x style trojan link
server = trim(configs[0].substr(0, configs[0].rfind(":")));
port = trim(configs[0].substr(configs[0].rfind(":") + 1));
server = trim(configs[0].substr(0, configs[0].rfind(':')));
port = trim(configs[0].substr(configs[0].rfind(':') + 1));
if(port == "0")
continue;

Expand Down Expand Up @@ -2006,8 +2014,8 @@ bool explodeSurge(std::string surge, std::vector<Proxy> &nodes)
trojanConstruct(node, TROJAN_DEFAULT_GROUP, remarks, server, port, password, "", host, "", tls == "true", udp, tfo, scv, tls13);
break;
case "http"_hash: //quantumult x style http links
server = trim(configs[0].substr(0, configs[0].rfind(":")));
port = trim(configs[0].substr(configs[0].rfind(":") + 1));
server = trim(configs[0].substr(0, configs[0].rfind(':')));
port = trim(configs[0].substr(configs[0].rfind(':') + 1));
if(port == "0")
continue;

Expand Down Expand Up @@ -2113,7 +2121,7 @@ void explodeSSTap(std::string sstap, std::vector<Proxy> &nodes)
{
json["configs"][i]["obfsparam"] >> obfsparam;
json["configs"][i]["protocolparam"] >> protoparam;
ssrConstruct(node, group, remarks, base64Encode(remarks), server, port, protocol, cipher, obfs, pass, obfsparam, protoparam);
ssrConstruct(node, group, remarks, server, port, protocol, cipher, obfs, pass, obfsparam, protoparam);
}
break;
default:
Expand Down Expand Up @@ -2198,20 +2206,19 @@ int explodeConfContent(const std::string &content, std::vector<Proxy> &nodes)

void explode(const std::string &link, Proxy &node)
{
// TODO: replace strFind with startsWith if appropriate
if(strFind(link, "ssr://"))
if(startsWith(link, "ssr://"))
explodeSSR(link, node);
else if(strFind(link, "vmess://") || strFind(link, "vmess1://"))
else if(startsWith(link, "vmess://") || startsWith(link, "vmess1://"))
explodeVmess(link, node);
else if(strFind(link, "ss://"))
else if(startsWith(link, "ss://"))
explodeSS(link, node);
else if(strFind(link, "socks://") || strFind(link, "https://t.me/socks") || strFind(link, "tg://socks"))
else if(startsWith(link, "socks://") || startsWith(link, "https://t.me/socks") || startsWith(link, "tg://socks"))
explodeSocks(link, node);
else if(strFind(link, "https://t.me/http") || strFind(link, "tg://http")) //telegram style http link
else if(startsWith(link, "https://t.me/http") || startsWith(link, "tg://http")) //telegram style http link
explodeHTTP(link, node);
else if(strFind(link, "Netch://"))
else if(startsWith(link, "Netch://"))
explodeNetch(link, node);
else if(strFind(link, "trojan://"))
else if(startsWith(link, "trojan://"))
explodeTrojan(link, node);
else if(isLink(link))
explodeHTTPSub(link, node);
Expand Down Expand Up @@ -2271,7 +2278,7 @@ void explodeSub(std::string sub, std::vector<Proxy> &nodes)
while(getline(strstream, strLink, delimiter))
{
Proxy node;
if(strLink.rfind("\r") != strLink.npos)
if(strLink.rfind('\r') != std::string::npos)
strLink.erase(strLink.size() - 1);
explode(strLink, node);
if(strLink.empty() || node.Type == ProxyType::Unknown)
Expand Down
2 changes: 1 addition & 1 deletion src/script/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ int duktape_get_res_int(duk_context *ctx)
std::string duktape_get_res_str(duk_context *ctx)
{
if(duk_is_null_or_undefined(ctx, -1))
return std::string();
return "";
std::string retstr = duk_safe_to_string(ctx, -1);
duk_pop(ctx);
return retstr;
Expand Down
Loading

0 comments on commit e2236ed

Please sign in to comment.