Skip to content
API Documentation
BastionGuard™
BrowserProxy Namespace Reference

Classes

struct  Settings

Functions

static std::string home_dir ()
static void write_file (const fs::path &p, const std::string &content)
static void remove_lines_containing (const fs::path &p, const std::string &needle)
static fs::path firefox_active_profile (const fs::path &profiles_dir)
static void firefox_set_profile (const fs::path &profile, const Settings &s)
static void firefox_clear_profile (const fs::path &profile)
static void configure_firefox_family (const Settings &s, bool clear)
static std::string make_chromium_policy (const Settings &s)
static void chromium_set_policy (const fs::path &dir, const Settings &s)
static void chromium_clear_policy (const fs::path &dir)
static void configure_chromium_family (const Settings &s, bool clear)
static bool nss_contains_ca (const std::string &database)
static void install_ca_firefox_profile (const fs::path &profile)
static void install_ca_firefox_family ()
static void install_ca_nssdb ()
static void install_ca_all_browsers ()
static void apply (const Settings &s)
static void restore ()

Variables

static constexpr const char * LAN_BYPASS_CIDR
static constexpr const char * LAN_BYPASS_NOCIDR
static constexpr const char * LAN_BYPASS_GVARIANT
static constexpr const char * FF_MARKER = "// BastionGuard-proxy"
static constexpr const char * BG_CA_CERT = "/etc/BastionGuard/certs/BastionGuard-ca.crt.pem"
static constexpr const char * BG_CA_NICK = "BastionGuard-CA"

Function Documentation

◆ apply()

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

Definition at line 375 of file proxy_config.hpp.

375 {
376 std::cerr << _("[browser-proxy] configuring browsers\n");
377 configure_firefox_family(s, false);
379}
#define _(s)
static void configure_firefox_family(const Settings &s, bool clear)
static void configure_chromium_family(const Settings &s, bool clear)

References _, apply(), configure_chromium_family(), and configure_firefox_family().

Referenced by apply(), and ProxyConfig::apply().

◆ chromium_clear_policy()

void BrowserProxy::chromium_clear_policy ( const fs::path & dir)
static

Definition at line 275 of file proxy_config.hpp.

275 {
276 std::error_code ec;
277 fs::remove(dir / "bastionguard_proxy.json", ec);
278}

References chromium_clear_policy().

Referenced by chromium_clear_policy(), and configure_chromium_family().

◆ chromium_set_policy()

void BrowserProxy::chromium_set_policy ( const fs::path & dir,
const Settings & s )
static

Definition at line 262 of file proxy_config.hpp.

262 {
263 std::error_code ec;
264 fs::create_directories(dir, ec);
265 if (ec) return;
266
267 write_file(dir / "bastionguard_proxy.json", make_chromium_policy(s));
268
269 char buf[256];
270 std::snprintf(buf, sizeof(buf),
271 _("[browser-proxy] Chromium policy written: %s\n"), dir.c_str());
272 std::cerr << buf;
273}
static std::string make_chromium_policy(const Settings &s)
static void write_file(const fs::path &p, const std::string &content)

References _, chromium_set_policy(), make_chromium_policy(), and write_file().

Referenced by chromium_set_policy(), and configure_chromium_family().

◆ configure_chromium_family()

void BrowserProxy::configure_chromium_family ( const Settings & s,
bool clear )
static

Definition at line 280 of file proxy_config.hpp.

280 {
281 const std::string home = home_dir();
282 if (home.empty()) return;
283
284 const std::vector<fs::path> dirs = {
285 fs::path(home) / ".config/chromium/policies/managed",
286 fs::path(home) / ".config/google-chrome/policies/managed",
287 fs::path(home) / ".config/BraveSoftware/Brave-Browser/policies/managed",
288 fs::path(home) / ".config/microsoft-edge/policies/managed",
289 fs::path(home) / ".config/vivaldi/policies/managed",
290 fs::path(home) / ".config/opera/policies/managed",
291 fs::path(home) / ".var/app/org.chromium.Chromium/config/chromium/policies/managed",
292 fs::path(home) / ".var/app/com.google.Chrome/config/google-chrome/policies/managed",
293 fs::path(home) / ".var/app/com.brave.Browser/config/BraveSoftware/Brave-Browser/policies/managed",
294 fs::path(home) / "snap/chromium/current/.config/chromium/policies/managed",
295 };
296
297 for (const auto& dir : dirs) {
298 if (clear) {
300 } else {
301 if (fs::exists(dir.parent_path().parent_path()))
302 chromium_set_policy(dir, s);
303 }
304 }
305}
static std::string home_dir()
static void chromium_clear_policy(const fs::path &dir)
static void chromium_set_policy(const fs::path &dir, const Settings &s)

