| 1 |
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* |
| 3 |
* GIO - GLib Input, Output and Streaming Library |
| 4 |
* |
| 5 |
* Copyright 2011 Red Hat, Inc. |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General |
| 18 |
* Public License along with this library; if not, see |
| 19 |
* <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <stdlib.h> |
| 25 |
|
| 26 |
#include <gio/gio.h> |
| 27 |
#include "glibproxyresolver.h" |
| 28 |
|
| 29 |
static const gchar introspection_xml[] = |
| 30 |
"<node>" |
| 31 |
" <interface name='org.gtk.GLib.PACRunner'>" |
| 32 |
" <method name='Lookup'>" |
| 33 |
" <arg type='s' name='pac_url' direction='in'/>" |
| 34 |
" <arg type='s' name='lookup_url' direction='in'/>" |
| 35 |
" <arg type='as' name='proxies' direction='out'/>" |
| 36 |
" </method>" |
| 37 |
" </interface>" |
| 38 |
"</node>"; |
| 39 |
|
| 40 |
static GProxyResolver *resolver; |
| 41 |
static GMainLoop *loop; |
| 42 |
|
| 43 |
static void |
| 44 |
got_proxies (GObject *source, |
| 45 |
GAsyncResult *result, |
| 46 |
gpointer user_data) |
| 47 |
{ |
| 48 |
GDBusMethodInvocation *invocation = user_data; |
| 49 |
gchar **proxies; |
| 50 |
GError *error = NULL; |
| 51 |
|
| 52 |
proxies = g_proxy_resolver_lookup_finish (resolver, result, &error); |
| 53 |
if (error) |
| 54 |
g_dbus_method_invocation_take_error (invocation, error); |
| 55 |
else |
| 56 |
{ |
| 57 |
g_dbus_method_invocation_return_value (invocation, |
| 58 |
g_variant_new ("(^as)", proxies)); |
| 59 |
g_strfreev (proxies); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
static void |
| 64 |
handle_method_call (GDBusConnection *connection, |
| 65 |
const gchar *sender, |
| 66 |
const gchar *object_path, |
| 67 |
const gchar *interface_name, |
| 68 |
const gchar *method_name, |
| 69 |
GVariant *parameters, |
| 70 |
GDBusMethodInvocation *invocation, |
| 71 |
gpointer user_data) |
| 72 |
{ |
| 73 |
const gchar *pac_url, *lookup_url; |
| 74 |
|
| 75 |
g_variant_get (parameters, "(&s&s)", &pac_url, &lookup_url); |
| 76 |
|
| 77 |
if (!g_ascii_strncasecmp (pac_url, "http", 4) || |
| 78 |
!g_ascii_strncasecmp (pac_url, "file:", 5)) |
| 79 |
{ |
| 80 |
gchar *libproxy_url = g_strdup_printf ("pac+%s", pac_url); |
| 81 |
g_setenv ("http_proxy", libproxy_url, TRUE); |
| 82 |
g_free (libproxy_url); |
| 83 |
} |
| 84 |
else |
| 85 |
g_setenv ("http_proxy", "wpad://", TRUE); |
| 86 |
|
| 87 |
g_proxy_resolver_lookup_async (resolver, lookup_url, |
| 88 |
NULL, got_proxies, invocation); |
| 89 |
} |
| 90 |
|
| 91 |
static const GDBusInterfaceVTable interface_vtable = |
| 92 |
{ |
| 93 |
handle_method_call, |
| 94 |
NULL, |
| 95 |
NULL |
| 96 |
}; |
| 97 |
|
| 98 |
static void |
| 99 |
on_bus_acquired (GDBusConnection *connection, |
| 100 |
const gchar *name, |
| 101 |
gpointer user_data) |
| 102 |
{ |
| 103 |
GDBusNodeInfo *introspection_data; |
| 104 |
GError *error = NULL; |
| 105 |
|
| 106 |
introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL); |
| 107 |
g_dbus_connection_register_object (connection, |
| 108 |
"/org/gtk/GLib/PACRunner", |
| 109 |
introspection_data->interfaces[0], |
| 110 |
&interface_vtable, |
| 111 |
NULL, |
| 112 |
NULL, |
| 113 |
&error); |
| 114 |
if (error) |
| 115 |
g_error ("Could not register server: %s", error->message); |
| 116 |
} |
| 117 |
|
| 118 |
static void |
| 119 |
on_name_acquired (GDBusConnection *connection, |
| 120 |
const gchar *name, |
| 121 |
gpointer user_data) |
| 122 |
{ |
| 123 |
} |
| 124 |
|
| 125 |
static void |
| 126 |
on_name_lost (GDBusConnection *connection, |
| 127 |
const gchar *name, |
| 128 |
gpointer user_data) |
| 129 |
{ |
| 130 |
g_main_loop_quit (loop); |
| 131 |
} |
| 132 |
|
| 133 |
int |
| 134 |
main (int argc, char *argv[]) |
| 135 |
{ |
| 136 |
int owner_id; |
| 137 |
|
| 138 |
/* Unset variables that would make libproxy try to use gconf or ksettings */ |
| 139 |
g_unsetenv ("GNOME_DESKTOP_SESSION_ID"); |
| 140 |
g_unsetenv ("DESKTOP_SESSION"); |
| 141 |
g_unsetenv ("KDE_FULL_SESSION"); |
| 142 |
|
| 143 |
/* Unset variables that libproxy would look at if it were smarter, and which |
| 144 |
* it might possibly look at in the future. Just covering our bases. */ |
| 145 |
g_unsetenv ("XDG_CURRENT_DESKTOP"); |
| 146 |
|
| 147 |
/* Unset static proxy settings */ |
| 148 |
g_unsetenv ("http_proxy"); |
| 149 |
g_unsetenv ("HTTP_PROXY"); |
| 150 |
g_unsetenv ("https_proxy"); |
| 151 |
g_unsetenv ("HTTPS_PROXY"); |
| 152 |
g_unsetenv ("ftp_proxy"); |
| 153 |
g_unsetenv ("FTP_PROXY"); |
| 154 |
g_unsetenv ("no_proxy"); |
| 155 |
g_unsetenv ("NO_PROXY"); |
| 156 |
|
| 157 |
resolver = g_object_new (G_TYPE_LIBPROXY_RESOLVER, NULL); |
| 158 |
|
| 159 |
owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, |
| 160 |
"org.gtk.GLib.PACRunner", |
| 161 |
G_BUS_NAME_OWNER_FLAGS_NONE, |
| 162 |
on_bus_acquired, |
| 163 |
on_name_acquired, |
| 164 |
on_name_lost, |
| 165 |
NULL, |
| 166 |
NULL); |
| 167 |
|
| 168 |
loop = g_main_loop_new (NULL, FALSE); |
| 169 |
g_main_loop_run (loop); |
| 170 |
|
| 171 |
g_bus_unown_name (owner_id); |
| 172 |
return 0; |
| 173 |
} |