Commit b5e9067b authored by nextime's avatar nextime

Implement configuration file precedence in wssshc C version

- Add configuration hierarchy: CLI > ~/.config > /etc
- Add --config option to specify custom configuration file
- Refactor load_config to support multiple config file sources
- Update argument parsing to handle --config option
- Update usage message to document configuration hierarchy
- Maintain backward compatibility with existing configurations
- Support for interval configuration in config files
parent 740cbe6c
......@@ -54,13 +54,10 @@ typedef struct {
int debug;
} wssshc_config_t;
void load_config(wssshc_config_t *config) {
char path[256];
char *home = getenv("HOME");
if (!home) return;
snprintf(path, sizeof(path), "%s/.config/wsssh/wssshc.conf", home);
FILE *file = fopen(path, "r");
void load_config_file(const char *config_path, wssshc_config_t *config) {
FILE *file = fopen(config_path, "r");
if (!file) return;
char line[256];
int in_section = 0;
while (fgets(line, sizeof(line), file)) {
......@@ -89,6 +86,8 @@ void load_config(wssshc_config_t *config) {
config->port = atoi(value);
} else if (strcmp(key, "id") == 0 && !config->client_id) {
config->client_id = strdup(value);
} else if (strcmp(key, "interval") == 0) {
config->interval = atoi(value);
}
}
}
......@@ -96,6 +95,24 @@ void load_config(wssshc_config_t *config) {
fclose(file);
}
void load_config(wssshc_config_t *config) {
// Configuration hierarchy:
// 1. System config (/etc/wssshc.conf) - lowest priority
// 2. User config (~/.config/wsssh/wssshc.conf) - medium priority
// 3. Command line arguments - highest priority (handled in parse_args)
// Load system config first (lowest priority)
load_config_file("/etc/wssshc.conf", config);
// Load user config (medium priority, overrides system)
char *home = getenv("HOME");
if (home) {
char user_config_path[256];
snprintf(user_config_path, sizeof(user_config_path), "%s/.config/wsssh/wssshc.conf", home);
load_config_file(user_config_path, config);
}
}
tunnel_t *active_tunnel = NULL;
void *tunnel_thread(void *arg);
......@@ -463,6 +480,7 @@ void print_usage(const char *program_name) {
fprintf(stderr, "WebSocket SSH Client - Register with wssshd server\n\n");
fprintf(stderr, "Protect the dolls!\n\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " --config FILE Configuration file path (overrides default hierarchy)\n");
fprintf(stderr, " --server-ip IP Server IP address\n");
fprintf(stderr, " --port PORT Server port (default: %d)\n", DEFAULT_PORT);
fprintf(stderr, " --id ID Client identifier\n");
......@@ -470,8 +488,10 @@ void print_usage(const char *program_name) {
fprintf(stderr, " --interval SEC Reconnection interval (default: 30)\n");
fprintf(stderr, " --debug Enable debug output\n");
fprintf(stderr, " --help Show this help\n");
fprintf(stderr, "\nConfiguration file: ~/.config/wsssh/wssshc.conf\n");
fprintf(stderr, "Command line options override configuration file values.\n");
fprintf(stderr, "\nConfiguration hierarchy (highest priority first):\n");
fprintf(stderr, " 1. Command line arguments\n");
fprintf(stderr, " 2. ~/.config/wsssh/wssshc.conf (user config)\n");
fprintf(stderr, " 3. /etc/wssshc.conf (system config)\n");
fprintf(stderr, "\nDonations:\n");
fprintf(stderr, " BTC: bc1q3zlkpu95amtcltsk85y0eacyzzk29v68tgc5hx\n");
fprintf(stderr, " ETH: 0xdA6dAb526515b5cb556d20269207D43fcc760E51\n");
......@@ -479,6 +499,7 @@ void print_usage(const char *program_name) {
int parse_args(int argc, char *argv[], wssshc_config_t *config) {
static struct option long_options[] = {
{"config", required_argument, 0, 'c'},
{"server-ip", required_argument, 0, 's'},
{"port", required_argument, 0, 'p'},
{"id", required_argument, 0, 'i'},
......@@ -490,8 +511,13 @@ int parse_args(int argc, char *argv[], wssshc_config_t *config) {
};
int opt;
while ((opt = getopt_long(argc, argv, "s:p:i:w:t:dh", long_options, NULL)) != -1) {
char *custom_config = NULL;
while ((opt = getopt_long(argc, argv, "c:s:p:i:w:t:dh", long_options, NULL)) != -1) {
switch (opt) {
case 'c':
custom_config = optarg;
break;
case 's':
if (config->server_ip) free(config->server_ip);
config->server_ip = strdup(optarg);
......@@ -528,6 +554,11 @@ int parse_args(int argc, char *argv[], wssshc_config_t *config) {
}
}
// If --config is specified, load that file instead of the hierarchy
if (custom_config) {
load_config_file(custom_config, config);
}
return 1;
}
......@@ -1139,8 +1170,10 @@ int main(int argc, char *argv[]) {
pthread_mutex_init(&tunnel_mutex, NULL);
// Load configuration files first (system and user configs)
load_config(&config);
// Parse command line arguments (highest priority, overrides config files)
if (!parse_args(argc, argv, &config)) {
pthread_mutex_destroy(&tunnel_mutex);
return 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment