-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathDatabase.cpp
394 lines (350 loc) · 13.3 KB
/
Database.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "Database.hpp"
#include "fmt/includes.h"
#include <QFile>
#include <QDir>
#include <QColor>
namespace NekoGui {
ProfileManager *profileManager = new ProfileManager();
ProfileManager::ProfileManager() : JsonStore("groups/pm.json") {
_add(new configItem("groups", &groupsTabOrder, itemType::integerList));
}
QList<int> filterIntJsonFile(const QString &path) {
QList<int> result;
QDir dr(path);
auto entryList = dr.entryList(QDir::Files);
for (auto e: entryList) {
e = e.toLower();
if (!e.endsWith(".json", Qt::CaseInsensitive)) continue;
e = e.remove(".json", Qt::CaseInsensitive);
bool ok;
auto id = e.toInt(&ok);
if (ok) {
result << id;
}
}
std::sort(result.begin(), result.end());
return result;
}
void ProfileManager::LoadManager() {
JsonStore::Load();
//
profiles = {};
groups = {};
profilesIdOrder = filterIntJsonFile("profiles");
groupsIdOrder = filterIntJsonFile("groups");
// Load Proxys
QList<int> delProfile;
for (auto id: profilesIdOrder) {
auto ent = LoadProxyEntity(QStringLiteral("profiles/%1.json").arg(id));
// Corrupted profile?
if (ent == nullptr || ent->bean == nullptr || ent->bean->version == -114514) {
delProfile << id;
continue;
}
profiles[id] = ent;
}
// Clear Corrupted profile
for (auto id: delProfile) {
DeleteProfile(id);
}
// Load Groups
auto loadedOrder = groupsTabOrder;
groupsTabOrder = {};
for (auto id: groupsIdOrder) {
auto ent = LoadGroup(QStringLiteral("groups/%1.json").arg(id));
// Corrupted group?
if (ent->id != id) {
continue;
}
// Ensure order contains every group
if (!loadedOrder.contains(id)) {
loadedOrder << id;
}
groups[id] = ent;
}
// Ensure groups contains order
for (auto id: loadedOrder) {
if (groups.count(id)) {
groupsTabOrder << id;
}
}
// First setup
if (groups.empty()) {
auto defaultGroup = NekoGui::ProfileManager::NewGroup();
defaultGroup->name = QObject::tr("Default");
NekoGui::profileManager->AddGroup(defaultGroup);
}
//
if (dataStore->flag_reorder) {
{
// remove all (contains orphan)
for (const auto &profile: profiles) {
QFile::remove(profile.second->fn);
}
}
std::map<int, int> gidOld2New;
{
int i = 0;
int ii = 0;
QList<int> newProfilesIdOrder;
std::map<int, std::shared_ptr<ProxyEntity>> newProfiles;
for (auto gid: groupsTabOrder) {
auto group = GetGroup(gid);
gidOld2New[gid] = ii++;
for (auto const &profile: group->ProfilesWithOrder()) {
auto oldId = profile->id;
auto newId = i++;
profile->id = newId;
profile->gid = gidOld2New[gid];
profile->fn = QStringLiteral("profiles/%1.json").arg(newId);
profile->Save();
newProfiles[newId] = profile;
newProfilesIdOrder << newId;
}
group->order = {};
group->Save();
}
profiles = newProfiles;
profilesIdOrder = newProfilesIdOrder;
}
{
QList<int> newGroupsIdOrder;
std::map<int, std::shared_ptr<Group>> newGroups;
for (auto oldGid: groupsTabOrder) {
auto newId = gidOld2New[oldGid];
auto group = groups[oldGid];
QFile::remove(group->fn);
group->id = newId;
group->fn = QStringLiteral("groups/%1.json").arg(newId);
group->Save();
newGroups[newId] = group;
newGroupsIdOrder << newId;
}
groups = newGroups;
groupsIdOrder = newGroupsIdOrder;
groupsTabOrder = newGroupsIdOrder;
}
MessageBoxInfo(software_name, "Profiles and groups reorder complete.");
}
}
void ProfileManager::SaveManager() {
JsonStore::Save();
}
std::shared_ptr<ProxyEntity> ProfileManager::LoadProxyEntity(const QString &jsonPath) {
// Load type
ProxyEntity ent0(nullptr, nullptr);
ent0.fn = jsonPath;
auto validJson = ent0.Load();
auto type = ent0.type;
// Load content
std::shared_ptr<ProxyEntity> ent;
bool validType = validJson;
if (validType) {
ent = NewProxyEntity(type);
validType = ent->bean->version != -114514;
}
if (validType) {
ent->load_control_must = true;
ent->fn = jsonPath;
ent->Load();
}
return ent;
}
// 新建的不给 fn 和 id
std::shared_ptr<ProxyEntity> ProfileManager::NewProxyEntity(const QString &type) {
NekoGui_fmt::AbstractBean *bean;
if (type == "socks") {
bean = new NekoGui_fmt::SocksHttpBean(NekoGui_fmt::SocksHttpBean::type_Socks5);
} else if (type == "http") {
bean = new NekoGui_fmt::SocksHttpBean(NekoGui_fmt::SocksHttpBean::type_HTTP);
} else if (type == "shadowsocks") {
bean = new NekoGui_fmt::ShadowSocksBean();
} else if (type == "chain") {
bean = new NekoGui_fmt::ChainBean();
} else if (type == "vmess") {
bean = new NekoGui_fmt::VMessBean();
} else if (type == "trojan") {
bean = new NekoGui_fmt::TrojanVLESSBean(NekoGui_fmt::TrojanVLESSBean::proxy_Trojan);
} else if (type == "vless") {
bean = new NekoGui_fmt::TrojanVLESSBean(NekoGui_fmt::TrojanVLESSBean::proxy_VLESS);
} else if (type == "naive") {
bean = new NekoGui_fmt::NaiveBean();
} else if (type == "hysteria2") {
bean = new NekoGui_fmt::QUICBean(NekoGui_fmt::QUICBean::proxy_Hysteria2);
} else if (type == "tuic") {
bean = new NekoGui_fmt::QUICBean(NekoGui_fmt::QUICBean::proxy_TUIC);
} else if (type == "custom") {
bean = new NekoGui_fmt::CustomBean();
} else {
bean = new NekoGui_fmt::AbstractBean(-114514);
}
auto ent = std::make_shared<ProxyEntity>(bean, type);
return ent;
}
std::shared_ptr<Group> ProfileManager::NewGroup() {
auto ent = std::make_shared<Group>();
return ent;
}
// ProxyEntity
ProxyEntity::ProxyEntity(NekoGui_fmt::AbstractBean *bean, const QString &type_) {
if (type_ != nullptr) this->type = type_;
_add(new configItem("type", &type, itemType::string));
_add(new configItem("id", &id, itemType::integer));
_add(new configItem("gid", &gid, itemType::integer));
_add(new configItem("yc", &latency, itemType::integer));
_add(new configItem("report", &full_test_report, itemType::string));
// 可以不关联 bean,只加载 ProxyEntity 的信息
if (bean != nullptr) {
this->bean = std::shared_ptr<NekoGui_fmt::AbstractBean>(bean);
// 有虚函数就要在这里 dynamic_cast
_add(new configItem("bean", dynamic_cast<JsonStore *>(bean), itemType::jsonStore));
_add(new configItem("traffic", dynamic_cast<JsonStore *>(traffic_data.get()), itemType::jsonStore));
}
};
QString ProxyEntity::DisplayLatency() const {
if (latency < 0) {
return QObject::tr("Unavailable");
} else if (latency > 0) {
return UNICODE_LRO + QStringLiteral("%1 ms").arg(latency);
} else {
return "";
}
}
QColor ProxyEntity::DisplayLatencyColor() const {
if (latency < 0) {
return Qt::red;
} else if (latency > 0) {
auto greenMs = dataStore->test_latency_url.startsWith("https://") ? 200 : 100;
if (latency < greenMs) {
return Qt::darkGreen;
} else {
return Qt::darkYellow;
}
} else {
return {};
}
}
// Profile
int ProfileManager::NewProfileID() const {
if (profiles.empty()) {
return 0;
} else {
return profilesIdOrder.last() + 1;
}
}
bool ProfileManager::AddProfile(const std::shared_ptr<ProxyEntity> &ent, int gid) {
if (ent->id >= 0) {
return false;
}
ent->gid = gid < 0 ? dataStore->current_group : gid;
ent->id = NewProfileID();
profiles[ent->id] = ent;
profilesIdOrder.push_back(ent->id);
ent->fn = QStringLiteral("profiles/%1.json").arg(ent->id);
ent->Save();
return true;
}
void ProfileManager::DeleteProfile(int id) {
if (id < 0) return;
if (dataStore->started_id == id) return;
profiles.erase(id);
profilesIdOrder.removeAll(id);
QFile(QStringLiteral("profiles/%1.json").arg(id)).remove();
}
void ProfileManager::MoveProfile(const std::shared_ptr<ProxyEntity> &ent, int gid) {
if (gid == ent->gid || gid < 0) return;
auto oldGroup = GetGroup(ent->gid);
if (oldGroup != nullptr && !oldGroup->order.isEmpty()) {
oldGroup->order.removeAll(ent->id);
oldGroup->Save();
}
auto newGroup = GetGroup(gid);
if (newGroup != nullptr && !newGroup->order.isEmpty()) {
newGroup->order.push_back(ent->id);
newGroup->Save();
}
ent->gid = gid;
ent->Save();
}
std::shared_ptr<ProxyEntity> ProfileManager::GetProfile(int id) {
return profiles.count(id) ? profiles[id] : nullptr;
}
// Group
Group::Group() {
_add(new configItem("id", &id, itemType::integer));
_add(new configItem("front_proxy_id", &front_proxy_id, itemType::integer));
_add(new configItem("archive", &archive, itemType::boolean));
_add(new configItem("skip_auto_update", &skip_auto_update, itemType::boolean));
_add(new configItem("name", &name, itemType::string));
_add(new configItem("order", &order, itemType::integerList));
_add(new configItem("url", &url, itemType::string));
_add(new configItem("info", &info, itemType::string));
_add(new configItem("lastup", &sub_last_update, itemType::integer64));
_add(new configItem("manually_column_width", &manually_column_width, itemType::boolean));
_add(new configItem("column_width", &column_width, itemType::integerList));
}
std::shared_ptr<Group> ProfileManager::LoadGroup(const QString &jsonPath) {
auto ent = std::make_shared<Group>();
ent->fn = jsonPath;
ent->Load();
return ent;
}
int ProfileManager::NewGroupID() const {
if (groups.empty()) {
return 0;
} else {
return groupsIdOrder.last() + 1;
}
}
bool ProfileManager::AddGroup(const std::shared_ptr<Group> &ent) {
if (ent->id >= 0) {
return false;
}
ent->id = NewGroupID();
groups[ent->id] = ent;
groupsIdOrder.push_back(ent->id);
groupsTabOrder.push_back(ent->id);
ent->fn = QStringLiteral("groups/%1.json").arg(ent->id);
ent->Save();
return true;
}
void ProfileManager::DeleteGroup(int gid) {
if (groups.size() <= 1) return;
QList<int> toDelete;
for (const auto &[id, profile]: profiles) {
if (profile->gid == gid) toDelete += id; // map访问中,不能操作
}
for (const auto &id: toDelete) {
DeleteProfile(id);
}
groups.erase(gid);
groupsIdOrder.removeAll(gid);
groupsTabOrder.removeAll(gid);
QFile(QStringLiteral("groups/%1.json").arg(gid)).remove();
}
std::shared_ptr<Group> ProfileManager::GetGroup(int id) {
return groups.count(id) ? groups[id] : nullptr;
}
std::shared_ptr<Group> ProfileManager::CurrentGroup() {
return GetGroup(dataStore->current_group);
}
QList<std::shared_ptr<ProxyEntity>> Group::Profiles() const {
QList<std::shared_ptr<ProxyEntity>> ret;
for (const auto &[_, profile]: profileManager->profiles) {
if (id == profile->gid) ret += profile;
}
return ret;
}
QList<std::shared_ptr<ProxyEntity>> Group::ProfilesWithOrder() const {
if (order.isEmpty()) {
return Profiles();
} else {
QList<std::shared_ptr<ProxyEntity>> ret;
for (auto _id: order) {
auto ent = profileManager->GetProfile(_id);
if (ent != nullptr) ret += ent;
}
return ret;
}
}
} // namespace NekoGui