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
|
/*******************************************************************************
* libproxy - A library for proxy configuration
* Copyright (C) 2010 Intel Corporation
*
* 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
******************************************************************************/
#include "../extension_config.hpp"
using namespace libproxy;
#include <string.h>
#include <dbus/dbus.h>
class pacrunner_config_extension : public config_extension {
public:
pacrunner_config_extension() {
this->conn = NULL;
}
~pacrunner_config_extension() {
if (this->conn) dbus_connection_close(this->conn);
}
class scoped_dbus_message {
public:
scoped_dbus_message(DBusMessage *msg) {
this->msg = msg;
}
~scoped_dbus_message() {
if (this->msg)
dbus_message_unref(msg);
}
private:
DBusMessage *msg;
};
vector<url> get_config(const url &dest) {
// Make sure we have a valid connection with a proper match
DBusConnection *conn = this->conn;
vector<url> response;
if (!conn || !dbus_connection_get_is_connected(conn))
{
// If the connection was disconnected,
// close it an clear the queue
if (conn)
{
dbus_connection_close(conn);
dbus_connection_read_write(conn, 0);
for (DBusMessage *msg=NULL ; (msg = dbus_connection_pop_message(conn)) ; dbus_message_unref(msg)) {};
}
// Create a new connections
conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, NULL);
this->conn = conn;
if (!conn)
throw runtime_error("Unable to set up DBus connection");
// If connection was successful, set it up
dbus_connection_set_exit_on_disconnect(conn, false);
}
DBusMessage *msg, *reply;
msg = dbus_message_new_method_call("org.pacrunner",
"/org/pacrunner/client",
"org.pacrunner.Client",
"FindProxyForURL");
if (!msg)
throw runtime_error("Unable to create PacRunner DBus call");
string dest_str = dest.to_string();
string dest_host = dest.get_host();
const char *dest_cstr = dest_str.c_str();
const char *dest_host_cstr = dest_host.c_str();
dbus_message_append_args(msg, DBUS_TYPE_STRING, &dest_cstr,
DBUS_TYPE_STRING, &dest_host_cstr,
DBUS_TYPE_INVALID);
reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, NULL);
dbus_message_unref(msg);
if (!reply)
throw runtime_error("Failed to get DBus response from PacRunner");
scoped_dbus_message smsg(reply);
char *str = NULL;
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID);
if (!str || !strlen(str) || !strcasecmp(str, "DIRECT"))
response.push_back(url("direct://"));
else if (!strncasecmp(str, "PROXY ", 6))
response.push_back(url("http://" + string(str + 6)));
else if (!strncasecmp(str, "SOCKS ", 6))
response.push_back(url("socks://" + string(str + 6)));
else if (!strncasecmp(str, "SOCKS4 ", 7))
response.push_back(url("socks4://" + string(str + 7)));
else if (!strncasecmp(str, "SOCKS5 ", 7))
response.push_back(url("socks5://" + string(str + 7)));
else {
throw runtime_error("Unrecognised proxy response from PacRunner: " + string(str));
}
return response;
}
private:
DBusConnection *conn;
};
#define TEST_TIMEOUT_MS 100
static bool is_pacrunner_available(void)
{
DBusMessage *msg, *reply;
DBusConnection* system;
dbus_bool_t owned;
bool found = false;
const char *name = "org.pacrunner";
msg = dbus_message_new_method_call("org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"NameHasOwner");
if (!msg)
return false;
dbus_message_append_args(msg, DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID);
system = dbus_bus_get_private(DBUS_BUS_SYSTEM, NULL);
if (!system)
goto out_msg;
reply = dbus_connection_send_with_reply_and_block(system, msg,
TEST_TIMEOUT_MS,
NULL);
if (!reply)
goto out_sys;
if (dbus_message_get_args(reply, NULL, DBUS_TYPE_BOOLEAN, &owned,
DBUS_TYPE_INVALID))
found = owned;
out_reply:
dbus_message_unref(reply);
out_sys:
dbus_connection_close(system);
dbus_connection_unref(system);
out_msg:
dbus_message_unref(msg);
return found;
}
MM_MODULE_INIT_EZ(pacrunner_config_extension, is_pacrunner_available(),
NULL, NULL);
|