Skip to content

Commit

Permalink
Add support for parsing standard VMess links
Browse files Browse the repository at this point in the history
Optimize codes.
  • Loading branch information
tindy2013 committed Aug 16, 2020
1 parent 3ee39e6 commit 4bc2a58
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ struct UAProfile
std::string version_match;
std::string version_target;
std::string target;
tribool clash_new_name;
tribool clash_new_name = tribool();
int surge_ver = -1;
};

Expand Down
104 changes: 81 additions & 23 deletions src/speedtestutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ void explodeVmess(std::string vmess, const std::string &custom_port, nodeInfo &n
std::string version, ps, add, port, type, id, aid, net, path, host, tls;
Document jsondata;
std::vector<std::string> vArray;
if(regMatch(vmess, "vmess://(.*?)\\?(.*)")) //shadowrocket style link
if(regMatch(vmess, "vmess://(.*?)@(.*)"))
{
explodeStdVMess(vmess, custom_port, node);
return;
}
else if(regMatch(vmess, "vmess://(.*?)\\?(.*)")) //shadowrocket style link
{
explodeShadowrocket(vmess, custom_port, node);
return;
Expand Down Expand Up @@ -1001,26 +1006,27 @@ void explodeClash(Node yamlnode, const std::string &custom_port, std::vector<nod
{
switch(hash_(safe_as<std::string>(singleproxy["plugin"])))
{
case "obfs"_hash:
plugin = "simple-obfs";
if(singleproxy["plugin-opts"].IsDefined())
{
singleproxy["plugin-opts"]["mode"] >>= pluginopts_mode;
singleproxy["plugin-opts"]["host"] >>= pluginopts_host;
}
break;
case "v2ray-plugin"_hash:
plugin = "v2ray-plugin";
if(singleproxy["plugin-opts"].IsDefined())
{
singleproxy["plugin-opts"]["mode"] >>= pluginopts_mode;
singleproxy["plugin-opts"]["host"] >>= pluginopts_host;
tls = safe_as<bool>(singleproxy["plugin-opts"]["tls"]) ? "tls;" : "";
singleproxy["plugin-opts"]["path"] >>= path;
pluginopts_mux = safe_as<bool>(singleproxy["plugin-opts"]["mux"]) ? "mux=4;" : "";
}
break;
default: break;
case "obfs"_hash:
plugin = "simple-obfs";
if(singleproxy["plugin-opts"].IsDefined())
{
singleproxy["plugin-opts"]["mode"] >>= pluginopts_mode;
singleproxy["plugin-opts"]["host"] >>= pluginopts_host;
}
break;
case "v2ray-plugin"_hash:
plugin = "v2ray-plugin";
if(singleproxy["plugin-opts"].IsDefined())
{
singleproxy["plugin-opts"]["mode"] >>= pluginopts_mode;
singleproxy["plugin-opts"]["host"] >>= pluginopts_host;
tls = safe_as<bool>(singleproxy["plugin-opts"]["tls"]) ? "tls;" : "";
singleproxy["plugin-opts"]["path"] >>= path;
pluginopts_mux = safe_as<bool>(singleproxy["plugin-opts"]["mux"]) ? "mux=4;" : "";
}
break;
default:
break;
}
}
else if(singleproxy["obfs"].IsDefined())
Expand Down Expand Up @@ -1133,15 +1139,67 @@ void explodeClash(Node yamlnode, const std::string &custom_port, std::vector<nod
return;
}

void explodeStdVMess(std::string vmess, const std::string &custom_port, nodeInfo &node)
{
std::string add, port, type, id, aid, net, path, host, tls, remarks;
std::string addition;
vmess = vmess.substr(8);
string_size pos;

pos = vmess.rfind("#");
if(pos != vmess.npos)
{
remarks = UrlDecode(vmess.substr(pos + 1));
vmess.erase(pos);
}
const std::string stdvmess_matcher = R"(^([a-z]+)(?:\+([a-z]+))?:([\da-f]{4}(?:[\da-f]{4}-){4}[\da-f]{12})-(\d+)@(.+):(\d+)(?:\/?\?(.*))?$)";
if(regGetMatch(vmess, stdvmess_matcher, 8, 0, &net, &tls, &id, &aid, &add, &port, &addition))
return;

switch(hash_(net))
{
case "tcp"_hash:
case "kcp"_hash:
type = getUrlArg(addition, "type");
break;
case "http"_hash:
case "ws"_hash:
host = getUrlArg(addition, "host");
path = getUrlArg(addition, "path");
break;
case "quic"_hash:
type = getUrlArg(addition, "security");
host = getUrlArg(addition, "type");
path = getUrlArg(addition, "key");
break;
default:
return;
}

if(!custom_port.empty())
port = custom_port;
if(remarks.empty())
remarks = add + ":" + port;

node.linkType = SPEEDTEST_MESSAGE_FOUNDVMESS;
node.group = V2RAY_DEFAULT_GROUP;
node.remarks = remarks;
node.server = add;
node.port = to_int(port, 0);
node.proxyStr = vmessConstruct(node.group, remarks, add, port, type, id, aid, net, "auto", path, host, "", tls);
return;
}

void explodeShadowrocket(std::string rocket, const std::string &custom_port, nodeInfo &node)
{
std::string add, port, type, id, aid, net = "tcp", path, host, tls, cipher, remarks;
std::string obfs; //for other style of link
std::string addition;
rocket = rocket.substr(8);

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

if(regGetMatch(urlsafe_base64_decode(rocket), "(.*?):(.*)@(.*):(.*)", 5, 0, &cipher, &id, &add, &port))
return;
Expand Down
1 change: 1 addition & 0 deletions src/speedtestutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void explodeSSR(std::string ssr, bool ss_libev, bool libev, const std::string &c
void explodeSS(std::string ss, bool libev, const std::string &custom_port, nodeInfo &node);
void explodeTrojan(std::string trojan, const std::string &custom_port, nodeInfo &node);
void explodeQuan(const std::string &quan, const std::string &custom_port, nodeInfo &node);
void explodeStdVMess(std::string vmess, const std::string &custom_port, nodeInfo &node);
void explodeShadowrocket(std::string kit, const std::string &custom_port, nodeInfo &node);
void explodeKitsunebi(std::string kit, const std::string &custom_port, nodeInfo &node);
/// Parse a link
Expand Down
1 change: 1 addition & 0 deletions src/webget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ static int curlGet(const FetchArgument &argument, FetchResult &result)
list = curl_slist_append(list, (x.first + ": " + x.second).data());
}
list = curl_slist_append(list, "SubConverter-Request: 1");
list = curl_slist_append(list, "SubConverter-Version: " VERSION);
if(list)
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);

Expand Down

0 comments on commit 4bc2a58

Please sign in to comment.