Skip to content

Commit 0d86b3f

Browse files
committed
feat: add mode support to poke and peek
1 parent ff37be4 commit 0d86b3f

File tree

1 file changed

+193
-157
lines changed

1 file changed

+193
-157
lines changed

sys-botbaseplus/source/http.cpp

Lines changed: 193 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -14,182 +14,218 @@ void Http::registerRoutes(httplib::Server *svr)
1414
{
1515
svr->Get("/version", [](const httplib::Request &req, httplib::Response &res)
1616
{
17-
if (validateAuthentication(req, res) == false)
18-
return;
17+
if (validateAuthentication(req, res) == false)
18+
return;
1919

20-
std::shared_ptr<Document> d = Json::createDocument();
20+
std::shared_ptr<Document> d = Json::createDocument();
2121

22-
d->AddMember("version", Constants::version, d->GetAllocator());
22+
d->AddMember("version", Constants::version, d->GetAllocator());
2323

24-
setContent(res, Json::toString(d));
25-
});
24+
setContent(res, Json::toString(d));
25+
});
2626

2727
svr->Get("/healthz", [](const httplib::Request &req, httplib::Response &res)
2828
{
29-
if (validateAuthentication(req, res) == false)
30-
return;
29+
if (validateAuthentication(req, res) == false)
30+
return;
3131

32-
std::shared_ptr<Document> d = Json::createDocument();
32+
std::shared_ptr<Document> d = Json::createDocument();
3333

34-
d->AddMember("status", "UP", d->GetAllocator());
34+
d->AddMember("status", "UP", d->GetAllocator());
3535

36-
setContent(res, Json::toString(d));
37-
});
36+
setContent(res, Json::toString(d));
37+
});
3838

3939
svr->Get("/metadata", [](const httplib::Request &req, httplib::Response &res)
4040
{
41-
if (validateAuthentication(req, res) == false)
42-
return;
41+
if (validateAuthentication(req, res) == false)
42+
return;
4343

44-
std::shared_ptr<Document> d = Json::createDocument();
44+
std::shared_ptr<Document> d = Json::createDocument();
4545

46-
Commands::MetaData meta = Commands::getMetaData();
46+
Commands::MetaData meta = Commands::getMetaData();
4747

48-
d->AddMember("titleId", Util::str_fmt("%16lX", meta.titleID), d->GetAllocator());
49-
d->AddMember("buildId", Util::str_fmt("%02x%02x%02x%02x%02x%02x%02x%02x", meta.buildID[0], meta.buildID[1], meta.buildID[2], meta.buildID[3], meta.buildID[4], meta.buildID[5], meta.buildID[6], meta.buildID[7]), d->GetAllocator());
48+
d->AddMember("titleId", Util::str_fmt("%16lX", meta.titleID), d->GetAllocator());
49+
d->AddMember("buildId", Util::str_fmt("%02x%02x%02x%02x%02x%02x%02x%02x", meta.buildID[0], meta.buildID[1], meta.buildID[2], meta.buildID[3], meta.buildID[4], meta.buildID[5], meta.buildID[6], meta.buildID[7]), d->GetAllocator());
5050

51-
setContent(res, Json::toString(d));
52-
});
51+
setContent(res, Json::toString(d));
52+
});
5353

