1
# -*- coding: utf-8 -*-
3
# Copyright 2011-2012 Canonical Ltd.
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License version 3, as published
7
# by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License along
15
# with this program. If not, see <http://www.gnu.org/licenses/>.
17
# In addition, as a special exception, the copyright holders give
18
# permission to link the code of portions of this program with the
19
# OpenSSL library under certain conditions as described in each
20
# individual source file, and distribute linked combinations
22
# You must obey the GNU General Public License in all respects
23
# for all of the code used other than OpenSSL. If you modify
24
# file(s) with this exception, you may extend this exception to your
25
# version of the file(s), but you are not obligated to do so. If you
26
# do not wish to do so, delete this exception statement from your
27
# version. If you delete this exception statement from all source
28
# files in the program, then also delete it here.
29
"""Retrieve the proxy configuration from Gnome."""
33
GSETTINGS_CMDLINE = "gsettings list-recursively org.gnome.system.proxy"
36
def parse_proxy_host(hostname):
37
"""Parse the host to get username and password."""
41
username, hostname = hostname.rsplit("@", 1)
43
username, password = username.split(":", 1)
44
return hostname, username, password
47
def parse_manual_proxy_settings(scheme, gsettings):
48
"""Parse the settings for a given scheme."""
49
host, username, pwd = parse_proxy_host(gsettings[scheme + ".host"])
50
# if the user did not set a proxy for a type (http/https/ftp) we should
51
# return None to ensure that it is not used
57
"port": gsettings[scheme + ".port"],
59
if scheme == "http" and gsettings["http.use-authentication"]:
60
username = gsettings["http.authentication-user"]
61
pwd = gsettings["http.authentication-password"]
62
if username is not None and pwd is not None:
70
def get_proxy_settings():
71
"""Parse the proxy settings as returned by the gsettings executable."""
72
output = subprocess.check_output(GSETTINGS_CMDLINE.split())
74
base_len = len("org.gnome.system.proxy.")
75
# pylint: disable=E1103
76
for line in output.split("\n"):
78
path, key, value = line.split(" ", 2)
81
if value.startswith("'"):
82
parsed_value = value[1:-1]
83
elif value.startswith('['):
85
elif value in ('true', 'false'):
86
parsed_value = (value == 'true')
88
parsed_value = int(value)
89
relative_key = (path + "." + key)[base_len:]
90
gsettings[relative_key] = parsed_value
91
mode = gsettings["mode"]
94
elif mode == "manual":
96
for scheme in ["http", "https"]:
97
scheme_settings = parse_manual_proxy_settings(scheme, gsettings)
98
if scheme_settings is not None:
99
settings[scheme] = scheme_settings
101
# If mode is automatic the PAC javascript should be interpreted
102
# on each request. That is out of scope so it's ignored for now