Skip to content

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

feat(linux): long Target Host lists and a multiline editor

AnoF-Cyberc468d58fa6b9
diff --git a/Linux/gui/gui.h b/Linux/gui/gui.h
index 97184bb..60b58b9 100644
--- a/Linux/gui/gui.h
+++ b/Linux/gui/gui.h
@@ -70,6 +70,10 @@ GtkWidget *hint_label(const char *text);
void set_error_label(GtkWidget *label, const char *msg);
void grid_row(GtkWidget *grid, int row, const char *label, GtkWidget *widget);
+// Target Host list helpers (gui_list.c): the editor shows one entry per line, the engine gets one clean ";" list.
+char *list_normalize(const char *in); // g_free() the result
+char *list_format(const char *in); // g_free() the result
+
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);
diff --git a/Linux/gui/gui_list.c b/Linux/gui/gui_list.c
new file mode 100644
index 0000000..74a43ec
--- /dev/null
+++ b/Linux/gui/gui_list.c
@@ -0,0 +1,44 @@
+#include "gui.h"
+
+// Target Host list helpers. Line breaks exist only in the editor, for readability; the engine and the
+// saved profile never see them.
+
+// Returns a newly allocated copy without line breaks/control characters, without blanks around the
+// separators (';' or ','), without empty entries, joined with ';'. Never longer than the input.
+char *list_normalize(const char *in) {
+ if (!in) in = "";
+ char *out = g_malloc(strlen(in) + 1);
+ char *w = out;
+ const char *r = in;
+ gboolean any = FALSE;
+ while (*r) {
+ while (*r && (*r == ';' || *r == ',' || (unsigned char)*r <= ' ')) r++; // separators, blanks, line breaks
+ if (!*r) break;
+ if (any) *w++ = ';';
+ char *last = w; // end of the entry without trailing blanks
+ while (*r && *r != ';' && *r != ',') {
+ char c = *r++;
+ if ((unsigned char)c < ' ') continue; // a line break inside an entry is ignored
+ *w++ = c;
+ if (c != ' ') last = w;
+ }
+ w = last;
+ any = TRUE;
+ }
+ *w = 0;
+ return out;
+}
+
+// Returns a newly allocated copy with a line break after every ';' (one entry per line).
+char *list_format(const char *in) {
+ if (!in) in = "";
+ size_t n = strlen(in);
+ char *out = g_malloc(n * 2 + 1); // every ';' grows by one byte
+ char *w = out;
+ for (const char *p = in; *p; p++) {
+ *w++ = *p;
+ if (*p == ';') *w++ = '\n';
+ }
+ *w = 0;
+ return out;
+}
diff --git a/Linux/gui/gui_rules.c b/Linux/gui/gui_rules.c
index 435e9f4..a3b7916 100644
--- a/Linux/gui/gui_rules.c
+++ b/Linux/gui/gui_rules.c
@@ -151,7 +151,16 @@ static void open_rule_dialog(RuleData *rule) {
gtk_box_pack_start(GTK_BOX(proc_box), proc_entry, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(proc_box), browse_btn, FALSE, FALSE, 0);
- GtkWidget *host_entry = gtk_entry_new();
+ // Target Host: multiline for long lists. Line breaks are only for readability and are stripped before
+ // the text reaches the engine; ';' stays the separator.
+ GtkWidget *host_view = gtk_text_view_new();
+ gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(host_view), GTK_WRAP_WORD_CHAR);
+ gtk_text_view_set_accepts_tab(GTK_TEXT_VIEW(host_view), FALSE);
+ GtkWidget *host_entry = gtk_scrolled_window_new(NULL, NULL);
+ gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(host_entry), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
+ gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(host_entry), GTK_SHADOW_IN);
+ gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(host_entry), 90);
+ gtk_container_add(GTK_CONTAINER(host_entry), host_view);
GtkWidget *port_entry = gtk_entry_new();
GtkWidget *proto_combo = combo_new("TCP", "UDP", "BOTH", NULL);
GtkWidget *action_combo = combo_new("PROXY", "DIRECT", "BLOCK", NULL);
@@ -168,7 +177,11 @@ static void open_rule_dialog(RuleData *rule) {
g_signal_connect(action_combo, "changed", G_CALLBACK(on_action_changed), proxy_combo);
gtk_entry_set_text(GTK_ENTRY(proc_entry), rule ? rule->process_name : "");
- gtk_entry_set_text(GTK_ENTRY(host_entry), rule ? rule->target_hosts : "*");
+ {
+ char *shown = list_format(rule && rule->target_hosts && *rule->target_hosts ? rule->target_hosts : "*");
+ gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(host_view)), shown, -1);
+ g_free(shown);
+ }
gtk_entry_set_text(GTK_ENTRY(port_entry), rule ? rule->target_ports : "*");
gtk_combo_box_set_active(GTK_COMBO_BOX(proto_combo), rule ? (int)rule->protocol : RULE_PROTOCOL_BOTH);
gtk_combo_box_set_active(GTK_COMBO_BOX(action_combo), rule ? (int)rule->action : RULE_ACTION_PROXY);
@@ -177,7 +190,7 @@ static void open_rule_dialog(RuleData *rule) {
grid_row(grid, 0, "Process Name:", proc_box);
gtk_grid_attach(GTK_GRID(grid), hint_label("Example: firefox; chrome; /usr/bin/wget"), 1, 1, 1, 1);
grid_row(grid, 2, "Target Host:", host_entry);
- gtk_grid_attach(GTK_GRID(grid), hint_label("Example: 192.168.1.*; 10.0.0.0/8; 10.0.0.1-10.0.0.50; github.com; *.github.com; * (domains are matched via DNS answers)"), 1, 3, 1, 1);
+ gtk_grid_attach(GTK_GRID(grid), hint_label("Example: 192.168.1.*; 10.0.0.0/8; 10.0.0.1-10.0.0.50; github.com; *.github.com; * (domains are matched via DNS answers). Long lists are fine; line breaks are for readability only, separate entries with ;"), 1, 3, 1, 1);
grid_row(grid, 4, "Target Port:", port_entry);
gtk_grid_attach(GTK_GRID(grid), hint_label("Example: 80; 443; 8000-8080; *"), 1, 5, 1, 1);
grid_row(grid, 6, "Protocol:", proto_combo);
@@ -196,9 +209,17 @@ static void open_rule_dialog(RuleData *rule) {
ProxyData *pd = (action == RULE_ACTION_PROXY && psel > 0) ? g_list_nth_data(g_proxies_list, psel - 1) : NULL;
char err[256] = "";
- saved = save_rule(rule, gtk_entry_get_text(GTK_ENTRY(proc_entry)), gtk_entry_get_text(GTK_ENTRY(host_entry)),
+ GtkTextBuffer *hbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(host_view));
+ GtkTextIter hs, he;
+ gtk_text_buffer_get_bounds(hbuf, &hs, &he);
+ char *raw_hosts = gtk_text_buffer_get_text(hbuf, &hs, &he, FALSE);
+ char *clean_hosts = list_normalize(raw_hosts);
+ g_free(raw_hosts);
+
+ saved = save_rule(rule, gtk_entry_get_text(GTK_ENTRY(proc_entry)), clean_hosts,
gtk_entry_get_text(GTK_ENTRY(port_entry)), proto < 0 ? RULE_PROTOCOL_BOTH : (RuleProtocol)proto,
action < 0 ? RULE_ACTION_PROXY : (RuleAction)action, pd ? pd->name : NULL, err, sizeof(err)) != NULL;
+ g_free(clean_hosts);
if (saved) {
save_config();
refresh_rules_ui();
diff --git a/Linux/gui/tests/list_test.c b/Linux/gui/tests/list_test.c
new file mode 100644
index 0000000..65187a9
--- /dev/null
+++ b/Linux/gui/tests/list_test.c
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <glib.h>
+#include "../gui.h"
+
+static int fails;
+#define CHECK(c) do { if (!(c)) { printf("FAIL %s:%d %s\n", __FILE__, __LINE__, #c); fails++; } } while (0)
+
+static void norm(const char *in, const char *want) {
+ char *g = list_normalize(in);
+ if (strcmp(g, want)) { printf("FAIL [%s] -> [%s] want [%s]\n", in, g, want); fails++; }
+ g_free(g);
+}
+
+int main(void) {
+ norm("google.com;\n*.google.com;\n*.gstatic.com;\ngithub.com;\n*.github.com;\n", "google.com;*.google.com;*.gstatic.com;github.com;*.github.com");
+ norm(" a ; b ,c ", "a;b;c");
+ norm("a\r\nb", "ab"); // a line break alone is not a separator
+ norm(";;;", ""); norm("", ""); norm(" \r\n\t ", "");
+ norm("a;;b;;;c;", "a;b;c");
+ norm("*", "*");
+ norm("a b ; c", "a b;c");
+ norm("\x01x\x02;y", "x;y");
+ norm("\xc3\xa9xample.com;\n b.org", "\xc3\xa9xample.com;b.org"); // UTF-8 passes through
+ CHECK(list_normalize(NULL) != NULL);
+
+ char *f = list_format("a;b;c"); CHECK(!strcmp(f, "a;\nb;\nc")); g_free(f);
+ f = list_format(""); CHECK(!strcmp(f, "")); g_free(f);
+ f = list_format("a;"); CHECK(!strcmp(f, "a;\n")); g_free(f);
+ { // normalise(format(x)) == x for a clean list
+ char *x = list_normalize("a.com; b.com ;c.com"); char *y = list_format(x); char *z = list_normalize(y);
+ CHECK(!strcmp(x, z)); g_free(x); g_free(y); g_free(z);
+ }
+ srand(1); // random input: result never longer, never contains control chars or ',', no edge ';'
+ const char alpha[] = "ab;, \r\n\t.*\x01Z";
+ for (int it = 0; it < 100000; it++) {
+ int n = rand() % 80; char *in = g_malloc(n + 1);
+ for (int i = 0; i < n; i++) in[i] = alpha[rand() % (int)(sizeof alpha - 1)];
+ in[n] = 0;
+ char *o = list_normalize(in); size_t m = strlen(o);
+ CHECK(m <= (size_t)n);
+ for (size_t i = 0; i < m; i++) CHECK((unsigned char)o[i] >= ' ' && o[i] != ',');
+ if (m) CHECK(o[0] != ';' && o[m - 1] != ';' && o[0] != ' ' && o[m - 1] != ' ');
+ g_free(o); g_free(in);
+ }
+ printf(fails ? "FAILED %d\n" : "list_test OK\n", fails);
+ return fails ? 1 : 0;
+}
diff --git a/Linux/gui/tests/run_tests.sh b/Linux/gui/tests/run_tests.sh
index cd23bb2..2a9179b 100755
--- a/Linux/gui/tests/run_tests.sh
+++ b/Linux/gui/tests/run_tests.sh
@@ -7,10 +7,12 @@ CF="-Wall -Wextra -Wno-unused-parameter -g -fsanitize=address,undefined -I../src
LF="$(pkg-config --libs gtk+-3.0)"
gcc $CF tests/config_test.c gui_config.c ../profile/pb_profile.c -o "$OUT/config_test" $LF
gcc $CF tests/json_test.c gui_json.c -o "$OUT/json_test" $LF
+gcc $CF tests/list_test.c gui_list.c -o "$OUT/list_test" $LF
gcc -O1 -I../src -I../profile -I. -D_GNU_SOURCE $(pkg-config --cflags gtk+-3.0) tests/config_test.c gui_config.c ../profile/pb_profile.c -o "$OUT/config_plain" $LF
"$OUT/config_plain"
ASAN_OPTIONS=detect_leaks=1 "$OUT/config_test"
ASAN_OPTIONS=detect_leaks=1 "$OUT/json_test"
+ASAN_OPTIONS=detect_leaks=1 "$OUT/list_test"
gcc -O2 -Wall -Wextra -I. tests/events_test.c gui_events.c -o "$OUT/events_plain" -pthread
time "$OUT/events_plain"
gcc -g -Wall -Wextra -fsanitize=address,undefined -I. tests/events_test.c gui_events.c -o "$OUT/events_asan" -pthread
diff --git a/Linux/src/pb_rules.c b/Linux/src/pb_rules.c
index de231af..764d9f2 100644
--- a/Linux/src/pb_rules.c
+++ b/Linux/src/pb_rules.c
@@ -259,15 +259,19 @@ bool pb_rule_parse(struct pb_rule *out, const char *process, const char *hosts,
if (!process) process = "";
if (!hosts) hosts = "";
if (!ports) ports = "";
- if (strlen(process) >= sizeof out->process_src || strlen(hosts) >= sizeof out->hosts_src || strlen(ports) >= sizeof out->ports_src) {
+ if (strlen(process) >= sizeof out->process_src || strlen(ports) >= sizeof out->ports_src) {
set_err(err, errlen, "pattern too long (max 511 characters)");
return false;
}
+ if (strlen(hosts) >= sizeof out->hosts_src) {
+ set_err(err, errlen, "host list too long (max %d characters)", PB_HOSTS_MAX - 1);
+ return false;
+ }
snprintf(out->process_src, sizeof out->process_src, "%s", process);
snprintf(out->hosts_src, sizeof out->hosts_src, "%s", hosts);
snprintf(out->ports_src, sizeof out->ports_src, "%s", ports);
- char (*tok)[256] = malloc(sizeof(char[256]) * PB_MAX_PATTERNS);
+ char (*tok)[256] = malloc(sizeof(char[256]) * PB_MAX_HOST_PATTERNS); // sized for the longest list (hosts)
if (!tok) { set_err(err, errlen, "out of memory"); return false; }
if (is_any(process)) out->any_proc = true;
@@ -285,8 +289,8 @@ bool pb_rule_parse(struct pb_rule *out, const char *process, const char *hosts,
if (is_any(hosts)) out->any_host = true;
else {
- int n = split_list(hosts, tok, PB_MAX_PATTERNS);
- if (n < 0) { set_err(err, errlen, "too many host patterns (max %d)", PB_MAX_PATTERNS); goto fail; }
+ int n = split_list(hosts, tok, PB_MAX_HOST_PATTERNS);
+ if (n < 0) { set_err(err, errlen, "too many host patterns (max %d)", PB_MAX_HOST_PATTERNS); goto fail; }
out->hosts = calloc((size_t)(n ? n : 1), sizeof *out->hosts);
out->domains = malloc(sizeof(char[256]) * (size_t)(n ? n : 1));
if (!out->hosts || !out->domains) goto oom;
diff --git a/Linux/src/pb_rules.h b/Linux/src/pb_rules.h
index 02cec44..a2dfc02 100644
--- a/Linux/src/pb_rules.h
+++ b/Linux/src/pb_rules.h
@@ -4,7 +4,9 @@
#include "pb_internal.h"
-#define PB_MAX_PATTERNS 64
+#define PB_MAX_PATTERNS 64 // process and port lists
+#define PB_MAX_HOST_PATTERNS 1024 // Target Host list (IPs, ranges and domains together)
+#define PB_HOSTS_MAX 8192 // longest Target Host text, including the terminating NUL
enum { PB_HOST_V4_RANGE = 1, PB_HOST_V4_BOX, PB_HOST_V6_RANGE };
@@ -24,7 +26,7 @@ struct pb_rule {
RuleAction action;
uint32_t proxy_id; // action == PROXY: 0 = default proxy
- char process_src[512], hosts_src[512], ports_src[512]; // original text, kept for the UI / export
+ char process_src[512], hosts_src[PB_HOSTS_MAX], ports_src[512]; // original text, kept for the UI / export
bool any_proc;
char (*procs)[256]; int nprocs;
diff --git a/Linux/src/tests/rules_test.c b/Linux/src/tests/rules_test.c
index 5934631..b8a3988 100644
--- a/Linux/src/tests/rules_test.c
+++ b/Linux/src/tests/rules_test.c
@@ -92,6 +92,28 @@ int main(void)
r = R("", "github.com;*.github.com", ""); CHECK(r.ndomains == 2); pb_rule_free(&r);
CHECK(pb_domain_match("a?c.com", "abc.com")); CHECK(!pb_domain_match("a?c.com", "abbc.com")); CHECK(pb_domain_match("*", "x.y"));
CHECK(bad("", "bad name.com", ""));
+
+ { // long Target Host lists: hundreds of domains, up to PB_HOSTS_MAX - 1 characters, and the limits themselves
+ static char big[PB_HOSTS_MAX + 64];
+ size_t o = 0; int n = 0;
+ while (o + 24 < 7000) { o += (size_t)snprintf(big + o, sizeof big - o, "host%d.example.org;", n++); }
+ r = R("", big, ""); CHECK(r.ndomains == n); CHECK(r.hosts_src[0] == 'h');
+ CHECK(pb_rule_match_dest(&r, AF_INET, (a4(a, "1.2.3.4"), a), 443, (const char *[]){ "host77.example.org" }, 1));
+ CHECK(!pb_rule_match_dest(&r, AF_INET, (a4(a, "1.2.3.4"), a), 443, (const char *[]){ "other.example.org" }, 1));
+ pb_rule_free(&r);
+
+ struct pb_rule t; char e[128];
+ memset(big, 'a', PB_HOSTS_MAX); big[PB_HOSTS_MAX] = 0; // 8192 characters: rejected by the length limit
+ CHECK(!pb_rule_parse(&t, "", big, "", e, sizeof e)); CHECK(strstr(e, "too long") != NULL);
+
+ for (int k = 0; k < 2; k++) { // exactly PB_MAX_HOST_PATTERNS entries parse, one more is rejected
+ o = 0;
+ for (int i = 0; i < PB_MAX_HOST_PATTERNS + k; i++) o += (size_t)snprintf(big + o, sizeof big - o, "x.y;");
+ if (k == 0) { CHECK(pb_rule_parse(&t, "", big, "", e, sizeof e)); pb_rule_free(&t); }
+ else { CHECK(!pb_rule_parse(&t, "", big, "", e, sizeof e)); CHECK(strstr(e, "too many") != NULL); }
+ }
+ CHECK(bad("a1;a2;a3;a4;a5;a6;a7;a8;a9;a10;a11;a12;a13;a14;a15;a16;a17;a18;a19;a20;a21;a22;a23;a24;a25;a26;a27;a28;a29;a30;a31;a32;a33;a34;a35;a36;a37;a38;a39;a40;a41;a42;a43;a44;a45;a46;a47;a48;a49;a50;a51;a52;a53;a54;a55;a56;a57;a58;a59;a60;a61;a62;a63;a64;a65", "", "")); // process list limit unchanged
+ }
printf(fails ? "RULES TESTS FAILED (%d)\n" : "rules tests passed\n", fails);
return fails != 0;
}