Skip to content

Commit

Permalink
Add filter nodes by group or group ID
Browse files Browse the repository at this point in the history
  • Loading branch information
tindy2013 committed Nov 8, 2019
1 parent 6533df2 commit e4491e3
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 49 deletions.
16 changes: 10 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ int main()
if(!url.size()) url = default_url;
string_array urls = split(url, "|");
std::vector<nodeInfo> nodes;
int groupID = 0;
if(include.size())
{
eraseElements(def_include_remarks);
Expand All @@ -197,7 +198,8 @@ int main()
for(std::string &x : urls)
{
std::cerr<<"Fetching node data from url '"<<x<<"'. Generate target: Clash"<<std::endl;
addNodes(x, nodes);
addNodes(x, nodes, groupID);
groupID++;
}
if(fileExist(clash_rule_base))
clash_base_content = fileGet(clash_rule_base, false);
Expand All @@ -218,6 +220,7 @@ int main()
if(!url.size()) url = default_url;
string_array urls = split(url, "|");
std::vector<nodeInfo> nodes;
int groupID = 0;
if(include.size())
{
eraseElements(def_include_remarks);
Expand All @@ -227,7 +230,8 @@ int main()
for(std::string &x : urls)
{
std::cerr<<"Fetching node data from url '"<<x<<"'. Generate target: ClashR"<<std::endl;
addNodes(x, nodes);
addNodes(x, nodes, groupID);
groupID++;
}
if(fileExist(clash_rule_base))
clash_base_content = fileGet(clash_rule_base, false);
Expand All @@ -249,6 +253,7 @@ int main()
if(!url.size()) url = default_url;
string_array urls = split(url, "|");
std::vector<nodeInfo> nodes;
int groupID = 0;
if(include.size())
{
eraseElements(def_include_remarks);
Expand All @@ -258,12 +263,11 @@ int main()
for(std::string &x : urls)
{
std::cerr<<"Fetching node data from url '"<<x<<"'. Generate target: Surge "<<surge_ver<<std::endl;
addNodes(x, nodes);
addNodes(x, nodes, groupID);
groupID++;
}
if(fileExist(surge_rule_base))
{
surge_base_content = fileGet(surge_rule_base, false);
}
else
surge_base_content = webGet(surge_rule_base, getSystemProxy());

Expand All @@ -286,7 +290,7 @@ int main()
});
}

listener_args args = {listen_address, listen_port, 10240, 4};
listener_args args = {listen_address, listen_port, max_pending_connections, max_concurrent_threads};
std::cout<<"Serving HTTP @ http://"<<listen_address<<":"<<listen_port<<std::endl;
start_web_server_multi(&args);

Expand Down
3 changes: 2 additions & 1 deletion nodeinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
struct nodeInfo
{
int linkType = -1;
int id = 0;
int id = -1;
int groupID = -1;
std::string group;
std::string remarks;
std::string server;
Expand Down
10 changes: 9 additions & 1 deletion nodemanip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
std::string override_conf_port, custom_group;
int socksport;
bool ss_libev, ssr_libev;
extern bool api_mode;

void copyNodes(std::vector<nodeInfo> *source, std::vector<nodeInfo> *dest)
{
Expand All @@ -20,7 +21,7 @@ void copyNodes(std::vector<nodeInfo> *source, std::vector<nodeInfo> *dest)
}
}

void addNodes(std::string link, std::vector<nodeInfo> &allNodes)
void addNodes(std::string link, std::vector<nodeInfo> &allNodes, int groupID)
{
int linkType = -1;
std::vector<nodeInfo> nodes;
Expand Down Expand Up @@ -67,6 +68,8 @@ void addNodes(std::string link, std::vector<nodeInfo> &allNodes)
{
writeLog(LOG_TYPE_INFO, "Parsing subscription data...");
explodeConfContent(strSub, override_conf_port, socksport, ss_libev, ssr_libev, nodes);
for(nodeInfo &x : nodes)
x.groupID = groupID;
copyNodes(&nodes, &allNodes);
}
else
Expand All @@ -75,13 +78,17 @@ void addNodes(std::string link, std::vector<nodeInfo> &allNodes)
}
break;
case SPEEDTEST_MESSAGE_FOUNDLOCAL:
if(api_mode)
break;
writeLog(LOG_TYPE_INFO, "Parsing configuration file data...");
if(explodeConf(link, override_conf_port, socksport, ss_libev, ssr_libev, nodes) == SPEEDTEST_ERROR_UNRECOGFILE)
{
writeLog(LOG_TYPE_ERROR, "Invalid configuration file!");
}
else
{
for(nodeInfo &x : nodes)
x.groupID = groupID;
copyNodes(&nodes, &allNodes);
}
break;
Expand All @@ -97,6 +104,7 @@ void addNodes(std::string link, std::vector<nodeInfo> &allNodes)
}
else
{
node.groupID = groupID;
allNodes.push_back(node);
}
}
Expand Down
2 changes: 1 addition & 1 deletion nodemanip.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

