Skip to content
API Documentation
BastionGuard™
ProxyConfig Namespace Reference

Classes

struct  Settings

Enumerations

enum class  DE {
  GNOME , KDE , XFCE , MATE ,
  CINNAMON , LXQT , OTHER
}

Functions

static bool run (const std::vector< std::string > &args, bool silent=false)
static DE detect ()
static void gnome_set (const Settings &s)
static void gnome_clear ()
static void kde_refresh ()
static void kde_set (const Settings &s)
static void kde_clear ()
static void xfce_set (const Settings &s)
static void xfce_clear ()
static void mate_set (const Settings &s)
static void mate_clear ()
static void lxqt_set (const Settings &s)
static void env_set (const Settings &s)
static void env_clear ()
static void apply (const Settings &s)
static void restore ()

Enumeration Type Documentation

◆ DE

enum class ProxyConfig::DE
strong
Enumerator
GNOME 
KDE 
XFCE 
MATE 
CINNAMON 
LXQT 
OTHER 

Definition at line 405 of file proxy_config.hpp.

Function Documentation

◆ apply()

void ProxyConfig::apply ( const Settings & s)
static

Definition at line 571 of file proxy_config.hpp.

571 {
572 // Validate addresses before persisting them to configuration files.
573 const bool valid_host = !s.host.empty() && s.host.size() <= 253 &&
574 std::all_of(s.host.begin(), s.host.end(), [](unsigned char c) {
575 return std::isalnum(c) || c == '.' || c == '-' || c == ':' || c == '[' || c == ']';
576 });
577 if (!valid_host || s.port < 1 || s.port > 65535) {
578 std::cerr << "[proxy-config] refusing unsafe host or port\n";
579 return;
580 }
581 char msg[256];
582 std::snprintf(msg, sizeof(msg), _("[proxy-config] setting system proxy → %s:%d\n"),
583 s.host.c_str(), s.port);
584 std::cerr << msg;
585
586 env_set(s);
587
588 switch (detect()) {
589 case DE::GNOME:
590 case DE::CINNAMON: gnome_set(s); break;
591 case DE::KDE: kde_set(s); break;
592 case DE::XFCE: xfce_set(s); break;
593 case DE::MATE: gnome_set(s); mate_set(s); break;
594 case DE::LXQT: lxqt_set(s); break;
595 case DE::OTHER: break;
596 }
598
599 BrowserProxy::apply({ s.host, s.port });
600}
#define _(s)
static void install_ca_all_browsers()
static void apply(const Settings &s)
static void lxqt_set(const Settings &s)
static void mate_set(const Settings &s)
static void env_set(const Settings &s)
static DE detect()
static void xfce_set(const Settings &s)
static void gnome_set(const Settings &s)
static void kde_set(const Settings &s)

References _, BrowserProxy::apply(), apply(), CINNAMON, detect(), env_set(), GNOME, gnome_set(), ProxyConfig::Settings::host, BrowserProxy::install_ca_all_browsers(), KDE, kde_set(), LXQT, lxqt_set(), MATE, mate_set(), OTHER, ProxyConfig::Settings::port, XFCE, and xfce_set().

Referenced by apply().

◆ detect()

DE ProxyConfig::detect ( )
static

Definition at line 407 of file proxy_config.hpp.

407 {
408 const char* xdg = std::getenv("XDG_CURRENT_DESKTOP");
409 if (xdg) {
410 std::string d(xdg);
411 for (auto& c : d) c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
412 if (d.find("GNOME") != std::string::npos) return DE::GNOME;
413 if (d.find("KDE") != std::string::npos) return DE::KDE;
414 if (d.find("XFCE") != std::string::npos) return DE::XFCE;
415 if (d.find("MATE") != std::string::npos) return DE::MATE;
416 if (d.find("CINNAMON") != std::string::npos) return DE::CINNAMON;
417 if (d.find("LXQT") != std::string::npos) return DE::LXQT;
418 }
419 if (std::getenv("KDE_FULL_SESSION")) return DE::KDE;
420 if (std::getenv("GNOME_DESKTOP_SESSION_ID")) return DE::GNOME;
421 return DE::OTHER;
422}