References chromium_clear_policy(), chromium_set_policy(), configure_chromium_family(), and home_dir().

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

◆ configure_firefox_family()

void BrowserProxy::configure_firefox_family ( const Settings & s,
bool clear )
static

Definition at line 232 of file proxy_config.hpp.

232 {
233 const std::string home = home_dir();
234 if (home.empty()) return;
235
236 const std::vector<fs::path> roots = {
237 fs::path(home) / ".mozilla/firefox",
238 fs::path(home) / ".librewolf",
239 fs::path(home) / ".waterfox",
240 fs::path(home) / "snap/firefox/common/.mozilla/firefox",
241 fs::path(home) / ".var/app/org.mozilla.firefox/.mozilla/firefox",
242 fs::path(home) / ".var/app/io.gitlab.librewolf-community/.librewolf",
243 };
244
245 for (const auto& root : roots) {
246 if (!fs::exists(root)) continue;
247 const fs::path profile = firefox_active_profile(root);
248 if (profile.empty()) continue;
249 if (clear) firefox_clear_profile(profile);
250 else firefox_set_profile(profile, s);
251 }
252}
static fs::path firefox_active_profile(const fs::path &profiles_dir)
static void firefox_clear_profile(const fs::path &profile)
static void firefox_set_profile(const fs::path &profile, const Settings &s)

References configure_firefox_family(), firefox_active_profile(), firefox_clear_profile(), firefox_set_profile(), and home_dir().

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

◆ firefox_active_profile()

fs::path BrowserProxy::firefox_active_profile ( const fs::path & profiles_dir)
static

Definition at line 128 of file proxy_config.hpp.

128 {
129
130 const fs::path ini = profiles_dir / "profiles.ini";
131 if (!fs::exists(ini)) return {};
132
133 struct Profile {
134 std::string path;
135 bool is_relative = true;
136 bool is_default = false;
137 };
138
139 std::vector<Profile> profiles;
140 std::string install_default;
141 bool in_install = false;
142 bool in_profile = false;
143
144 std::ifstream f(ini);
145 std::string line;
146
147 while (std::getline(f, line)) {
148 line.erase(0, line.find_first_not_of(" \t\r\n"));
149 if (!line.empty()) line.erase(line.find_last_not_of(" \t\r\n") + 1);
150 if (line.empty() || line[0] == ';') continue;
151
152 if (line[0] == '[') {
153 in_install = (line.rfind("[Install", 0) == 0);
154 in_profile = (line.rfind("[Profile", 0) == 0);
155 if (in_profile) profiles.push_back({});
156 } else if (in_install) {
157 if (line.rfind("Default=", 0) == 0)
158 install_default = line.substr(8);
159 } else if (in_profile && !profiles.empty()) {
160 auto& p = profiles.back();
161 if (line.rfind("Default=1", 0) == 0) p.is_default = true;
162 else if (line.rfind("Path=", 0) == 0) p.path = line.substr(5);
163 else if (line.rfind("IsRelative=0", 0) == 0) p.is_relative = false;
164 }
165 }
166 if (!install_default.empty()) {
167 for (const auto& p : profiles) {
168 if (p.path == install_default) {
169 fs::path full = p.is_relative
170 ? profiles_dir / p.path
171 : fs::path(p.path);
172 if (fs::exists(full)) return full;
173 }
174 }
175
176 fs::path full = profiles_dir / install_default;
177 if (fs::exists(full)) return full;
178 }
179 for (const auto& p : profiles) {
180 if (!p.path.empty() && p.path.find("default-release") != std::string::npos) {
181 fs::path full = p.is_relative ? profiles_dir / p.path : fs::path(p.path);
182 if (fs::exists(full)) return full;
183 }
184 }
185
186 for (const auto& p : profiles) {
187 if (p.is_default && !p.path.empty()) {
188 fs::path full = p.is_relative ? profiles_dir / p.path : fs::path(p.path);
189 if (fs::exists(full)) return full;
190 }
191 }
192
193 std::error_code ec;
194 for (auto& e : fs::directory_iterator(profiles_dir, ec))
195 if (e.is_directory() && e.path().filename().string().find('.') != std::string::npos)
196 return e.path();
197
198 return {};
199}

