summaryrefslogtreecommitdiff
path: root/src/backend/dbus/dbus.c
blob: e1e97aaa445c51af112cdf37ee3702b9b5d871c6 (plain)
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
/* dbus.c
 *
 * Copyright 2022-2023 The Libproxy Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "px-manager.h"
#include "px-interface.h"

#include <gio/gio.h>

static gboolean replace;
static gboolean use_system;

static GApplication *app;

const GOptionEntry options[] = {
  { "replace", 'r', 0, G_OPTION_ARG_NONE, &replace, "Replace old daemon.", NULL },
  { "system", 's', 0, G_OPTION_ARG_NONE, &use_system, "Use system bus.", NULL },
  { NULL }
};

static void
handle_method_call (GDBusConnection       *connection,
                    const gchar           *sender,
                    const gchar           *object_path,
                    const gchar           *interface_name,
                    const gchar           *method_name,
                    GVariant              *parameters,
                    GDBusMethodInvocation *invocation,
                    gpointer               user_data)
{
  PxManager *manager = PX_MANAGER (user_data);
  GVariantBuilder *result;
  g_auto (GStrv) proxies = NULL;
  g_autoptr (GError) error = NULL;
  const gchar *url;
  int idx;

  g_application_hold (app);
  if (g_strcmp0 (method_name, "GetProxiesFor") != 0) {
    g_warning ("Invalid method name '%s', aborting.", method_name);
    g_dbus_method_invocation_return_error (invocation,
                                           PX_MANAGER_ERROR,
                                           PX_MANAGER_ERROR_UNKNOWN_METHOD,
                                           "Unknown method");
    g_application_release (app);
    return;
  }

  g_variant_get (parameters, "(&s)", &url);

  proxies = px_manager_get_proxies_sync (manager, url, &error);
  if (error) {
    g_warning ("Could not query proxy servers: %s", error->message);
    g_dbus_method_invocation_return_gerror (invocation, error);
    g_application_release (app);
    return;
  }

  result = g_variant_builder_new (G_VARIANT_TYPE ("as"));
  if (proxies) {
    for (idx = 0; proxies[idx]; idx++) {
      g_variant_builder_add (result, "s", proxies[idx]);
    }
  }

  g_dbus_method_invocation_return_value (invocation,
                                         g_variant_new ("(as)", result));
  g_application_release (app);
}

static const GDBusInterfaceVTable interface_vtable = {
  handle_method_call,
};

static void
on_bus_acquired (GDBusConnection *connection,
                 const gchar     *name,
                 gpointer         user_data)
{
  g_autoptr (GError) error = NULL;
  PxManager *manager = NULL;

  manager = px_manager_new ();
  g_dbus_connection_register_object (connection,
                                     "/org/libproxy/proxy",
                                     (GDBusInterfaceInfo *)&org_libproxy_proxy_interface,
                                     &interface_vtable,
                                     manager,
                                     g_object_unref,
                                     &error);
  g_application_release (user_data);

  if (error) {
    g_warning ("Could not register dbus object: %s", error->message);
    g_application_quit (user_data);
    return;
  }
}

static void
on_name_lost (GDBusConnection *connection,
              const gchar     *name,
              gpointer         user_data)
{
  if (!connection) {
    g_warning ("Can't connect proxy bus");
    g_application_quit (user_data);
  } else {
    g_warning ("Unknown name lost error");
    g_application_quit (user_data);
  }
}

static void
activate (GApplication *application)
{
  GBusNameOwnerFlags flags;

  flags = G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
  if (replace)
    flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;

  g_bus_own_name (use_system ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
                  "org.libproxy.proxy",
                  flags,
                  on_bus_acquired,
                  NULL,
                  on_name_lost,
                  app,
                  NULL);

  g_application_hold (app);
}

int
main (int    argc,
      char **argv)
{
  GOptionContext *context;
  g_autoptr (GError) error = NULL;

  replace = FALSE;
  use_system = FALSE;

  context = g_option_context_new ("");
  g_option_context_set_summary (context, "Libproxy D-Bus Service");
  g_option_context_add_main_entries (context, options, "libproxy");

  if (!g_option_context_parse (context, &argc, &argv, &error)) {
    g_printerr ("%s: %s", g_get_application_name (), error->message);
    g_printerr ("\n");
    g_printerr ("Try \"%s --help\" for more information.",
                g_get_prgname ());
    g_printerr ("\n");
    g_option_context_free (context);
    return 1;
  }

  app = g_application_new ("org.libproxy.proxy-service",
#if GLIB_CHECK_VERSION (2, 73, 0)
                           G_APPLICATION_DEFAULT_FLAGS
#else
                           G_APPLICATION_FLAGS_NONE
#endif
                           );

  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);

  /* Set application timeout to 60 seconds */
  g_application_set_inactivity_timeout (app, 60000);

  return g_application_run (app, argc, argv);
}