References CINNAMON, detect(), GNOME, KDE, LXQT, MATE, OTHER, and XFCE.

Referenced by apply(), detect(), and restore().

◆ env_clear()

void ProxyConfig::env_clear ( )
static

Definition at line 553 of file proxy_config.hpp.

553 {
554 unsetenv("http_proxy");
555 unsetenv("HTTP_PROXY");
556 unsetenv("https_proxy");
557 unsetenv("HTTPS_PROXY");
558 unsetenv("all_proxy");
559 unsetenv("ALL_PROXY");
560 unsetenv("no_proxy");
561 unsetenv("NO_PROXY");
562
563 const char* home = std::getenv("HOME");
564 if (home) {
565 std::error_code ec;
566 std::filesystem::remove(
567 std::filesystem::path(home) / ".config/environment.d/bastionguard-proxy.conf", ec);
568 }
569}

References env_clear().

Referenced by env_clear(), and restore().

◆ env_set()

void ProxyConfig::env_set ( const Settings & s)
static

Definition at line 518 of file proxy_config.hpp.

518 {
519 const std::string val = "http://" + s.host + ":" + std::to_string(s.port);
520 const char* nopx = BrowserProxy::LAN_BYPASS_NOCIDR;
521
522 setenv("http_proxy", val.c_str(), 1);
523 setenv("HTTP_PROXY", val.c_str(), 1);
524 setenv("https_proxy", val.c_str(), 1);
525 setenv("HTTPS_PROXY", val.c_str(), 1);
526 setenv("all_proxy", val.c_str(), 1);
527 setenv("ALL_PROXY", val.c_str(), 1);
528 setenv("no_proxy", nopx, 1);
529 setenv("NO_PROXY", nopx, 1);
530
531 const char* home = std::getenv("HOME");
532 if (home) {
533 namespace fs = std::filesystem;
534 const fs::path conf_dir = fs::path(home) / ".config/environment.d";
535 std::error_code ec;
536 fs::create_directories(conf_dir, ec);
537 if (!ec) {
538 std::ofstream f(conf_dir / "bastionguard-proxy.conf");
539 if (f) {
540 f << "http_proxy=" << val << "\n"
541 << "HTTP_PROXY=" << val << "\n"
542 << "https_proxy=" << val << "\n"
543 << "HTTPS_PROXY=" << val << "\n"
544 << "all_proxy=" << val << "\n"
545 << "ALL_PROXY=" << val << "\n"
546 << "no_proxy=" << nopx << "\n"
547 << "NO_PROXY=" << nopx << "\n";
548 }
549 }
550 }
551}
static constexpr const char * LAN_BYPASS_NOCIDR

References env_set(), ProxyConfig::Settings::host, BrowserProxy::LAN_BYPASS_NOCIDR, and ProxyConfig::Settings::port.

Referenced by apply(), and env_set().

◆ gnome_clear()

void ProxyConfig::gnome_clear ( )
static

Definition at line 438 of file proxy_config.hpp.

438 {
439 run({"gsettings", "set", "org.gnome.system.proxy", "mode", "none"});
440}
static bool run(const std::vector< std::string > &args, bool silent=false)

References gnome_clear(), and run().

Referenced by gnome_clear(), and restore().

◆ gnome_set()

void ProxyConfig::gnome_set ( const Settings & s)
static

Definition at line 424 of file proxy_config.hpp.

