Skip to content

Commit

Permalink
Implement proxy groups menu
Browse files Browse the repository at this point in the history
  • Loading branch information
ysc3839 committed Mar 30, 2021
1 parent 624d9b9 commit 44a8f36
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 169 deletions.
8 changes: 8 additions & 0 deletions ClashApi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ class ClashApi
return json::parse(res.data).get<ClashProxies>();
}

bool UpdateProxyGroup(std::string_view group, std::string_view selectProxy)
{
std::wstring path = L"/proxies/";
path.append(Utf8ToUtf16(group));
auto res = Request(path.c_str(), L"PUT", { {"name", selectProxy} });
return res.statusCode == 204; // HTTP 204 No Content
}

private:
void Connect()
{
Expand Down
33 changes: 24 additions & 9 deletions ClashModel.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#pragma once

struct ClashProxySpeedHistory
{
// Date time; // FIXME
uint32_t delay;

friend void from_json(const json& j, ClashProxySpeedHistory& value) {
JSON_FROM(delay);
}
};

struct ClashProxy
{
using Name = std::string; // UTF-8
Expand All @@ -24,16 +34,17 @@ struct ClashProxy
};

Name name;
Type type;
Type type = Type::Unknown;
std::vector<Name> all;
// ClashProxySpeedHistory history; // FIXME
std::vector<ClashProxySpeedHistory> history;
std::optional<Name> now;

friend void from_json(const json& j, ClashProxy& value) {
JSON_FROM(name);
JSON_FROM(type);
JSON_TRY_FROM(all);
try { value.now.emplace(j.at("now").get<decltype(now)::value_type>()); } CATCH_LOG();
JSON_FROM(history);
try { value.now.emplace(j.at("now").get<decltype(now)::value_type>()); } catch (...) {}
}
};

Expand All @@ -57,13 +68,17 @@ NLOHMANN_JSON_SERIALIZE_ENUM(ClashProxy::Type, {

struct ClashProxies
{
std::map<ClashProxy::Name, ClashProxy> proxiesMap;
using MapType = std::unordered_map<ClashProxy::Name, ClashProxy>;

MapType proxies;

friend void from_json(const json& j, ClashProxies& value) {
j.at("proxies").get_to(value.proxiesMap);
JSON_FROM(proxies);
}
};

ClashProxies g_clashProxies;

struct ClashProvider
{
using Name = std::string; // UTF-8
Expand Down Expand Up @@ -104,9 +119,9 @@ NLOHMANN_JSON_SERIALIZE_ENUM(ClashProvider::VehicleType, {

struct ClashProviders
{
std::map<ClashProvider::Name, ClashProxy> allProviders;
std::map<ClashProvider::Name, ClashProvider> providers;

friend void from_json(const json& j, ClashProviders& value) {
j.at("providers").get_to(value.allProviders);
}
/*friend void from_json(const json& j, ClashProviders& value) {
JSON_FROM(providers);
}*/
};
Loading

0 comments on commit 44a8f36

Please sign in to comment.