Skip to content

Commit

Permalink
feat: basic pair APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
ABeltramo committed Sep 6, 2024
1 parent 90fa131 commit 2bae3f6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
33 changes: 22 additions & 11 deletions src/moonlight-server/api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class UnixSocketServer {
public:
UnixSocketServer(boost::asio::io_context &io_context,
const std::string &socket_path,
std::shared_ptr<events::EventBusType> event_bus)
: io_context_(io_context), event_bus_(event_bus),
immer::box<state::AppState> app_state)
: io_context_(io_context), app_state(app_state),
acceptor_(io_context, boost::asio::local::stream_protocol::endpoint(socket_path)) {
start_accept();
}
Expand Down Expand Up @@ -86,20 +86,31 @@ class UnixSocketServer {
// curl -v --http1.0 --unix-socket /tmp/wolf.sock http://localhost/api/v1/pending-pair-requests
auto res = rfl::Generic::Object();
res["success"] = true;
// TODO: res["requests"] = pairing_atom->load();
auto requests = std::vector<rfl::Generic>();
for (auto [secret, pair_request] : *app_state->pairing_atom->load()) {
auto pair_request_obj = rfl::Generic::Object();
pair_request_obj["pair_secret"] = secret;
pair_request_obj["client_ip"] = pair_request->client_ip;
requests.push_back(pair_request_obj);
}
res["requests"] = requests;
send_http(socket, 200, rfl::json::write(res));
return;
}

if (req.method == "POST" && req.path == "/api/v1/pair-client") {
// curl -v --http1.0 --unix-socket /tmp/wolf.sock -d '{"pair_secret": "xxxx", "pin": "1234"}'
// http://localhost/api/v1/pair-client
if (auto event = rfl::json::read<rfl::Generic>(req.body)) {
// TODO: auto pair_request = pairing_atom->load()->at(secret);
// TODO: pair_request->user_pin->set_value(pin);
auto res = rfl::Generic::Object();
res["success"] = true;
send_http(socket, 200, rfl::json::write(res));
if (auto event = rfl::json::read<PairClient>(req.body)) {
if (auto pair_request = app_state->pairing_atom->load()->find(event.value().pair_secret)) {
pair_request->get().user_pin->set_value(event.value().pin); // Resolve the promise
auto res = rfl::Generic::Object();
res["success"] = true;
send_http(socket, 200, rfl::json::write(res));
} else {
logs::log(logs::warning, "[API] Invalid pair secret: {}", event.value().pair_secret);
send_http(socket, 404, "");
}
} else {
logs::log(logs::warning, "[API] Invalid event: {}", req.body);
send_http(socket, 500, "");
Expand Down Expand Up @@ -186,7 +197,7 @@ class UnixSocketServer {
boost::asio::io_context &io_context_;
boost::asio::local::stream_protocol::acceptor acceptor_;
std::vector<std::shared_ptr<UnixSocket>> sockets_;
std::shared_ptr<events::EventBusType> event_bus_;
immer::box<state::AppState> app_state;
};

void start_server(immer::box<state::AppState> app_state) {
Expand All @@ -206,7 +217,7 @@ void start_server(immer::box<state::AppState> app_state) {

::unlink(socket_path);
boost::asio::io_context io_context;
UnixSocketServer server(io_context, socket_path, app_state->event_bus);
UnixSocketServer server(io_context, socket_path, app_state);

while (true) {
io_context.run_for(std::chrono::milliseconds(100));
Expand Down
5 changes: 5 additions & 0 deletions src/moonlight-server/api/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ namespace wolf::api {

void start_server(immer::box<state::AppState> app_state);

struct PairClient {
std::string pair_secret;
std::string pin;
};

} // namespace wolf::api
2 changes: 1 addition & 1 deletion src/moonlight-server/rest/servers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void startServer(HttpServer *server, const immer::box<state::AppState> state, in
endpoints::pair<SimpleWeb::HTTP>(resp, req, state);
};

auto pairing_atom = std::make_shared<immer::atom<immer::map<std::string, immer::box<events::PairSignal>>>>();
auto pairing_atom = state->pairing_atom;

server->resource["^/pin/$"]["GET"] = [](auto resp, auto req) { resp->write(pin_html); };
server->resource["^/pin/$"]["POST"] = [pairing_atom](auto resp, auto req) {
Expand Down
7 changes: 6 additions & 1 deletion src/moonlight-server/state/data-structures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,16 @@ struct AppState {
immer::box<Host> host;

/**
* Mutable temporary results in order to achieve the multistep pairing process
* Mutable, temporary results in order to achieve the multistep pairing process
* It's shared between the two HTTP/HTTPS threads
*/
std::shared_ptr<immer::atom<immer::map<std::string, PairCache>>> pairing_cache;

/**
* Mutable, temporary promises to be resolved when the client sends the correct pin
*/
std::shared_ptr<immer::atom<immer::map<std::string, immer::box<events::PairSignal>>>> pairing_atom;

/**
* A shared bus of events so that we can decouple modules
*/
Expand Down
1 change: 1 addition & 0 deletions src/moonlight-server/wolf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ auto initialize(std::string_view config_file, std::string_view pkey_filename, st
.config = config,
.host = host,
.pairing_cache = std::make_shared<immer::atom<immer::map<std::string, state::PairCache>>>(),
.pairing_atom = std::make_shared<immer::atom<immer::map<std::string, immer::box<events::PairSignal>>>>(),
.event_bus = event_bus,
.running_sessions = std::make_shared<immer::atom<immer::vector<events::StreamSession>>>()};
return immer::box<state::AppState>(state);
Expand Down

0 comments on commit 2bae3f6

Please sign in to comment.