References firefox_active_profile().

Referenced by configure_firefox_family(), firefox_active_profile(), and install_ca_firefox_family().

◆ firefox_clear_profile()

void BrowserProxy::firefox_clear_profile ( const fs::path & profile)
static

Definition at line 227 of file proxy_config.hpp.

227 {
228 if (profile.empty()) return;
229 remove_lines_containing(profile / "user.js", FF_MARKER);
230}
static void remove_lines_containing(const fs::path &p, const std::string &needle)
static constexpr const char * FF_MARKER

References FF_MARKER, firefox_clear_profile(), and remove_lines_containing().

Referenced by configure_firefox_family(), and firefox_clear_profile().

◆ firefox_set_profile()

void BrowserProxy::firefox_set_profile ( const fs::path & profile,
const Settings & s )
static

Definition at line 201 of file proxy_config.hpp.

201 {
202 if (profile.empty() || !fs::exists(profile)) return;
203
204 const fs::path user_js = profile / "user.js";
206
207 std::ofstream f(user_js, std::ios::app);
208 if (!f) return;
209
210 f << "\n" << FF_MARKER << "\n"
211 << "user_pref(\"network.proxy.type\", 1); " << FF_MARKER << "\n"
212 << "user_pref(\"network.proxy.http\", \"" << s.host << "\"); " << FF_MARKER << "\n"
213 << "user_pref(\"network.proxy.http_port\", " << s.port << "); " << FF_MARKER << "\n"
214 << "user_pref(\"network.proxy.ssl\", \"" << s.host << "\"); " << FF_MARKER << "\n"
215 << "user_pref(\"network.proxy.ssl_port\", " << s.port << "); " << FF_MARKER << "\n"
216 << "user_pref(\"network.proxy.no_proxies_on\", \"" << LAN_BYPASS_CIDR << "\"); " << FF_MARKER << "\n"
217 // Importante: Firefox di default proxy-a anche le risoluzioni DNS, e
218 // questo rompe Avahi/mDNS. Forziamo le LAN a NON essere proxate.
219 << "user_pref(\"network.proxy.allow_hijacking_localhost\", false); " << FF_MARKER << "\n";
220
221 char buf[256];
222 std::snprintf(buf, sizeof(buf),
223 _("[browser-proxy] Firefox profile configured: %s\n"), profile.c_str());
224 std::cerr << buf;
225}
static constexpr const char * LAN_BYPASS_CIDR

References _, FF_MARKER, firefox_set_profile(), BrowserProxy::Settings::host, LAN_BYPASS_CIDR, BrowserProxy::Settings::port, and remove_lines_containing().

Referenced by configure_firefox_family(), and firefox_set_profile().

◆ home_dir()

std::string BrowserProxy::home_dir ( )
static

Definition at line 104 of file proxy_config.hpp.

104 {
105 const char* h = std::getenv("HOME");
106 return h ? h : "";
107}

References home_dir().

Referenced by configure_chromium_family(), configure_firefox_family(), home_dir(), install_ca_firefox_family(), and install_ca_nssdb().

◆ install_ca_all_browsers()

void BrowserProxy::install_ca_all_browsers ( )
static

Definition at line 364 of file proxy_config.hpp.

364 {
365 if (!fs::exists(BG_CA_CERT)) {
366 std::cerr << _("[ca-install] CA cert not found, skipping\n");
367 return;
368 }
369
370 std::cerr << _("[ca-install] installing BastionGuard CA in browsers\n");
373}
static constexpr const char * BG_CA_CERT
static void install_ca_firefox_family()
static void install_ca_nssdb()

References _, BG_CA_CERT, install_ca_all_browsers(), install_ca_firefox_family(), and install_ca_nssdb().

Referenced by ProxyConfig::apply(), and install_ca_all_browsers().

◆ install_ca_firefox_family()

void BrowserProxy::install_ca_firefox_family ( )
static

Definition at line 323 of file proxy_config.hpp.

