-
Notifications
You must be signed in to change notification settings - Fork 1
/
autosocks.py
89 lines (78 loc) · 2.37 KB
/
autosocks.py
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
import logging
import os
import subprocess
def detect_gnome():
""" Gnome via python-gconf """
from gconf import client_get_default
gconf_client = client_get_default()
mode = gconf_client.get_string("/system/proxy/mode")
if mode != "manual":
return None, None
host = gconf_client.get_string("/system/proxy/socks_host")
port = gconf_client.get_int("/system/proxy/socks_port")
return host, port
def detect_osx():
""" OS X 10.5 and up via PyObjC """
from SystemConfiguration import SCDynamicStoreCopyProxies
osx_proxy = SCDynamicStoreCopyProxies(None)
if osx_proxy.get("SOCKSEnable"):
host = osx_proxy.get("SOCKSProxy")
port = int(osx_proxy.get("SOCKSPort"))
return host, port
return None, None
def detect_kde():
""" KDE via command line, why no python bindings for KDE proxy settings? """
if os.environ.get("KDE_FULL_SESSION") != "true":
return None, None
p = subprocess.Popen(
[
"kreadconfig",
"--file",
"kioslaverc",
"--group",
"Proxy Settings",
"--key",
"socksProxy",
],
shell=True,
stdout=subprocess.PIPE,
)
host, port = p.stdout.readline()[:-1].split(":")
p.close()
port = int(port)
return host, port
def detect_env():
""" fallback to environment variables """
socks_environ = os.environ.get("SOCKS_SERVER")
if not socks_environ:
return None, None
host, port = socks_environ
port = int(port)
return host, port
def configure_socks(host, port):
""" hijack socket.socket using SocksiPy """
try:
import socks, socket
except ImportError:
logging.error("Failed to use configured SOCKS proxy: %s:%s", host, port)
logging.error("Try installing SocksiPy: http://socksipy.sf.net")
return False
socket.socket = socks.socksocket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, host, port)
return True
def try_autosocks():
functions = [
detect_gnome,
detect_osx,
detect_kde,
detect_env,
]
for func in functions:
host, port = None, None
try:
host, port = func()
except Exception as e:
pass
if host is not None and port is not None:
return configure_socks(host, port)
return False