Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions SWProxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,13 @@ def priority(ip):

# ref: http://stackoverflow.com/a/1267524
# excluding reserved ip ranges defined on http://tools.ietf.org/html/rfc5735
try:
sockets = [[(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]
ordered_list = sorted(socket.gethostbyname_ex(socket.gethostname())[2], key=priority)
ips = [l for l in ([ip for ip in ordered_list if
not address_in_network(ip, "127.0.0.0", 8) and not address_in_network(ip, "169.254.0.0", 16) and
not address_in_network(ip, "172.16.0.0", 12) and not address_in_network(ip, "192.0.0.0", 12) and
not address_in_network(ip, "192.0.2.0", 24) and not address_in_network(ip, "192.88.99.0", 24) and
not address_in_network(ip, "198.18.0.0", 15)][:1], sockets) if l]
return ips[0][0]
except (KeyError, socket.gaierror):
try:
# first fallback
return socket.gethostbyname(socket.gethostname())
except (KeyError, socket.gaierror):
# sometimes, this just works, fixed OSX
return sockets[0]
sockets = [[(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]
ordered_list = sorted(sockets, key=priority)
ips = [ip for ip in ordered_list if
not address_in_network(ip, "127.0.0.0", 8) and not address_in_network(ip, "169.254.0.0", 16) and
not address_in_network(ip, "172.16.0.0", 12) and not address_in_network(ip, "192.0.2.0", 24) and
not address_in_network(ip, "192.88.99.0", 24) and not address_in_network(ip, "198.18.0.0", 15)]
return ips[0]


def read_file_lines(fpath):
Expand Down