323 {
324 const std::string home = home_dir();
325 if (home.empty()) return;
326
327 const std::vector<fs::path> roots = {
328 fs::path(home) / ".mozilla/firefox",
329 fs::path(home) / ".librewolf",
330 fs::path(home) / ".waterfox",
331 fs::path(home) / "snap/firefox/common/.mozilla/firefox",
332 fs::path(home) / ".var/app/org.mozilla.firefox/.mozilla/firefox",
333 fs::path(home) / ".var/app/io.gitlab.librewolf-community/.librewolf",
334 };
335
336 for (const auto& root : roots) {
337 if (!fs::exists(root)) continue;
338 const fs::path profile = firefox_active_profile(root);
339 if (!profile.empty())
341 }
342}
static void install_ca_firefox_profile(const fs::path &profile)

References firefox_active_profile(), home_dir(), install_ca_firefox_family(), and install_ca_firefox_profile().

Referenced by install_ca_all_browsers(), and install_ca_firefox_family().

◆ install_ca_firefox_profile()

void BrowserProxy::install_ca_firefox_profile ( const fs::path & profile)
static

Definition at line 316 of file proxy_config.hpp.

316 {
317 if (profile.empty() || !fs::exists(profile) || !fs::exists(BG_CA_CERT)) return;
318 if (nss_contains_ca(profile.string())) return;
319 (void)bg_process::run({"certutil", "-A", "-d", profile.string(),
320 "-n", BG_CA_NICK, "-t", "CT,C,C", "-i", BG_CA_CERT});
321}
static bool nss_contains_ca(const std::string &database)
static constexpr const char * BG_CA_NICK
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_CA_CERT, BG_CA_NICK, install_ca_firefox_profile(), nss_contains_ca(), and bg_process::run().

Referenced by install_ca_firefox_family(), and install_ca_firefox_profile().

◆ install_ca_nssdb()

void BrowserProxy::install_ca_nssdb ( )
static

Definition at line 344 of file proxy_config.hpp.

344 {
345 if (!fs::exists(BG_CA_CERT)) return;
346 const std::string home = home_dir();
347 if (home.empty()) return;
348
349 const fs::path nssdb = fs::path(home) / ".pki/nssdb";
350 if (!fs::exists(nssdb)) {
351 std::error_code ec;
352 fs::create_directories(nssdb, ec);
353 if (!ec) (void)bg_process::run({"certutil", "-N", "--empty-password", "-d", nssdb.string()});
354 }
355 for (const fs::path& database : {nssdb,
356 fs::path(home) / "snap/chromium/current/.pki/nssdb"}) {
357 if (fs::exists(database) && !nss_contains_ca(database.string())) {
358 (void)bg_process::run({"certutil", "-A", "-d", database.string(),
359 "-n", BG_CA_NICK, "-t", "CT,C,C", "-i", BG_CA_CERT});
360 }
361 }
362}

References BG_CA_CERT, BG_CA_NICK, home_dir(), install_ca_nssdb(), nss_contains_ca(), and bg_process::run().

Referenced by install_ca_all_browsers(), and install_ca_nssdb().

◆ make_chromium_policy()

std::string BrowserProxy::make_chromium_policy ( const Settings & s)
static

Definition at line 254 of file proxy_config.hpp.

254 {
255 return "{\n"
256 " \"ProxyMode\": \"fixed_servers\",\n"
257 " \"ProxyServer\": \"http://" + s.host + ":" + std::to_string(s.port) + "\",\n"
258 " \"ProxyBypassList\": \"<-loopback>," + std::string(LAN_BYPASS_CIDR) + "\"\n"
259 "}\n";
260}

References BrowserProxy::Settings::host, LAN_BYPASS_CIDR, make_chromium_policy(), and BrowserProxy::Settings::port.

Referenced by chromium_set_policy(), and make_chromium_policy().

◆ nss_contains_ca()

bool BrowserProxy::nss_contains_ca ( const std::string & database)
static

Definition at line 311 of file proxy_config.hpp.

311 {
312 const auto list = bg_process::run({"certutil", "-L", "-d", database});
313 return list.ok() && list.output.find(BG_CA_NICK) != std::string::npos;
314}

References BG_CA_NICK, nss_contains_ca(), bg_process::Result::ok(), and bg_process::run().

