Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trying to fix local ip detection #69

Closed
wants to merge 1 commit into from
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
trying to fix local ip detection
  • Loading branch information
fperegrinvs committed Mar 29, 2016
commit e5e1f5d3da32cda06ea28cbc6008333e1c37c526
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