Add canonical_origin config option

This commit is contained in:
blankie 2023-06-04 12:08:49 +07:00
parent 42da969ac5
commit 6aeba6acb6
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
5 changed files with 10 additions and 1 deletions

View File

@ -16,6 +16,7 @@ liking. Here's a list of what they are:
- `bind_host` (string): What address to bind to - `bind_host` (string): What address to bind to
- `bind_port` (zero or positive integer): What port to bind to - `bind_port` (zero or positive integer): What port to bind to
- `image_proxy_url` (string): URL to proxy images to (see https://pixiv.cat/reverseproxy.html), a trailing slash is not needed - `image_proxy_url` (string): URL to proxy images to (see https://pixiv.cat/reverseproxy.html), a trailing slash is not needed
- `canonical_origin` (string or null): A fallback canonical origin if set, useful if you're, say, running Pixwhile behind Ngrok
Pixwhile is intended to be run behind a reverse proxy (e.g. Nginx), so you Pixwhile is intended to be run behind a reverse proxy (e.g. Nginx), so you
should set your reverse proxy to proxy requests to should set your reverse proxy to proxy requests to

View File

@ -17,4 +17,7 @@ void from_json(const nlohmann::json& j, Config& config) {
throw std::invalid_argument("Invalid port to bind to: "s + std::to_string(config.bind_port)); throw std::invalid_argument("Invalid port to bind to: "s + std::to_string(config.bind_port));
} }
config.image_proxy_url = j.at("image_proxy_url").get<std::string>(); config.image_proxy_url = j.at("image_proxy_url").get<std::string>();
if (j.contains("canonical_origin") && j["canonical_origin"].is_string()) {
config.canonical_origin = j["canonical_origin"].get<std::string>();
}
} }

View File

@ -8,6 +8,7 @@ struct Config {
std::string bind_host = "127.0.0.1"; std::string bind_host = "127.0.0.1";
int bind_port = 8080; int bind_port = 8080;
blankie::murl::Url image_proxy_url{"https://i.pixiv.cat"}; blankie::murl::Url image_proxy_url{"https://i.pixiv.cat"};
std::optional<std::string> canonical_origin;
}; };
Config load_config(const char* path); Config load_config(const char* path);

View File

@ -1,5 +1,6 @@
{ {
"bind_host": "127.0.0.1", "bind_host": "127.0.0.1",
"bind_port": 8080, "bind_port": 8080,
"image_proxy_url": "https://i.pixiv.cat" "image_proxy_url": "https://i.pixiv.cat",
"canonical_origin": null
} }

View File

@ -79,6 +79,9 @@ std::string get_origin(const httplib::Request& req, const Config& config) {
if (req.has_header("X-Canonical-Origin")) { if (req.has_header("X-Canonical-Origin")) {
return req.get_header_value("X-Canonical-Origin"); return req.get_header_value("X-Canonical-Origin");
} }
if (config.canonical_origin) {
return *config.canonical_origin;
}
std::string origin = "http://"; std::string origin = "http://";
if (req.has_header("Host")) { if (req.has_header("Host")) {