Skip to content

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

added local storage support

anof-cyberc34f266556fd
diff --git a/Linux/gui/gui.h b/Linux/gui/gui.h
index 777f2bf..035c1ee 100644
--- a/Linux/gui/gui.h
+++ b/Linux/gui/gui.h
@@ -75,6 +75,8 @@ extern char g_proxy_user[256];
extern char g_proxy_pass[256];
extern GList *g_rules_list;
+extern bool g_chk_logging;
+extern bool g_chk_dns;
long safe_strtol(const char *nptr);
void show_message(GtkWindow *parent, GtkMessageType type, const char *format, ...);
@@ -84,6 +86,10 @@ char *escape_json_string(const char *src);
char *extract_sub_json_str(const char *json, const char *key);
bool extract_sub_json_bool(const char *json, const char *key);
+// config storage
+void save_config();
+void load_config();
+
// settings
void on_proxy_configure(GtkWidget *widget, gpointer data);
diff --git a/Linux/gui/gui_config.c b/Linux/gui/gui_config.c
new file mode 100644
index 0000000..ee184dc
--- /dev/null
+++ b/Linux/gui/gui_config.c
@@ -0,0 +1,109 @@
+#include "gui.h"
+
+#define CONFIG_DIR "/etc/proxybridge"
+#define CONFIG_PATH "/etc/proxybridge/config.ini"
+
+// save settings to file
+void save_config() {
+ struct stat st = {0};
+ if (stat(CONFIG_DIR, &st) == -1) {
+ if (mkdir(CONFIG_DIR, 0755) != 0) {
+ perror("failed to create config dir");
+ return;
+ }
+ }
+
+ FILE *f = fopen(CONFIG_PATH, "w");
+ if (!f) {
+ printf("failed to save config to %s\n", CONFIG_PATH);
+ return;
+ }
+
+ // settings section
+ fprintf(f, "[SETTINGS]\n");
+ fprintf(f, "ip=%s\n", g_proxy_ip);
+ fprintf(f, "port=%d\n", g_proxy_port);
+ fprintf(f, "type=%d\n", g_proxy_type);
+ fprintf(f, "user=%s\n", g_proxy_user);
+ fprintf(f, "pass=%s\n", g_proxy_pass);
+ fprintf(f, "logging=%d\n", g_chk_logging);
+ fprintf(f, "dns=%d\n", g_chk_dns);
+
+ // rules section
+ fprintf(f, "[RULES]\n");
+ for (GList *l = g_rules_list; l != NULL; l = l->next) {
+ RuleData *rule = (RuleData *)l->data;
+ // format: id|protocol|action|enabled|procname|hosts|ports
+ fprintf(f, "%u|%d|%d|%d|%s|%s|%s\n",
+ rule->id,
+ rule->protocol,
+ rule->action,
+ rule->enabled,
+ rule->process_name ? rule->process_name : "",
+ rule->target_hosts ? rule->target_hosts : "",
+ rule->target_ports ? rule->target_ports : ""
+ );
+ }
+
+ fclose(f);
+}
+
+// load settings from file
+void load_config() {
+ FILE *f = fopen(CONFIG_PATH, "r");
+ if (!f) return;
+
+ char line[2048];
+ int section = 0; // 0=none, 1=settings, 2=rules
+
+ while (fgets(line, sizeof(line), f)) {
+ // trim newline
+ line[strcspn(line, "\r\n")] = 0;
+
+ if (strlen(line) == 0 || line[0] == '#') continue;
+
+ if (strcmp(line, "[SETTINGS]") == 0) { section = 1; continue; }
+ if (strcmp(line, "[RULES]") == 0) { section = 2; continue; }
+
+ if (section == 1) {
+ char *val = strchr(line, '=');
+ if (!val) continue;
+ *val = 0; val++;
+
+ if (strcmp(line, "ip") == 0) strncpy(g_proxy_ip, val, sizeof(g_proxy_ip) - 1);
+ else if (strcmp(line, "port") == 0) g_proxy_port = atoi(val);
+ else if (strcmp(line, "type") == 0) g_proxy_type = atoi(val);
+ else if (strcmp(line, "user") == 0) strncpy(g_proxy_user, val, sizeof(g_proxy_user) - 1);
+ else if (strcmp(line, "pass") == 0) strncpy(g_proxy_pass, val, sizeof(g_proxy_pass) - 1);
+ else if (strcmp(line, "logging") == 0) g_chk_logging = atoi(val);
+ else if (strcmp(line, "dns") == 0) g_chk_dns = atoi(val);
+ }
+ else if (section == 2) {
+ // parse rule line
+ RuleData *rule = g_malloc0(sizeof(RuleData));
+ char *p = line;
+ char *token;
+ int idx = 0;
+
+ while ((token = strsep(&p, "|")) != NULL) {
+ switch(idx) {
+ case 0: rule->id = atoi(token); break;
+ case 1: rule->protocol = atoi(token); break;
+ case 2: rule->action = atoi(token); break;
+ case 3: rule->enabled = atoi(token); break;
+ case 4: rule->process_name = g_strdup(token); break;
+ case 5: rule->target_hosts = g_strdup(token); break;
+ case 6: rule->target_ports = g_strdup(token); break;
+ }
+ idx++;
+ }
+
+ if (idx >= 4) {
+ g_rules_list = g_list_append(g_rules_list, rule);
+ } else {
+ g_free(rule);
+ }
+ }
+ }
+ fclose(f);
+}
\ No newline at end of file
diff --git a/Linux/gui/gui_rules.c b/Linux/gui/gui_rules.c
index 2beca0f..9a8fde0 100644
--- a/Linux/gui/gui_rules.c
+++ b/Linux/gui/gui_rules.c
@@ -20,6 +20,7 @@ static void on_rule_delete(GtkWidget *widget, gpointer data) {
ProxyBridge_DeleteRule(rule->id);
g_rules_list = g_list_remove(g_rules_list, rule);
free_rule_data(rule);
+ save_config();
refresh_rules_ui();
}
@@ -28,6 +29,7 @@ static void on_rule_toggle(GtkToggleButton *btn, gpointer data) {
rule->enabled = gtk_toggle_button_get_active(btn);
if (rule->enabled) ProxyBridge_EnableRule(rule->id);
else ProxyBridge_DisableRule(rule->id);
+ save_config();
}
static void on_save_rule(GtkWidget *widget, gpointer data) {
@@ -66,6 +68,7 @@ static void on_save_rule(GtkWidget *widget, gpointer data) {
new_rule->selected = false;
g_rules_list = g_list_append(g_rules_list, new_rule);
}
+ save_config();
refresh_rules_ui();
gtk_widget_destroy(dialog);
free(widgets);
diff --git a/Linux/gui/gui_settings.c b/Linux/gui/gui_settings.c
index 2cad586..c034cf1 100644
--- a/Linux/gui/gui_settings.c
+++ b/Linux/gui/gui_settings.c
@@ -209,6 +209,8 @@ void on_proxy_configure(GtkWidget *widget, gpointer data) {
ProxyBridge_SetProxyConfig(g_proxy_type, g_proxy_ip, g_proxy_port, g_proxy_user, g_proxy_pass);
+ save_config();
+
char status_msg[512];
snprintf(status_msg, sizeof(status_msg), "Configuration updated: %s:%d", g_proxy_ip, g_proxy_port);
gtk_statusbar_push(GTK_STATUSBAR(status_bar), status_context_id, status_msg);
diff --git a/Linux/gui/main.c b/Linux/gui/main.c
index da7efb9..025cf65 100644
--- a/Linux/gui/main.c
+++ b/Linux/gui/main.c
@@ -17,15 +17,21 @@ char g_proxy_user[256] = "";
char g_proxy_pass[256] = "";
GList *g_rules_list = NULL;
+bool g_chk_logging = true;
+bool g_chk_dns = true;
static void on_log_traffic_toggled(GtkCheckMenuItem *item, gpointer data) {
bool active = gtk_check_menu_item_get_active(item);
ProxyBridge_SetTrafficLoggingEnabled(active);
+ g_chk_logging = active;
+ save_config();
}
static void on_dns_proxy_toggled(GtkCheckMenuItem *item, gpointer data) {
bool active = gtk_check_menu_item_get_active(item);
ProxyBridge_SetDnsViaProxy(active);
+ g_chk_dns = active;
+ save_config();
}
static void on_create_update_script_and_run() {
@@ -119,6 +125,10 @@ int main(int argc, char *argv[]) {
if (getuid() != 0) { gtk_init(&argc, &argv); show_message(NULL, GTK_MESSAGE_ERROR, "ProxyBridge must be run as root (sudo)."); return 1; }
setenv("GSETTINGS_BACKEND", "memory", 1);
+
+ // load saved config
+ load_config();
+
gtk_init(&argc, &argv);
GtkSettings *settings = gtk_settings_get_default();
@@ -140,11 +150,11 @@ int main(int argc, char *argv[]) {
GtkWidget *rules_item = gtk_menu_item_new_with_label("Proxy Rules");
GtkWidget *log_check_item = gtk_check_menu_item_new_with_label("Enable Traffic Logging");
- gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(log_check_item), TRUE);
+ gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(log_check_item), g_chk_logging);
g_signal_connect(log_check_item, "toggled", G_CALLBACK(on_log_traffic_toggled), NULL);
GtkWidget *dns_check_item = gtk_check_menu_item_new_with_label("DNS via Proxy");
- gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(dns_check_item), TRUE);
+ gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(dns_check_item), g_chk_dns);
g_signal_connect(dns_check_item, "toggled", G_CALLBACK(on_dns_proxy_toggled), NULL);
GtkWidget *exit_item = gtk_menu_item_new_with_label("Exit");
@@ -232,10 +242,21 @@ int main(int argc, char *argv[]) {
// start it up
ProxyBridge_SetLogCallback(lib_log_callback);
ProxyBridge_SetConnectionCallback(lib_connection_callback);
- ProxyBridge_SetTrafficLoggingEnabled(true);
+ ProxyBridge_SetTrafficLoggingEnabled(g_chk_logging);
+ ProxyBridge_SetDnsViaProxy(g_chk_dns);
if (ProxyBridge_Start()) {
- gtk_statusbar_push(GTK_STATUSBAR(status_bar), status_context_id, "ProxyBridge Service Started. Please configure proxy settings.");
+ // apply config
+ ProxyBridge_SetProxyConfig(g_proxy_type, g_proxy_ip, g_proxy_port, g_proxy_user, g_proxy_pass);
+
+ // restore rules
+ for (GList *l = g_rules_list; l != NULL; l = l->next) {
+ RuleData *r = (RuleData *)l->data;
+ r->id = ProxyBridge_AddRule(r->process_name, r->target_hosts, r->target_ports, r->protocol, r->action);
+ if (!r->enabled) ProxyBridge_DisableRule(r->id);
+ }
+
+ gtk_statusbar_push(GTK_STATUSBAR(status_bar), status_context_id, "ProxyBridge Service Started.");
} else {
gtk_statusbar_push(GTK_STATUSBAR(status_bar), status_context_id, "Failed to start ProxyBridge engine.");
}