| 1 |
/* config-gnome.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-gnome.h" |
| 25 |
|
| 26 |
#include "px-plugin-config.h" |
| 27 |
#include "px-manager.h" |
| 28 |
|
| 29 |
struct _PxConfigGnome { |
| 30 |
GObject parent_instance; |
| 31 |
GSettings *proxy_settings; |
| 32 |
GSettings *http_proxy_settings; |
| 33 |
GSettings *https_proxy_settings; |
| 34 |
GSettings *ftp_proxy_settings; |
| 35 |
GSettings *socks_proxy_settings; |
| 36 |
gboolean available; |
| 37 |
}; |
| 38 |
|
| 39 |
typedef enum { |
| 40 |
GNOME_PROXY_MODE_NONE, |
| 41 |
GNOME_PROXY_MODE_MANUAL, |
| 42 |
GNOME_PROXY_MODE_AUTO |
| 43 |
} GnomeProxyMode; |
| 44 |
|
| 45 |
static void px_config_iface_init (PxConfigInterface *iface); |
| 46 |
|
| 47 |
G_DEFINE_FINAL_TYPE_WITH_CODE (PxConfigGnome, |
| 48 |
px_config_gnome, |
| 49 |
G_TYPE_OBJECT, |
| 50 |
G_IMPLEMENT_INTERFACE (PX_TYPE_CONFIG, px_config_iface_init)) |
| 51 |
|
| 52 |
enum { |
| 53 |
PROP_0, |
| 54 |
PROP_CONFIG_OPTION |
| 55 |
}; |
| 56 |
|
| 57 |
static void |
| 58 |
px_config_gnome_init (PxConfigGnome *self) |
| 59 |
{ |
| 60 |
GSettingsSchemaSource *source; |
| 61 |
g_autoptr (GSettingsSchema) proxy_schema = NULL; |
| 62 |
const char *desktops; |
| 63 |
|
| 64 |
self->available = FALSE; |
| 65 |
|
| 66 |
desktops = getenv ("XDG_CURRENT_DESKTOP"); |
| 67 |
if (!desktops) |
| 68 |
return; |
| 69 |
|
| 70 |
/* Remember that XDG_CURRENT_DESKTOP is a list of strings. */ |
| 71 |
if (strstr (desktops, "GNOME") == NULL && |
| 72 |
strstr (desktops, "MATE") == NULL && |
| 73 |
strstr (desktops, "Pantheon") == NULL && |
| 74 |
strstr (desktops, "Cinnamon") == NULL) |
| 75 |
return; |
| 76 |
|
| 77 |
source = g_settings_schema_source_get_default (); |
| 78 |
if (!source) { |
| 79 |
g_warning ("GNOME desktop detected but no schemes installed, aborting."); |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
proxy_schema = g_settings_schema_source_lookup (source, "org.gnome.system.proxy", TRUE); |
| 84 |
|
| 85 |
self->available = proxy_schema != NULL; |
| 86 |
if (!self->available) |
| 87 |
return; |
| 88 |
|
| 89 |
self->proxy_settings = g_settings_new ("org.gnome.system.proxy"); |
| 90 |
self->http_proxy_settings = g_settings_new ("org.gnome.system.proxy.http"); |
| 91 |
self->https_proxy_settings = g_settings_new ("org.gnome.system.proxy.https"); |
| 92 |
self->ftp_proxy_settings = g_settings_new ("org.gnome.system.proxy.ftp"); |
| 93 |
self->socks_proxy_settings = g_settings_new ("org.gnome.system.proxy.socks"); |
| 94 |
} |
| 95 |
|
| 96 |
static void |
| 97 |
px_config_gnome_set_property (GObject *object, |
| 98 |
guint prop_id, |
| 99 |
const GValue *value, |
| 100 |
GParamSpec *pspec) |
| 101 |
{ |
| 102 |
switch (prop_id) { |
| 103 |
case PROP_CONFIG_OPTION: |
| 104 |
break; |
| 105 |
|
| 106 |
default: |
| 107 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 108 |
break; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
static void |
| 113 |
px_config_gnome_get_property (GObject *object, |
| 114 |
guint prop_id, |
| 115 |
GValue *value, |
| 116 |
GParamSpec *pspec) |
| 117 |
{ |
| 118 |
switch (prop_id) { |
| 119 |
case PROP_CONFIG_OPTION: |
| 120 |
break; |
| 121 |
|
| 122 |
default: |
| 123 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 124 |
break; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
static void |
| 129 |
px_config_gnome_class_init (PxConfigGnomeClass *klass) |
| 130 |
{ |
| 131 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 132 |
|
| 133 |
object_class->set_property = px_config_gnome_set_property; |
| 134 |
object_class->get_property = px_config_gnome_get_property; |
| 135 |
|
| 136 |
g_object_class_override_property (object_class, PROP_CONFIG_OPTION, "config-option"); |
| 137 |
} |
| 138 |
|
| 139 |
static void |
| 140 |
store_response (GStrvBuilder *builder, |
| 141 |
const char *type, |
| 142 |
char *host, |
| 143 |
int port, |
| 144 |
gboolean auth, |
| 145 |
char *username, |
| 146 |
char *password) |
| 147 |
{ |
| 148 |
if (type && host && strlen (type) > 0 && strlen (host) > 0 && port != 0) { |
| 149 |
g_autoptr (GString) tmp = g_string_new (type); |
| 150 |
|
| 151 |
g_string_append (tmp, "://"); |
| 152 |
if (auth) |
| 153 |
g_string_append_printf (tmp, "%s:%s@", username, password); |
| 154 |
|
| 155 |
g_string_append_printf (tmp, "%s:%d", host, port); |
| 156 |
|
| 157 |
px_strv_builder_add_proxy (builder, tmp->str); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
static void |
| 162 |
px_config_gnome_get_config (PxConfig *config, |
| 163 |
GUri *uri, |
| 164 |
GStrvBuilder *builder) |
| 165 |
{ |
| 166 |
PxConfigGnome *self = PX_CONFIG_GNOME (config); |
| 167 |
g_autofree char *proxy = NULL; |
| 168 |
GnomeProxyMode mode; |
| 169 |
|
| 170 |
if (!self->available) |
| 171 |
return; |
| 172 |
|
| 173 |
mode = g_settings_get_enum (self->proxy_settings, "mode"); |
| 174 |
if (mode == GNOME_PROXY_MODE_NONE) |
| 175 |
return; |
| 176 |
|
| 177 |
if (px_manager_is_ignore (uri, g_settings_get_strv (self->proxy_settings, "ignore-hosts"))) |
| 178 |
return; |
| 179 |
|
| 180 |
if (mode == GNOME_PROXY_MODE_AUTO) { |
| 181 |
char *autoconfig_url = g_settings_get_string (self->proxy_settings, "autoconfig-url"); |
| 182 |
|
| 183 |
if (strlen (autoconfig_url) != 0) |
| 184 |
proxy = g_strdup_printf ("pac+%s", autoconfig_url); |
| 185 |
else |
| 186 |
proxy = g_strdup ("wpad://"); |
| 187 |
|
| 188 |
px_strv_builder_add_proxy (builder, proxy); |
| 189 |
} else if (mode == GNOME_PROXY_MODE_MANUAL) { |
| 190 |
g_autofree char *username = g_settings_get_string (self->http_proxy_settings, "authentication-user"); |
| 191 |
g_autofree char *password = g_settings_get_string (self->http_proxy_settings, "authentication-password"); |
| 192 |
const char *scheme = g_uri_get_scheme (uri); |
| 193 |
gboolean auth = g_settings_get_boolean (self->http_proxy_settings, "use-authentication"); |
| 194 |
|
| 195 |
if (g_strcmp0 (scheme, "http") == 0) { |
| 196 |
g_autofree char *host = g_settings_get_string (self->http_proxy_settings, "host"); |
| 197 |
store_response (builder, |
| 198 |
"http", |
| 199 |
host, |
| 200 |
g_settings_get_int (self->http_proxy_settings, "port"), |
| 201 |
auth, |
| 202 |
username, |
| 203 |
password); |
| 204 |
} else if (g_strcmp0 (scheme, "https") == 0) { |
| 205 |
g_autofree char *host = g_settings_get_string (self->https_proxy_settings, "host"); |
| 206 |
store_response (builder, |
| 207 |
"http", |
| 208 |
host, |
| 209 |
g_settings_get_int (self->https_proxy_settings, "port"), |
| 210 |
auth, |
| 211 |
username, |
| 212 |
password); |
| 213 |
} else if (g_strcmp0 (scheme, "ftp") == 0) { |
| 214 |
g_autofree char *host = g_settings_get_string (self->ftp_proxy_settings, "host"); |
| 215 |
store_response (builder, |
| 216 |
"http", |
| 217 |
host, |
| 218 |
g_settings_get_int (self->ftp_proxy_settings, "port"), |
| 219 |
auth, |
| 220 |
username, |
| 221 |
password); |
| 222 |
} else { |
| 223 |
g_autofree char *host = g_settings_get_string (self->socks_proxy_settings, "host"); |
| 224 |
store_response (builder, |
| 225 |
"socks", |
| 226 |
host, |
| 227 |
g_settings_get_int (self->socks_proxy_settings, "port"), |
| 228 |
auth, |
| 229 |
username, |
| 230 |
password); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
static void |
| 236 |
px_config_iface_init (PxConfigInterface *iface) |
| 237 |
{ |
| 238 |
iface->name = "config-gnome"; |
| 239 |
iface->priority = PX_CONFIG_PRIORITY_DEFAULT; |
| 240 |
iface->get_config = px_config_gnome_get_config; |
| 241 |
} |