Skip to content

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

fix(linux/gui): an upgrade from 3.2.x or the Linux beta keeps its settings when the window uses the service

anof-cyberaaa84e66bc59
diff --git a/Linux/cli/daemon.c b/Linux/cli/daemon.c
index 057d855..fb725ec 100644
--- a/Linux/cli/daemon.c
+++ b/Linux/cli/daemon.c
@@ -769,6 +769,27 @@ static void handle(struct client *c, pb_msg *m)
} else if (!strcmp(cmd, "store.delete")) {
NEED(1);
store_delete(c, id, ARG(0));
+ } else if (!strcmp(cmd, "store.legacy")) {
+ // the config.ini of 3.2.x and the Linux beta: handed to the window once, to be converted into a profile
+ NEED(0);
+ char path[4400];
+ snprintf(path, sizeof path, "%s/config.ini", pbp_config_dir());
+ struct stat lst;
+ FILE *lf = lstat(path, &lst) == 0 && S_ISREG(lst.st_mode) && lst.st_size <= (1 << 20) ? fopen(path, "rb") : NULL;
+ if (!lf) { reply_err(c, id, "none"); return; }
+ char *lbuf = malloc((size_t)lst.st_size + 1);
+ size_t ln = lbuf ? fread(lbuf, 1, (size_t)lst.st_size, lf) : 0;
+ fclose(lf);
+ if (!lbuf) { reply_err(c, id, "out of memory"); return; }
+ lbuf[ln] = '\0';
+ reply_ok1(c, id, lbuf);
+ free(lbuf);
+ } else if (!strcmp(cmd, "store.legacy_done")) {
+ NEED(0);
+ char from[4400], to[4400];
+ snprintf(from, sizeof from, "%s/config.ini", pbp_config_dir());
+ snprintf(to, sizeof to, "%s/config.ini.bak", pbp_config_dir());
+ if (rename(from, to) != 0 && errno != ENOENT) reply_err(c, id, strerror(errno)); else reply_ok(c, id);
} else if (!strcmp(cmd, "store.settings.get")) {
NEED(0);
char *active = NULL;
diff --git a/Linux/gui/gui.h b/Linux/gui/gui.h
index 145a121..633ca79 100644
--- a/Linux/gui/gui.h
+++ b/Linux/gui/gui.h
@@ -60,6 +60,8 @@ struct gui_store {
bool (*exists)(const char *name);
bool (*settings_get)(char **active, bool *check_updates, bool *start_at_boot); // *active g_malloc'd or NULL
bool (*settings_set)(const char *active, bool check_updates, bool start_at_boot, char *err, size_t errn);
+ char *(*legacy_get)(void); // text of a config.ini from an older version, g_malloc'd, or NULL
+ void (*legacy_done)(void); // set that file aside (config.ini.bak) once it has been converted
};
extern const struct gui_store gui_store_files;
extern const struct gui_store pbc_store; // pb_client.c
diff --git a/Linux/gui/gui_config.c b/Linux/gui/gui_config.c
index ba58949..44ea6eb 100644
--- a/Linux/gui/gui_config.c
+++ b/Linux/gui/gui_config.c
@@ -97,7 +97,25 @@ static bool file_settings_set(const char *active, bool updates, bool boot, char
return ok;
}
-const struct gui_store gui_store_files = { file_list, file_get, file_put, file_del, file_exists, file_settings_get, file_settings_set };
+static char *file_legacy_get(void) {
+ char *path = g_strdup_printf("%s/config.ini", config_dir_path());
+ chmod(path, 0600);
+ char *data = NULL;
+ gsize len = 0;
+ if (!g_file_get_contents(path, &data, &len, NULL) || len > (1u << 20)) { g_free(data); data = NULL; }
+ g_free(path);
+ return data;
+}
+
+static void file_legacy_done(void) {
+ char *from = g_strdup_printf("%s/config.ini", config_dir_path()), *to = g_strdup_printf("%s/config.ini.bak", config_dir_path());
+ if (rename(from, to) != 0) perror("could not set the old config.ini aside");
+ g_free(from);
+ g_free(to);
+}
+
+const struct gui_store gui_store_files = { file_list, file_get, file_put, file_del, file_exists, file_settings_get, file_settings_set,
+ file_legacy_get, file_legacy_done };
static const struct gui_store *g_store = &gui_store_files;
void gui_store_use(const struct gui_store *s) { g_store = s ? s : &gui_store_files; }
@@ -534,11 +552,8 @@ static RuleData *parse_rule_line(char *line) {
// config.ini of the previous versions; files without profile blocks end up in a profile called "Default".
// Returns false when there is no such file.
-static bool load_legacy_ini(const char *dir, char **saved_active_out) {
- char *path = g_strdup_printf("%s/config.ini", dir);
- chmod(path, 0600);
- FILE *f = fopen(path, "r");
- g_free(path);
+static bool load_legacy_ini(const char *text, char **saved_active_out) {
+ FILE *f = fmemopen((void *)text, strlen(text), "r");
if (!f) return false;
char line[8192];
@@ -676,14 +691,17 @@ static void load_profile_files() {
// profiles/<name>.pbprofile + settings.json; a config.ini from an older version is converted on first start
void load_config() {
- char *dir = config_dir();
char *settings_active = NULL, *legacy_active = NULL;
bool migrate = false;
load_profile_files();
g_store->settings_get(&settings_active, &g_chk_updates, &g_chk_boot);
- if (!g_profiles && g_store == &gui_store_files) { // a config.ini of an older version only exists next to the files
- migrate = load_legacy_ini(dir, &legacy_active);
+ if (!g_profiles) { // a config.ini of an older version (3.2.x, the Linux beta) sits next to the profiles
+ char *old = g_store->legacy_get();
+ if (old) {
+ migrate = load_legacy_ini(old, &legacy_active);
+ g_free(old);
+ }
for (GList *l = g_profiles; l != NULL; l = l->next) { // the old file had one pair of switches for all profiles
((Profile *)l->data)->logging = g_chk_logging;
((Profile *)l->data)->dns = g_chk_dns;
@@ -696,12 +714,8 @@ void load_config() {
if (migrate) {
save_config();
- char *from = g_strdup_printf("%s/config.ini", dir), *to = g_strdup_printf("%s/config.ini.bak", dir);
- if (rename(from, to) != 0) perror("could not set the old config.ini aside");
- g_free(from);
- g_free(to);
+ g_store->legacy_done();
}
- g_free(dir);
}
struct pbp_filter *gui_filters(size_t *n) {
diff --git a/Linux/gui/pb_client.c b/Linux/gui/pb_client.c
index c599d4d..6ebaa97 100644
--- a/Linux/gui/pb_client.c
+++ b/Linux/gui/pb_client.c
@@ -633,4 +633,15 @@ static bool st_settings_set(const char *active, bool updates, bool boot, char *e
static bool st_exists(const char *name) { (void)name; return true; } // the service owns the files; nothing can delete them behind our back
-const struct gui_store pbc_store = { st_list, st_get, st_put, st_del, st_exists, st_settings_get, st_settings_set };
+static char *st_legacy_get(void)
+{
+ reply_t r;
+ char *out = NULL;
+ if (rpc(&r, "store.legacy", 0, NULL, NULL, NULL) && r.n) out = g_strdup(r.f[0]);
+ reply_free(&r);
+ return out;
+}
+
+static void st_legacy_done(void) { simple("store.legacy_done", 0, NULL); }
+
+const struct gui_store pbc_store = { st_list, st_get, st_put, st_del, st_exists, st_settings_get, st_settings_set, st_legacy_get, st_legacy_done };
diff --git a/Linux/tests/gui_service.sh b/Linux/tests/gui_service.sh
index b6d29ad..ae5fd6a 100755
--- a/Linux/tests/gui_service.sh
+++ b/Linux/tests/gui_service.sh
@@ -59,6 +59,32 @@ wait_routing() { for _ in $(seq 40); do bpf_up && return 0; kill -0 $GUI_PID 2>/
stop_gui() { kill -TERM $GUI_PID 2>/dev/null; for _ in $(seq 40); do kill -0 $GUI_PID 2>/dev/null || break; sleep 0.25; done
if kill -0 $GUI_PID 2>/dev/null; then kill -KILL $GUI_PID; bad "the window did not exit on SIGTERM"; else wait $GUI_PID; fi; GUI_PID=; }
+echo "== upgrade: the config.ini of 3.2.x / the Linux beta is converted through the service"
+cat > "$WORK/cfg/config.ini" <<CFG
+[SETTINGS]
+activeProfile=Default
+logging=1
+dns=1
+[PROFILE:Default]
+defaultProxy=a
+[PROXIES]
+a|1|127.0.0.1|19080||
+[RULES]
+1|0|0|1|curl|192.0.2.0/24|*|a
+CFG
+chmod 600 "$WORK/cfg/config.ini"
+usermod -aG proxybridge nobody
+start_service
+run_gui nobody --clear-groups
+if wait_routing; then ok "the window started the engine from the old configuration"; else bad "no routing: $(head -5 "$WORK/gui.out")"; fi
+sleep 1
+out=$(curl -s -m 5 http://192.0.2.7/); [[ "$out" == *"hello-from-A dst=192.0.2.7:80"* ]] && ok "the old rule and proxy work through the service" || bad "old configuration not applied: $out"
+stop_gui
+[ -f "$WORK/cfg/profiles/Default.pbprofile" ] && ok "it became a profile held by the service" || bad "no profile written: $(ls "$WORK/cfg" "$WORK/cfg/profiles" 2>&1)"
+[ -f "$WORK/cfg/config.ini.bak" ] && [ ! -f "$WORK/cfg/config.ini" ] && ok "the old file was set aside as config.ini.bak" || bad "config.ini handling"
+stop_service
+rm -rf "$WORK/cfg"; mkdir -p "$WORK/cfg"
+
echo "== the service is running, the window is not root"
start_service
ok "service started (socket present)"