Referenced by install_ca_firefox_profile(), install_ca_nssdb(), and nss_contains_ca().

◆ remove_lines_containing()

void BrowserProxy::remove_lines_containing ( const fs::path & p,
const std::string & needle )
static

Definition at line 114 of file proxy_config.hpp.

114 {
115 if (!fs::exists(p)) return;
116 std::ifstream in(p);
117 std::ostringstream out;
118 std::string line;
119 while (std::getline(in, line))
120 if (line.find(needle) == std::string::npos)
121 out << line << "\n";
122 in.close();
123 write_file(p, out.str());
124}

References remove_lines_containing(), and write_file().

Referenced by firefox_clear_profile(), firefox_set_profile(), and remove_lines_containing().

◆ restore()

void BrowserProxy::restore ( )
static

Definition at line 381 of file proxy_config.hpp.

381 {
382 std::cerr << _("[browser-proxy] restoring browser proxy settings\n");
383 configure_firefox_family({}, true);
385}

References _, configure_chromium_family(), configure_firefox_family(), and restore().

Referenced by restore(), and ProxyConfig::restore().

◆ write_file()

void BrowserProxy::write_file ( const fs::path & p,
const std::string & content )
static

Definition at line 109 of file proxy_config.hpp.

109 {
110 std::ofstream f(p);
111 if (f) f << content;
112}

References write_file().

Referenced by chromium_set_policy(), remove_lines_containing(), and write_file().

Variable Documentation

◆ BG_CA_CERT

const char* BrowserProxy::BG_CA_CERT = "/etc/BastionGuard/certs/BastionGuard-ca.crt.pem"
staticconstexpr

◆ BG_CA_NICK

const char* BrowserProxy::BG_CA_NICK = "BastionGuard-CA"
staticconstexpr

Definition at line 308 of file proxy_config.hpp.

Referenced by install_ca_firefox_profile(), install_ca_nssdb(), and nss_contains_ca().

◆ FF_MARKER

const char* BrowserProxy::FF_MARKER = "// BastionGuard-proxy"
staticconstexpr

Definition at line 126 of file proxy_config.hpp.

Referenced by firefox_clear_profile(), and firefox_set_profile().

◆ LAN_BYPASS_CIDR

const char* BrowserProxy::LAN_BYPASS_CIDR
staticconstexpr
Initial value:
=
"localhost,"
"127.0.0.0/8,"
"10.0.0.0/8,"
"172.16.0.0/12,"
"192.168.0.0/16,"
"169.254.0.0/16,"
"100.64.0.0/10,"
"::1,"
"fe80::/10,"
"fc00::/7,"
"*.local,*.lan,*.intranet,*.internal,*.home.arpa,*.corp,*.private"

Definition at line 64 of file proxy_config.hpp.

Referenced by firefox_set_profile(), ProxyConfig::kde_set(), and make_chromium_policy().

◆ LAN_BYPASS_GVARIANT

const char* BrowserProxy::LAN_BYPASS_GVARIANT
staticconstexpr
Initial value:
=
"['localhost',"
"'127.0.0.0/8',"
"'10.0.0.0/8',"
"'172.16.0.0/12',"
"'192.168.0.0/16',"
"'169.254.0.0/16',"
"'100.64.0.0/10',"
"'::1','fe80::/10','fc00::/7',"
"'*.local','*.lan','*.intranet','*.internal','*.home.arpa']"

Definition at line 93 of file proxy_config.hpp.

Referenced by ProxyConfig::gnome_set(), ProxyConfig::mate_set(), and ProxyConfig::xfce_set().

◆ LAN_BYPASS_NOCIDR

const char* BrowserProxy::LAN_BYPASS_NOCIDR
staticconstexpr
Initial value:
=
"localhost,"
"127.0.0.1,"
"::1,"
"10.,"
"192.168.,"
"172.16.,172.17.,172.18.,172.19.,"
"172.20.,172.21.,172.22.,172.23.,"
"172.24.,172.25.,172.26.,172.27.,"
"172.28.,172.29.,172.30.,172.31.,"
"169.254.,"
".local,.lan,.intranet,.internal,.home.arpa,.corp,.private"

Definition at line 79 of file proxy_config.hpp.

Referenced by ProxyConfig::env_set(), and ProxyConfig::lxqt_set().