5454
svr->Post("/", [](const httplib::Request &req, httplib::Response &res)
5555
{
56-
if (validateAuthentication(req, res) == false)
57-
return;
58-
59-
std::unique_ptr<Document> document = std::make_unique<Document>();
60-
61-
document->Parse(req.body);
62-
63-
if (document->IsObject() == false)
64-
{
65-
badRequest(res, "Expected Object as Root");
66-
return;
67-
}
68-
69-
if (document->HasMember("command") == false)
70-
{
71-
badRequest(res, "Expected command property to be present");
72-
return;
73-
}
74-
75-
if ((*document)["command"].IsString() == false)
76-
{
77-
badRequest(res, "Expected command property to have type String");
78-
return;
79-
}
80-
81-
std::string command = (*document)["command"].GetString();
82-
83-
auto &f = std::use_facet<std::ctype<char>>(std::locale());
84-
f.tolower(command.data(), command.data() + command.size());
85-
86-
if (command == "press" || command == "click" || command == "release")
87-
{
88-
if (document->HasMember("button") == false)
89-
{
90-
badRequest(res, "Expected button property to be present");
91-
return;
92-
}
93-
94-
if ((*document)["button"].IsString() == false)
95-
{
96-
badRequest(res, "Expected button property to have type String");
97-
return;
98-
}
99-
100-
std::string btn = (*document)["button"].GetString();
101-
102-
f.toupper(btn.data(), btn.data() + btn.size());
103-
104-
HidNpadButton key = Util::parseStringToButton((char *)btn.c_str());
105-
106-
if (command == "press")
107-
Commands::press(key);
108-
else if (command == "click")
109-
Commands::click(key);
110-
else if (command == "release")
111-
Commands::release(key);
112-
}
113-
else if (command == "peek" || command == "poke")
114-
{
115-
if (document->HasMember("offset") == false)
116-
{
117-
badRequest(res, "Expected offset property to be present");
118-
return;
119-
}
120-
121-
if ((*document)["offset"].IsString() == false)
122-
{
123-
badRequest(res, "Expected offset property to have type String");
124-
return;
125-
}
126-
127-
u8 *val = NULL;
128-
u64 size = 0;
129-
130-
if (command == "poke")
131-
{
132-
if (document->HasMember("value") == false)
133-
{
134-
badRequest(res, "Expected value property to be present");
135-
return;
136-
}
137-
138-
if ((*document)["value"].IsString() == false)
139-
{
140-
badRequest(res, "Expected value property to have type String");
141-
return;
142-
}
143-
144-
val = Util::parseStringToByteBuffer((char *)(*document)["value"].GetString(), &size);
145-
}
146-
else
147-
{
148-
if (document->HasMember("size") == false)
149-
{
150-
badRequest(res, "Expected size property to be present");
151-
return;
152-
}
153-
154-
if ((*document)["size"].IsUint64() == false)
155-
{
156-
badRequest(res, "Expected size property to have type uint64");
157-
return;
158-
}
159-
}
160-
161-
Commands::MetaData meta = Commands::getMetaData();
162-
u64 offset = Util::parseStringToInt((char *)(*document)["offset"].GetString());
163-
164-
if (command == "poke")
165-
{
166-
Commands::poke(meta.heap_base + offset, size, val);
167-
168-
delete val;
169-
}
170-
else
171-
{
172-
size = (*document)["size"].GetUint64();
173-
174-
std::string str = Commands::peekReturn(meta.heap_base + offset, size);
175-
176-
std::shared_ptr<Document> d = Json::createDocument();
177-
178-
d->AddMember("value", str, d->GetAllocator());
179-
180-
setContent(res, Json::toString(d));
181-
182-
return;
183-
}
184-
}
185-
else
186-
{
187-
badRequest(res, "Invalid Command");
188-
return;
189-
}
190-
191-
res.status = 204;
192-
});
56+
if (validateAuthentication(req, res) == false)
57+
return;
58+
59+
std::unique_ptr<Document> document = std::make_unique<Document>();
60+
61+
document->Parse(req.body);
62+
63+
if (document->IsObject() == false)
64+
{
65+
badRequest(res, "Expected Object as Root");
66+
return;
67+
}
68+
69+
if (document->HasMember("command") == false)
70+
{
71+
badRequest(res, "Expected command property to be present");
72+
return;
73+
}
74+
75+
if ((*document)["command"].IsString() == false)
76+
{
77+
badRequest(res, "Expected command property to have type String");
78+
return;
79+
}
80+
81+
std::string command = (*document)["command"].GetString();
82+
83+
auto &f = std::use_facet<std::ctype<char>>(std::locale());
84+
f.tolower(command.data(), command.data() + command.size());
85+
86+
if (command == "press" || command == "click" || command == "release")
87+
{
88+
if (document->HasMember("button") == false)
89+
{
90+
badRequest(res, "Expected button property to be present");
91+
return;
92+
}
93+
94+
if ((*document)["button"].IsString() == false)
95+
{
96+
badRequest(res, "Expected button property to have type String");
97+
return;
98+
}
99+
100+
std::string btn = (*document)["button"].GetString();
101+
102+
f.toupper(btn.data(), btn.data() + btn.size());
103+
104+
HidNpadButton key = Util::parseStringToButton((char *)btn.c_str());
105+
106+
if (command == "press")
107+
Commands::press(key);
108+
else if (command == "click")
109+
Commands::click(key);
110+
else if (command == "release")
111+
Commands::release(key);
112+
}
113+
else if (command == "peek" || command == "poke")
114+
{
115+
if (document->HasMember("offset") == false)
116+
{
117+
badRequest(res, "Expected offset property to be present");
118+
return;
119+
}
120+
121+
if ((*document)["offset"].IsString() == false)
122+
{
123+
badRequest(res, "Expected offset property to have type String");
124+
return;
125+
}
126+
127+
std::string mode = "heap";
128+
129+
if (document->HasMember("mode") == true)
130+
{
131+
mode = (*document)["mode"].GetString();
132+
if (mode != "heap" && mode != "main" && mode != "absolute")
133+
{
134+
badRequest(res, "Mode needs to be either \"heap\", \"main\" or \"absolute\"");
135+
return;
136+
}
137+
}
138+
139+
u8 *val = NULL;
140+
u64 size = 0;
141+
142+
if (command == "poke")
143+
{
144+
if (document->HasMember("value") == false)
145+
{
146+
badRequest(res, "Expected value property to be present");
147+
return;
148+
}
149+
150+
if ((*document)["value"].IsString() == false)
151+
{
152+
badRequest(res, "Expected value property to have type String");
153+
return;
154+
}
155+
156+
val = Util::parseStringToByteBuffer((char *)(*document)["value"].GetString(), &size);
157+
}
158+
else
159+
{
160+
if (document->HasMember("size") == false)
161+
{
162+
badRequest(res, "Expected size property to be present");
163+
return;
164+
}
165+
166+
if ((*document)["size"].IsUint64() == false)
167+
{
168+
badRequest(res, "Expected size property to have type uint64");
169+
return;
170+
}
171+
}
172+
173+
Commands::MetaData meta = Commands::getMetaData();
174+
u64 offset = Util::parseStringToInt((char *)(*document)["offset"].GetString());
175+
176+
if (command == "poke")
177+
{
178+
if (mode == "heap")
179+
{
180+
Commands::poke(meta.heap_base + offset, size, val);
181+
}
182+
else if (mode == "main")
183+
{
184+
Commands::poke(meta.main_nso_base + offset, size, val);
185+
}
186+
else if (mode == "absolute")
187+
{
188+
Commands::poke(offset, size, val);
189+
}
190+
191+
delete val;
192+
}
193+
else
194+
{
195+
size = (*document)["size"].GetUint64();
196+
197+
std::string peekRes = "";
198+
199+
if (mode == "heap")
200+
{
201+
peekRes = Commands::peekReturn(meta.heap_base + offset, size);
202+
}
203+
else if (mode == "main")
204+
{
205+
peekRes = Commands::peekReturn(meta.main_nso_base + offset, size);
206+
}
207+
else if (mode == "absolute")
208+
{
209+
peekRes = Commands::peekReturn(offset, size);
210+
}
211+
212+
std::shared_ptr<Document> d = Json::createDocument();
213+
214+
d->AddMember("value", peekRes, d->GetAllocator());
215+
216+
setContent(res, Json::toString(d));
217+
218+
return;
219+
}
220+
}
221+
else
222+
{
223+
badRequest(res, "Invalid Command");
224+
return;
225+
}
226+
227+
res.status = 204;
228+
});
193229
}
194230

195231
bool Http::validateAuthentication(const httplib::Request &req, httplib::Response &res)

0 commit comments

Comments
 (0)