Skip to content

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

removed unwanted code for linux

Sourav Kalal3406f9f51ee5
diff --git a/Linux/cli/main.c b/Linux/cli/main.c
index b8807de..6781f14 100644
--- a/Linux/cli/main.c
+++ b/Linux/cli/main.c
@@ -227,7 +227,7 @@ static bool parse_proxy_url(const char* url, ProxyType* type, char* host, uint16
username[0] = '\0';
password[0] = '\0';
- // Parse type://
+ // parse type://
char* scheme_end = strstr(buffer, "://");
if (scheme_end == NULL)
{
@@ -254,7 +254,7 @@ static bool parse_proxy_url(const char* url, ProxyType* type, char* host, uint16
return false;
}
- // Parse host:port[:username:password]
+ // parse host:port[:user:pass]
char* parts[4];
int num_parts = 0;
char* token = strtok(rest, ":");
@@ -298,7 +298,7 @@ static bool is_root(void)
int main(int argc, char *argv[])
{
- // Check for cleanup flag first - ignores all other args
+ // check cleanup flag
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "--cleanup") == 0)
@@ -315,7 +315,7 @@ int main(int argc, char *argv[])
int num_rules = 0;
bool dns_via_proxy = true;
- // Parse arguments
+ // parse args
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
@@ -371,7 +371,7 @@ int main(int argc, char *argv[])
show_banner();
- // Check root privileges
+ // need root
if (!is_root())
{
printf("\033[31m\nERROR: ProxyBridge requires root privileges!\033[0m\n");
@@ -379,7 +379,7 @@ int main(int argc, char *argv[])
return 1;
}
- // Parse proxy configuration
+ // parse proxy config
ProxyType proxy_type;
char proxy_host[256];
uint16_t proxy_port;
@@ -389,11 +389,8 @@ int main(int argc, char *argv[])
if (!parse_proxy_url(proxy_url, &proxy_type, proxy_host, &proxy_port, proxy_username, proxy_password))
return 1;
- // Setup callbacks based on verbose level - only enable what we need
- // Verbose 0: No callbacks, no logging (most efficient)
- // Verbose 1: Only log messages
- // Verbose 2: Only connection events
- // Verbose 3: Both logs and connections
+ // setup callbacks based on verbose
+ // 0=nothing 1=logs 2=connections 3=both
if (verbose_level == 1 || verbose_level == 3)
ProxyBridge_SetLogCallback(log_callback);
@@ -405,10 +402,10 @@ int main(int argc, char *argv[])
else
ProxyBridge_SetConnectionCallback(NULL); // Explicitly disable
- // Enable traffic logging in C library only when needed (prevents unnecessary processing)
+ // turn on traffic logging when needed
ProxyBridge_SetTrafficLoggingEnabled(verbose_level > 0);
- // Display configuration
+ // show config
printf("Proxy: %s://%s:%u\n",
proxy_type == PROXY_TYPE_HTTP ? "http" : "socks5",
proxy_host, proxy_port);
@@ -418,7 +415,7 @@ int main(int argc, char *argv[])
printf("DNS via Proxy: %s\n", dns_via_proxy ? "Enabled" : "Disabled");
- // Configure proxy
+ // setup proxy
if (!ProxyBridge_SetProxyConfig(proxy_type, proxy_host, proxy_port,
proxy_username[0] ? proxy_username : "",
proxy_password[0] ? proxy_password : ""))
@@ -429,7 +426,7 @@ int main(int argc, char *argv[])
ProxyBridge_SetDnsViaProxy(dns_via_proxy);
- // Add rules
+ // add rules
if (num_rules > 0)
{
printf("Rules: %d\n", num_rules);
@@ -469,7 +466,7 @@ int main(int argc, char *argv[])
printf("Use --rule to add proxy rules. See --help for examples.\n");
}
- // Start ProxyBridge
+ // start proxybridge
if (!ProxyBridge_Start())
{
fprintf(stderr, "ERROR: Failed to start ProxyBridge\n");
@@ -485,13 +482,13 @@ int main(int argc, char *argv[])
signal(SIGABRT, signal_handler); // Catch abort
signal(SIGBUS, signal_handler); // Catch bus error
- // Main loop
+ // main loop
while (keep_running)
{
sleep(1);
}
- // Cleanup
+ // cleanup
ProxyBridge_Stop();
printf("ProxyBridge stopped.\n");
diff --git a/Linux/gui/gui_config.c b/Linux/gui/gui_config.c
index ee184dc..0abf955 100644
--- a/Linux/gui/gui_config.c
+++ b/Linux/gui/gui_config.c
@@ -3,7 +3,7 @@
#define CONFIG_DIR "/etc/proxybridge"
#define CONFIG_PATH "/etc/proxybridge/config.ini"
-// save settings to file
+// save config
void save_config() {
struct stat st = {0};
if (stat(CONFIG_DIR, &st) == -1) {
diff --git a/Linux/gui/gui_logs.c b/Linux/gui/gui_logs.c
index 3410269..3ff4c68 100644
--- a/Linux/gui/gui_logs.c
+++ b/Linux/gui/gui_logs.c
@@ -1,6 +1,6 @@
#include "gui.h"
-// filter the text view based on user imput
+// filter logs based on search
static void filter_text_view(GtkTextBuffer *buffer, const char *text) {
if (!buffer) return;
@@ -18,7 +18,7 @@ static void filter_text_view(GtkTextBuffer *buffer, const char *text) {
char *line_text = gtk_text_buffer_get_text(buffer, &line_start, &line_end, FALSE);
- // case insensitive search
+ // search case insensitive
char *lower_line = g_utf8_strdown(line_text, -1);
char *lower_search = g_utf8_strdown(text, -1);
@@ -64,7 +64,7 @@ static void free_connection_data(ConnectionData *data) {
}
}
-// update logs safely from main thread
+// update logs from main thread
static gboolean update_log_gui(gpointer user_data) {
LogData *data = (LogData *)user_data;
if (!data) return FALSE;
@@ -86,7 +86,7 @@ static gboolean update_log_gui(gpointer user_data) {
return FALSE; // done
}
-// update connection info safely
+// update conn info
static gboolean update_connection_gui_append(gpointer user_data) {
ConnectionData *data = (ConnectionData *)user_data;
if (!data) return FALSE;
diff --git a/Linux/gui/gui_rules.c b/Linux/gui/gui_rules.c
index 9a8fde0..df625ed 100644
--- a/Linux/gui/gui_rules.c
+++ b/Linux/gui/gui_rules.c
@@ -3,7 +3,7 @@
static GtkWidget *rules_list_box = NULL;
static GtkWidget *btn_select_all_header = NULL;
-// forward decl
+// forward
static void refresh_rules_ui();
static void free_rule_data(RuleData *rule) {
@@ -36,7 +36,7 @@ static void on_save_rule(GtkWidget *widget, gpointer data) {
GtkWidget **widgets = (GtkWidget **)data;
GtkWidget *dialog = widgets[0];
RuleData *edit_rule = (RuleData *)widgets[6]; // existing rule if present
-
+
const char *proc = gtk_entry_get_text(GTK_ENTRY(widgets[1]));
const char *hosts = gtk_entry_get_text(GTK_ENTRY(widgets[2]));
const char *ports = gtk_entry_get_text(GTK_ENTRY(widgets[3]));
@@ -44,18 +44,18 @@ static void on_save_rule(GtkWidget *widget, gpointer data) {
RuleAction action = gtk_combo_box_get_active(GTK_COMBO_BOX(widgets[5]));
if (strlen(proc) == 0) return; // need process name
-
+
if (edit_rule) {
- // update backend
+ // update backend and local copy
ProxyBridge_EditRule(edit_rule->id, proc, hosts, ports, proto, action);
- // update local
+
free(edit_rule->process_name); edit_rule->process_name = strdup(proc);
free(edit_rule->target_hosts); edit_rule->target_hosts = strdup(hosts);
free(edit_rule->target_ports); edit_rule->target_ports = strdup(ports);
edit_rule->protocol = proto;
edit_rule->action = action;
} else {
- // new rule
+ // add new rule
uint32_t new_id = ProxyBridge_AddRule(proc, hosts, ports, proto, action);
RuleData *new_rule = malloc(sizeof(RuleData));
new_rule->id = new_id;
@@ -64,7 +64,7 @@ static void on_save_rule(GtkWidget *widget, gpointer data) {
new_rule->target_ports = strdup(ports);
new_rule->protocol = proto;
new_rule->action = action;
- new_rule->enabled = true;
+ new_rule->enabled = true;
new_rule->selected = false;
g_rules_list = g_list_append(g_rules_list, new_rule);
}
@@ -104,8 +104,8 @@ static void open_rule_dialog(RuleData *rule) {
gtk_grid_set_row_spacing(GTK_GRID(grid), 8);
gtk_grid_set_column_spacing(GTK_GRID(grid), 10);
gtk_container_set_border_width(GTK_CONTAINER(grid), 15);
-
- // process input
+
+ // proc input
GtkWidget *proc_entry = gtk_entry_new();
GtkWidget *browse_btn = gtk_button_new_with_label("Browse...");
g_signal_connect(browse_btn, "clicked", G_CALLBACK(on_browse_clicked), proc_entry);
@@ -115,13 +115,13 @@ static void open_rule_dialog(RuleData *rule) {
GtkWidget *host_entry = gtk_entry_new();
GtkWidget *port_entry = gtk_entry_new();
-
+
// protocol list
GtkWidget *proto_combo = gtk_combo_box_text_new();
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(proto_combo), "TCP");
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(proto_combo), "UDP");
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(proto_combo), "BOTH");
-
+
// action list
GtkWidget *action_combo = gtk_combo_box_text_new();
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(action_combo), "PROXY");
@@ -135,8 +135,8 @@ static void open_rule_dialog(RuleData *rule) {
gtk_combo_box_set_active(GTK_COMBO_BOX(proto_combo), rule->protocol);
gtk_combo_box_set_active(GTK_COMBO_BOX(action_combo), rule->action);
} else {
- gtk_entry_set_text(GTK_ENTRY(port_entry), "*");
- gtk_entry_set_text(GTK_ENTRY(host_entry), "*");
+ gtk_entry_set_text(GTK_ENTRY(port_entry), "*");
+ gtk_entry_set_text(GTK_ENTRY(host_entry), "*");
gtk_combo_box_set_active(GTK_COMBO_BOX(proto_combo), 2); // BOTH default
gtk_combo_box_set_active(GTK_COMBO_BOX(action_combo), 0); // PROXY default
}
@@ -144,7 +144,7 @@ static void open_rule_dialog(RuleData *rule) {
int row = 0;
gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Process Name:"), 0, row, 1, 1);
gtk_grid_attach(GTK_GRID(grid), proc_box, 1, row, 1, 1); row++;
-
+
GtkWidget *proc_hint = gtk_label_new("Example: firefox; chrome; /usr/bin/wget");
gtk_style_context_add_class(gtk_widget_get_style_context(proc_hint), "dim-label");
gtk_widget_set_halign(proc_hint, GTK_ALIGN_START);
@@ -152,7 +152,7 @@ static void open_rule_dialog(RuleData *rule) {
gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Target Host:"), 0, row, 1, 1);
gtk_grid_attach(GTK_GRID(grid), host_entry, 1, row, 1, 1); row++;
-
+
GtkWidget *host_hint = gtk_label_new("Example: 192.168.1.*; 10.0.0.1-50; *");
gtk_style_context_add_class(gtk_widget_get_style_context(host_hint), "dim-label");
gtk_widget_set_halign(host_hint, GTK_ALIGN_START);
@@ -160,25 +160,25 @@ static void open_rule_dialog(RuleData *rule) {
gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Target Port:"), 0, row, 1, 1);
gtk_grid_attach(GTK_GRID(grid), port_entry, 1, row, 1, 1); row++;
-
+
GtkWidget *port_hint = gtk_label_new("Example: 80; 443; 8000-8080; *");
gtk_style_context_add_class(gtk_widget_get_style_context(port_hint), "dim-label");
gtk_widget_set_halign(port_hint, GTK_ALIGN_START);
gtk_grid_attach(GTK_GRID(grid), port_hint, 1, row, 1, 1); row++;
-
+
gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Protocol:"), 0, row, 1, 1);
gtk_grid_attach(GTK_GRID(grid), proto_combo, 1, row, 1, 1); row++;
-
+
gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Action:"), 0, row, 1, 1);
gtk_grid_attach(GTK_GRID(grid), action_combo, 1, row, 1, 1); row++;
gtk_container_add(GTK_CONTAINER(content), grid);
-
+
GtkWidget *save_btn = gtk_button_new_with_label("Save");
GtkWidget *cancel_btn = gtk_button_new_with_label("Cancel");
gtk_dialog_add_action_widget(GTK_DIALOG(dialog), cancel_btn, GTK_RESPONSE_CANCEL);
gtk_dialog_add_action_widget(GTK_DIALOG(dialog), save_btn, GTK_RESPONSE_ACCEPT);
-
+
GtkWidget **data = malloc(7 * sizeof(GtkWidget*));
data[0] = dialog;
data[1] = proc_entry;
@@ -187,15 +187,15 @@ static void open_rule_dialog(RuleData *rule) {
data[4] = proto_combo;
data[5] = action_combo;
data[6] = (GtkWidget*)rule;
-
+
g_signal_connect(save_btn, "clicked", G_CALLBACK(on_save_rule), data);
- g_signal_connect(cancel_btn, "clicked", G_CALLBACK(gtk_widget_destroy), NULL);
+ g_signal_connect(cancel_btn, "clicked", G_CALLBACK(gtk_widget_destroy), NULL);
gtk_widget_show_all(dialog);
}
static void on_rule_edit(GtkWidget *widget, gpointer data) {
on_proxy_rules_clicked(NULL, NULL); // Re-open if closed? Usually modal.
- // Wait, on_rule_edit is clicked FROM the rules list.
+ // Wait, on_rule_edit clicked from rules list already
open_rule_dialog((RuleData *)data);
}
@@ -274,14 +274,14 @@ static void on_rule_import_clicked(GtkWidget *widget, gpointer data) {
char *end = strchr(curr, '}');
if (!end) break;
char saved = *end; *end = '\0';
-
+
char *proc = extract_sub_json_str(curr, "processNames");
char *host = extract_sub_json_str(curr, "targetHosts");
char *port = extract_sub_json_str(curr, "targetPorts");
char *proto_s = extract_sub_json_str(curr, "protocol");
char *act_s = extract_sub_json_str(curr, "action");
bool en = extract_sub_json_bool(curr, "enabled");
-
+
if (proc && host && port && proto_s && act_s) {
RuleProtocol p = RULE_PROTOCOL_BOTH;
if (strcmp(proto_s, "TCP") == 0) p = RULE_PROTOCOL_TCP;
@@ -289,7 +289,7 @@ static void on_rule_import_clicked(GtkWidget *widget, gpointer data) {
RuleAction a = RULE_ACTION_PROXY;
if (strcmp(act_s, "DIRECT") == 0) a = RULE_ACTION_DIRECT;
else if (strcmp(act_s, "BLOCK") == 0) a = RULE_ACTION_BLOCK;
-
+
uint32_t nid = ProxyBridge_AddRule(proc, host, port, p, a);
if (!en) ProxyBridge_DisableRule(nid);
RuleData *nd = malloc(sizeof(RuleData));
@@ -354,7 +354,7 @@ static void refresh_rules_ui() {
}
gtk_button_set_label(GTK_BUTTON(btn_select_all_header), all_selected ? "Deselect All" : "Select All");
}
-
+
GtkWidget *grid = gtk_grid_new();
gtk_grid_set_row_spacing(GTK_GRID(grid), 10);
gtk_grid_set_column_spacing(GTK_GRID(grid), 15);
@@ -386,7 +386,7 @@ static void refresh_rules_ui() {
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chk), r->enabled);
g_signal_connect(chk, "toggled", G_CALLBACK(on_rule_toggle), r);
gtk_grid_attach(GTK_GRID(grid), chk, 1, row, 1, 1);
-
+
GtkWidget *act_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
GtkWidget *btn_edit = gtk_button_new_with_label("Edit");
g_signal_connect(btn_edit, "clicked", G_CALLBACK(on_rule_edit), r);
@@ -395,21 +395,21 @@ static void refresh_rules_ui() {
gtk_box_pack_start(GTK_BOX(act_box), btn_edit, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(act_box), btn_del, FALSE, FALSE, 0);
gtk_grid_attach(GTK_GRID(grid), act_box, 2, row, 1, 1);
-
+
char sr_str[16]; snprintf(sr_str, sizeof(sr_str), "%d", sr_counter++);
gtk_grid_attach(GTK_GRID(grid), gtk_label_new(sr_str), 3, row, 1, 1);
-
+
GtkWidget *l_proc = gtk_label_new(r->process_name); gtk_widget_set_halign(l_proc, GTK_ALIGN_START);
gtk_label_set_ellipsize(GTK_LABEL(l_proc), PANGO_ELLIPSIZE_END);
gtk_grid_attach(GTK_GRID(grid), l_proc, 4, row, 1, 1);
-
+
GtkWidget *l_host = gtk_label_new(r->target_hosts); gtk_widget_set_halign(l_host, GTK_ALIGN_START);
gtk_label_set_ellipsize(GTK_LABEL(l_host), PANGO_ELLIPSIZE_END);
gtk_grid_attach(GTK_GRID(grid), l_host, 5, row, 1, 1);
-
+
const char* proto_strs[] = {"TCP", "UDP", "BOTH"};
gtk_grid_attach(GTK_GRID(grid), gtk_label_new(proto_strs[r->protocol]), 6, row, 1, 1);
-
+
const char* action_strs[] = {"PROXY", "DIRECT", "BLOCK"};
GtkWidget *l_act = gtk_label_new(action_strs[r->action]);
GtkStyleContext *context = gtk_widget_get_style_context(l_act);
@@ -417,7 +417,7 @@ static void refresh_rules_ui() {
else if (r->action == RULE_ACTION_DIRECT) gtk_style_context_add_class(context, "info");
else gtk_style_context_add_class(context, "warning");
gtk_grid_attach(GTK_GRID(grid), l_act, 7, row, 1, 1);
-
+
row++;
gtk_grid_attach(GTK_GRID(grid), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), 0, row, 8, 1); row++;
}
@@ -432,7 +432,7 @@ void on_proxy_rules_clicked(GtkWidget *widget, gpointer data) {
GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
gtk_container_set_border_width(GTK_CONTAINER(vbox), 20);
gtk_container_add(GTK_CONTAINER(dialog), vbox);
-
+
GtkWidget *header_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
GtkWidget *title = gtk_label_new(NULL);
gtk_label_set_markup(GTK_LABEL(title), "<span size='x-large' weight='bold'>Process Rules</span>");
@@ -457,13 +457,13 @@ void on_proxy_rules_clicked(GtkWidget *widget, gpointer data) {
gtk_box_pack_end(GTK_BOX(header_box), export_btn, FALSE, FALSE, 5);
gtk_box_pack_end(GTK_BOX(header_box), import_btn, FALSE, FALSE, 5);
gtk_box_pack_start(GTK_BOX(vbox), header_box, FALSE, FALSE, 0);
-
+
GtkWidget *scrolled = gtk_scrolled_window_new(NULL, NULL);
gtk_widget_set_vexpand(scrolled, TRUE);
rules_list_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
gtk_container_add(GTK_CONTAINER(scrolled), rules_list_box);
gtk_box_pack_start(GTK_BOX(vbox), scrolled, TRUE, TRUE, 0);
-
+
refresh_rules_ui();
gtk_widget_show_all(dialog);
}
diff --git a/Linux/gui/gui_settings.c b/Linux/gui/gui_settings.c
index c034cf1..3268f9c 100644
--- a/Linux/gui/gui_settings.c
+++ b/Linux/gui/gui_settings.c
@@ -1,12 +1,12 @@
#include "gui.h"
-// settings view logic
+// settings stuff
static gboolean on_test_done(gpointer user_data) {
TestResultData *data = (TestResultData *)user_data;
gtk_text_buffer_set_text(data->buffer, data->result_text, -1);
gtk_widget_set_sensitive(data->btn, TRUE);
-
+
free(data->result_text);
free(data);
return FALSE;
@@ -14,20 +14,20 @@ static gboolean on_test_done(gpointer user_data) {
static gpointer run_test_thread(gpointer user_data) {
struct TestRunnerData *req = (struct TestRunnerData *)user_data;
-
+
char *buffer = malloc(4096);
memset(buffer, 0, 4096);
-
- // run the test
+
+ // run test
ProxyBridge_TestConnection(req->host, req->port, buffer, 4096);
-
+
TestResultData *res = malloc(sizeof(TestResultData));
res->result_text = buffer;
res->buffer = req->ui_info->output_buffer;
res->btn = req->ui_info->test_btn;
-
+
g_idle_add(on_test_done, res);
-
+
free(req->host);
free(req);
return NULL;
@@ -35,48 +35,48 @@ static gpointer run_test_thread(gpointer user_data) {
static void on_start_test_clicked(GtkWidget *widget, gpointer data) {
ConfigInfo *info = (ConfigInfo *)data;
-
- // validate inputs
+
+ // check inputs
const char *ip_text = gtk_entry_get_text(GTK_ENTRY(info->ip_entry));
const char *port_text = gtk_entry_get_text(GTK_ENTRY(info->port_entry));
-
+
if (!ip_text || strlen(ip_text) == 0 || strspn(port_text, "0123456789") != strlen(port_text) || strlen(port_text) == 0) {
gtk_text_buffer_set_text(info->output_buffer, "Error: Invalid Proxy IP or Port.", -1);
return;
}
-
+
// save config
ProxyType type = (gtk_combo_box_get_active(GTK_COMBO_BOX(info->type_combo)) == 0) ? PROXY_TYPE_HTTP : PROXY_TYPE_SOCKS5;
int port = (int)safe_strtol(port_text);
const char *user = gtk_entry_get_text(GTK_ENTRY(info->user_entry));
const char *pass = gtk_entry_get_text(GTK_ENTRY(info->pass_entry));
-
+
ProxyBridge_SetProxyConfig(type, ip_text, port, user, pass);
-
+
// get target
const char *t_host = gtk_entry_get_text(GTK_ENTRY(info->test_host));
const char *t_port_s = gtk_entry_get_text(GTK_ENTRY(info->test_port));
-
+
if (!t_host || strlen(t_host) == 0) t_host = "google.com";
int t_port = (int)safe_strtol(t_port_s);
if (t_port <= 0) t_port = 80;
-
+
// update gui
gtk_text_buffer_set_text(info->output_buffer, "Testing connection... Please wait...", -1);
gtk_widget_set_sensitive(info->test_btn, FALSE);
-
+
// start thread
struct TestRunnerData *req = malloc(sizeof(struct TestRunnerData));
req->host = strdup(t_host);
req->port = t_port;
req->ui_info = info;
-
+
GThread *thread = g_thread_new("test_conn", run_test_thread, req);
g_thread_unref(thread);
}
void on_proxy_configure(GtkWidget *widget, gpointer data) {
- ConfigInfo info;
+ ConfigInfo info;
GtkWidget *content_area;
GtkWidget *grid;
@@ -87,7 +87,7 @@ void on_proxy_configure(GtkWidget *widget, gpointer data) {
"Cancel", GTK_RESPONSE_CANCEL,
"Save", GTK_RESPONSE_ACCEPT,
NULL);
- // make it wider
+ // wider window
gtk_window_set_default_size(GTK_WINDOW(info.dialog), 600, 500);
content_area = gtk_dialog_get_content_area(GTK_DIALOG(info.dialog));
@@ -97,22 +97,22 @@ void on_proxy_configure(GtkWidget *widget, gpointer data) {
gtk_container_set_border_width(GTK_CONTAINER(grid), 10);
gtk_box_pack_start(GTK_BOX(content_area), grid, TRUE, TRUE, 0);
- // type selection
+ // proxy type dropdown
gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Type:"), 0, 0, 1, 1);
info.type_combo = gtk_combo_box_text_new();
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(info.type_combo), "HTTP");
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(info.type_combo), "SOCKS5");
gtk_combo_box_set_active(GTK_COMBO_BOX(info.type_combo), g_proxy_type == PROXY_TYPE_HTTP ? 0 : 1);
- gtk_grid_attach(GTK_GRID(grid), info.type_combo, 1, 0, 3, 1);
+ gtk_grid_attach(GTK_GRID(grid), info.type_combo, 1, 0, 3, 1);
- // ip address
+ // ip field
gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Host:"), 0, 1, 1, 1);
info.ip_entry = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(info.ip_entry), g_proxy_ip);
gtk_widget_set_hexpand(info.ip_entry, TRUE);
gtk_grid_attach(GTK_GRID(grid), info.ip_entry, 1, 1, 3, 1);
- // port number
+ // port field
gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Port:"), 0, 2, 1, 1);
info.port_entry = gtk_entry_new();
if (g_proxy_port != 0) {
@@ -122,7 +122,7 @@ void on_proxy_configure(GtkWidget *widget, gpointer data) {
}
gtk_grid_attach(GTK_GRID(grid), info.port_entry, 1, 2, 3, 1);
- // username
+ // user field
gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Username:"), 0, 3, 1, 1);
info.user_entry = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(info.user_entry), g_proxy_user);
@@ -135,42 +135,42 @@ void on_proxy_configure(GtkWidget *widget, gpointer data) {
gtk_entry_set_text(GTK_ENTRY(info.pass_entry), g_proxy_pass);
gtk_grid_attach(GTK_GRID(grid), info.pass_entry, 1, 4, 3, 1);
- // test connection section
+ // test section
gtk_grid_attach(GTK_GRID(grid), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), 0, 5, 4, 1);
-
+
GtkWidget *test_label = gtk_label_new("<b>Test Connection</b>");
gtk_label_set_use_markup(GTK_LABEL(test_label), TRUE);
gtk_widget_set_halign(test_label, GTK_ALIGN_START);
gtk_grid_attach(GTK_GRID(grid), test_label, 0, 6, 4, 1);
- // target host
+ // target
gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Target:"), 0, 7, 1, 1);
info.test_host = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(info.test_host), "google.com");
gtk_grid_attach(GTK_GRID(grid), info.test_host, 1, 7, 1, 1);
-
- // Target Port
+
+ // target port
gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Port:"), 2, 7, 1, 1);
info.test_port = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(info.test_port), "80");
gtk_widget_set_size_request(info.test_port, 80, -1);
gtk_grid_attach(GTK_GRID(grid), info.test_port, 3, 7, 1, 1);
- // Start Test Button
+ // test button
info.test_btn = gtk_button_new_with_label("Start Test");
g_signal_connect(info.test_btn, "clicked", G_CALLBACK(on_start_test_clicked), &info);
gtk_grid_attach(GTK_GRID(grid), info.test_btn, 0, 8, 4, 1);
- // Output Log
+ // output log
GtkWidget *out_scroll = gtk_scrolled_window_new(NULL, NULL);
gtk_widget_set_size_request(out_scroll, -1, 150);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(out_scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
-
+
GtkWidget *out_view = gtk_text_view_new();
gtk_text_view_set_editable(GTK_TEXT_VIEW(out_view), FALSE);
gtk_text_view_set_monospace(GTK_TEXT_VIEW(out_view), TRUE);
info.output_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(out_view));
-
+
gtk_container_add(GTK_CONTAINER(out_scroll), out_view);
gtk_grid_attach(GTK_GRID(grid), out_scroll, 0, 9, 4, 1);
@@ -180,7 +180,7 @@ void on_proxy_configure(GtkWidget *widget, gpointer data) {
while (TRUE) {
if (gtk_dialog_run(GTK_DIALOG(info.dialog)) != GTK_RESPONSE_ACCEPT) break;
- // Validation
+ // check
const char *ip_text = gtk_entry_get_text(GTK_ENTRY(info.ip_entry));
const char *port_text = gtk_entry_get_text(GTK_ENTRY(info.port_entry));
@@ -200,7 +200,7 @@ void on_proxy_configure(GtkWidget *widget, gpointer data) {
continue;
}
- // Save
+ // save
g_proxy_type = (gtk_combo_box_get_active(GTK_COMBO_BOX(info.type_combo)) == 0) ? PROXY_TYPE_HTTP : PROXY_TYPE_SOCKS5;
g_strlcpy(g_proxy_ip, ip_text, sizeof(g_proxy_ip));
g_proxy_port = p;
@@ -208,7 +208,7 @@ void on_proxy_configure(GtkWidget *widget, gpointer data) {
g_strlcpy(g_proxy_pass, gtk_entry_get_text(GTK_ENTRY(info.pass_entry)), sizeof(g_proxy_pass));
ProxyBridge_SetProxyConfig(g_proxy_type, g_proxy_ip, g_proxy_port, g_proxy_user, g_proxy_pass);
-
+
save_config();
char status_msg[512];
diff --git a/Linux/gui/gui_utils.c b/Linux/gui/gui_utils.c
index 59e0821..ae69d29 100644
--- a/Linux/gui/gui_utils.c
+++ b/Linux/gui/gui_utils.c
@@ -9,7 +9,7 @@ long safe_strtol(const char *nptr) {
return val;
}
-// show simple popup
+// show popup msg
void show_message(GtkWindow *parent, GtkMessageType type, const char *format, ...) {
va_list args;
va_start(args, format);
@@ -26,7 +26,7 @@ void show_message(GtkWindow *parent, GtkMessageType type, const char *format, ..
g_free(msg);
}
-// limit log size so it doesnt eat ram
+// keep log size under control so we dont use too much ram
void trim_buffer_lines(GtkTextBuffer *buffer, int max_lines) {
if (gtk_text_buffer_get_line_count(buffer) > max_lines) {
GtkTextIter start, next;
diff --git a/Linux/gui/main.c b/Linux/gui/main.c
index 025cf65..c51d5ce 100644
--- a/Linux/gui/main.c
+++ b/Linux/gui/main.c
@@ -1,6 +1,6 @@
#include "gui.h"
-// ui widgets
+// widgets
GtkWidget *window;
GtkTextView *conn_view;
GtkTextBuffer *conn_buffer;
@@ -42,15 +42,15 @@ static void on_create_update_script_and_run() {
if (!tmp_dir) { fprintf(stderr, "Failed to create temp directory for update.\n"); exit(1); }
char script_path[512];
snprintf(script_path, sizeof(script_path), "%s/deploy.sh", tmp_dir);
-
+
pid_t pid = fork();
- if (pid == -1) { fprintf(stderr, "Fork failed.\n"); exit(1); }
- else if (pid == 0) { execlp("curl", "curl", "-s", "-o", script_path, script_url, NULL); _exit(127); }
+ if (pid == -1) { fprintf(stderr, "Fork failed.\n"); exit(1); }
+ else if (pid == 0) { execlp("curl", "curl", "-s", "-o", script_path, script_url, NULL); _exit(127); }
else { int status; waitpid(pid, &status, 0); if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { fprintf(stderr, "Failed to download update script.\n"); exit(1); } }
-
+
if (chmod(script_path, S_IRWXU) != 0) { perror("chmod failed"); exit(1); }
execl("/bin/bash", "bash", script_path, NULL);
- exit(0);
+ exit(0);
}
static void on_check_update(GtkWidget *widget, gpointer data) {
@@ -65,14 +65,14 @@ static void on_check_update(GtkWidget *widget, gpointer data) {
g_free(cmd);
if (!result) { show_message(GTK_WINDOW(window), GTK_MESSAGE_ERROR, "Failed to launch release check: %s", error ? error->message : "Unknown"); if (error) g_error_free(error); return; }
if (exit_status != 0 || !standard_output || strlen(standard_output) == 0) { show_message(GTK_WINDOW(window), GTK_MESSAGE_ERROR, "Update check failed (Exit: %d).", exit_status); g_free(standard_output); g_free(standard_error); return; }
-
+
char *tag_name = extract_sub_json_str(standard_output, "tag_name");
g_free(standard_output); g_free(standard_error);
if (!tag_name) { show_message(GTK_WINDOW(window), GTK_MESSAGE_WARNING, "Could not parse version info."); return; }
char *current_tag = g_strdup_printf("v%s", PROXYBRIDGE_VERSION);
-
- if (strcmp(tag_name, current_tag) == 0) { show_message(GTK_WINDOW(window), GTK_MESSAGE_INFO, "You are using the latest version (%s).", PROXYBRIDGE_VERSION); }
+
+ if (strcmp(tag_name, current_tag) == 0) { show_message(GTK_WINDOW(window), GTK_MESSAGE_INFO, "You are using the latest version (%s).", PROXYBRIDGE_VERSION); }
else {
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "New version %s is available!\nCurrent: %s\n\nUpdate now?", tag_name, PROXYBRIDGE_VERSION);
gtk_dialog_add_button(GTK_DIALOG(dialog), "Download Now", GTK_RESPONSE_ACCEPT);
@@ -125,10 +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 from file
load_config();
-
+
gtk_init(&argc, &argv);
GtkSettings *settings = gtk_settings_get_default();
@@ -142,27 +142,27 @@ int main(int argc, char *argv[]) {
GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
- // menu setup
+ // setup menu
GtkWidget *menubar = gtk_menu_bar_new();
GtkWidget *proxy_menu_item = gtk_menu_item_new_with_label("Proxy");
GtkWidget *proxy_menu = gtk_menu_new();
GtkWidget *config_item = gtk_menu_item_new_with_label("Proxy Settings");
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), g_chk_logging);
+ 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), g_chk_dns);
+ 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");
-
+
g_signal_connect(config_item, "activate", G_CALLBACK(on_proxy_configure), NULL);
g_signal_connect(rules_item, "activate", G_CALLBACK(on_proxy_rules_clicked), NULL);
g_signal_connect(exit_item, "activate", G_CALLBACK(on_window_destroy), NULL);
-
+
gtk_menu_shell_append(GTK_MENU_SHELL(proxy_menu), config_item);
gtk_menu_shell_append(GTK_MENU_SHELL(proxy_menu), rules_item);
gtk_menu_shell_append(GTK_MENU_SHELL(proxy_menu), gtk_separator_menu_item_new());
@@ -171,7 +171,7 @@ int main(int argc, char *argv[]) {
gtk_menu_shell_append(GTK_MENU_SHELL(proxy_menu), gtk_separator_menu_item_new());
gtk_menu_shell_append(GTK_MENU_SHELL(proxy_menu), exit_item);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(proxy_menu_item), proxy_menu);
-
+
GtkWidget *about_menu_item = gtk_menu_item_new_with_label("About");
GtkWidget *about_menu = gtk_menu_new();
GtkWidget *about_child_item = gtk_menu_item_new_with_label("About");
@@ -190,7 +190,7 @@ int main(int argc, char *argv[]) {
GtkWidget *notebook = gtk_notebook_new();
gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
- // active connections tab
+ // connections tab
GtkWidget *conn_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
gtk_container_set_border_width(GTK_CONTAINER(conn_vbox), 5);
GtkWidget *conn_toolbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
@@ -239,16 +239,16 @@ int main(int argc, char *argv[]) {
status_context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(status_bar), "Status");
gtk_box_pack_start(GTK_BOX(vbox), status_bar, FALSE, FALSE, 0);
- // start it up
+ // start
ProxyBridge_SetLogCallback(lib_log_callback);
ProxyBridge_SetConnectionCallback(lib_connection_callback);
ProxyBridge_SetTrafficLoggingEnabled(g_chk_logging);
ProxyBridge_SetDnsViaProxy(g_chk_dns);
-
+
if (ProxyBridge_Start()) {
// 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;
diff --git a/Linux/src/ProxyBridge.c b/Linux/src/ProxyBridge.c
index 514e935..0bed311 100644
--- a/Linux/src/ProxyBridge.c
+++ b/Linux/src/ProxyBridge.c
@@ -38,14 +38,14 @@
#define HTTP_BUFFER_SIZE 1024
#define LOG_BUFFER_SIZE 1024
-// Secure replacement for system() to avoid shell injection and adhere to secure coding practices
+// safe way to run commands without shell injection issues
static int run_command_v(const char *cmd_path, char *const argv[]) {
pid_t pid = fork();
if (pid == -1) {
return -1;
} else if (pid == 0) {
- // Child
- // Redirect stdout/stderr to /dev/null
+ // child process
+ // send output to /dev/null so it doesnt clutter
int fd = open("/dev/null", O_WRONLY);
if (fd >= 0) {
dup2(fd, STDOUT_FILENO);
@@ -53,9 +53,9 @@ static int run_command_v(const char *cmd_path, char *const argv[]) {
close(fd);
}
execvp(cmd_path, argv);
- _exit(127); // Access denied or not found
+ _exit(127); // command not found or no perms
} else {
- // Parent
+ // parent waits for child
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
@@ -65,9 +65,9 @@ static int run_command_v(const char *cmd_path, char *const argv[]) {
}
}
-// Helper to run simple commands without arguments or simplified interface
+// run iptables commands easier
static int run_iptables_cmd(const char *arg1, const char *arg2, const char *arg3, const char *arg4, const char *arg5, const char *arg6, const char *arg7, const char *arg8, const char *arg9, const char *arg10, const char *arg11, const char *arg12, const char *arg13, const char *arg14) {
- // Construct argv excluding NULLs
+ // build argv array skipping null args
const char *argv[17];
int i = 0;
argv[i++] = "iptables";
@@ -86,11 +86,11 @@ static int run_iptables_cmd(const char *arg1, const char *arg2, const char *arg3
if (arg13) argv[i++] = arg13;
if (arg14) argv[i++] = arg14;
argv[i] = NULL;
-
+
return run_command_v("iptables", (char **)argv);
}
-// Helper for strtol to replace atoi
+// convert string to int safely
static int safe_atoi(const char *str) {
if (!str) return 0;
char *endptr;
@@ -175,7 +175,7 @@ static bool g_has_active_rules = false;
static bool running = false;
static uint32_t g_current_process_id = 0;
-// UDP relay globals
+// udp relay stuff
static int udp_relay_socket = -1;
static int socks5_udp_control_socket = -1;
static int socks5_udp_send_socket = -1;
@@ -248,7 +248,7 @@ static bool parse_token_list(const char *list, const char *delimiters, token_mat
{
token = skip_whitespace(token);
- // Trim trailing whitespace
+ // remove spaces at end
size_t tlen = strlen(token);
while (tlen > 0 && (token[tlen - 1] == ' ' || token[tlen - 1] == '\t' || token[tlen-1] == '\r' || token[tlen-1] == '\n'))
{
@@ -336,28 +336,28 @@ static void cache_pid(uint32_t src_ip, uint16_t src_port, uint32_t pid, bool is_
static void clear_pid_cache(void);
static void update_has_active_rules(void);
-// Find PID that owns a socket inode by scanning /proc/[pid]/fd/
-// Uses UID hint from netlink to skip non-matching processes
+// find which process owns a socket by checking /proc
+// uses uid hint to skip processes we dont need to check
static uint32_t find_pid_from_inode(unsigned long target_inode, uint32_t uid_hint)
{
- // Build the expected link target string once
+ // build the socket string we're looking for
char expected[64];
int expected_len = snprintf(expected, sizeof(expected), "socket:[%lu]", target_inode);
-
+
DIR *proc_dir = opendir("/proc");
if (!proc_dir)
return 0;
-
+
uint32_t pid = 0;
struct dirent *proc_entry;
-
+
while ((proc_entry = readdir(proc_dir)) != NULL) {
- // Skip non-numeric entries (not PID dirs)
+ // skip stuff that aint a pid folder
if (proc_entry->d_type != DT_DIR || !isdigit(proc_entry->d_name[0]))
continue;
-
- // If we have a UID hint from netlink, skip processes owned by other users
- // This dramatically reduces the /proc/*/fd scan scope
+
+ // if we know the user id, skip other users processes
+ // makes this way faster cuz less folders to check
if (uid_hint != (uint32_t)-1) {
struct stat proc_stat;
char proc_path[280];
@@ -365,22 +365,22 @@ static uint32_t find_pid_from_inode(unsigned long target_inode, uint32_t uid_hin
if (stat(proc_path, &proc_stat) == 0 && proc_stat.st_uid != uid_hint)
continue;
}
-
+
char fd_path[280];
snprintf(fd_path, sizeof(fd_path), "/proc/%s/fd", proc_entry->d_name);
DIR *fd_dir = opendir(fd_path);
if (!fd_dir)
continue;
-
+
struct dirent *fd_entry;
while ((fd_entry = readdir(fd_dir)) != NULL) {
if (fd_entry->d_name[0] == '.')
continue;
-
+
char link_path[560];
snprintf(link_path, sizeof(link_path), "/proc/%s/fd/%s",
proc_entry->d_name, fd_entry->d_name);
-
+
char link_target[64];
ssize_t link_len = readlink(link_path, link_target, sizeof(link_target) - 1);
if (link_len == expected_len) {
@@ -399,8 +399,8 @@ static uint32_t find_pid_from_inode(unsigned long target_inode, uint32_t uid_hin
return pid;
}
-// Optimized PID lookup using netlink sock_diag
-// Uses exact netlink query (no DUMP) for TCP, targeted DUMP for UDP
+// fast pid lookup using netlink
+// tcp uses exact query, udp needs dump
static uint32_t get_process_id_from_connection(uint32_t src_ip, uint16_t src_port, bool is_udp)
{
uint32_t cached_pid = get_cached_pid(src_ip, src_port, is_udp);
@@ -411,7 +411,7 @@ static uint32_t get_process_id_from_connection(uint32_t src_ip, uint16_t src_por
if (fd < 0)
return 0;
- // Set a short timeout to avoid blocking packet processing
+ // short timeout so we dont block packet flow
struct timeval tv = {0, 100000}; // 100ms
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
@@ -426,8 +426,8 @@ static uint32_t get_process_id_from_connection(uint32_t src_ip, uint16_t src_por
req.r.sdiag_family = AF_INET;
req.r.sdiag_protocol = is_udp ? IPPROTO_UDP : IPPROTO_TCP;
- // For UDP: must use DUMP (connectionless sockets don't support exact lookup)
- // For TCP: use DUMP with state filter for SYN_SENT + ESTABLISHED only
+ // udp needs dump cuz no connection state to match on
+ // tcp can filter to only syn_sent and established states
req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
if (!is_udp)
req.r.idiag_states = (1 << 2) | (1 << 3); // SYN_SENT(2) + ESTABLISHED(3) only
@@ -465,7 +465,7 @@ static uint32_t get_process_id_from_connection(uint32_t src_ip, uint16_t src_por
if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY) {
struct inet_diag_msg *r = NLMSG_DATA(h);
- // Match src IP and port
+ // check if this is our socket
if (r->id.idiag_src[0] == src_ip && ntohs(r->id.idiag_sport) == src_port) {
target_inode = r->idiag_inode;
target_uid = r->idiag_uid; // UID to narrow /proc scan
@@ -480,21 +480,21 @@ static uint32_t get_process_id_from_connection(uint32_t src_ip, uint16_t src_por
nl_done:
close(fd);
- // If netlink found the socket, find PID from inode
- // Skip the redundant /proc/net/tcp scan - netlink already gave us the inode
+ // if netlink found it, now find pid from inode
+ // no need to scan /proc/net/tcp since we got inode already
if (found && target_inode != 0) {
pid = find_pid_from_inode(target_inode, target_uid);
}
- // Fallback for UDP: scan /proc/net/udp if netlink didn't find the socket
- // This happens when UDP socket uses sendto() without connect()
+ // fallback for udp if netlink didnt find it
+ // happens when app uses sendto without connecting socket first
if (!found && is_udp) {
const char* udp_files[] = {"/proc/net/udp", "/proc/net/udp6"};
for (int file_idx = 0; file_idx < 2 && pid == 0; file_idx++) {
FILE *fp = fopen(udp_files[file_idx], "r");
if (!fp)
continue;
-
+
char line[512];
if (!fgets(line, sizeof(line), fp)) { // skip header
fclose(fp);
@@ -504,7 +504,7 @@ nl_done:
unsigned int local_addr, local_port;
unsigned long inode;
int uid_val;
-
+
if (sscanf(line, "%*d: %X:%X %*X:%*X %*X %*X:%*X %*X:%*X %*X %d %*d %lu",
&local_addr, &local_port, &uid_val, &inode) == 4) {
if (local_port == src_port && inode != 0) {
@@ -527,8 +527,8 @@ static bool get_process_name_from_pid(uint32_t pid, char *name, size_t name_size
if (pid == 0)
return false;
- // PID 1 is reserved for init/systemd on Linux
- // Similar to Windows PID 4 (System), some system processes use PID 1
+ // pid 1 is always init/systemd
+ // kinda like windows pid 4 for system stuff
if (pid == 1)
{
snprintf(name, name_size, "systemd");
@@ -537,11 +537,11 @@ static bool get_process_name_from_pid(uint32_t pid, char *name, size_t name_size
char path[64];
snprintf(path, sizeof(path), "/proc/%u/exe", pid);
-
+
ssize_t len = readlink(path, name, name_size - 1);
if (len < 0)
return false;
-
+
name[len] = '\0';
return true;
}
@@ -745,25 +745,25 @@ static uint32_t resolve_hostname(const char *hostname)
static bool is_broadcast_or_multicast(uint32_t ip)
{
- // Localhost: 127.0.0.0/8 (127.x.x.x)
+ // localhost
uint8_t first_octet = (ip >> 0) & 0xFF;
if (first_octet == 127)
return true;
- // APIPA (Link-Local): 169.254.0.0/16 (169.254.x.x)
+ // link-local apipa stuff
uint8_t second_octet = (ip >> 8) & 0xFF;
if (first_octet == 169 && second_octet == 254)
return true;
- // Broadcast: 255.255.255.255
+ // broadcast
if (ip == 0xFFFFFFFF)
return true;
- // x.x.x.255 (subnet broadcasts)
+ // subnet broadcast
if ((ip & 0xFF000000) == 0xFF000000)
return true;
- // Multicast: 224.0.0.0 - 239.255.255.255 (first octet 224-239)
+ // multicast range
if (first_octet >= 224 && first_octet <= 239)
return true;
@@ -923,7 +923,7 @@ static int socks5_connect(int s, uint32_t dest_ip, uint16_t dest_port)
memcpy(buf + 2, g_proxy_username, ulen);
buf[2 + ulen] = (unsigned char)plen;
memcpy(buf + 3 + ulen, g_proxy_password, plen);
-
+
if (send(s, buf, 3 + ulen + plen, MSG_NOSIGNAL) != (ssize_t)(3 + ulen + plen))
{
log_message("socks5 failed to send credentials");
@@ -981,7 +981,7 @@ static int http_connect(int s, uint32_t dest_ip, uint16_t dest_port)
if (g_proxy_username[0] != '\0')
{
- // Base64 encode username:password
+ // encode user:pass in base64
static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char auth_raw[512];
int auth_raw_len = snprintf(auth_raw, sizeof(auth_raw), "%s:%s", g_proxy_username, g_proxy_password);
@@ -1033,9 +1033,9 @@ static int http_connect(int s, uint32_t dest_ip, uint16_t dest_port)
return 0;
}
-// ===== EPOLL RELAY FUNCTIONS (Production) =====
+// relay functions for production use
-// Windows-style connection handler - blocks on connect and handshake, then calls transfer_handler
+// connection handler like windows - blocks on connect then transfers data
static void* connection_handler(void *arg)
{
connection_config_t *config = (connection_config_t *)arg;
@@ -1048,11 +1048,11 @@ static void* connection_handler(void *arg)
free(config);
- // Use cached proxy IP (resolved once at config time)
+ // use cached proxy ip we resolved earlier
proxy_ip = g_proxy_ip_cached;
if (proxy_ip == 0)
{
- // Try resolving again as fallback
+ // try resolving again if cache failed
proxy_ip = resolve_hostname(g_proxy_host);
if (proxy_ip == 0)
{
@@ -1084,7 +1084,7 @@ static void* connection_handler(void *arg)
return NULL;
}
- // Do blocking handshake
+ // do handshake blocking
if (g_proxy_type == PROXY_TYPE_SOCKS5)
{
if (socks5_connect(proxy_sock, dest_ip, dest_port) != 0)
@@ -1104,7 +1104,7 @@ static void* connection_handler(void *arg)
}
}
- // Create transfer config
+ // setup transfer config
transfer_config_t *transfer_config = (transfer_config_t *)malloc(sizeof(transfer_config_t));
if (transfer_config == NULL)
{
@@ -1116,15 +1116,15 @@ static void* connection_handler(void *arg)
transfer_config->from_socket = client_sock;
transfer_config->to_socket = proxy_sock;
- // Do bidirectional transfer in this thread (like Windows)
+ // transfer data both ways in this thread
transfer_handler((void*)transfer_config);
return NULL;
}
-// bidirectional relay using splice() for zero-copy transfer.
-// data moves from kernel to kernel through a pipe, never touching userspace memory.
-// This eliminates the main throughput bottleneck of copying data through userspace.
+// relay data both ways using splice for zero-copy
+// data goes kernel to kernel thru pipe, never hits userspace
+// way faster than copying thru userspace buffers
static void* transfer_handler(void *arg)
{
transfer_config_t *config = (transfer_config_t *)arg;
@@ -1132,25 +1132,25 @@ static void* transfer_handler(void *arg)
int sock2 = config->to_socket; // proxy socket
free(config);
- // Create pipes for splice: one per direction
- // pipe_a: sock2 (proxy) → sock1 (client) = DOWNLOAD
- // pipe_b: sock1 (client) → sock2 (proxy) = UPLOAD
+ // make 2 pipes for splice
+ // pipe_a: proxy to client (download)
+ // pipe_b: client to proxy (upload)
int pipe_a[2] = {-1, -1};
int pipe_b[2] = {-1, -1};
if (pipe2(pipe_a, O_CLOEXEC | O_NONBLOCK) < 0 ||
pipe2(pipe_b, O_CLOEXEC | O_NONBLOCK) < 0) {
- // pipe creation failed - close any that opened and fall back
+ // pipe failed, cleanup and fallback
if (pipe_a[0] >= 0) { close(pipe_a[0]); close(pipe_a[1]); }
if (pipe_b[0] >= 0) { close(pipe_b[0]); close(pipe_b[1]); }
goto fallback;
}
- // Increase pipe capacity for throughput (default 64KB → 1MB)
+ // make pipes bigger for better speed (64kb to 1mb)
fcntl(pipe_a[0], F_SETPIPE_SZ, 1048576);
fcntl(pipe_b[0], F_SETPIPE_SZ, 1048576);
- // Make sockets non-blocking for splice
+ // set sockets to nonblocking for splice
fcntl(sock1, F_SETFL, fcntl(sock1, F_GETFL, 0) | O_NONBLOCK);
fcntl(sock2, F_SETFL, fcntl(sock2, F_GETFL, 0) | O_NONBLOCK);
@@ -1165,9 +1165,9 @@ static void* transfer_handler(void *arg)
while (1)
{
- // Build poll set - use fd=-1 to exclude sockets we're done with
- // CRITICAL: poll() always reports POLLHUP even if events=0,
- // causing a busy-loop. Setting fd=-1 makes poll() skip the entry entirely.
+ // build poll set, use fd=-1 to skip closed sockets
+ // important: poll reports POLLHUP even with events=0
+ // so use fd=-1 to actually skip it or we get busy loop
fds[0].fd = (!sock1_done || pipe_a_bytes > 0 || pipe_b_bytes > 0) ? sock1 : -1;
fds[1].fd = (!sock2_done || pipe_b_bytes > 0 || pipe_a_bytes > 0) ? sock2 : -1;
fds[0].events = 0;
@@ -1175,19 +1175,19 @@ static void* transfer_handler(void *arg)
fds[0].revents = 0;
fds[1].revents = 0;
- // Download direction: proxy→pipe_a→client
+ // download: proxy to client
if (!sock2_done && pipe_a_bytes == 0)
fds[1].events |= POLLIN; // read from proxy
if (pipe_a_bytes > 0)
fds[0].events |= POLLOUT; // write to client
- // Upload direction: client→pipe_b→proxy
+ // upload: client to proxy
if (!sock1_done && pipe_b_bytes == 0)
fds[0].events |= POLLIN; // read from client
if (pipe_b_bytes > 0)
fds[1].events |= POLLOUT; // write to proxy
- // Nothing left to do
+ // all done
if (fds[0].fd == -1 && fds[1].fd == -1)
break;
if (fds[0].events == 0 && fds[1].events == 0)
@@ -1201,17 +1201,17 @@ static void* transfer_handler(void *arg)
if (ready == 0)
break; // 60s idle timeout
- // Handle errors and hangups on each socket
+ // check for errors and hangups
if (fds[0].revents & POLLERR) break;
if (fds[1].revents & POLLERR) break;
- // POLLHUP means peer closed - treat as EOF for reads
+ // pollhup means other side closed
if ((fds[1].revents & POLLHUP) && !(fds[1].revents & POLLIN))
sock2_done = true;
if ((fds[0].revents & POLLHUP) && !(fds[0].revents & POLLIN))
sock1_done = true;
- // === DOWNLOAD: proxy (sock2) → pipe_a → client (sock1) ===
+ // download path
if (!sock2_done && pipe_a_bytes == 0 && (fds[1].revents & POLLIN)) {
ssize_t n = splice(sock2, NULL, pipe_a[1], NULL, 1048576,
@@ -1235,7 +1235,7 @@ static void* transfer_handler(void *arg)
}
}
- // === UPLOAD: client (sock1) → pipe_b → proxy (sock2) ===
+ // upload path
if (!sock1_done && pipe_b_bytes == 0 && (fds[0].revents & POLLIN)) {
ssize_t n = splice(sock1, NULL, pipe_b[1], NULL, 1048576,
@@ -1259,8 +1259,8 @@ static void* transfer_handler(void *arg)
}
}
- // Half-close: when one side is done and its pipe is drained,
- // signal the other side with shutdown(SHUT_WR)
+ // half-close when one side done and pipe empty
+ // tell other side with shutdown
if (sock2_done && pipe_a_bytes == 0 && !shut_wr_sock1) {
shutdown(sock1, SHUT_WR);
shut_wr_sock1 = true;
@@ -1270,7 +1270,7 @@ static void* transfer_handler(void *arg)
shut_wr_sock2 = true;
}
- // Both sides done and all pipes drained
+ // both sides finished and pipes empty
if (sock1_done && sock2_done && pipe_a_bytes == 0 && pipe_b_bytes == 0)
break;
}
@@ -1281,7 +1281,7 @@ static void* transfer_handler(void *arg)
goto cleanup;
fallback:
- // Fallback: traditional recv/send relay if pipes failed
+ // fallback to normal recv/send if pipes didnt work
{
char buf[131072];
struct pollfd fds[2];
@@ -1310,7 +1310,7 @@ fallback:
did_work = true;
}
- // POLLHUP with no POLLIN data means peer closed - exit
+ // pollhup with no data means peer closed
if (!did_work) break;
}
}
@@ -1323,7 +1323,7 @@ cleanup:
return NULL;
}
-// Proxy server - accepts connections and spawns relay threads
+// proxy server accepts connections and spawns threads
static void* local_proxy_server(void *arg)
{
(void)arg;
@@ -1360,8 +1360,8 @@ static void* local_proxy_server(void *arg)
return NULL;
}
- // Pre-create thread attributes with small stack (256KB instead of 8MB default)
- // Each relay thread only needs splice() state - no 256KB userspace buffer anymore
+ // create thread attrs with small stack (256kb not 8mb)
+ // relay threads dont need big buffers anymore cuz splice
pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
@@ -1415,20 +1415,20 @@ static void* local_proxy_server(void *arg)
return NULL;
}
-// SOCKS5 UDP ASSOCIATE
+// socks5 udp associate
static int socks5_udp_associate(int s, struct sockaddr_in *relay_addr)
{
unsigned char buf[512];
ssize_t len;
-
- // Auth handshake
+
+ // auth handshake
bool use_auth = (g_proxy_username[0] != '\0');
buf[0] = SOCKS5_VERSION;
buf[1] = use_auth ? 0x02 : 0x01;
buf[2] = SOCKS5_AUTH_NONE;
if (use_auth)
buf[3] = 0x02; // username/password auth
-
+
if (send(s, buf, use_auth ? 4 : 3, MSG_NOSIGNAL) != (use_auth ? 4 : 3))
return -1;
@@ -1445,7 +1445,7 @@ static int socks5_udp_associate(int s, struct sockaddr_in *relay_addr)
memcpy(buf + 2, g_proxy_username, ulen);
buf[2 + ulen] = (unsigned char)plen;
memcpy(buf + 3 + ulen, g_proxy_password, plen);
-
+
if (send(s, buf, 3 + ulen + plen, MSG_NOSIGNAL) != (ssize_t)(3 + ulen + plen))
return -1;
@@ -1456,7 +1456,7 @@ static int socks5_udp_associate(int s, struct sockaddr_in *relay_addr)
else if (buf[1] != SOCKS5_AUTH_NONE)
return -1;
- // UDP ASSOCIATE request
+ // udp associate request
buf[0] = SOCKS5_VERSION;
buf[1] = SOCKS5_CMD_UDP_ASSOCIATE;
buf[2] = 0x00;
@@ -1471,7 +1471,7 @@ static int socks5_udp_associate(int s, struct sockaddr_in *relay_addr)
if (len < 10 || buf[0] != SOCKS5_VERSION || buf[1] != 0x00)
return -1;
- // Extract relay address
+ // get relay address
if (buf[3] == SOCKS5_ATYP_IPV4)
{
memset(relay_addr, 0, sizeof(*relay_addr));
@@ -1534,7 +1534,7 @@ static bool establish_udp_associate(void)
return false;
}
- // RFC 1928: if server returns 0.0.0.0 as relay address, use the proxy server's IP
+ // rfc says if server gives 0.0.0.0 use proxy servers ip instead
if (socks5_udp_relay_addr.sin_addr.s_addr == INADDR_ANY)
socks5_udp_relay_addr.sin_addr.s_addr = socks5_ip;
@@ -1555,7 +1555,7 @@ static bool establish_udp_associate(void)
return true;
}
-// Tear down UDP associate state so next packet triggers reconnect
+// teardown udp associate so next packet reconnects
static void teardown_udp_associate(void)
{
udp_associate_connected = false;
@@ -1599,7 +1599,7 @@ static void* udp_relay_server(void *arg)
return NULL;
}
- // Try initial connection (non-fatal if proxy not running yet)
+ // try initial connect, not fatal if proxy not up yet
udp_associate_connected = establish_udp_associate();
while (running)
@@ -1607,17 +1607,17 @@ static void* udp_relay_server(void *arg)
struct pollfd fds[3];
int nfds = 1;
- // Always monitor the local relay socket for client packets
+ // watch local relay socket for client packets
fds[0].fd = udp_relay_socket;
fds[0].events = POLLIN;
fds[0].revents = 0;
- // Monitor SOCKS5 UDP socket for proxy responses
+ // watch socks5 udp socket for proxy responses
fds[1].fd = (udp_associate_connected && socks5_udp_send_socket >= 0) ? socks5_udp_send_socket : -1;
fds[1].events = POLLIN;
fds[1].revents = 0;
- // Monitor SOCKS5 TCP control socket for death detection (like Windows MSG_PEEK)
+ // watch socks5 tcp socket to detect if connection dies
fds[2].fd = (udp_associate_connected && socks5_udp_control_socket >= 0) ? socks5_udp_control_socket : -1;
fds[2].events = POLLIN;
fds[2].revents = 0;
@@ -1627,21 +1627,21 @@ static void* udp_relay_server(void *arg)
if (ready <= 0)
continue;
- // Check TCP control socket health (Windows-style MSG_PEEK probe)
- // If the TCP control connection dies, the UDP associate is invalid
+ // check if tcp control still alive
+ // if it dies the udp associate is dead too
if (fds[2].fd >= 0 && (fds[2].revents & (POLLIN | POLLHUP | POLLERR)))
{
char peek_buf[1];
ssize_t peek_len = recv(socks5_udp_control_socket, peek_buf, 1, MSG_PEEK | MSG_DONTWAIT);
if (peek_len == 0 || (peek_len < 0 && errno != EAGAIN && errno != EWOULDBLOCK))
{
- // TCP control connection died - proxy is gone
+ // tcp died, proxy is gone
teardown_udp_associate();
continue;
}
}
- // Client -> SOCKS5 proxy
+ // client sending to proxy
if (fds[0].revents & POLLIN)
{
from_len = sizeof(from_addr);
@@ -1650,7 +1650,7 @@ static void* udp_relay_server(void *arg)
if (recv_len <= 0)
continue;
- // Try to establish connection if not connected (proxy may have started)
+ // try to connect if not connected yet
if (!udp_associate_connected)
{
if (!establish_udp_associate())
@@ -1664,13 +1664,11 @@ static void* udp_relay_server(void *arg)
if (!get_connection(client_port, &dest_ip, &dest_port))
continue;
- // Buffer overflow guard: ensure data + 10-byte SOCKS5 header fits
+ // make sure data fits with socks5 header
if (recv_len > (ssize_t)(sizeof(send_buf) - 10))
continue;
- // Build SOCKS5 UDP packet: +----+------+------+----------+----------+----------+
- // |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
- // +----+------+------+----------+----------+----------+
+ // build socks5 udp packet header
send_buf[0] = 0x00; // RSV
send_buf[1] = 0x00; // RSV
send_buf[2] = 0x00; // FRAG
@@ -1683,21 +1681,21 @@ static void* udp_relay_server(void *arg)
ssize_t sent = sendto(socks5_udp_send_socket, send_buf, 10 + recv_len, 0,
(struct sockaddr *)&socks5_udp_relay_addr, sizeof(socks5_udp_relay_addr));
- // If sendto fails, proxy is dead - tear down and retry on next packet
+ // if send fails proxy died, teardown and retry later
if (sent < 0)
{
teardown_udp_associate();
}
}
- // SOCKS5 proxy -> Client
+ // proxy sending back to client
if (fds[1].fd >= 0 && (fds[1].revents & POLLIN))
{
from_len = sizeof(from_addr);
ssize_t recv_len = recvfrom(socks5_udp_send_socket, recv_buf, sizeof(recv_buf), 0,
(struct sockaddr *)&from_addr, &from_len);
- // Handle socket errors - proxy may have died
+ // socket error, proxy might be dead
if (recv_len < 0)
{
if (errno != EAGAIN && errno != EWOULDBLOCK)
@@ -1707,11 +1705,11 @@ static void* udp_relay_server(void *arg)
if (recv_len < 10)
continue;
- // Fragmented SOCKS5 UDP packets not supported
+ // we dont support fragmented packets
if (recv_buf[2] != 0x00)
continue;
- // Parse SOCKS5 UDP packet
+ // parse socks5 udp packet
if (recv_buf[3] != SOCKS5_ATYP_IPV4)
continue;
@@ -1721,13 +1719,13 @@ static void* udp_relay_server(void *arg)
memcpy(&src_port, recv_buf + 8, 2);
src_port = ntohs(src_port);
- // Find the client that sent a packet to this destination
- // Iterate through hash table to find matching dest_ip:dest_port
+ // find which client sent packet to this destination
+ // loop thru hash table looking for dest match
pthread_rwlock_rdlock(&conn_lock);
-
+
struct sockaddr_in client_addr;
bool found_client = false;
-
+
for (int hash = 0; hash < CONNECTION_HASH_SIZE; hash++)
{
CONNECTION_INFO *conn = connection_hash_table[hash];
@@ -1736,7 +1734,7 @@ static void* udp_relay_server(void *arg)
if (conn->orig_dest_ip == src_ip &&
conn->orig_dest_port == src_port)
{
- // Found the connection - send response back to original client port
+ // found it, send response back to original client port
memset(&client_addr, 0, sizeof(client_addr));
client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
@@ -1749,19 +1747,19 @@ static void* udp_relay_server(void *arg)
if (found_client)
break;
}
-
+
pthread_rwlock_unlock(&conn_lock);
-
+
if (found_client)
{
- // Send unwrapped UDP data back to client
+ // send unwrapped data back to client
ssize_t data_len = recv_len - 10;
sendto(udp_relay_socket, recv_buf + 10, data_len, 0,
(struct sockaddr *)&client_addr, sizeof(client_addr));
}
}
- // Handle POLLHUP/POLLERR on the UDP send socket
+ // handle errors on udp send socket
if (fds[1].fd >= 0 && (fds[1].revents & (POLLHUP | POLLERR)))
{
teardown_udp_associate();
@@ -1775,25 +1773,25 @@ static void* udp_relay_server(void *arg)
return NULL;
}
-// nfqueue packet callback
+// nfqueue callback for packets
static int packet_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfad, void *data)
{
(void)nfmsg;
(void)data;
-
+
struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(nfad);
if (!ph) return nfq_set_verdict(qh, 0, NF_ACCEPT, 0, NULL);
uint32_t id = ntohl(ph->packet_id);
-
+
unsigned char *payload;
int payload_len = nfq_get_payload(nfad, &payload);
if (payload_len < (int)sizeof(struct iphdr))
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
struct iphdr *iph = (struct iphdr *)payload;
-
- // fast path - no rules configured
+
+ // fast path when no rules
if (!g_has_active_rules && g_connection_callback == NULL)
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
@@ -1813,14 +1811,14 @@ static int packet_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, stru
src_port = ntohs(tcph->source);
dest_port = ntohs(tcph->dest);
- // skip packets from local relay
+ // skip our own packets from local relay
if (src_port == g_local_relay_port)
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
if (is_connection_tracked(src_port))
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
- // only process SYN packets for new connections
+ // only look at syn packets for new connections
if (!(tcph->syn && !tcph->ack))
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
@@ -1832,7 +1830,7 @@ static int packet_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, stru
if (action == RULE_ACTION_PROXY && is_broadcast_or_multicast(dest_ip))
action = RULE_ACTION_DIRECT;
- // log connection (skip our own process)
+ // log it if not from our own process
if (g_traffic_logging_enabled && g_connection_callback != NULL && (tcph->syn && !tcph->ack) && pid > 0 && pid != g_current_process_id)
{
char process_name[MAX_PROCESS_NAME];
@@ -1861,7 +1859,7 @@ static int packet_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, stru
const char* display_name = extract_filename(process_name);
g_connection_callback(display_name, pid, dest_ip_str, dest_port, proxy_info);
-
+
add_logged_connection(pid, dest_ip, dest_port, action);
}
}
@@ -1875,7 +1873,7 @@ static int packet_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, stru
{
// store connection info
add_connection(src_port, src_ip, dest_ip, dest_port);
-
+
// mark packet so nat table REDIRECT rule will catch it
uint32_t mark = 1;
return nfq_set_verdict2(qh, id, NF_ACCEPT, mark, 0, NULL);
@@ -1906,7 +1904,7 @@ static int packet_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, stru
if (action == RULE_ACTION_PROXY && (dest_port == 67 || dest_port == 68))
action = RULE_ACTION_DIRECT;
-
+
// UDP proxy only works with SOCKS5, not HTTP
if (action == RULE_ACTION_PROXY && g_proxy_type != PROXY_TYPE_SOCKS5)
action = RULE_ACTION_DIRECT;
@@ -1916,7 +1914,7 @@ static int packet_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, stru
{
char process_name[MAX_PROCESS_NAME];
uint32_t log_pid = (pid == 0) ? 1 : pid; // Use PID 1 for unknown processes
-
+
if (pid > 0 && get_process_name_from_pid(pid, process_name, sizeof(process_name)))
{
// Got process name from PID
@@ -1926,7 +1924,7 @@ static int packet_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, stru
// UDP socket not found - ephemeral or timing issue
snprintf(process_name, sizeof(process_name), "unknown");
}
-
+
if (!is_connection_already_logged(log_pid, dest_ip, dest_port, action))
{
char dest_ip_str[32];
@@ -1949,7 +1947,7 @@ static int packet_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, stru
const char* display_name = extract_filename(process_name);
g_connection_callback(display_name, log_pid, dest_ip_str, dest_port, proxy_info);
-
+
add_logged_connection(log_pid, dest_ip, dest_port, action);
}
}
@@ -1962,7 +1960,7 @@ static int packet_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, stru
{
// UDP proxy via SOCKS5 UDP ASSOCIATE
add_connection(src_port, src_ip, dest_ip, dest_port);
-
+
// Mark UDP packet for redirect to local UDP relay (port 34011)
uint32_t mark = 2; // Use mark=2 for UDP (mark=1 is for TCP)
return nfq_set_verdict2(qh, id, NF_ACCEPT, mark, 0, NULL);
@@ -2186,7 +2184,7 @@ static bool is_connection_already_logged(uint32_t pid, uint32_t dest_ip, uint16_
LOGGED_CONNECTION *logged = logged_connections;
while (logged != NULL)
{
- if (logged->pid == pid && logged->dest_ip == dest_ip &&
+ if (logged->pid == pid && logged->dest_ip == dest_ip &&
logged->dest_port == dest_port && logged->action == action)
{
pthread_mutex_unlock(&log_lock);
@@ -2737,20 +2735,20 @@ bool ProxyBridge_Start(void)
// mangle table runs before nat, so we can mark packets there
int ret1 = run_iptables_cmd("-t", "mangle", "-A", "OUTPUT", "-p", "tcp", "-j", "NFQUEUE", "--queue-num", "0", NULL, NULL, NULL, NULL);
int ret2 = run_iptables_cmd("-t", "mangle", "-A", "OUTPUT", "-p", "udp", "-j", "NFQUEUE", "--queue-num", "0", NULL, NULL, NULL, NULL);
-
+
if (ret1 != 0 || ret2 != 0) {
log_message("failed to add iptables rules ret1=%d ret2=%d", ret1, ret2);
} else {
log_message("iptables nfqueue rules added successfully");
}
-
+
// setup nat redirect for marked packets
int ret3 = run_iptables_cmd("-t", "nat", "-A", "OUTPUT", "-p", "tcp", "-m", "mark", "--mark", "1", "-j", "REDIRECT", "--to-port", "34010");
int ret4 = run_iptables_cmd("-t", "nat", "-A", "OUTPUT", "-p", "udp", "-m", "mark", "--mark", "2", "-j", "REDIRECT", "--to-port", "34011");
if (ret3 != 0 || ret4 != 0) {
log_message("failed to add nat redirect rules");
}
-
+
(void)ret3;
(void)ret4;