From 4e1a0731e73da85ddfd2841c4f118f349f9181c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sun, 4 Aug 2024 23:43:05 +0200 Subject: [PATCH] core: convert proxy option `ipv6` from boolean to enum (disable, auto, force) (issue #2164) --- CHANGELOG.md | 1 + src/core/core-command.c | 24 +++++++++++++++++++---- src/core/core-config.c | 43 ++++++++++++++++++++++++++++++++++++++++- src/core/core-config.h | 2 +- src/core/core-network.c | 21 ++++++++++++++++++-- src/core/core-proxy.c | 14 +++++++++----- src/core/core-proxy.h | 10 ++++++++++ 7 files changed, 102 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52d9490b1..7ec94bd69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - **breaking:** irc: rename parameter `-re` to `-raw` in command `/list` ([#2124](https://github.com/weechat/weechat/issues/2124)) - **breaking:** api: add constants for IPv6 and allow force of IPv6 in function hook_connect ([#2164](https://github.com/weechat/weechat/issues/2164)) - **breaking:** irc: convert server option `ipv6` from boolean to enum (disable, auto, force) ([#2164](https://github.com/weechat/weechat/issues/2164)) +- **breaking:** core: convert proxy option `ipv6` from boolean to enum (disable, auto, force) ([#2164](https://github.com/weechat/weechat/issues/2164)) - core: add option `addreplace` in commands `/bar` and `/proxy` ([#2095](https://github.com/weechat/weechat/issues/2095)) - irc: add option `addreplace` in commands `/ignore`, `/notify` and `/server` ([#2095](https://github.com/weechat/weechat/issues/2095)) - relay: add option `addreplace` in commands `/relay` and `/remote` ([#2095](https://github.com/weechat/weechat/issues/2095)) diff --git a/src/core/core-command.c b/src/core/core-command.c index 13481a9aa..027f83179 100644 --- a/src/core/core-command.c +++ b/src/core/core-command.c @@ -5492,6 +5492,7 @@ void command_proxy_list () { struct t_proxy *ptr_proxy; + const char *ipv6_status; if (weechat_proxies) { @@ -5500,6 +5501,21 @@ command_proxy_list () for (ptr_proxy = weechat_proxies; ptr_proxy; ptr_proxy = ptr_proxy->next_proxy) { + switch (CONFIG_ENUM(ptr_proxy->options[PROXY_OPTION_IPV6])) + { + case PROXY_IPV6_DISABLE: + ipv6_status = _("IPv6: disabled"); + break; + case PROXY_IPV6_AUTO: + ipv6_status = _("IPv6: automatic"); + break; + case PROXY_IPV6_FORCE: + ipv6_status = _("IPv6: forced"); + break; + default: + ipv6_status = NULL; + break; + } gui_chat_printf (NULL, _(" %s%s%s: %s, %s/%d (%s), username: %s, " "password: %s"), @@ -5509,7 +5525,7 @@ command_proxy_list () proxy_type_string[CONFIG_ENUM(ptr_proxy->options[PROXY_OPTION_TYPE])], CONFIG_STRING(ptr_proxy->options[PROXY_OPTION_ADDRESS]), CONFIG_INTEGER(ptr_proxy->options[PROXY_OPTION_PORT]), - (CONFIG_INTEGER(ptr_proxy->options[PROXY_OPTION_IPV6])) ? "IPv6" : "IPv4", + (ipv6_status) ? ipv6_status : "?", (CONFIG_STRING(ptr_proxy->options[PROXY_OPTION_USERNAME]) && CONFIG_STRING(ptr_proxy->options[PROXY_OPTION_USERNAME])[0]) ? CONFIG_STRING(ptr_proxy->options[PROXY_OPTION_USERNAME]) : _("(none)"), @@ -5583,7 +5599,7 @@ COMMAND_CALLBACK(proxy) if (error && !error[0]) { /* add proxy */ - if (proxy_new (argv[2], argv[3], "off", argv[4], argv[5], + if (proxy_new (argv[2], argv[3], "disable", argv[4], argv[5], (argc >= 7) ? argv[6] : NULL, (argc >= 8) ? argv_eol[7] : NULL)) { @@ -9185,9 +9201,9 @@ command_init () N_("Examples:"), N_(" add a http proxy, running on local host, port 8888:"), AI(" /proxy add local http 127.0.0.1 8888"), - N_(" add a http proxy using IPv6 protocol:"), + N_(" add a http proxy using IPv6 protocol only:"), AI(" /proxy add local http ::1 8888"), - AI(" /proxy set local ipv6 on"), + AI(" /proxy set local ipv6 force"), N_(" add a socks5 proxy with username/password:"), AI(" /proxy add myproxy socks5 sample.host.org 3128 myuser mypass"), N_(" delete a proxy:"), diff --git a/src/core/core-config.c b/src/core/core-config.c index d02ae4eef..579f26f3c 100644 --- a/src/core/core-config.c +++ b/src/core/core-config.c @@ -1652,7 +1652,7 @@ config_weechat_update_cb (const void *pointer, void *data, { "number_desc" "-buffer.number" }, { NULL, NULL }, }; - char *new_option; + char *new_option, *new_value, *pos_option; int changes, i; /* make C compiler happy */ @@ -1806,6 +1806,47 @@ config_weechat_update_cb (const void *pointer, void *data, } } + if (version_read < 4) + { + /* + * changes in v4 (WeeChat 4.4.0): + * - proxy option "ipv6" is converted from boolean to enum: + * - "on" -> "auto" + * - "off" -> "disable" + * (new possible value "force" is not set by this function) + */ + ptr_config = hashtable_get (data_read, "config"); + ptr_section = hashtable_get (data_read, "section"); + ptr_option = hashtable_get (data_read, "option"); + ptr_value = hashtable_get (data_read, "value"); + if (ptr_section + && ptr_option + && (strcmp (ptr_section, "proxy") == 0) + && ptr_value) + { + pos_option = strrchr (ptr_option, '.'); + if (pos_option && (strcmp (pos_option + 1, "ipv6") == 0)) + { + new_value = (strcmp (ptr_value, "off") == 0) ? + strdup ("disable") : strdup ("auto"); + if (new_value) + { + gui_chat_printf ( + NULL, + _("Value of option \"%s.%s.%s\" has been converted: \"%s\" => \"%s\""), + ptr_config, + ptr_section, + ptr_option, + ptr_value, + new_value); + hashtable_set (data_read, "value", new_value); + changes++; + free (new_value); + } + } + } + } + return (changes) ? data_read : NULL; } diff --git a/src/core/core-config.h b/src/core/core-config.h index e57e4ad0d..4960d971a 100644 --- a/src/core/core-config.h +++ b/src/core/core-config.h @@ -31,7 +31,7 @@ struct t_gui_buffer; #define WEECHAT_CONFIG_NAME "weechat" #define WEECHAT_CONFIG_PRIO_NAME "110000|weechat" -#define WEECHAT_CONFIG_VERSION 3 +#define WEECHAT_CONFIG_VERSION 4 #define TAB_MAX_WIDTH 64 diff --git a/src/core/core-network.c b/src/core/core-network.c index e2a7b6b6f..3d437bf8c 100644 --- a/src/core/core-network.c +++ b/src/core/core-network.c @@ -994,8 +994,25 @@ network_connect_child (struct t_hook *hook_connect) res_init (); if (ptr_proxy) { - hints.ai_family = (CONFIG_BOOLEAN(ptr_proxy->options[PROXY_OPTION_IPV6])) ? - AF_UNSPEC : AF_INET; + switch (CONFIG_ENUM(ptr_proxy->options[PROXY_OPTION_IPV6])) + { + case PROXY_IPV6_DISABLE: + /* force IPv4 */ + hints.ai_family = AF_INET; + break; + case PROXY_IPV6_AUTO: + /* auto: IPv6 / IPv4 */ + hints.ai_family = AF_UNSPEC; + break; + case PROXY_IPV6_FORCE: + /* force IPv6 */ + hints.ai_family = AF_INET6; + break; + default: + /* auto by default */ + hints.ai_family = AF_UNSPEC; + break; + } snprintf (port, sizeof (port), "%d", CONFIG_INTEGER(ptr_proxy->options[PROXY_OPTION_PORT])); rc = getaddrinfo (CONFIG_STRING(ptr_proxy->options[PROXY_OPTION_ADDRESS]), port, &hints, &res_remote); diff --git a/src/core/core-proxy.c b/src/core/core-proxy.c index edd0c6b4c..b464bdf96 100644 --- a/src/core/core-proxy.c +++ b/src/core/core-proxy.c @@ -40,9 +40,11 @@ char *proxy_option_string[PROXY_NUM_OPTIONS] = { "type", "ipv6", "address", "port", "username", "password" }; char *proxy_option_default[PROXY_NUM_OPTIONS] = -{ "http", "off", "127.0.0.1", "3128", "", "" }; +{ "http", "auto", "127.0.0.1", "3128", "", "" }; char *proxy_type_string[PROXY_NUM_TYPES] = { "http", "socks4", "socks5" }; +char *proxy_ipv6_string[PROXY_NUM_IPV6] = +{ "disable", "auto", "force" }; struct t_proxy *weechat_proxies = NULL; /* first proxy */ struct t_proxy *last_weechat_proxy = NULL; /* last proxy */ @@ -279,9 +281,9 @@ proxy_create_option (const char *proxy_name, int index_option, case PROXY_OPTION_IPV6: ptr_option = config_file_new_option ( weechat_config_file, weechat_config_section_proxy, - option_name, "boolean", - N_("connect to proxy using ipv6"), - NULL, 0, 0, value, NULL, 0, + option_name, "enum", + N_("connect to proxy using IPv6"), + "disable|auto|force", 0, 0, value, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); break; case PROXY_OPTION_ADDRESS: @@ -660,7 +662,9 @@ proxy_print_log () log_printf (" type . . . . . . . . . : %d (%s)", CONFIG_ENUM(ptr_proxy->options[PROXY_OPTION_TYPE]), proxy_type_string[CONFIG_ENUM(ptr_proxy->options[PROXY_OPTION_TYPE])]); - log_printf (" ipv6 . . . . . . . . . : %d", CONFIG_INTEGER(ptr_proxy->options[PROXY_OPTION_IPV6])); + log_printf (" ipv6 . . . . . . . . . : %d (%s)", + CONFIG_ENUM(ptr_proxy->options[PROXY_OPTION_IPV6]), + proxy_ipv6_string[CONFIG_ENUM(ptr_proxy->options[PROXY_OPTION_IPV6])]); log_printf (" address. . . . . . . . : '%s'", CONFIG_STRING(ptr_proxy->options[PROXY_OPTION_ADDRESS])); log_printf (" port . . . . . . . . . : %d", CONFIG_INTEGER(ptr_proxy->options[PROXY_OPTION_PORT])); log_printf (" username . . . . . . . : '%s'", CONFIG_STRING(ptr_proxy->options[PROXY_OPTION_USERNAME])); diff --git a/src/core/core-proxy.h b/src/core/core-proxy.h index 67f7f7a05..7b511f493 100644 --- a/src/core/core-proxy.h +++ b/src/core/core-proxy.h @@ -43,6 +43,15 @@ enum t_proxy_type PROXY_NUM_TYPES, }; +enum t_proxy_ipv6 +{ + PROXY_IPV6_DISABLE = 0, + PROXY_IPV6_AUTO, + PROXY_IPV6_FORCE, + /* number of IPv6 options */ + PROXY_NUM_IPV6, +}; + struct t_proxy { char *name; /* proxy name */ @@ -56,6 +65,7 @@ struct t_proxy extern char *proxy_option_string[]; extern char *proxy_type_string[]; +extern char *proxy_ipv6_string[]; extern struct t_proxy *weechat_proxies; extern struct t_proxy *last_weechat_proxy; extern struct t_proxy *weechat_temp_proxies;