#include "nodeinfo.h"

void addNodes(std::string link, std::vector<nodeInfo> &allNodes);
void addNodes(std::string link, std::vector<nodeInfo> &allNodes, int groupID);

#endif // NODEMANIP_H_INCLUDED
6 changes: 5 additions & 1 deletion pref.ini
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ surge_ruleset=💕 规则自选,[]MATCH
;custom_proxy_group=FallBack`fallback`.*`http://www.gstatic.com/generate_204`300
;custom_proxy_group=LoadBalance`load-balance`.*`http://www.gstatic.com/generate_204`300

;custom_proxy_group=g1`select`!!GROUPID=0
;custom_proxy_group=g2`select`!!GROUPID=1
;custom_proxy_group=v2ray`select`!!GROUP=V2RayProvider

;for forcerule.yml
;custom_proxy_group=Proxy`select`.*[]AUTO`[]DIRECT`.*
;custom_proxy_group=AUTO`url-test`.*`http://www.gstatic.com/generate_204`300
Expand Down Expand Up @@ -142,5 +146,5 @@ listen=0.0.0.0
port=25500

[advanced]
max_pending_connections=10
max_pending_connections=10240
max_concurrent_threads=4
40 changes: 14 additions & 26 deletions speedtestutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,19 @@ void explodeVmess(std::string vmess, std::string custom_port, int local_port, no
version = "1"; //link without version will treat as version 1
GetMember(jsondata, "v", version); //try to get version

jsondata["ps"] >> ps;
jsondata["add"] >> add;
jsondata["type"] >> type;
jsondata["id"] >> id;
jsondata["aid"] >> aid;
jsondata["net"] >> net;
jsondata["tls"] >> tls;
GetMember(jsondata, "ps", ps);
GetMember(jsondata, "add", add);
GetMember(jsondata, "type", type);
GetMember(jsondata, "id", id);
GetMember(jsondata, "aid", aid);
GetMember(jsondata, "net", net);
GetMember(jsondata, "tls", tls);
if(custom_port != "")
port = custom_port;
else
jsondata["port"] >> port;
GetMember(jsondata, "port", port);

jsondata["host"] >> host;
GetMember(jsondata, "host", host);
if(version == "1")
{
if(host != "")
Expand Down Expand Up @@ -872,24 +872,12 @@ void explodeClash(Node yamlnode, std::string custom_port, int local_port, std::v
yamlnode["Proxy"][i]["password"] >> password;
if(yamlnode["Proxy"][i]["plugin"].IsDefined())
{
if(yamlnode["Proxy"][i]["plugin"].as<std::string>() == "obfs")
{
plugin = "simple-obfs";
if(yamlnode["Proxy"][i]["plugin-opts"].IsDefined())
{
yamlnode["Proxy"][i]["plugin-opts"]["mode"] >> pluginopts_mode;
if(yamlnode["Proxy"][i]["plugin-opts"]["host"].IsDefined())
yamlnode["Proxy"][i]["plugin-opts"]["host"] >> pluginopts_host;
}
}
}
else if(yamlnode["Proxy"][i]["obfs"].IsDefined())
{
plugin = "simple-obfs";
yamlnode["Proxy"][i]["obfs"] >> pluginopts_mode;
if(yamlnode["Proxy"][i]["obfs-host"].IsDefined())
yamlnode["Proxy"][i]["plugin"] >> plugin;
if(yamlnode["Proxy"][i]["plugin-opts"].IsDefined())
{
yamlnode["Proxy"][i]["obfs-host"] >> pluginopts_host;
yamlnode["Proxy"][i]["plugin-opts"]["mode"] >> pluginopts_mode;
if(yamlnode["Proxy"][i]["plugin-opts"]["host"].IsDefined())
yamlnode["Proxy"][i]["plugin-opts"]["host"] >> pluginopts_host;
}
}
else
Expand Down
63 changes: 50 additions & 13 deletions subexport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ std::string netchToClash(std::vector<nodeInfo> &nodes, std::string &baseConf, st
std::string plugin, pluginopts;
std::string protocol, protoparam, obfs, obfsparam;
std::string id, aid, transproto, faketype, host, path, quicsecure, quicsecret;
std::vector<std::string> nodelist;
std::vector<nodeInfo> nodelist;
std::string group;
bool tlssecure, replace_flag;
string_array vArray, filtered_nodelist;

Expand All @@ -305,7 +306,7 @@ std::string netchToClash(std::vector<nodeInfo> &nodes, std::string &baseConf, st
singleproxy.reset();
json.Parse(x.proxyStr.data());
type = GetMember(json, "Type");
remark = addEmoji(trim(removeEmoji(nodeRename(x.remarks))));
remark = x.remarks = addEmoji(trim(removeEmoji(nodeRename(x.remarks))));
hostname = GetMember(json, "Hostname");
port = GetMember(json, "Port");
username = GetMember(json, "Username");
Expand Down Expand Up @@ -378,7 +379,7 @@ std::string netchToClash(std::vector<nodeInfo> &nodes, std::string &baseConf, st
singleproxy["server"] = hostname;
singleproxy["port"] = (unsigned short)stoi(port);
proxies.push_back(singleproxy);
nodelist.emplace_back(remark);
nodelist.emplace_back(x);
}

yamlnode["Proxy"] = proxies;
Expand Down Expand Up @@ -439,12 +440,30 @@ std::string netchToClash(std::vector<nodeInfo> &nodes, std::string &baseConf, st
{
filtered_nodelist.emplace_back(vArray[i].substr(2));
}
else if(vArray[i].find("!!GROUP=") == 0)
{
group = vArray[i].substr(8);
for(nodeInfo &y : nodelist)
{
if(regFind(y.group, group) && std::find(filtered_nodelist.begin(), filtered_nodelist.end(), y.remarks) == filtered_nodelist.end())
filtered_nodelist.emplace_back(y.remarks);
}
}
else if(vArray[i].find("!!GROUPID=") == 0)
{
group = vArray[i].substr(10);
for(nodeInfo &y : nodelist)
{
if(y.groupID == stoi(group) && std::find(filtered_nodelist.begin(), filtered_nodelist.end(), y.remarks) == filtered_nodelist.end())
filtered_nodelist.emplace_back(y.remarks);
}
}
else
{
for(std::string &y : nodelist)
for(nodeInfo &y : nodelist)
{
if(regFind(y, vArray[i]) && std::find(filtered_nodelist.begin(), filtered_nodelist.end(), y) == filtered_nodelist.end())
filtered_nodelist.emplace_back(y);
if(regFind(y.remarks, vArray[i]) && std::find(filtered_nodelist.begin(), filtered_nodelist.end(), y.remarks) == filtered_nodelist.end())
filtered_nodelist.emplace_back(y.remarks);
}
}
}
Expand Down Expand Up @@ -484,8 +503,8 @@ std::string netchToSurge(std::vector<nodeInfo> &nodes, std::string &base_conf, s
std::string type, remark, hostname, port, username, password, method;
std::string plugin, pluginopts;
std::string id, aid, transproto, faketype, host, path, quicsecure, quicsecret;
std::string url;
std::vector<std::string> nodelist;
std::string url, group;
std::vector<nodeInfo> nodelist;
bool tlssecure;
string_array vArray, filtered_nodelist;

Expand All @@ -500,7 +519,7 @@ std::string netchToSurge(std::vector<nodeInfo> &nodes, std::string &base_conf, s
{
json.Parse(x.proxyStr.data());
type = GetMember(json, "Type");
remark = addEmoji(trim(removeEmoji(nodeRename(x.remarks))));
remark = x.remarks = addEmoji(trim(removeEmoji(nodeRename(x.remarks))));
hostname = GetMember(json, "Hostname");
port = std::__cxx11::to_string((unsigned short)stoi(GetMember(json, "Port")));
username = GetMember(json, "Username");
Expand Down Expand Up @@ -565,7 +584,7 @@ std::string netchToSurge(std::vector<nodeInfo> &nodes, std::string &base_conf, s
else
continue;
ini.Set(remark, proxy);
nodelist.emplace_back(remark);
nodelist.emplace_back(x);
}

ini.SetCurrentSection("Proxy Group");
Expand Down Expand Up @@ -600,12 +619,30 @@ std::string netchToSurge(std::vector<nodeInfo> &nodes, std::string &base_conf, s
{
filtered_nodelist.emplace_back(vArray[i].substr(2));
}
else if(vArray[i].find("!!GROUP=") == 0)
{
group = vArray[i].substr(8);
for(nodeInfo &y : nodelist)
{
if(regFind(y.group, group) && std::find(filtered_nodelist.begin(), filtered_nodelist.end(), y.remarks) == filtered_nodelist.end())
filtered_nodelist.emplace_back(y.remarks);
}
}
else if(vArray[i].find("!!GROUPID=") == 0)
{
group = vArray[i].substr(10);
for(nodeInfo &y : nodelist)
{
if(y.groupID == stoi(group) && std::find(filtered_nodelist.begin(), filtered_nodelist.end(), y.remarks) == filtered_nodelist.end())
filtered_nodelist.emplace_back(y.remarks);
}
}
else
{
for(std::string &y : nodelist)
for(nodeInfo &y : nodelist)
{
if(regFind(y, vArray[i]) && std::find(filtered_nodelist.begin(), filtered_nodelist.end(), y) == filtered_nodelist.end())
filtered_nodelist.emplace_back(y);
if(regFind(y.remarks, vArray[i]) && std::find(filtered_nodelist.begin(), filtered_nodelist.end(), y.remarks) == filtered_nodelist.end())
filtered_nodelist.emplace_back(y.remarks);
}
}
}
Expand Down

0 comments on commit e4491e3

Please sign in to comment.