mu5h1e
mu5h1e
C++ Backend - Remote Script Execution
For some context im working on this leetcode type application im trying to write where people can post their own questions, submit solutions, leaderboards etc. The user will be presented with a question, some examples and a function prototype they need to implement for the solution. I've come pretty far along but now I just need to handle running the scripts securely, without too much overhead. Having some sort of test case generation (I'll implement the whole TLE and Space Limit stuff later) Now what I'm thinking for the "secure" part is to have a pool of docker containers or something, I use when available. For running the Python scripts I'm thinking of having some sort of Python wrapper that takes in the script's path imports the function and runs it against our generated test cases. Now for our test case generation, I was thinking of having like a config file type thing the user fills out to determine what kind of inputs and outputs to expect. But this all still feels like a lot of work for a person to do to post a question. (question body, correct answer script and a config defining function prototype, return types etc). Idk, what I'm getting at is this just feels like a bit of a shitty way to approach this problem and was looking for alternate POVs to how I can implement something like this. https://github.com/mush1e/RCE P.S (Don't ask me why it's in C++, I'm a bit special in the head)
38 replies
C++ backend URL Routing
currently im writing this web app with my backend being in c++ basically after parsing the http request string I just have a bunch of conditional statements calling relevant "controllers" to handle actions for example
c++
...
else if(req.URI == "/logout") {
handle_logout(req, client_socket);
}

else if (req.URI.find("/search") == 0) {
std::unordered_map<std::string, std::string> params = parse_parameters(req.URI);
if (params.find("query") != params.end())
handle_search(req, client_socket, params["query"]);
else
sendNotFoundResponse(client_socket);
}
...etc
c++
...
else if(req.URI == "/logout") {
handle_logout(req, client_socket);
}

else if (req.URI.find("/search") == 0) {
std::unordered_map<std::string, std::string> params = parse_parameters(req.URI);
if (params.find("query") != params.end())
handle_search(req, client_socket, params["query"]);
else
sendNotFoundResponse(client_socket);
}
...etc
with controllers which are defined like bellow with some logic
c++
void handle_logout(HTTPRequest& req, int client_socket);
void handle_search(HTTPRequest& req, int client_socket, std::string query);
c++
void handle_logout(HTTPRequest& req, int client_socket);
void handle_search(HTTPRequest& req, int client_socket, std::string query);
rather than having an increasingly large if else tree for every route im thinking of something like this
c++
class Router {
std::unordered_map<std::string, std::unordered_map<std::string, std::function<void()>>> router {};
};
c++
class Router {
std::unordered_map<std::string, std::unordered_map<std::string, std::function<void()>>> router {};
};
where router[URL][http method] will store the relevant controller now the major issue im having is not all controllers have the same number of arguments would anyone be able to help me with a design for my router class to circumvent this?
21 replies
Uniformly Distributed Random Function in c?
Trying to implement a function to approximate the value of pi, for which to be accurate I need uniformly distributed random numbers(float). Any hints on how I would go about implementing that?
13 replies