1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (C) Copyright 2016 Atul Anand <atulhjp@gmail.com>.
*/
#include "nm-default.h"
#include "nm-proxy-config.h"
#include <stdlib.h>
#include "nm-core-internal.h"
typedef struct {
NMProxyConfigMethod method;
GPtrArray *proxies;
GPtrArray *excludes;
gboolean browser_only;
char *pac_url;
char *pac_script;
} NMProxyConfigPrivate;
struct _NMProxyConfig {
GObject parent;
NMProxyConfigPrivate _priv;
};
struct _NMProxyConfigClass {
GObjectClass parent;
};
G_DEFINE_TYPE (NMProxyConfig, nm_proxy_config, G_TYPE_OBJECT)
#define NM_PROXY_CONFIG_GET_PRIVATE(self) \
({ \
/* preserve the const-ness of self. Unfortunately, that
* way, @self cannot be a void pointer */ \
typeof (self) _self = (self); \
\
/* Get compiler error if variable is of wrong type */ \
_nm_unused const NMProxyConfig *_self2 = (_self); \
\
nm_assert (NM_IS_PROXY_CONFIG (_self)); \
&_self->_priv; \
})
/*****************************************************************************/
NMProxyConfig *
nm_proxy_config_new (void)
{
return NM_PROXY_CONFIG (g_object_new (NM_TYPE_PROXY_CONFIG, NULL));
}
void
nm_proxy_config_set_method (NMProxyConfig *config, NMProxyConfigMethod method)
{
NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
priv->method = method;
}
NMProxyConfigMethod
nm_proxy_config_get_method (const NMProxyConfig *config)
{
const NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
return priv->method;
}
void
nm_proxy_config_merge_setting (NMProxyConfig *config, NMSettingProxy *setting)
{
const char *tmp = NULL;
guint32 port = 0;
NMProxyConfigPrivate *priv;
NMSettingProxyMethod method;
if (!setting)
return;
g_return_if_fail (NM_IS_SETTING_PROXY (setting));
priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
g_ptr_array_free (priv->proxies, TRUE);
g_ptr_array_free (priv->excludes, TRUE);
g_free (priv->pac_script);
priv->proxies = NULL;
priv->excludes = NULL;
priv->pac_script = NULL;
method = nm_setting_proxy_get_method (setting);
switch (method) {
case NM_SETTING_PROXY_METHOD_AUTO:
priv->method = NM_PROXY_CONFIG_METHOD_AUTO;
/* Free DHCP Obtained PAC Url (i.e Option 252)
* only when libnm overrides it.
*/
tmp = nm_setting_proxy_get_pac_url (setting);
if (tmp) {
g_free (priv->pac_url);
priv->pac_url = g_strdup (tmp);
}
tmp = nm_setting_proxy_get_pac_script (setting);
priv->pac_script = g_strdup (tmp);
break;
case NM_SETTING_PROXY_METHOD_MANUAL:
priv->method = NM_PROXY_CONFIG_METHOD_MANUAL;
priv->excludes = _nm_utils_strv_to_ptrarray ((char **) nm_setting_proxy_get_no_proxy_for (setting));
priv->proxies = g_ptr_array_new_with_free_func (g_free);
tmp = nm_setting_proxy_get_http_proxy (setting);
port = nm_setting_proxy_get_http_port (setting);
/* If HTTP Proxy has been selected for all Protocols
* set up a generic proxy in PacRunner i.e without a
* protocol prefix.
*/
if (nm_setting_proxy_get_http_default (setting)) {
if (tmp && port)
g_ptr_array_add (priv->proxies, g_strdup_printf ("%s:%u/", tmp, port));
break;
}
if (tmp && port)
g_ptr_array_add (priv->proxies, g_strdup_printf ("http://%s:%u/", tmp, port));
tmp = nm_setting_proxy_get_ssl_proxy (setting);
port = nm_setting_proxy_get_ssl_port (setting);
if (tmp && port)
g_ptr_array_add (priv->proxies, g_strdup_printf ("https://%s:%u/", tmp, port));
tmp = nm_setting_proxy_get_ftp_proxy (setting);
port = nm_setting_proxy_get_ftp_port (setting);
if (tmp && port)
g_ptr_array_add (priv->proxies, g_strdup_printf ("ftp://%s:%u/", tmp, port));
tmp = nm_setting_proxy_get_socks_proxy (setting);
port = nm_setting_proxy_get_socks_port (setting);
if (tmp && port)
g_ptr_array_add (priv->proxies, g_strdup_printf (nm_setting_proxy_get_socks_version_5 (setting) ?
"socks5://%s:%u/" : "socks4://%s:%u/", tmp, port));
break;
case NM_SETTING_PROXY_METHOD_NONE:
priv->method = NM_PROXY_CONFIG_METHOD_NONE;
/* Do Nothing */
}
priv->browser_only = nm_setting_proxy_get_browser_only (setting);
}
char **
nm_proxy_config_get_proxies (const NMProxyConfig *config)
{
const NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
return _nm_utils_ptrarray_to_strv (priv->proxies);
}
char **
nm_proxy_config_get_excludes (const NMProxyConfig *config)
{
const NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
return _nm_utils_ptrarray_to_strv (priv->excludes);
}
gboolean
nm_proxy_config_get_browser_only (const NMProxyConfig *config)
{
const NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
return priv->browser_only;
}
void
nm_proxy_config_set_pac_url (NMProxyConfig *config, const char *url)
{
NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
g_free (priv->pac_url);
priv->pac_url = g_strdup (url);
}
const char *
nm_proxy_config_get_pac_url (const NMProxyConfig *config)
{
const NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
return priv->pac_url;
}
void
nm_proxy_config_set_pac_script (NMProxyConfig *config, const char *script)
{
NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
g_free (priv->pac_script);
priv->pac_script = g_strdup (script);
}
const char *
nm_proxy_config_get_pac_script (const NMProxyConfig *config)
{
const NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
return priv->pac_script;
}
static void
nm_proxy_config_init (NMProxyConfig *config)
{
NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
priv->method = NM_PROXY_CONFIG_METHOD_NONE;
priv->proxies = g_ptr_array_new_with_free_func (g_free);
priv->excludes = g_ptr_array_new_with_free_func (g_free);
}
static void
finalize (GObject *object)
{
NMProxyConfig *self = NM_PROXY_CONFIG (object);
NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (self);
if (priv->proxies)
g_ptr_array_free (priv->proxies, TRUE);
if (priv->excludes)
g_ptr_array_free (priv->excludes, TRUE);
g_free (priv->pac_url);
g_free (priv->pac_script);
G_OBJECT_CLASS (nm_proxy_config_parent_class)->finalize (object);
}
static void
nm_proxy_config_class_init (NMProxyConfigClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = finalize;
}
|