Add support for extracting UserInfo from SSD subscriptions

This commit is contained in:
Tindy X committed 2020-02-01 01:16:05 +08:00
1 parent 8eb5e1e5e3
commit cc35c6f8a6
4 files changed
+55 -6

No files matched your search

+16 -1
View File
@@ -15,7 +15,12 @@
#endif // USE_STD_REGEX
#include <rapidjson/document.h>
#ifdef USE_MBEDTLS
#include <mbedtls/md5.h>
#else
#include <openssl/md5.h>
#endif // USE_MBEDTLS
#include "misc.h"
@@ -625,14 +630,24 @@ std::string urlsafe_base64_encode(std::string string_to_encode)
std::string getMD5(std::string data)
{
MD5_CTX ctx;
std::string result;
unsigned int i = 0;
unsigned char digest[16] = {};
#ifdef USE_MBEDTLS
mbedtls_md5_context ctx;
mbedtls_md5_init(&ctx);
mbedtls_md5_update(&ctx, data.data(), data.size());
mbedtls_md5_finish(&ctx, (unsigned char *)&digest);
#else
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, data.data(), data.size());
MD5_Final((unsigned char *)&digest, &ctx);
#endif // USE_MBEDTLS
char tmp[3] = {};
for(i = 0; i < 16; i++)
+17 -3
View File
@@ -75,8 +75,15 @@ int addNodes(std::string link, std::vector<nodeInfo> &allNodes, int groupID, std
writeLog(LOG_TYPE_ERROR, "Invalid subscription!");
return -1;
}
if(!getSubInfoFromHeader(extra_headers, subInfo))
getSubInfoFromNodes(nodes, stream_rules, time_rules, subInfo);
if(strSub.find("ssd://") == 0)
{
getSubInfoFromSSD(strSub, subInfo);
}
else
{
if(!getSubInfoFromHeader(extra_headers, subInfo))
getSubInfoFromNodes(nodes, stream_rules, time_rules, subInfo);
}
filterNodes(nodes, exclude_remarks, include_remarks, groupID);
for(nodeInfo &x : nodes)
x.groupID = groupID;
@@ -97,7 +104,14 @@ int addNodes(std::string link, std::vector<nodeInfo> &allNodes, int groupID, std
writeLog(LOG_TYPE_ERROR, "Invalid configuration file!");
return -1;
}
getSubInfoFromNodes(nodes, stream_rules, time_rules, subInfo);
if(strSub.find("ssd://") == 0)
{
getSubInfoFromSSD(strSub, subInfo);
}
else
{
getSubInfoFromNodes(nodes, stream_rules, time_rules, subInfo);
}
filterNodes(nodes, exclude_remarks, include_remarks, groupID);
for(nodeInfo &x : nodes)
x.groupID = groupID;
+21 -2
View File
@@ -1408,7 +1408,6 @@ int explodeConf(std::string filepath, std::string custom_port, int local_port, b
int explodeConfContent(std::string content, std::string custom_port, int local_port, bool sslibev, bool ssrlibev, std::vector<nodeInfo> &nodes)
{
int filetype = -1;
std::vector<nodeInfo>::iterator iter;
if(strFind(content, "\"version\""))
filetype = SPEEDTEST_MESSAGE_FOUNDSS;
@@ -1702,7 +1701,27 @@ bool getSubInfoFromNodes(std::vector<nodeInfo> &nodes, string_array &stream_rule
//calculate expire time
expire = dateStringToTimestamp(time_info);
if(expire != 0)
if(expire)
result += " expire=" + std::to_string(expire) + ";";
return true;
}
bool getSubInfoFromSSD(std::string &sub, std::string &result)
{
rapidjson::Document json;
json.Parse(urlsafe_base64_decode(sub.substr(6)).data());
if(json.HasParseError())
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())
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) + ";";
expire = dateStringToTimestamp(regReplace(expire_str, "(\\d+)-(\\d+)-(\\d+) (.*)", "$1:$2:$3:$4"));
if(expire)
result += " expire=" + std::to_string(expire) + ";";
return true;
+1
View File
@@ -27,5 +27,6 @@ bool chkIgnore(const nodeInfo &node, string_array &exclude_remarks, string_array
void filterNodes(std::vector<nodeInfo> &nodes, string_array &exclude_remarks, string_array &include_remarks, int groupID);
bool getSubInfoFromHeader(std::string &header, std::string &result);
bool getSubInfoFromNodes(std::vector<nodeInfo> &nodes, string_array &stream_rules, string_array &time_rules, std::string &result);
bool getSubInfoFromSSD(std::string &sub, std::string &result);
#endif // SPEEDTESTUTIL_H_INCLUDED