Skip to content

Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic

feat(linux): font zoom, auto-clear logs, connection-log filters, start at boot, update check on startup

anof-cyber18da10d52765
diff --git a/Linux/gui/gui.h b/Linux/gui/gui.h
index 37a6642..4c000de 100644
--- a/Linux/gui/gui.h
+++ b/Linux/gui/gui.h
@@ -10,6 +10,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include "ProxyBridge.h"
+#include "pb_profile.h"
typedef struct {
uint32_t id; // library id, 0 = not registered
@@ -43,6 +44,10 @@ typedef struct {
char *default_proxy;
GList *rules;
bool logging, dns, log_every; // traffic logging, DNS via proxy and "log every connection", stored in the profile like the Windows app does
+ bool auto_clear; // wipe a log once it passes 500 lines
+ int zoom; // font zoom in percent, 60-300
+ struct pbp_filter *filters; // connection-log filters (malloc'd strings, freed with the profile)
+ size_t nfilters;
char *saved; // what is on disk, to skip rewriting unchanged profiles
} Profile;
@@ -67,6 +72,9 @@ extern GList *g_rules_list;
extern bool g_chk_logging;
extern bool g_chk_dns;
extern bool g_chk_log_every;
+extern bool g_chk_auto_clear;
+extern int g_zoom;
+extern bool g_chk_updates; // check for a new version when the program starts (settings.json, not per profile)
void show_message(GtkWindow *parent, GtkMessageType type, const char *format, ...);
GtkWidget *combo_new(const char *first, ...) G_GNUC_NULL_TERMINATED;
@@ -82,6 +90,16 @@ char *extract_sub_json_str(const char *json, const char *key);
int rules_from_json(const char *json, RuleImportCb cb, void *ud);
char *rules_to_json(GList *rules, bool only_selected);
+// Filters of the active profile. The edit functions take ownership of the array and refresh the log view.
+struct pbp_filter *gui_filters(size_t *n);
+void gui_filters_replace(struct pbp_filter *filters, size_t n);
+struct pbp_filter pbp_filter_dup(const struct pbp_filter *f);
+void pbp_filter_clear(struct pbp_filter *f);
+void gui_logs_set_filters(const struct pbp_filter *f, size_t n);
+void on_log_filters_clicked(GtkWidget *widget, gpointer data);
+void gui_set_zoom(int percent); // clamps to 60-300, applies and saves
+const char *gui_zoom_css();
+void gui_check_updates_on_startup();
void gui_sync_switches();
void gui_refresh_flows(); // refreshes the Live Connections tab from the engine // pushes the active profile's logging/DNS switches to the engine and the menu
void save_config();
@@ -109,7 +127,7 @@ void refresh_rules_window();
void gui_log(const char *format, ...);
void lib_log_callback(const char *message);
-void lib_connection_callback(const char *process_name, uint32_t pid, const char *dest_ip, uint16_t dest_port, const char *proxy_info);
+void lib_connection_callback_ex(const char *process_name, uint32_t pid, const char *dest_ip, uint16_t dest_port, const char *protocol, const char *proxy_info);
void gui_logs_init();
void gui_logs_stop_timers();
void gui_logs_free();
diff --git a/Linux/gui/gui_boot.c b/Linux/gui/gui_boot.c
new file mode 100644
index 0000000..b146dd2
--- /dev/null
+++ b/Linux/gui/gui_boot.c
@@ -0,0 +1,154 @@
+#include "gui_boot.h"
+#include <fcntl.h>
+#include <glib/gstdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define UNIT_NAME "proxybridge.service"
+
+static const char *unit_dir() {
+#ifdef PB_TEST_HOOKS
+ const char *e = getenv("PROXYBRIDGE_SYSTEMD_DIR"); // tests only
+ if (e && *e) return e;
+#endif
+ return "/etc/systemd/system";
+}
+
+static const char *systemctl_cmd() {
+#ifdef PB_TEST_HOOKS
+ const char *e = getenv("PROXYBRIDGE_SYSTEMCTL");
+ if (e && *e) return e;
+#endif
+ return "systemctl";
+}
+
+static char *unit_path() { return g_strdup_printf("%s/" UNIT_NAME, unit_dir()); }
+
+// The command line program that sits next to this one (an installed pair), otherwise the default install location.
+static char *cli_path() {
+#ifdef PB_TEST_HOOKS
+ const char *e = getenv("PROXYBRIDGE_CLI");
+ if (e && *e) return g_strdup(e);
+#endif
+ char self[4096];
+ ssize_t n = readlink("/proc/self/exe", self, sizeof self - 1);
+ if (n > 0) {
+ self[n] = 0;
+ char *dir = g_path_get_dirname(self), *cli = g_build_filename(dir, "ProxyBridge", NULL);
+ g_free(dir);
+ if (g_file_test(cli, G_FILE_TEST_IS_EXECUTABLE)) return cli;
+ g_free(cli);
+ }
+ return g_strdup("/usr/local/bin/ProxyBridge");
+}
+
+// systemd's own quoting for one ExecStart word: backslash, double quote, % (specifier) and $ (variable) are escaped
+static void quote_word(GString *out, const char *s) {
+ g_string_append_c(out, '"');
+ for (; *s; s++) {
+ if (*s == '\\' || *s == '"') g_string_append_c(out, '\\');
+ else if (*s == '%') g_string_append_c(out, '%');
+ else if (*s == '$') g_string_append_c(out, '$');
+ g_string_append_c(out, *s);
+ }
+ g_string_append_c(out, '"');
+}
+
+char *boot_unit_text(const char *cli, const char *profile, char **error) {
+ for (const char *p = profile; *p; p++)
+ if ((unsigned char)*p < 0x20 || *p == 0x7f) {
+ if (error) *error = g_strdup("the profile name contains control characters and cannot be used in a service file");
+ return NULL;
+ }
+ for (const char *p = cli; *p; p++)
+ if ((unsigned char)*p < 0x20 || *p == 0x7f) {
+ if (error) *error = g_strdup("the program path contains control characters");
+ return NULL;
+ }
+ GString *s = g_string_new("[Unit]\nDescription=ProxyBridge\nAfter=network-online.target\nWants=network-online.target\n\n[Service]\nType=simple\nExecStart=");
+ quote_word(s, cli);
+ g_string_append(s, " --profile ");
+ quote_word(s, profile);
+ g_string_append(s, "\nRestart=on-failure\nRestartSec=5\n\n[Install]\nWantedBy=multi-user.target\n");
+ return g_string_free(s, FALSE);
+}
+
+bool boot_is_enabled(void) {
+ char *path = unit_path();
+ bool yes = g_file_test(path, G_FILE_TEST_IS_REGULAR);
+ g_free(path);
+ return yes;
+}
+
+static bool run_systemctl(const char *a, const char *b, char **error) {
+ char *argv[] = { (char *)systemctl_cmd(), (char *)a, (char *)b, NULL };
+ char *err_out = NULL;
+ int status = 0;
+ GError *gerr = NULL;
+ if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL, NULL, NULL, NULL, &err_out, &status, &gerr)) {
+ if (error) *error = g_strdup_printf("could not run %s: %s", systemctl_cmd(), gerr ? gerr->message : "unknown error");
+ if (gerr) g_error_free(gerr);
+ return false;
+ }
+ bool ok = g_spawn_check_wait_status(status, NULL);
+ if (!ok && error) {
+ g_strstrip(err_out ? err_out : (char *)"");
+ *error = g_strdup_printf("systemctl %s %s failed%s%s", a, b ? b : "", err_out && *err_out ? ": " : "", err_out ? err_out : "");
+ }
+ g_free(err_out);
+ return ok;
+}
+
+static bool write_unit(const char *profile, char **error) {
+ char *cli = cli_path();
+ char *text = boot_unit_text(cli, profile, error);
+ g_free(cli);
+ if (!text) return false;
+ char *path = unit_path(), *tmp = g_strdup_printf("%s.tmp", path);
+ bool ok = false;
+ int fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0644);
+ if (fd >= 0) {
+ size_t n = strlen(text), off = 0;
+ while (off < n) {
+ ssize_t w = write(fd, text + off, n - off);
+ if (w <= 0) break;
+ off += (size_t)w;
+ }
+ ok = off == n && close(fd) == 0 && rename(tmp, path) == 0;
+ if (!ok) g_unlink(tmp);
+ }
+ if (!ok && error) *error = g_strdup_printf("cannot write %s: %s", path, g_strerror(errno));
+ g_free(tmp); g_free(path); g_free(text);
+ return ok;
+}
+
+bool boot_set(bool enable, const char *profile, char **error) {
+ if (error) *error = NULL;
+ if (enable) {
+ if (!write_unit(profile, error)) return false;
+ if (!run_systemctl("daemon-reload", NULL, error) || !run_systemctl("enable", UNIT_NAME, error)) {
+ char *path = unit_path();
+ g_unlink(path); // do not leave a unit behind that is not enabled
+ g_free(path);
+ return false;
+ }
+ return true;
+ }
+ char *path = unit_path();
+ bool existed = g_file_test(path, G_FILE_TEST_EXISTS);
+ bool ok = !existed || run_systemctl("disable", UNIT_NAME, error);
+ if (ok && existed && g_unlink(path) != 0) {
+ if (error) *error = g_strdup_printf("cannot remove %s: %s", path, g_strerror(errno));
+ ok = false;
+ }
+ if (ok && existed) run_systemctl("daemon-reload", NULL, NULL);
+ g_free(path);
+ return ok;
+}
+
+bool boot_refresh(const char *profile, char **error) {
+ if (error) *error = NULL;
+ if (!boot_is_enabled()) return true;
+ return write_unit(profile, error) && run_systemctl("daemon-reload", NULL, error);
+}
diff --git a/Linux/gui/gui_boot.h b/Linux/gui/gui_boot.h
new file mode 100644
index 0000000..225ea88
--- /dev/null
+++ b/Linux/gui/gui_boot.h
@@ -0,0 +1,18 @@
+// "Start at boot": a systemd service that runs the command line program with the active profile.
+#ifndef PROXYBRIDGE_GUI_BOOT_H
+#define PROXYBRIDGE_GUI_BOOT_H
+
+#include <glib.h>
+#include <stdbool.h>
+
+// Text of the unit file. NULL (with *error set) if the profile name cannot be written safely (control characters).
+char *boot_unit_text(const char *cli_path, const char *profile, char **error);
+// True when the unit file exists.
+bool boot_is_enabled(void);
+// Writes the unit and enables it, or disables it and removes the file. On failure returns false with a message in *error
+// (g_malloc'd). The program the unit starts is the ProxyBridge next to this program, or /usr/local/bin/ProxyBridge.
+bool boot_set(bool enable, const char *profile, char **error);
+// Rewrites the unit for another active profile if it is enabled.
+bool boot_refresh(const char *profile, char **error);
+
+#endif
diff --git a/Linux/gui/gui_config.c b/Linux/gui/gui_config.c
index 6062dfc..7b1fbf4 100644
--- a/Linux/gui/gui_config.c
+++ b/Linux/gui/gui_config.c
@@ -40,9 +40,28 @@ static void free_profile(Profile *p) {
g_free(p->default_proxy);
g_free(p->name);
free(p->saved);
+ for (size_t i = 0; i < p->nfilters; i++) pbp_filter_clear(&p->filters[i]);
+ free(p->filters);
g_free(p);
}
+struct pbp_filter pbp_filter_dup(const struct pbp_filter *f) {
+ struct pbp_filter d = { f->exclude, strdup(f->process ? f->process : ""), strdup(f->ip ? f->ip : ""), strdup(f->port ? f->port : ""),
+ strdup(f->protocol ? f->protocol : "All"), strdup(f->action ? f->action : "All") };
+ return d;
+}
+
+void pbp_filter_clear(struct pbp_filter *f) {
+ free(f->process); free(f->ip); free(f->port); free(f->protocol); free(f->action);
+ memset(f, 0, sizeof *f);
+}
+
+static struct pbp_filter *filters_dup(const struct pbp_filter *src, size_t n) {
+ struct pbp_filter *d = calloc(n ? n : 1, sizeof *d);
+ for (size_t i = 0; d && i < n; i++) d[i] = pbp_filter_dup(&src[i]);
+ return d;
+}
+
Profile *profile_find(const char *name) {
if (!name) return NULL;
for (GList *l = g_profiles; l != NULL; l = l->next)
@@ -60,6 +79,8 @@ static void profile_sync_active() {
p->logging = g_chk_logging;
p->dns = g_chk_dns;
p->log_every = g_chk_log_every;
+ p->auto_clear = g_chk_auto_clear;
+ p->zoom = g_zoom;
}
static void load_globals_from(Profile *p) {
@@ -69,6 +90,9 @@ static void load_globals_from(Profile *p) {
g_chk_logging = p->logging;
g_chk_dns = p->dns;
g_chk_log_every = p->log_every;
+ g_chk_auto_clear = p->auto_clear;
+ g_zoom = p->zoom;
+ gui_logs_set_filters(p->filters, p->nfilters);
}
void config_free_all() {
@@ -85,6 +109,8 @@ static Profile *profile_new_empty(const char *name) {
Profile *p = g_malloc0(sizeof(Profile));
p->name = g_strdup(name);
p->logging = p->dns = true;
+ p->auto_clear = true;
+ p->zoom = 100;
g_profiles = g_list_append(g_profiles, p);
return p;
}
@@ -127,6 +153,10 @@ Profile *profile_create(const char *name, bool copy_active) {
p->logging = src->logging;
p->dns = src->dns;
p->log_every = src->log_every;
+ p->auto_clear = src->auto_clear;
+ p->zoom = src->zoom;
+ p->filters = filters_dup(src->filters, src->nfilters);
+ p->nfilters = src->nfilters;
}
return p;
}
@@ -186,6 +216,10 @@ static void profile_to_pbp(const Profile *prof, struct pbp_profile *out) {
out->logging = prof->logging;
out->dns_via_proxy = prof->dns;
out->log_every = prof->log_every;
+ out->auto_clear = prof->auto_clear;
+ out->font_zoom = prof->zoom;
+ out->filters = prof->filters;
+ out->nfilters = prof->nfilters;
out->nproxies = g_list_length(prof->proxies);
out->nrules = g_list_length(prof->rules);
out->proxies = g_new0(struct pbp_proxy, out->nproxies + 1);
@@ -262,6 +296,12 @@ static void fill_profile_from_pbp(Profile *p, const struct pbp_profile *src) {
p->logging = src->logging;
p->dns = src->dns_via_proxy;
p->log_every = src->log_every;
+ p->auto_clear = src->auto_clear;
+ p->zoom = src->font_zoom >= PBP_ZOOM_MIN && src->font_zoom <= PBP_ZOOM_MAX ? src->font_zoom : 100;
+ for (size_t i = 0; i < p->nfilters; i++) pbp_filter_clear(&p->filters[i]);
+ free(p->filters);
+ p->filters = filters_dup(src->filters, src->nfilters);
+ p->nfilters = src->nfilters;
}
bool profile_export(const Profile *prof, const char *path, char *err, size_t errlen) {
@@ -350,7 +390,7 @@ void save_config() {
g_free(path);
}
char *settings = g_strdup_printf("%s/settings.json", dir);
- if (pbp_settings_save(settings, g_active_profile, err, sizeof err) != 0)
+ if (pbp_settings_save(settings, g_active_profile, g_chk_updates, err, sizeof err) != 0)
fprintf(stderr, "failed to save settings: %s\n", err);
g_free(settings);
out:
@@ -566,7 +606,7 @@ void load_config() {
load_profile_files(dir);
char *settings = g_strdup_printf("%s/settings.json", dir);
- pbp_settings_load(settings, &settings_active);
+ pbp_settings_load(settings, &settings_active, &g_chk_updates);
g_free(settings);
if (!g_profiles) {
migrate = load_legacy_ini(dir, &legacy_active);
@@ -589,3 +629,19 @@ void load_config() {
}
g_free(dir);
}
+
+struct pbp_filter *gui_filters(size_t *n) {
+ Profile *p = profile_find(g_active_profile);
+ if (n) *n = p ? p->nfilters : 0;
+ return p ? p->filters : NULL;
+}
+
+void gui_filters_replace(struct pbp_filter *filters, size_t n) {
+ Profile *p = profile_find(g_active_profile);
+ if (!p) return;
+ for (size_t i = 0; i < p->nfilters; i++) pbp_filter_clear(&p->filters[i]);
+ free(p->filters);
+ p->filters = filters;
+ p->nfilters = n;
+ gui_logs_set_filters(p->filters, p->nfilters);
+}
diff --git a/Linux/gui/gui_logs.c b/Linux/gui/gui_logs.c
index 4771e26..ef93dfd 100644
--- a/Linux/gui/gui_logs.c
+++ b/Linux/gui/gui_logs.c
@@ -1,6 +1,7 @@
#include "gui.h"
#include "gui_events.h"
#include <time.h>
+#include <pthread.h>
#define RING_CAPACITY 4096
#define SLOT_SIZE 512
@@ -9,6 +10,7 @@
#define DRAIN_INTERVAL_MS 100
#define CONN_HISTORY 2000
#define LOG_HISTORY 1000
+#define AUTO_CLEAR_LINES 500 // a log is wiped once it grows past this, when auto-clear is on
typedef struct {
GtkTextBuffer **buffer;
@@ -21,6 +23,24 @@ typedef struct {
static View conn_view = {&conn_buffer, NULL, CONN_HISTORY, NULL, false};
static View log_view = {&log_buffer, NULL, LOG_HISTORY, NULL, false};
static char *drain_buf = NULL;
+
+// Connection-log filters. The library calls the connection callback from its own threads, so the copy has its own lock.
+static pthread_mutex_t filter_mu = PTHREAD_MUTEX_INITIALIZER;
+static struct pbp_filter *filters;
+static size_t nfilters;
+
+void gui_logs_set_filters(const struct pbp_filter *f, size_t n) {
+ struct pbp_filter *copy = calloc(n ? n : 1, sizeof *copy);
+ for (size_t i = 0; copy && i < n; i++) copy[i] = pbp_filter_dup(&f[i]);
+ pthread_mutex_lock(&filter_mu);
+ struct pbp_filter *old = filters;
+ size_t nold = nfilters;
+ filters = copy;
+ nfilters = copy ? n : 0;
+ pthread_mutex_unlock(&filter_mu);
+ for (size_t i = 0; i < nold; i++) pbp_filter_clear(&old[i]);
+ free(old);
+}
static guint drain_source = 0;
static void filter_from_line(GtkTextBuffer *buffer, int first_line, const char *lower_search) {
@@ -95,7 +115,12 @@ static void append_batch(View *v, const char *text, uint64_t dropped) {
if (v->filter) filter_from_line(buffer, first_line, v->filter);
- int excess = gtk_text_buffer_get_line_count(buffer) - 1 - v->max_lines;
+ int lines = gtk_text_buffer_get_line_count(buffer) - 1;
+ if (g_chk_auto_clear && lines > AUTO_CLEAR_LINES) {
+ gtk_text_buffer_set_text(buffer, "", 0);
+ return;
+ }
+ int excess = lines - v->max_lines;
if (excess > 0) {
GtkTextIter start, cut;
gtk_text_buffer_get_start_iter(buffer, &start);
@@ -140,6 +165,7 @@ void gui_logs_free() {
conn_view.filter = log_view.filter = NULL;
g_free(drain_buf);
drain_buf = NULL;
+ gui_logs_set_filters(NULL, 0);
}
uint64_t gui_conn_event_count() {
@@ -177,7 +203,15 @@ void gui_log(const char *format, ...) {
lib_log_callback(msg);
}
-void lib_connection_callback(const char *process_name, uint32_t pid, const char *dest_ip, uint16_t dest_port, const char *proxy_info) {
- push_line(&conn_view, "%s (PID:%u) -> %s:%u via %s", process_name ? process_name : "", pid,
- dest_ip ? dest_ip : "", dest_port, proxy_info ? proxy_info : "");
+void lib_connection_callback_ex(const char *process_name, uint32_t pid, const char *dest_ip, uint16_t dest_port, const char *protocol,
+ const char *proxy_info) {
+ const char *info = proxy_info ? proxy_info : "";
+ const char *action = strcmp(info, "DIRECT") == 0 ? "Direct" : strcmp(info, "BLOCK") == 0 ? "Blocked" : "Proxy";
+ char port[8];
+ snprintf(port, sizeof port, "%u", dest_port);
+ pthread_mutex_lock(&filter_mu);
+ bool show = pbp_filters_pass(filters, nfilters, process_name ? process_name : "", dest_ip ? dest_ip : "", port, protocol ? protocol : "", action);
+ pthread_mutex_unlock(&filter_mu);
+ if (!show) return;
+ push_line(&conn_view, "%s (PID:%u) -> %s:%u via %s", process_name ? process_name : "", pid, dest_ip ? dest_ip : "", dest_port, info);
}
diff --git a/Linux/gui/gui_settings.c b/Linux/gui/gui_settings.c
index 320ccb4..89c6110 100644
--- a/Linux/gui/gui_settings.c
+++ b/Linux/gui/gui_settings.c
@@ -1,4 +1,5 @@
#include "gui.h"
+#include "gui_boot.h"
GList *g_proxies_list = NULL;
char *g_default_proxy = NULL;
@@ -485,6 +486,11 @@ typedef struct {
} ProfilesInfo;
static void update_profile_ui() {
+ char *boot_err = NULL;
+ if (!boot_refresh(g_active_profile, &boot_err)) { // the boot service runs the active profile
+ gui_log("could not update the boot service: %s", boot_err ? boot_err : "unknown error");
+ g_free(boot_err);
+ }
char *title = g_strdup_printf("ProxyBridge - %s", g_active_profile);
gtk_window_set_title(GTK_WINDOW(window), title);
g_free(title);
@@ -717,3 +723,198 @@ void on_profiles_clicked(GtkWidget *widget, gpointer data) {
gtk_dialog_run(GTK_DIALOG(pi.dialog));
gtk_widget_destroy(pi.dialog);
}
+
+
+// ---- connection log filters ----
+typedef struct {
+ GtkWidget *dialog, *tree;
+ GtkListStore *store;
+ struct pbp_filter *work; // working copy; every change is applied at once
+ size_t n;
+} FiltersInfo;
+
+static void filters_commit(FiltersInfo *fi) {
+ struct pbp_filter *copy = calloc(fi->n ? fi->n : 1, sizeof *copy);
+ for (size_t i = 0; copy && i < fi->n; i++) copy[i] = pbp_filter_dup(&fi->work[i]);
+ gui_filters_replace(copy, copy ? fi->n : 0);
+ save_config();
+}
+
+static void filters_refresh(FiltersInfo *fi) {
+ gtk_list_store_clear(fi->store);
+ for (size_t i = 0; i < fi->n; i++) {
+ const struct pbp_filter *f = &fi->work[i];
+ GtkTreeIter it;
+ gtk_list_store_append(fi->store, &it);
+ gtk_list_store_set(fi->store, &it, 0, f->exclude ? "Exclude" : "Include", 1, f->process, 2, f->ip, 3, f->port, 4, f->protocol, 5, f->action, -1);
+ }
+}
+
+static bool port_pattern_ok(const char *p) {
+ if (!*p || strcasecmp(p, "All") == 0) return true;
+ for (; *p; p++) if (!g_ascii_isdigit(*p) && *p != '*' && *p != '?') return false;
+ return true;
+}
+
+// edits *f in place; false when cancelled
+static bool filter_edit_dialog(GtkWindow *parent, struct pbp_filter *f) {
+ GtkWidget *d = gtk_dialog_new_with_buttons("Log Filter", parent, GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+ "Cancel", GTK_RESPONSE_CANCEL, "Save", GTK_RESPONSE_ACCEPT, NULL);
+ gtk_widget_set_name(d, "dlg_filter");
+ GtkWidget *grid = gtk_grid_new();
+ gtk_grid_set_row_spacing(GTK_GRID(grid), 6);
+ gtk_grid_set_column_spacing(GTK_GRID(grid), 10);
+ gtk_container_set_border_width(GTK_CONTAINER(grid), 12);
+ gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(d))), grid, TRUE, TRUE, 0);
+ GtkWidget *mode = combo_new("Include", "Exclude", NULL), *proto = combo_new("All", "TCP", "UDP", NULL);
+ GtkWidget *action = combo_new("All", "Direct", "Proxy", "Blocked", NULL);
+ GtkWidget *proc = gtk_entry_new(), *ip = gtk_entry_new(), *port = gtk_entry_new();
+ gtk_entry_set_max_length(GTK_ENTRY(proc), 255);
+ gtk_entry_set_max_length(GTK_ENTRY(ip), 63);
+ gtk_entry_set_max_length(GTK_ENTRY(port), 15);
+ GtkWidget *err = hint_label("");
+ gtk_widget_set_name(mode, "flt_mode"); gtk_widget_set_name(proc, "flt_process"); gtk_widget_set_name(ip, "flt_ip");
+ gtk_widget_set_name(port, "flt_port"); gtk_widget_set_name(proto, "flt_proto"); gtk_widget_set_name(action, "flt_action");
+ gtk_widget_set_name(err, "flt_error");
+ grid_row(grid, 0, "Mode:", mode);
+ grid_row(grid, 1, "Process:", proc);
+ grid_row(grid, 2, "IP address:", ip);
+ grid_row(grid, 3, "Port:", port);
+ grid_row(grid, 4, "Protocol:", proto);
+ grid_row(grid, 5, "Action:", action);
+ gtk_grid_attach(GTK_GRID(grid), hint_label("Include shows only matching connections, Exclude hides them (Exclude wins). Patterns are case-insensitive: * and ? match anything, empty matches all."), 0, 6, 2, 1);
+ gtk_grid_attach(GTK_GRID(grid), err, 0, 7, 2, 1);
+ gtk_combo_box_set_active(GTK_COMBO_BOX(mode), f->exclude ? 1 : 0);
+ gtk_entry_set_text(GTK_ENTRY(proc), f->process ? f->process : "");
+ gtk_entry_set_text(GTK_ENTRY(ip), f->ip ? f->ip : "");
+ gtk_entry_set_text(GTK_ENTRY(port), f->port ? f->port : "");
+ const char *protos[] = {"All", "TCP", "UDP"}, *actions[] = {"All", "Direct", "Proxy", "Blocked"};
+ int pi = 0, ai = 0;
+ for (int i = 0; i < 3; i++) if (f->protocol && !strcasecmp(f->protocol, protos[i])) pi = i;
+ for (int i = 0; i < 4; i++) if (f->action && !strcasecmp(f->action, actions[i])) ai = i;
+ gtk_combo_box_set_active(GTK_COMBO_BOX(proto), pi);
+ gtk_combo_box_set_active(GTK_COMBO_BOX(action), ai);
+ gtk_widget_show_all(d);
+
+ bool saved = false;
+ while (!saved && gtk_dialog_run(GTK_DIALOG(d)) == GTK_RESPONSE_ACCEPT) {
+ const char *pt = gtk_entry_get_text(GTK_ENTRY(port));
+ if (!port_pattern_ok(pt)) { set_error_label(err, "Port must be digits, * or ? (or empty)."); continue; }
+ pbp_filter_clear(f);
+ f->exclude = gtk_combo_box_get_active(GTK_COMBO_BOX(mode)) == 1;
+ f->process = strdup(gtk_entry_get_text(GTK_ENTRY(proc)));
+ f->ip = strdup(gtk_entry_get_text(GTK_ENTRY(ip)));
+ f->port = strdup(pt);
+ int p2 = gtk_combo_box_get_active(GTK_COMBO_BOX(proto)), a2 = gtk_combo_box_get_active(GTK_COMBO_BOX(action));
+ f->protocol = strdup(protos[p2 < 0 ? 0 : p2]);
+ f->action = strdup(actions[a2 < 0 ? 0 : a2]);
+ saved = true;
+ }
+ gtk_widget_destroy(d);
+ return saved;
+}
+
+static int filters_selected(FiltersInfo *fi) {
+ GtkTreeModel *model;
+ GtkTreeIter it;
+ if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(fi->tree)), &model, &it)) return -1;
+ GtkTreePath *path = gtk_tree_model_get_path(model, &it);
+ int idx = gtk_tree_path_get_indices(path)[0];
+ gtk_tree_path_free(path);
+ return idx;
+}
+
+static void on_filter_add(GtkWidget *w, gpointer data) {
+ FiltersInfo *fi = data;
+ if (fi->n >= PBP_MAX_FILTERS) {
+ show_message(GTK_WINDOW(fi->dialog), GTK_MESSAGE_WARNING, "At most %d filters are supported.", PBP_MAX_FILTERS);
+ return;
+ }
+ struct pbp_filter f = { false, strdup(""), strdup(""), strdup(""), strdup("All"), strdup("All") };
+ if (filter_edit_dialog(GTK_WINDOW(fi->dialog), &f)) {
+ fi->work = realloc(fi->work, (fi->n + 1) * sizeof *fi->work);
+ fi->work[fi->n++] = f;
+ filters_commit(fi);
+ filters_refresh(fi);
+ } else {
+ pbp_filter_clear(&f);
+ }
+}
+
+static void on_filter_edit(GtkWidget *w, gpointer data) {
+ FiltersInfo *fi = data;
+ int i = filters_selected(fi);
+ if (i < 0) { show_message(GTK_WINDOW(fi->dialog), GTK_MESSAGE_WARNING, "Select a filter first."); return; }
+ struct pbp_filter f = pbp_filter_dup(&fi->work[i]);
+ if (filter_edit_dialog(GTK_WINDOW(fi->dialog), &f)) {
+ pbp_filter_clear(&fi->work[i]);
+ fi->work[i] = f;
+ filters_commit(fi);
+ filters_refresh(fi);
+ } else {
+ pbp_filter_clear(&f);
+ }
+}
+
+static void on_filter_delete(GtkWidget *w, gpointer data) {
+ FiltersInfo *fi = data;
+ int i = filters_selected(fi);
+ if (i < 0) { show_message(GTK_WINDOW(fi->dialog), GTK_MESSAGE_WARNING, "Select a filter first."); return; }
+ pbp_filter_clear(&fi->work[i]);
+ memmove(&fi->work[i], &fi->work[i + 1], (fi->n - (size_t)i - 1) * sizeof *fi->work);
+ fi->n--;
+ filters_commit(fi);
+ filters_refresh(fi);
+}
+
+static void on_filter_row_activated(GtkTreeView *tree, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data) {
+ on_filter_edit(NULL, data);
+}
+
+void on_log_filters_clicked(GtkWidget *widget, gpointer data) {
+ FiltersInfo fi = {0};
+ size_t n = 0;
+ struct pbp_filter *cur = gui_filters(&n);
+ fi.work = calloc(n ? n : 1, sizeof *fi.work);
+ for (size_t i = 0; i < n; i++) fi.work[i] = pbp_filter_dup(&cur[i]);
+ fi.n = n;
+
+ fi.dialog = gtk_dialog_new_with_buttons("Connection Log Filters", GTK_WINDOW(window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+ "Close", GTK_RESPONSE_CLOSE, NULL);
+ gtk_widget_set_name(fi.dialog, "dlg_filters");
+ gtk_window_set_default_size(GTK_WINDOW(fi.dialog), 640, 380);
+ GtkWidget *box = gtk_dialog_get_content_area(GTK_DIALOG(fi.dialog));
+ gtk_container_set_border_width(GTK_CONTAINER(box), 10);
+ fi.store = gtk_list_store_new(6, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
+ fi.tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(fi.store));
+ gtk_widget_set_name(fi.tree, "filter_tree");
+ g_object_unref(fi.store);
+ const char *titles[] = {"Mode", "Process", "IP address", "Port", "Protocol", "Action"};
+ for (int i = 0; i < 6; i++) {
+ GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes(titles[i], gtk_cell_renderer_text_new(), "text", i, NULL);
+ gtk_tree_view_column_set_resizable(c, TRUE);
+ gtk_tree_view_column_set_expand(c, i == 1 || i == 2);
+ gtk_tree_view_append_column(GTK_TREE_VIEW(fi.tree), c);
+ }
+ g_signal_connect(fi.tree, "row-activated", G_CALLBACK(on_filter_row_activated), &fi);
+ GtkWidget *scroll = gtk_scrolled_window_new(NULL, NULL);
+ gtk_container_add(GTK_CONTAINER(scroll), fi.tree);
+ gtk_box_pack_start(GTK_BOX(box), hint_label("Filters hide or show lines of the connection log for new connections. They belong to the active profile."), FALSE, FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(box), scroll, TRUE, TRUE, 5);
+ GtkWidget *btns = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
+ const char *labels[] = {"Add", "Edit", "Delete"}, *names[] = {"flt_add", "flt_edit", "flt_delete"};
+ void (*cbs[])(GtkWidget *, gpointer) = {on_filter_add, on_filter_edit, on_filter_delete};
+ for (int i = 0; i < 3; i++) {
+ GtkWidget *b = gtk_button_new_with_label(labels[i]);
+ gtk_widget_set_name(b, names[i]);
+ g_signal_connect(b, "clicked", G_CALLBACK(cbs[i]), &fi);
+ gtk_box_pack_start(GTK_BOX(btns), b, FALSE, FALSE, 0);
+ }
+ gtk_box_pack_start(GTK_BOX(box), btns, FALSE, FALSE, 5);
+ filters_refresh(&fi);
+ gtk_widget_show_all(fi.dialog);
+ gtk_dialog_run(GTK_DIALOG(fi.dialog));
+ gtk_widget_destroy(fi.dialog);
+ for (size_t i = 0; i < fi.n; i++) pbp_filter_clear(&fi.work[i]);
+ free(fi.work);
+}
diff --git a/Linux/gui/main.c b/Linux/gui/main.c
index e7ac65a..4061240 100644
--- a/Linux/gui/main.c
+++ b/Linux/gui/main.c
@@ -1,4 +1,5 @@
#include "gui.h"
+#include "gui_boot.h"
#include <glib-unix.h>
#include <signal.h>
@@ -12,7 +13,14 @@ GList *g_rules_list = NULL;
bool g_chk_logging = true;
bool g_chk_dns = true;
bool g_chk_log_every = false;
-static GtkWidget *log_item, *dns_item, *every_item, *enable_item;
+bool g_chk_auto_clear = true;
+int g_zoom = 100;
+bool g_chk_updates = true;
+static GtkWidget *log_item, *dns_item, *every_item, *enable_item, *clear_item, *boot_item, *update_item;
+static GtkCssProvider *zoom_provider;
+static char zoom_css[96];
+
+static void sync_update_item();
static void on_log_traffic_toggled(GtkCheckMenuItem *item, gpointer data) {
bool active = gtk_check_menu_item_get_active(item);
@@ -41,11 +49,66 @@ static void on_dns_proxy_toggled(GtkCheckMenuItem *item, gpointer data) {
save_config();
}
+static void on_auto_clear_toggled(GtkCheckMenuItem *item, gpointer data) {
+ g_chk_auto_clear = gtk_check_menu_item_get_active(item);
+ save_config();
+}
+
+static bool boot_syncing;
+
+static void on_boot_toggled(GtkCheckMenuItem *item, gpointer data) {
+ if (boot_syncing) return;
+ bool want = gtk_check_menu_item_get_active(item);
+ char *err = NULL;
+ if (!boot_set(want, g_active_profile, &err)) {
+ show_message(GTK_WINDOW(window), GTK_MESSAGE_ERROR, "Could not %s starting at boot: %s", want ? "enable" : "disable", err ? err : "unknown error");
+ g_free(err);
+ boot_syncing = true;
+ gtk_check_menu_item_set_active(item, boot_is_enabled());
+ boot_syncing = false;
+ return;
+ }
+ gui_log("ProxyBridge %s at boot (service proxybridge.service, profile '%s')", want ? "will start" : "will no longer start", g_active_profile);
+}
+
+static void on_update_startup_toggled(GtkCheckMenuItem *item, gpointer data) {
+ g_chk_updates = gtk_check_menu_item_get_active(item);
+ save_config();
+}
+
+static void sync_update_item() {
+ if (update_item) gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(update_item), g_chk_updates);
+}
+
+// ---- font zoom ----
+const char *gui_zoom_css() { return zoom_css; }
+
+static void apply_zoom_css() {
+ snprintf(zoom_css, sizeof zoom_css, "window, dialog, messagedialog { font-size: %d%%; }", g_zoom);
+ if (!zoom_provider) {
+ zoom_provider = gtk_css_provider_new();
+ gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(zoom_provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION + 1);
+ }
+ gtk_css_provider_load_from_data(zoom_provider, zoom_css, -1, NULL);
+}
+
+void gui_set_zoom(int percent) {
+ g_zoom = percent < PBP_ZOOM_MIN ? PBP_ZOOM_MIN : percent > PBP_ZOOM_MAX ? PBP_ZOOM_MAX : percent;
+ apply_zoom_css();
+ save_config();
+}
+
+static void on_zoom_in(GtkWidget *w, gpointer d) { gui_set_zoom(g_zoom + 10); }
+static void on_zoom_out(GtkWidget *w, gpointer d) { gui_set_zoom(g_zoom - 10); }
+static void on_zoom_reset(GtkWidget *w, gpointer d) { gui_set_zoom(100); }
+
// the switches belong to the profile: after a profile change, show and apply the new profile's values
void gui_sync_switches() {
ProxyBridge_SetTrafficLoggingEnabled(g_chk_logging);
ProxyBridge_SetDnsViaProxy(g_chk_dns);
ProxyBridge_SetLogEveryConnection(g_chk_log_every);
+ apply_zoom_css();
+ if (clear_item) gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(clear_item), g_chk_auto_clear);
if (every_item) gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(every_item), g_chk_log_every);
if (log_item) gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(log_item), g_chk_logging);
if (dns_item) gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(dns_item), g_chk_dns);
@@ -131,91 +194,130 @@ static const char *update_feed_url() {
return UPD_FEED_URL;
}
-static void on_check_update(GtkWidget *widget, gpointer data) {
+typedef struct {
+ char *version, *download, *notes;
+ char *problem; // why no answer could be had
+ GtkMessageType problem_type;
+ bool newer;
+ bool startup; // started by the program, not by the user: stay quiet unless there is news
+} UpdateInfo;
+
+static void update_free(UpdateInfo *u) {
+ g_free(u->version); g_free(u->download); g_free(u->notes); g_free(u->problem);
+ g_free(u);
+}
+
+// Asks the feed (curl) and compares versions. Runs on any thread.
+static UpdateInfo *update_lookup(void) {
+ UpdateInfo *u = g_malloc0(sizeof *u);
char *argv[] = {
- (char *)"curl", (char *)"-sS",
+ (char *)"curl", (char *)"-sS", (char *)"-m", (char *)"15",
(char *)"-H", (char *)"User-Agent: ProxyBridge-UpdateChecker",
(char *)update_feed_url(),
NULL
};
- char *json = NULL;
- char *curl_err = NULL;
+ char *json = NULL, *curl_err = NULL;
GError *spawn_err = NULL;
int exit_code = 0;
-
- if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
- &json, &curl_err, &exit_code, &spawn_err)) {
- show_message(GTK_WINDOW(window), GTK_MESSAGE_ERROR, "Update check failed: %s",
- spawn_err ? spawn_err->message : "unknown");
+ if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &json, &curl_err, &exit_code, &spawn_err)) {
+ u->problem = g_strdup_printf("Update check failed: %s", spawn_err ? spawn_err->message : "unknown");
+ u->problem_type = GTK_MESSAGE_ERROR;
if (spawn_err) g_error_free(spawn_err);
g_free(curl_err);
- return;
+ return u;
}
if (exit_code != 0 || !json || json[0] == '\0') {
- show_message(GTK_WINDOW(window), GTK_MESSAGE_ERROR, "Update check failed (curl exit %d).", exit_code);
- g_free(json);
- g_free(curl_err);
- return;
+ u->problem = g_strdup_printf("Update check failed (curl exit %d).", exit_code);
+ u->problem_type = GTK_MESSAGE_ERROR;
+ g_free(json); g_free(curl_err);
+ return u;
}
-
char *linux_obj = extract_platform_block(json, "linux");
g_free(json);
g_free(curl_err);
if (!linux_obj) {
- show_message(GTK_WINDOW(window), GTK_MESSAGE_WARNING, "Could not parse linux update info.");
- return;
+ u->problem = g_strdup("Could not parse linux update info.");
+ u->problem_type = GTK_MESSAGE_WARNING;
+ return u;
}
-
- char *version = extract_sub_json_str(linux_obj, "version");
- char *download = extract_sub_json_str(linux_obj, "download");
- char *notes = extract_sub_json_str(linux_obj, "release_notes");
+ u->version = extract_sub_json_str(linux_obj, "version");
+ u->download = extract_sub_json_str(linux_obj, "download");
+ u->notes = extract_sub_json_str(linux_obj, "release_notes");
g_free(linux_obj);
-
- if (!version || !version[0]) {
- show_message(GTK_WINDOW(window), GTK_MESSAGE_WARNING, "Could not parse version info.");
- g_free(version);
- g_free(download);
- g_free(notes);
- return;
+ if (!u->version || !u->version[0]) {
+ u->problem = g_strdup("Could not parse version info.");
+ u->problem_type = GTK_MESSAGE_WARNING;
+ return u;
}
-
int cur[3], lat[3];
parse_ver(PROXYBRIDGE_VERSION, cur);
- parse_ver(version, lat);
- if (ver_cmp(lat, cur) <= 0) {
- show_message(GTK_WINDOW(window), GTK_MESSAGE_INFO, "You are using the latest version (%s).", PROXYBRIDGE_VERSION);
- g_free(version);
- g_free(download);
- g_free(notes);
+ parse_ver(u->version, lat);
+ u->newer = ver_cmp(lat, cur) > 0;
+ return u;
+}
+
+// Main thread. A user-started check reports everything; a start-up check only speaks up when a newer version exists.
+static void update_show(UpdateInfo *u) {
+ if (u->problem) {
+ if (!u->startup) show_message(GTK_WINDOW(window), u->problem_type, "%s", u->problem);
+ return;
+ }
+ if (!u->newer) {
+ if (!u->startup) show_message(GTK_WINDOW(window), GTK_MESSAGE_INFO, "You are using the latest version (%s).", PROXYBRIDGE_VERSION);
return;
}
-
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
"Version %s is available (current %s).\nOpen the download page in your browser?",
- version, PROXYBRIDGE_VERSION);
+ u->version, PROXYBRIDGE_VERSION);
gtk_dialog_add_button(GTK_DIALOG(dialog), "Open in Browser", GTK_RESPONSE_ACCEPT);
gtk_dialog_add_button(GTK_DIALOG(dialog), "Close", GTK_RESPONSE_CANCEL);
+ if (u->startup) gtk_dialog_add_button(GTK_DIALOG(dialog), "Don't Check at Startup", GTK_RESPONSE_REJECT);
int resp = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
- if (resp == GTK_RESPONSE_ACCEPT) {
+ if (resp == GTK_RESPONSE_REJECT) {
+ g_chk_updates = false;
+ save_config();
+ sync_update_item();
+ } else if (resp == GTK_RESPONSE_ACCEPT) {
const char *url = NULL;
- if (https_url_ok(notes)) url = notes;
- else if (https_url_ok(download)) url = download;
+ if (https_url_ok(u->notes)) url = u->notes;
+ else if (https_url_ok(u->download)) url = u->download;
if (!url) {
show_message(GTK_WINDOW(window), GTK_MESSAGE_ERROR, "No safe https url in update feed.");
} else {
GError *uri_err = NULL;
if (!gtk_show_uri_on_window(GTK_WINDOW(window), url, GDK_CURRENT_TIME, &uri_err)) {
- show_message(GTK_WINDOW(window), GTK_MESSAGE_ERROR, "Could not open browser: %s",
- uri_err ? uri_err->message : "unknown");
+ show_message(GTK_WINDOW(window), GTK_MESSAGE_ERROR, "Could not open browser: %s", uri_err ? uri_err->message : "unknown");
if (uri_err) g_error_free(uri_err);
}
}
}
- g_free(version);
- g_free(download);
- g_free(notes);
+}
+
+static void on_check_update(GtkWidget *widget, gpointer data) {
+ UpdateInfo *u = update_lookup();
+ update_show(u);
+ update_free(u);
+}
+
+static gboolean update_show_idle(gpointer data) {
+ UpdateInfo *u = data;
+ update_show(u);
+ update_free(u);
+ return G_SOURCE_REMOVE;
+}
+
+static gpointer update_startup_thread(gpointer data) {
+ UpdateInfo *u = update_lookup();
+ u->startup = true;
+ g_idle_add(update_show_idle, u);
+ return NULL;
+}
+
+// Looks for a new version without blocking the window (the answer arrives on the main loop).
+void gui_check_updates_on_startup() {
+ g_thread_unref(g_thread_new("update_check", update_startup_thread, NULL));
}
static void on_about(GtkWidget *widget, gpointer data) {
@@ -497,14 +599,28 @@ int main(int argc, char *argv[]) {
log_item = add_check_item(proxy_menu, "Enable Traffic Logging", g_chk_logging, G_CALLBACK(on_log_traffic_toggled));
dns_item = add_check_item(proxy_menu, "DNS via Proxy", g_chk_dns, G_CALLBACK(on_dns_proxy_toggled));
every_item = add_check_item(proxy_menu, "Log Every Connection", g_chk_log_every, G_CALLBACK(on_log_every_toggled));
+ clear_item = add_check_item(proxy_menu, "Auto-clear Logs", g_chk_auto_clear, G_CALLBACK(on_auto_clear_toggled));
+ add_menu_item(proxy_menu, "Log Filters...", G_CALLBACK(on_log_filters_clicked));
enable_item = add_check_item(proxy_menu, "Enable ProxyBridge", true, G_CALLBACK(on_enable_toggled));
add_separator(proxy_menu);
+ boot_item = add_check_item(proxy_menu, "Start at Boot (systemd service)", boot_is_enabled(), G_CALLBACK(on_boot_toggled));
start_item = add_menu_item(proxy_menu, "Start Engine", G_CALLBACK(on_start_engine));
stop_item = add_menu_item(proxy_menu, "Stop Engine", G_CALLBACK(on_stop_engine));
add_menu_item(proxy_menu, "Exit", G_CALLBACK(on_window_destroy));
+ GtkWidget *view_menu = add_submenu(menubar, "View");
+ GtkAccelGroup *accel = gtk_accel_group_new();
+ gtk_window_add_accel_group(GTK_WINDOW(window), accel);
+ GtkWidget *zin = add_menu_item(view_menu, "Zoom In", G_CALLBACK(on_zoom_in));
+ GtkWidget *zout = add_menu_item(view_menu, "Zoom Out", G_CALLBACK(on_zoom_out));
+ GtkWidget *zreset = add_menu_item(view_menu, "Reset Zoom", G_CALLBACK(on_zoom_reset));
+ gtk_widget_add_accelerator(zin, "activate", accel, GDK_KEY_plus, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
+ gtk_widget_add_accelerator(zin, "activate", accel, GDK_KEY_equal, GDK_CONTROL_MASK, GTK_ACCEL_MASK);
+ gtk_widget_add_accelerator(zout, "activate", accel, GDK_KEY_minus, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
+ gtk_widget_add_accelerator(zreset, "activate", accel, GDK_KEY_0, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
GtkWidget *about_menu = add_submenu(menubar, "About");
add_menu_item(about_menu, "About", G_CALLBACK(on_about));
add_menu_item(about_menu, "Check for Updates", G_CALLBACK(on_check_update));
+ update_item = add_check_item(about_menu, "Check for Updates on Startup", g_chk_updates, G_CALLBACK(on_update_startup_toggled));
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
GtkWidget *notebook = gtk_notebook_new();
@@ -525,7 +641,7 @@ int main(int argc, char *argv[]) {
gui_logs_init();
ProxyBridge_SetLogCallback(lib_log_callback);
- ProxyBridge_SetConnectionCallback(lib_connection_callback);
+ ProxyBridge_SetConnectionCallbackEx(lib_connection_callback_ex);
ProxyBridge_SetTrafficLoggingEnabled(g_chk_logging);
ProxyBridge_SetDnsViaProxy(g_chk_dns);
ProxyBridge_SetLogEveryConnection(g_chk_log_every);
@@ -539,6 +655,8 @@ int main(int argc, char *argv[]) {
update_status_info();
status_source = g_timeout_add_seconds(1, status_tick, NULL);
engine_request(true);
+ apply_zoom_css();
+ if (g_chk_updates && !pb_gui_ready_hook) gui_check_updates_on_startup();
if (pb_gui_ready_hook) pb_gui_ready_hook();
else gtk_main();
diff --git a/Linux/profile/pb_profile.c b/Linux/profile/pb_profile.c
index 3ea3a40..317460c 100644
--- a/Linux/profile/pb_profile.c
+++ b/Linux/profile/pb_profile.c
@@ -3,6 +3,7 @@
#endif
#include "pb_profile.h"
+#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
@@ -341,6 +342,11 @@ void pbp_free(struct pbp_profile *p)
for (size_t i = 0; i < p->nrules; i++) {
free(p->rules[i].name); free(p->rules[i].process); free(p->rules[i].hosts); free(p->rules[i].ports);
}
+ for (size_t i = 0; i < p->nfilters; i++) {
+ free(p->filters[i].process); free(p->filters[i].ip); free(p->filters[i].port);
+ free(p->filters[i].protocol); free(p->filters[i].action);
+ }
+ free(p->filters);
free(p->proxies);
free(p->rules);
free(p->name);
@@ -499,6 +505,9 @@ int pbp_parse(const char *json, size_t len, struct pbp_profile *out, char *err,
out->logging = jbool(&root, "IsTrafficLoggingEnabled", true);
out->dns_via_proxy = jbool(&root, "DnsViaProxy", true);
out->log_every = jbool(&root, "LogEveryConnection", false);
+ out->auto_clear = jbool(&root, "AutoClearConnectionLogs", true);
+ long zoom = jnum(&root, "FontZoom", 100);
+ out->font_zoom = (zoom >= PBP_ZOOM_MIN && zoom <= PBP_ZOOM_MAX) ? (int)zoom : 100;
long dp = jnum(&root, "DefaultProxyId", 0);
out->default_proxy_id = dp > 0 ? (uint32_t)dp : 0;
if (cfgs && cfgs->nk) out->proxies = calloc(cfgs->nk, sizeof *out->proxies);
@@ -515,6 +524,27 @@ int pbp_parse(const char *json, size_t len, struct pbp_profile *out, char *err,
for (size_t i = 0; rc == 0 && rules && i < rules->nk; i++)
rc = rules->kid[i].t == J_OBJ ? read_rule(out, &rules->kid[i], i, default_localhost, err, errlen)
: (snprintf(err, errlen, "ProxyRules[%zu] is not an object", i), -1);
+ const struct jv *flts = jget(&root, "LogFilters");
+ if (rc == 0 && flts && flts->t != J_ARR) { snprintf(err, errlen, "LogFilters must be an array"); rc = -1; }
+ if (rc == 0 && flts && flts->nk > PBP_MAX_FILTERS) { snprintf(err, errlen, "too many log filters (max %d)", PBP_MAX_FILTERS); rc = -1; }
+ if (rc == 0 && flts && flts->nk) {
+ out->filters = calloc(flts->nk, sizeof *out->filters);
+ if (!out->filters) { snprintf(err, errlen, "out of memory"); rc = -1; }
+ }
+ for (size_t i = 0; rc == 0 && flts && i < flts->nk; i++) {
+ const struct jv *o = &flts->kid[i];
+ if (o->t != J_OBJ) { snprintf(err, errlen, "LogFilters[%zu] is not an object", i); rc = -1; break; }
+ struct pbp_filter *x = &out->filters[out->nfilters++];
+ char *mode = jtext(o, "Mode"), *proto = jtext(o, "Protocol"), *act = jtext(o, "Action");
+ x->exclude = mode && !strcasecmp(mode, "Exclude");
+ x->process = dup_or(jtext(o, "ProcessName"), "");
+ x->ip = dup_or(jtext(o, "Ip"), "");
+ x->port = dup_or(jtext(o, "Port"), "");
+ x->protocol = strdup(proto && !strcasecmp(proto, "TCP") ? "TCP" : proto && !strcasecmp(proto, "UDP") ? "UDP" : "All");
+ x->action = strdup(act && !strcasecmp(act, "Direct") ? "Direct" : act && !strcasecmp(act, "Proxy") ? "Proxy" : act && !strcasecmp(act, "Blocked") ? "Blocked" : "All");
+ free(mode); free(proto); free(act);
+ if (!x->process || !x->ip || !x->port || !x->protocol || !x->action) { snprintf(err, errlen, "out of memory"); rc = -1; }
+ }
if (rc == 0 && out->default_proxy_id && !pbp_find_proxy(out, out->default_proxy_id)) {
snprintf(err, errlen, "DefaultProxyId %u does not match any proxy", out->default_proxy_id);
rc = -1;
@@ -593,6 +623,8 @@ char *pbp_serialize(const struct pbp_profile *p)
sb_puts(&b, p->logging ? " \"IsTrafficLoggingEnabled\": true,\n" : " \"IsTrafficLoggingEnabled\": false,\n");
sb_puts(&b, p->dns_via_proxy ? " \"DnsViaProxy\": true,\n" : " \"DnsViaProxy\": false,\n");
sb_puts(&b, p->log_every ? " \"LogEveryConnection\": true,\n" : " \"LogEveryConnection\": false,\n");
+ sb_puts(&b, p->auto_clear ? " \"AutoClearConnectionLogs\": true,\n" : " \"AutoClearConnectionLogs\": false,\n");
+ sb_fmt(&b, " \"FontZoom\": %d,\n", p->font_zoom >= PBP_ZOOM_MIN && p->font_zoom <= PBP_ZOOM_MAX ? p->font_zoom : 100);
sb_fmt(&b, " \"DefaultProxyId\": %u,\n", p->default_proxy_id);
sb_puts(&b, " \"ProxyConfigs\": [");
@@ -631,7 +663,20 @@ char *pbp_serialize(const struct pbp_profile *p)
free(ips);
free(doms);
}
- sb_puts(&b, p->nrules ? "\n ]\n}\n" : "]\n}\n");
+ sb_puts(&b, p->nrules ? "\n ],\n" : "],\n");
+
+ sb_puts(&b, " \"LogFilters\": [");
+ for (size_t i = 0; i < p->nfilters; i++) {
+ const struct pbp_filter *x = &p->filters[i];
+ sb_puts(&b, i ? ",\n {\n" : "\n {\n");
+ sb_kv(&b, " ", "Mode", x->exclude ? "Exclude" : "Include", ",\n");
+ sb_kv(&b, " ", "ProcessName", x->process ? x->process : "", ",\n");
+ sb_kv(&b, " ", "Ip", x->ip ? x->ip : "", ",\n");
+ sb_kv(&b, " ", "Port", x->port ? x->port : "", ",\n");
+ sb_kv(&b, " ", "Protocol", x->protocol ? x->protocol : "All", ",\n");
+ sb_kv(&b, " ", "Action", x->action ? x->action : "All", "\n }");
+ }
+ sb_puts(&b, p->nfilters ? "\n ]\n}\n" : "]\n}\n");
if (b.oom) { free(b.p); return NULL; }
return b.p;
}
@@ -709,7 +754,7 @@ int pbp_save_file(const char *path, const struct pbp_profile *p, char *err, size
return rc;
}
-int pbp_settings_load(const char *path, char **active)
+int pbp_settings_load(const char *path, char **active, bool *check_updates)
{
char err[200];
FILE *f = fopen(path, "r");
@@ -725,23 +770,69 @@ int pbp_settings_load(const char *path, char **active)
if (rc == 0 && root.t == J_OBJ) {
char *a = jtext(&root, "ActiveProfileName");
if (a && *a) { free(*active); *active = a; } else free(a);
+ if (check_updates) *check_updates = jbool(&root, "CheckForUpdatesOnStartup", true);
} else rc = -1;
jfree(&root);
free(buf);
return rc;
}
-int pbp_settings_save(const char *path, const char *active, char *err, size_t errlen)
+int pbp_settings_save(const char *path, const char *active, bool check_updates, char *err, size_t errlen)
{
struct sb b = {0};
sb_puts(&b, "{\n");
- sb_kv(&b, " ", "ActiveProfileName", active ? active : "", "\n}\n");
+ sb_kv(&b, " ", "ActiveProfileName", active ? active : "", ",\n");
+ sb_puts(&b, check_updates ? " \"CheckForUpdatesOnStartup\": true\n}\n" : " \"CheckForUpdatesOnStartup\": false\n}\n");
if (b.oom) { free(b.p); snprintf(err, errlen, "out of memory"); return -1; }
int rc = pbp_write_file(path, b.p, err, errlen);
free(b.p);
return rc;
}
+// ---- log filters ----
+
+static bool ci_glob(const char *p, const char *s)
+{
+ while (*p) {
+ if (*p == '*') {
+ while (*p == '*') p++;
+ if (!*p) return true;
+ for (; *s; s++) if (ci_glob(p, s)) return true;
+ return false;
+ }
+ if (!*s || (*p != '?' && tolower((unsigned char)*p) != tolower((unsigned char)*s))) return false;
+ p++; s++;
+ }
+ return !*s;
+}
+
+static bool pat_ok(const char *pat, const char *actual)
+{
+ if (!pat || !*pat || !strcasecmp(pat, "All")) return true;
+ return ci_glob(pat, actual ? actual : "");
+}
+
+static bool filter_hits(const struct pbp_filter *f, const char *proc, const char *ip, const char *port, const char *proto, const char *action)
+{
+ return pat_ok(f->process, proc) && pat_ok(f->ip, ip) && pat_ok(f->port, port) &&
+ (!f->protocol[0] || !strcasecmp(f->protocol, "All") || !strcasecmp(f->protocol, proto ? proto : "")) &&
+ (!f->action[0] || !strcasecmp(f->action, "All") || !strcasecmp(f->action, action ? action : ""));
+}
+
+bool pbp_filters_pass(const struct pbp_filter *f, size_t n, const char *proc, const char *ip, const char *port, const char *proto, const char *action)
+{
+ if (!n) return true;
+ for (size_t i = 0; i < n; i++)
+ if (f[i].exclude && filter_hits(&f[i], proc, ip, port, proto, action)) return false;
+ bool has_include = false;
+ for (size_t i = 0; i < n; i++)
+ if (!f[i].exclude) {
+ has_include = true;
+ if (filter_hits(&f[i], proc, ip, port, proto, action)) return true;
+ }
+ return !has_include;
+}
+
// ---- profile names and files ----
#define MAX_FILENAME 240
diff --git a/Linux/profile/pb_profile.h b/Linux/profile/pb_profile.h
index c3ece74..b473331 100644
--- a/Linux/profile/pb_profile.h
+++ b/Linux/profile/pb_profile.h
@@ -21,6 +21,9 @@
#define PBP_MAX_PROXIES 256
#define PBP_MAX_RULES 4096
#define PBP_EXT ".pbprofile"
+#define PBP_MAX_FILTERS 64
+#define PBP_ZOOM_MIN 60
+#define PBP_ZOOM_MAX 300
struct pbp_proxy {
uint32_t id; // stable within the file; rules refer to it
@@ -42,11 +45,22 @@ struct pbp_rule {
uint32_t proxy_id; // 0 = default proxy
};
+// A connection-log filter (GUI). Patterns are case-insensitive globs; "" and "*" and "All" match anything.
+struct pbp_filter {
+ bool exclude; // Mode: "Exclude" hides matches, "Include" shows only matches
+ char *process, *ip, *port;
+ char *protocol; // "All", "TCP" or "UDP"
+ char *action; // "All", "Direct", "Proxy" or "Blocked"
+};
+
struct pbp_profile {
char *name;
bool logging; // IsTrafficLoggingEnabled
bool dns_via_proxy; // DnsViaProxy (Linux extension)
bool log_every; // LogEveryConnection: the connection log is not de-duplicated
+ bool auto_clear; // AutoClearConnectionLogs: a log is wiped once it passes 500 lines (default true)
+ int font_zoom; // FontZoom in percent, 60-300 (default 100)
+ struct pbp_filter *filters; size_t nfilters; // LogFilters
uint32_t default_proxy_id; // DefaultProxyId (Linux extension); 0 = first proxy
struct pbp_proxy *proxies; size_t nproxies;
struct pbp_rule *rules; size_t nrules;
@@ -63,9 +77,15 @@ int pbp_write_file(const char *path, const char *data, char *err, size_t errle
// Atomic (temporary file + rename), mode 0600 because the file holds proxy credentials.
int pbp_save_file(const char *path, const struct pbp_profile *p, char *err, size_t errlen);
-// settings.json next to the profiles holds only the name of the active profile; everything else lives in the profile files.
-int pbp_settings_load(const char *path, char **active); // -1 if missing or unreadable
-int pbp_settings_save(const char *path, const char *active, char *err, size_t errlen);
+// settings.json next to the profiles holds the name of the active profile and the update check switch (both are not per profile);
+// everything else lives in the profile files.
+int pbp_settings_load(const char *path, char **active, bool *check_updates); // -1 if missing or unreadable
+int pbp_settings_save(const char *path, const char *active, bool check_updates, char *err, size_t errlen);
+
+// Does a connection pass the filters? Excludes run first (any match hides); then, if there are Include filters, one must match.
+// protocol: "TCP"/"UDP"; action: "Direct"/"Proxy"/"Blocked".
+bool pbp_filters_pass(const struct pbp_filter *f, size_t n, const char *process, const char *ip, const char *port,
+ const char *protocol, const char *action);
// Profile name <-> file name inside a profiles directory (percent-encoding, so any name is safe).
char *pbp_filename_for(const char *name); // malloc'd, includes the extension
diff --git a/Linux/src/ProxyBridge.c b/Linux/src/ProxyBridge.c
index f36ede8..a474cf9 100644
--- a/Linux/src/ProxyBridge.c
+++ b/Linux/src/ProxyBridge.c
@@ -36,6 +36,7 @@ static struct {
uint64_t warned[16]; size_t nwarned;
LogCallback log_cb;
ConnectionCallback conn_cb;
+ ConnectionCallbackEx conn_cb_ex;
} G = { .lock = PTHREAD_RWLOCK_INITIALIZER, .maint_mu = PTHREAD_MUTEX_INITIALIZER, .maint_cv = PTHREAD_COND_INITIALIZER,
.next_rule_id = 1, .next_proxy_id = 1, .dns_via_proxy = true };
@@ -160,6 +161,7 @@ static void emit_connection(uint32_t pid, const char *comm, int family, const ui
snprintf(info, sizeof info, "%s", action == PB_ACT_PROXY ? "PROXY" : action == PB_ACT_BLOCK ? "BLOCK" : "DIRECT");
pthread_mutex_lock(&cb_lock);
if (G.conn_cb) G.conn_cb(name, pid, dest, port, info);
+ if (G.conn_cb_ex) G.conn_cb_ex(name, pid, dest, port, proto == IPPROTO_UDP ? "UDP" : "TCP", info);
pthread_mutex_unlock(&cb_lock);
}
@@ -175,7 +177,13 @@ static void on_bpf_event(const struct pb_event *e)
void pb_core_flow_event(const struct pb_orig *o, int action)
{
if (o->action != PB_ACT_DECIDE) return;
- emit_connection(o->pid, NULL, o->family, o->addr, o->port, o->proto, action, o->proxy_id);
+ char hint[4096];
+ const char *comm = NULL; // the process may be gone already: the kernel's executable identity still names it
+ if (pb_bpf_exe_path(o->exe_dev, o->exe_ino, hint, sizeof hint)) {
+ const char *base = strrchr(hint, '/');
+ comm = base ? base + 1 : hint;
+ }
+ emit_connection(o->pid, comm, o->family, o->addr, o->port, o->proto, action, o->proxy_id);
}
static struct pb_proxy *find_proxy_locked(uint32_t id)
@@ -341,7 +349,7 @@ int pb_core_name_for(const struct pb_orig *o, char *out, size_t n)
void pb_core_decide(const struct pb_orig *o, struct pb_decision *out)
{
char path[4096];
- exe_path_of_pid(o->pid, path, sizeof path);
+ if (!exe_path_of_pid(o->pid, path, sizeof path) && !pb_bpf_exe_path(o->exe_dev, o->exe_ino, path, sizeof path)) path[0] = 0;
out->action = PB_ACT_DIRECT;
out->proxy_id = 0;
@@ -773,6 +781,13 @@ void ProxyBridge_SetLogCallback(LogCallback cb)
pthread_mutex_unlock(&cb_lock);
}
+void ProxyBridge_SetConnectionCallbackEx(ConnectionCallbackEx cb)
+{
+ pthread_mutex_lock(&cb_lock);
+ G.conn_cb_ex = cb;
+ pthread_mutex_unlock(&cb_lock);
+}
+
void ProxyBridge_SetConnectionCallback(ConnectionCallback cb)
{
pthread_mutex_lock(&cb_lock);
diff --git a/Linux/src/ProxyBridge.h b/Linux/src/ProxyBridge.h
index baf074f..c8b65e6 100644
--- a/Linux/src/ProxyBridge.h
+++ b/Linux/src/ProxyBridge.h
@@ -87,6 +87,9 @@ PB_API void ProxyBridge_SetEnabled(bool enable);
PB_API void ProxyBridge_SetLogEveryConnection(bool enable);
PB_API void ProxyBridge_SetLogCallback(LogCallback callback);
PB_API void ProxyBridge_SetConnectionCallback(ConnectionCallback callback);
+/* Same events with the protocol ("TCP" or "UDP") as well; both callbacks may be set. */
+typedef void (*ConnectionCallbackEx)(const char* process_name, uint32_t pid, const char* dest_ip, uint16_t dest_port, const char* protocol, const char* proxy_info);
+PB_API void ProxyBridge_SetConnectionCallbackEx(ConnectionCallbackEx callback);
PB_API void ProxyBridge_SetTrafficLoggingEnabled(bool enable);
PB_API void ProxyBridge_ClearConnectionLogs(void);
PB_API bool ProxyBridge_Start(void); /* needs root (CAP_BPF/CAP_SYS_ADMIN) and a BTF kernel >= 5.11; returns false and logs why on failure */
diff --git a/Linux/src/pb_bpf.c b/Linux/src/pb_bpf.c
index a46801b..f107c6f 100644
--- a/Linux/src/pb_bpf.c
+++ b/Linux/src/pb_bpf.c
@@ -470,6 +470,16 @@ void pb_bpf_set_exempt(const uint32_t *tgids, size_t n)
pthread_mutex_unlock(&G.lock);
}
+bool pb_bpf_exe_path(uint32_t dev, uint64_t ino, char *out, size_t n)
+{
+ bool found = false;
+ pthread_mutex_lock(&G.lock);
+ struct exe_ent *e = ino ? exe_find(dev, ino) : NULL;
+ if (e && e->path) { snprintf(out, n, "%s", e->path); found = true; }
+ pthread_mutex_unlock(&G.lock);
+ return found;
+}
+
void pb_bpf_dom_set(uint32_t rule_idx, uint32_t ip_host)
{
if (!G.skel) return;
diff --git a/Linux/src/pb_bpf.h b/Linux/src/pb_bpf.h
index f1564b6..049a1ba 100644
--- a/Linux/src/pb_bpf.h
+++ b/Linux/src/pb_bpf.h
@@ -41,6 +41,10 @@ void pb_bpf_learn(const struct pb_orig *o, const char *exe_path, const struct pb
void pb_bpf_set_exempt(const uint32_t *tgids, size_t n);
// (rule, address) pairs whose DNS name matches a domain rule.
+// Path of an executable the kernel identified by (dev, ino) that this process has seen before; false if it has not.
+// Lets a decision be made for a process that exited before the relay got to it.
+bool pb_bpf_exe_path(uint32_t dev, uint64_t ino, char *out, size_t n);
+
void pb_bpf_dom_set(uint32_t rule_idx, uint32_t ip_host);
void pb_bpf_dom_clear(void);