Debian Codegraph @

src/backend/plugins/config-kde/config-kde.c

raw
1 /* config-kde.c
2 *
3 * Copyright 2022-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-kde.h"
25
26 #include "px-plugin-config.h"
27 #include "px-manager.h"
28
29 static void px_config_iface_init (PxConfigInterface *iface);
30
31 typedef enum {
32 KDE_PROXY_TYPE_NONE = 0,
33 KDE_PROXY_TYPE_MANUAL,
34 KDE_PROXY_TYPE_PAC,
35 KDE_PROXY_TYPE_WPAD,
36 KDE_PROXY_TYPE_SYSTEM,
37 } KdeProxyType;
38
39 struct _PxConfigKde {
40 GObject parent_instance;
41
42 char *config_file;
43 gboolean available;
44 GFileMonitor *monitor;
45
46 GStrv no_proxy;
47 char *http_proxy;
48 char *https_proxy;
49 char *ftp_proxy;
50 char *socks_proxy;
51 KdeProxyType proxy_type;
52 char *pac_script;
53 gboolean reversed_exception;
54 };
55
56 G_DEFINE_FINAL_TYPE_WITH_CODE (PxConfigKde,
57 px_config_kde,
58 G_TYPE_OBJECT,
59 G_IMPLEMENT_INTERFACE (PX_TYPE_CONFIG, px_config_iface_init))
60
61 enum {
62 PROP_0,
63 PROP_CONFIG_OPTION
64 };
65
66 static void px_config_kde_set_config_file (PxConfigKde *self,
67 char *proxy_file);
68
69 static void
70 on_file_changed (GFileMonitor *monitor,
71 GFile *file,
72 GFile *other_file,
73 GFileMonitorEvent event_type,
74 gpointer user_data)
75 {
76 PxConfigKde *self = PX_CONFIG_KDE (user_data);
77
78 g_debug ("%s: Reloading configuration\n", __FUNCTION__);
79 px_config_kde_set_config_file (self, g_file_get_path (file));
80 }
81
82 static void
83 px_config_kde_set_config_file (PxConfigKde *self,
84 char *proxy_file)
85 {
86 g_autoptr (GError) error = NULL;
87 g_autofree char *line = NULL;
88 g_autoptr (GFile) file = NULL;
89 g_autoptr (GFileInputStream) istr = NULL;
90 g_autoptr (GDataInputStream) dstr = NULL;
91 const char *desktops;
92
93 self->available = FALSE;
94
95 desktops = getenv ("XDG_CURRENT_DESKTOP");
96 if (!desktops)
97 return;
98
99 /* Remember that XDG_CURRENT_DESKTOP is a list of strings. */
100 if (strstr (desktops, "KDE") == NULL)
101 return;
102
103 g_clear_pointer (&self->config_file, g_free);
104 self->config_file = proxy_file ? g_strdup (proxy_file) : g_build_filename (g_get_user_config_dir (), "kioslaverc", NULL);
105
106 file = g_file_new_for_path (self->config_file);
107 istr = g_file_read (file, NULL, NULL);
108 if (!istr) {
109 g_debug ("%s: Could not read file %s", __FUNCTION__, self->config_file);
110 return;
111 }
112
113 dstr = g_data_input_stream_new (G_INPUT_STREAM (istr));
114
115 g_clear_object (&self->monitor);
116 self->monitor = g_file_monitor (file, G_FILE_MONITOR_NONE, NULL, &error);
117 if (!self->monitor)
118 g_warning ("Could not add a file monitor for %s, error: %s", g_file_get_uri (file), error->message);
119 else
120 g_signal_connect_object (G_OBJECT (self->monitor), "changed", G_CALLBACK (on_file_changed), self, 0);
121
122 do {
123 g_clear_pointer (&line, g_free);
124
125 line = g_data_input_stream_read_line (dstr, NULL, NULL, &error);
126 if (line) {
127 g_auto (GStrv) kv = NULL;
128 g_autoptr (GString) value = NULL;
129 kv = g_strsplit (line, "=", 2);
130
131 if (g_strv_length (kv) != 2)
132 continue;
133
134 value = g_string_new (kv[1]);
135 g_string_replace (value, "\"", "", 0);
136 g_string_replace (value, " ", ":", 0);
137 g_string_replace (value, "\r", "", 0);
138
139 if (strcmp (kv[0], "httpsProxy") == 0) {
140 self->https_proxy = g_strdup (value->str);
141 } else if (strcmp (kv[0], "httpProxy") == 0) {
142 self->http_proxy = g_strdup (value->str);
143 } else if (strcmp (kv[0], "ftpProxy") == 0) {
144 self->ftp_proxy = g_strdup (value->str);
145 } else if (strcmp (kv[0], "socksProxy") == 0) {
146 self->socks_proxy = g_strdup (value->str);
147 } else if (strcmp (kv[0], "NoProxyFor") == 0) {
148 self->no_proxy = g_strsplit (value->str, ",", -1);
149 } else if (strcmp (kv[0], "Proxy Config Script") == 0) {
150 if (!g_str_has_prefix (value->str, "/"))
151 self->pac_script = g_strdup (value->str);
152 else
153 self->pac_script = g_strconcat ("file://", value->str, NULL);
154 } else if (strcmp (kv[0], "ProxyType") == 0) {
155 self->proxy_type = atoi (value->str);
156 } else if (strcmp (kv[0], "ReversedException") == 0) {
157 self->reversed_exception = !!atoi (value->str);
158 }
159 }
160 } while (line);
161
162 self->available = TRUE;
163 }
164
165
166 static void
167 px_config_kde_init (PxConfigKde *self)
168 {
169 px_config_kde_set_config_file (self, NULL);
170 }
171
172 static void
173 px_config_kde_dispose (GObject *object)
174 {
175 PxConfigKde *self = PX_CONFIG_KDE (object);
176
177 g_clear_pointer (&self->config_file, g_free);
178 g_clear_object (&self->monitor);
179 g_clear_pointer (&self->no_proxy, g_strfreev);
180 g_clear_pointer (&self->http_proxy, g_free);
181 g_clear_pointer (&self->https_proxy, g_free);
182 g_clear_pointer (&self->ftp_proxy, g_free);
183 g_clear_pointer (&self->socks_proxy, g_free);
184 g_clear_pointer (&self->pac_script, g_free);
185
186 G_OBJECT_CLASS (px_config_kde_parent_class)->dispose (object);
187 }
188
189 static void
190 px_config_kde_set_property (GObject *object,
191 guint prop_id,
192 const GValue *value,
193 GParamSpec *pspec)
194 {
195 PxConfigKde *config = PX_CONFIG_KDE (object);
196
197 switch (prop_id) {
198 case PROP_CONFIG_OPTION:
199 px_config_kde_set_config_file (config, g_value_dup_string (value));
200 break;
201
202 default:
203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
204 break;
205 }
206 }
207
208 static void
209 px_config_kde_get_property (GObject *object,
210 guint prop_id,
211 GValue *value,
212 GParamSpec *pspec)
213 {
214 PxConfigKde *config = PX_CONFIG_KDE (object);
215
216 switch (prop_id) {
217 case PROP_CONFIG_OPTION:
218 g_value_set_string (value, config->config_file);
219 break;
220
221 default:
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 break;
224 }
225 }
226
227 static void
228 px_config_kde_class_init (PxConfigKdeClass *klass)
229 {
230 GObjectClass *object_class = G_OBJECT_CLASS (klass);
231
232 object_class->dispose = px_config_kde_dispose;
233 object_class->set_property = px_config_kde_set_property;
234 object_class->get_property = px_config_kde_get_property;
235
236 g_object_class_override_property (object_class, PROP_CONFIG_OPTION, "config-option");
237 }
238
239 static void
240 px_config_kde_get_config (PxConfig *config,
241 GUri *uri,
242 GStrvBuilder *builder)
243 {
244 PxConfigKde *self = PX_CONFIG_KDE (config);
245 const char *scheme;
246 g_autofree char *proxy = NULL;
247
248 if (!self->available)
249 return;
250
251 if (self->proxy_type == KDE_PROXY_TYPE_NONE)
252 return;
253
254 if (self->reversed_exception) {
255 /* ReversedException flips the meaning of the ignore list */
256 if (!px_manager_is_ignore (uri, self->no_proxy))
257 return;
258 } else {
259 if (px_manager_is_ignore (uri, self->no_proxy))
260 return;
261 }
262
263 scheme = g_uri_get_scheme (uri);
264
265 switch (self->proxy_type) {
266 case KDE_PROXY_TYPE_MANUAL:
267 case KDE_PROXY_TYPE_SYSTEM:
268 /* System is the same as manual, except that a button for auto dection
269 * is shown. Based on this manual fields are set.
270 */
271 if (g_strcmp0 (scheme, "ftp") == 0) {
272 proxy = g_strdup (self->ftp_proxy);
273 } else if (g_strcmp0 (scheme, "https") == 0) {
274 proxy = g_strdup (self->https_proxy);
275 } else if (g_strcmp0 (scheme, "http") == 0) {
276 proxy = g_strdup (self->http_proxy);
277 } else if (self->socks_proxy && strlen (self->socks_proxy) > 0) {
278 proxy = g_strdup (self->socks_proxy);
279 }
280 break;
281 case KDE_PROXY_TYPE_WPAD:
282 proxy = g_strdup ("wpad://");
283 break;
284 case KDE_PROXY_TYPE_PAC:
285 proxy = g_strdup_printf ("pac+%s", self->pac_script);
286 break;
287 case KDE_PROXY_TYPE_NONE:
288 default:
289 break;
290 }
291
292 if (proxy)
293 px_strv_builder_add_proxy (builder, proxy);
294 }
295
296 static void
297 px_config_iface_init (PxConfigInterface *iface)
298 {
299 iface->name = "config-kde";
300 iface->priority = PX_CONFIG_PRIORITY_DEFAULT;
301 iface->get_config = px_config_kde_get_config;
302 }