| 1 |
/* config-sysconfig.c |
| 2 |
* |
| 3 |
* Copyright 2023 The Libproxy Team |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
* |
| 19 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <gio/gio.h> |
| 23 |
|
| 24 |
#include "config-sysconfig.h" |
| 25 |
|
| 26 |
#include "px-manager.h" |
| 27 |
#include "px-plugin-config.h" |
| 28 |
|
| 29 |
struct _PxConfigSysConfig { |
| 30 |
GObject parent_instance; |
| 31 |
GFileMonitor *monitor; |
| 32 |
|
| 33 |
char *config_file; |
| 34 |
gboolean available; |
| 35 |
|
| 36 |
gboolean proxy_enabled; |
| 37 |
char *https_proxy; |
| 38 |
char *http_proxy; |
| 39 |
char *ftp_proxy; |
| 40 |
GStrv no_proxy; |
| 41 |
}; |
| 42 |
|
| 43 |
static void px_config_iface_init (PxConfigInterface *iface); |
| 44 |
|
| 45 |
G_DEFINE_FINAL_TYPE_WITH_CODE (PxConfigSysConfig, |
| 46 |
px_config_sysconfig, |
| 47 |
G_TYPE_OBJECT, |
| 48 |
G_IMPLEMENT_INTERFACE (PX_TYPE_CONFIG, px_config_iface_init)) |
| 49 |
|
| 50 |
enum { |
| 51 |
PROP_0, |
| 52 |
PROP_CONFIG_OPTION |
| 53 |
}; |
| 54 |
|
| 55 |
static void px_config_sysconfig_set_config_file (PxConfigSysConfig *self, |
| 56 |
const char *config_file); |
| 57 |
|
| 58 |
static void |
| 59 |
on_file_changed (GFileMonitor *monitor, |
| 60 |
GFile *file, |
| 61 |
GFile *other_file, |
| 62 |
GFileMonitorEvent event_type, |
| 63 |
gpointer user_data) |
| 64 |
{ |
| 65 |
PxConfigSysConfig *self = PX_CONFIG_SYSCONFIG (user_data); |
| 66 |
|
| 67 |
g_debug ("%s: Reloading configuration", __FUNCTION__); |
| 68 |
px_config_sysconfig_set_config_file (self, g_file_get_path (file)); |
| 69 |
} |
| 70 |
|
| 71 |
static |
| 72 |
void |
| 73 |
px_config_sysconfig_set_config_file (PxConfigSysConfig *self, |
| 74 |
const char *config_file) |
| 75 |
{ |
| 76 |
g_autoptr (GFile) file = NULL; |
| 77 |
g_autoptr (GError) error = NULL; |
| 78 |
g_autoptr (GFileInputStream) istr = NULL; |
| 79 |
g_autoptr (GDataInputStream) dstr = NULL; |
| 80 |
char *line = NULL; |
| 81 |
|
| 82 |
g_clear_pointer (&self->config_file, g_free); |
| 83 |
self->config_file = g_strdup (config_file ? config_file : "/etc/sysconfig/proxy"); |
| 84 |
self->available = FALSE; |
| 85 |
|
| 86 |
file = g_file_new_for_path (self->config_file); |
| 87 |
istr = g_file_read (file, NULL, NULL); |
| 88 |
if (!istr) { |
| 89 |
g_debug ("%s: Could not read file %s", __FUNCTION__, self->config_file); |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
dstr = g_data_input_stream_new (G_INPUT_STREAM (istr)); |
| 94 |
|
| 95 |
g_clear_object (&self->monitor); |
| 96 |
self->monitor = g_file_monitor (file, G_FILE_MONITOR_NONE, NULL, &error); |
| 97 |
if (!self->monitor) |
| 98 |
g_warning ("Could not add a file monitor for %s, error: %s", g_file_get_uri (file), error->message); |
| 99 |
else |
| 100 |
g_signal_connect_object (G_OBJECT (self->monitor), "changed", G_CALLBACK (on_file_changed), self, 0); |
| 101 |
|
| 102 |
do { |
| 103 |
g_clear_pointer (&line, g_free); |
| 104 |
|
| 105 |
line = g_data_input_stream_read_line (dstr, NULL, NULL, &error); |
| 106 |
if (line) { |
| 107 |
g_auto (GStrv) kv = NULL; |
| 108 |
g_autoptr (GString) value = NULL; |
| 109 |
kv = g_strsplit (line, "=", -1); |
| 110 |
|
| 111 |
if (g_strv_length (kv) != 2) |
| 112 |
continue; |
| 113 |
|
| 114 |
value = g_string_new (kv[1]); |
| 115 |
g_string_replace (value, "\"", "", 0); |
| 116 |
g_string_replace (value, "\r", "", 0); |
| 117 |
g_string_replace (value, " ", "", 0); |
| 118 |
|
| 119 |
if (strcmp (kv[0], "PROXY_ENABLED") == 0) { |
| 120 |
self->proxy_enabled = g_ascii_strncasecmp (value->str, "yes", 3) == 0; |
| 121 |
} else if (strcmp (kv[0], "HTTPS_PROXY") == 0) { |
| 122 |
self->https_proxy = g_strdup (value->str); |
| 123 |
} else if (strcmp (kv[0], "HTTP_PROXY") == 0) { |
| 124 |
self->http_proxy = g_strdup (value->str); |
| 125 |
} else if (strcmp (kv[0], "FTP_PROXY") == 0) { |
| 126 |
self->ftp_proxy = g_strdup (value->str); |
| 127 |
} else if (strcmp (kv[0], "NO_PROXY") == 0) { |
| 128 |
g_autofree char *tmp = g_strdup (value->str); |
| 129 |
self->no_proxy = g_strsplit (tmp, ",", -1); |
| 130 |
} |
| 131 |
} |
| 132 |
} while (line); |
| 133 |
|
| 134 |
self->available = TRUE; |
| 135 |
} |
| 136 |
|
| 137 |
static void |
| 138 |
px_config_sysconfig_init (PxConfigSysConfig *self) |
| 139 |
{ |
| 140 |
} |
| 141 |
|
| 142 |
static void |
| 143 |
px_config_sysconfig_set_property (GObject *object, |
| 144 |
guint prop_id, |
| 145 |
const GValue *value, |
| 146 |
GParamSpec *pspec) |
| 147 |
{ |
| 148 |
PxConfigSysConfig *config = PX_CONFIG_SYSCONFIG (object); |
| 149 |
|
| 150 |
switch (prop_id) { |
| 151 |
case PROP_CONFIG_OPTION: |
| 152 |
px_config_sysconfig_set_config_file (config, g_value_dup_string (value)); |
| 153 |
break; |
| 154 |
|
| 155 |
default: |
| 156 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 157 |
break; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
static void |
| 162 |
px_config_sysconfig_get_property (GObject *object, |
| 163 |
guint prop_id, |
| 164 |
GValue *value, |
| 165 |
GParamSpec *pspec) |
| 166 |
{ |
| 167 |
PxConfigSysConfig *config = PX_CONFIG_SYSCONFIG (object); |
| 168 |
|
| 169 |
switch (prop_id) { |
| 170 |
case PROP_CONFIG_OPTION: |
| 171 |
g_value_set_string (value, config->config_file); |
| 172 |
break; |
| 173 |
|
| 174 |
default: |
| 175 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 176 |
break; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
static void |
| 181 |
px_config_sysconfig_dispose (GObject *object) |
| 182 |
{ |
| 183 |
PxConfigSysConfig *self = PX_CONFIG_SYSCONFIG (object); |
| 184 |
|
| 185 |
g_clear_object (&self->monitor); |
| 186 |
g_clear_pointer (&self->no_proxy, g_strfreev); |
| 187 |
g_clear_pointer (&self->config_file, g_free); |
| 188 |
|
| 189 |
G_OBJECT_CLASS (px_config_sysconfig_parent_class)->dispose (object); |
| 190 |
} |
| 191 |
|
| 192 |
static void |
| 193 |
px_config_sysconfig_class_init (PxConfigSysConfigClass *klass) |
| 194 |
{ |
| 195 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 196 |
|
| 197 |
object_class->dispose = px_config_sysconfig_dispose; |
| 198 |
object_class->set_property = px_config_sysconfig_set_property; |
| 199 |
object_class->get_property = px_config_sysconfig_get_property; |
| 200 |
|
| 201 |
g_object_class_override_property (object_class, PROP_CONFIG_OPTION, "config-option"); |
| 202 |
} |
| 203 |
|
| 204 |
static void |
| 205 |
px_config_sysconfig_get_config (PxConfig *config, |
| 206 |
GUri *uri, |
| 207 |
GStrvBuilder *builder) |
| 208 |
{ |
| 209 |
PxConfigSysConfig *self = PX_CONFIG_SYSCONFIG (config); |
| 210 |
const char *scheme = g_uri_get_scheme (uri); |
| 211 |
g_autofree char *proxy = NULL; |
| 212 |
|
| 213 |
if (!self->proxy_enabled) |
| 214 |
return; |
| 215 |
|
| 216 |
if (px_manager_is_ignore (uri, self->no_proxy)) |
| 217 |
return; |
| 218 |
|
| 219 |
if (g_strcmp0 (scheme, "ftp") == 0) { |
| 220 |
proxy = g_strdup (self->ftp_proxy); |
| 221 |
} else if (g_strcmp0 (scheme, "https") == 0) { |
| 222 |
proxy = g_strdup (self->https_proxy); |
| 223 |
} else if (g_strcmp0 (scheme, "http") == 0) { |
| 224 |
proxy = g_strdup (self->http_proxy); |
| 225 |
} |
| 226 |
|
| 227 |
if (proxy) |
| 228 |
px_strv_builder_add_proxy (builder, proxy); |
| 229 |
} |
| 230 |
|
| 231 |
static void |
| 232 |
px_config_iface_init (PxConfigInterface *iface) |
| 233 |
{ |
| 234 |
iface->name = "config-sysconfig"; |
| 235 |
iface->priority = PX_CONFIG_PRIORITY_LAST; |
| 236 |
iface->get_config = px_config_sysconfig_get_config; |
| 237 |
} |