424 {
425 const std::string port = std::to_string(s.port);
426 run({"gsettings", "set", "org.gnome.system.proxy", "mode", "manual"});
427 for (const char* scheme : {"http", "https"}) {
428 const std::string schema = std::string("org.gnome.system.proxy.") + scheme;
429 run({"gsettings", "set", schema, "host", s.host});
430 run({"gsettings", "set", schema, "port", port});
431 }
432 run({"gsettings", "set", "org.gnome.system.proxy.socks", "host", ""});
433 run({"gsettings", "set", "org.gnome.system.proxy.socks", "port", "0"});
434 run({"gsettings", "set", "org.gnome.system.proxy", "ignore-hosts",
436}
static constexpr const char * LAN_BYPASS_GVARIANT

References gnome_set(), ProxyConfig::Settings::host, BrowserProxy::LAN_BYPASS_GVARIANT, ProxyConfig::Settings::port, and run().

Referenced by apply(), and gnome_set().

◆ kde_clear()

void ProxyConfig::kde_clear ( )
static

Definition at line 466 of file proxy_config.hpp.

466 {
467 for (const char* bin : {"kwriteconfig6", "kwriteconfig5"})
468 (void)run({bin, "--file", "kioslaverc", "--group", "Proxy Settings",
469 "--key", "ProxyType", "0"}, true);
470 kde_refresh();
471}
static void kde_refresh()

References kde_clear(), kde_refresh(), and run().

Referenced by kde_clear(), and restore().

◆ kde_refresh()

void ProxyConfig::kde_refresh ( )
static

Definition at line 442 of file proxy_config.hpp.

442 {
443 // Either kioclient version may be installed; failures are harmless.
444 if (!run({"kioclient6", "clear"}, true))
445 (void)run({"kioclient5", "clear"}, true);
446}

References kde_refresh(), and run().

Referenced by kde_clear(), kde_refresh(), and kde_set().

◆ kde_set()

void ProxyConfig::kde_set ( const Settings & s)
static

Definition at line 448 of file proxy_config.hpp.

448 {
449 const std::string hp = "http://" + s.host + ":" + std::to_string(s.port);
450 for (const char* bin : {"kwriteconfig6", "kwriteconfig5"}) {
451 const std::vector<std::string> common = {bin, "--file", "kioslaverc", "--group", "Proxy Settings"};
452 auto set = [&](const std::string& key, const std::string& val) {
453 std::vector<std::string> args = common;
454 args.insert(args.end(), {"--key", key, val});
455 (void)run(args, true);
456 };
457 set("ProxyType", "1");
458 set("httpProxy", hp);
459 set("httpsProxy", hp);
460 set("NoProxyFor", BrowserProxy::LAN_BYPASS_CIDR);
461 set("ReversedException", "false");
462 }
463 kde_refresh();
464}
static constexpr const char * LAN_BYPASS_CIDR

References ProxyConfig::Settings::host, kde_refresh(), kde_set(), BrowserProxy::LAN_BYPASS_CIDR, ProxyConfig::Settings::port, and run().

Referenced by apply(), and kde_set().

◆ lxqt_set()

void ProxyConfig::lxqt_set ( const Settings & s)
static

Definition at line 503 of file proxy_config.hpp.

503 {
504 const char* home = std::getenv("HOME");
505 if (!home) return;
506
507 namespace fs = std::filesystem;
508 std::ofstream f(fs::path(home) / ".config/lxqt/session.conf", std::ios::app);
509 if (f) {
510 const std::string val = "http://" + s.host + ":" + std::to_string(s.port);
511 f << "\n[Environment]\n"
512 << "http_proxy=" << val << "\n"
513 << "https_proxy=" << val << "\n"
514 << "no_proxy=" << BrowserProxy::LAN_BYPASS_NOCIDR << "\n";
515 }
516}

References ProxyConfig::Settings::host, BrowserProxy::LAN_BYPASS_NOCIDR, lxqt_set(), and ProxyConfig::Settings::port.

Referenced by apply(), and lxqt_set().

◆ mate_clear()

void ProxyConfig::mate_clear ( )
static

Definition at line 499 of file proxy_config.hpp.

499 {
500 (void)run({"gsettings", "set", "org.mate.network-preferences", "proxy-mode", "none"}, true);
501}

References mate_clear(), and run().

Referenced by mate_clear(), and restore().

◆ mate_set()

void ProxyConfig::mate_set ( const Settings & s)
static

Definition at line 489 of file proxy_config.hpp.

489 {
490 const std::string port = std::to_string(s.port);
491 for (const auto& [key, value] : std::vector<std::pair<std::string, std::string>>{
492 {"proxy-mode", "manual"}, {"http-host", s.host}, {"http-port", port},
493 {"https-host", s.host}, {"https-port", port},
494 {"ignore-hosts", BrowserProxy::LAN_BYPASS_GVARIANT}}) {
495 (void)run({"gsettings", "set", "org.mate.network-preferences", key, value}, true);
496 }
497}

References ProxyConfig::Settings::host, BrowserProxy::LAN_BYPASS_GVARIANT, mate_set(), ProxyConfig::Settings::port, and run().

Referenced by apply(), and mate_set().

◆ restore()

void ProxyConfig::restore ( )
static

Definition at line 602 of file proxy_config.hpp.

602 {
603 std::cerr << _("[proxy-config] restoring system proxy\n");
604
605 env_clear();
606
607 switch (detect()) {
608 case DE::GNOME:
609 case DE::CINNAMON: gnome_clear(); break;
610 case DE::KDE: kde_clear(); break;
611 case DE::XFCE: xfce_clear(); break;
612 case DE::MATE: gnome_clear(); mate_clear(); break;
613 case DE::LXQT: break;
614 case DE::OTHER: break;
615 }
616
618}
static void restore()
static void xfce_clear()
static void gnome_clear()
static void mate_clear()
static void env_clear()
static void kde_clear()

References _, CINNAMON, detect(), env_clear(), GNOME, gnome_clear(), KDE, kde_clear(), LXQT, MATE, mate_clear(), OTHER, BrowserProxy::restore(), restore(), XFCE, and xfce_clear().

Referenced by restore().

◆ run()

bool ProxyConfig::run ( const std::vector< std::string > & args,
bool silent = false )
static

Definition at line 397 of file proxy_config.hpp.

397 {
398 const auto result = bg_process::run(args, true, 256U * 1024U);
399 if (!result.ok() && !silent)
400 std::cerr << "[proxy-config] command failed: " << args.front() << " ("
401 << result.exit_code << ")\n";
402 return result.ok();
403}
Result run(const std::vector< std::string > &args, bool merge_stderr=false, std::size_t limit=8U *1024U *1024U, const std::map< std::string, std::string > &environment={}, const std::function< bool(const std::string &)> &on_line={})

References bg_process::run(), and run().

Referenced by gnome_clear(), gnome_set(), kde_clear(), kde_refresh(), kde_set(), mate_clear(), mate_set(), run(), xfce_clear(), and xfce_set().

◆ xfce_clear()

void ProxyConfig::xfce_clear ( )
static

Definition at line 485 of file proxy_config.hpp.

485 {
486 (void)run({"xfconf-query", "-c", "xfce4-settings-editor", "-p", "/Net/ProxyMode", "-s", "none"}, true);
487}

References run(), and xfce_clear().

Referenced by restore(), and xfce_clear().

◆ xfce_set()

void ProxyConfig::xfce_set ( const Settings & s)
static

Definition at line 473 of file proxy_config.hpp.

473 {
474 const std::string port = std::to_string(s.port);
475 (void)run({"xfconf-query", "-c", "xfce4-settings-editor", "-p", "/Net/ProxyMode", "-s", "manual"}, true);
476 for (const char* scheme : {"http", "https"}) {
477 const std::string key = std::string("/Net/") + scheme + "-proxy/";
478 (void)run({"xfconf-query", "-c", "xfce4-settings-editor", "-p", key + "host", "-s", s.host}, true);
479 (void)run({"xfconf-query", "-c", "xfce4-settings-editor", "-p", key + "port", "-s", port}, true);
480 }
481 (void)run({"gsettings", "set", "org.gnome.system.proxy", "ignore-hosts",
483}

References ProxyConfig::Settings::host, BrowserProxy::LAN_BYPASS_GVARIANT, ProxyConfig::Settings::port, run(), and xfce_set().

Referenced by apply(), and xfce_set().