* Remove ifaddr since psutil provides access to net interfaces
+ added mimcvdf dependency for future block spec
This commit is contained in:
parent
d4f4487fb6
commit
fe3b6c01c4
4 changed files with 75 additions and 34 deletions
|
@ -1,26 +1,44 @@
|
|||
"""Onionr - Private P2P Communication.
|
||||
|
||||
Identify LAN ip addresses and determine the best one
|
||||
"""
|
||||
from ipaddress import IPv4Address
|
||||
|
||||
from ifaddr import get_adapters
|
||||
from psutil import net_if_addrs
|
||||
from socket import AF_INET
|
||||
"""
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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 General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
lan_ips = []
|
||||
|
||||
for adapter in get_adapters():
|
||||
for ip in adapter.ips:
|
||||
ip = ip.ip
|
||||
try:
|
||||
ip = IPv4Address(ip)
|
||||
if not ip.is_private or ip.is_loopback:
|
||||
raise ValueError
|
||||
except ValueError:
|
||||
# Raised if not ipv4 or not link local
|
||||
continue
|
||||
else:
|
||||
lan_ips.append(ip.exploded)
|
||||
# https://psutil.readthedocs.io/en/latest/#psutil.net_if_addrs
|
||||
for interface in net_if_addrs().keys():
|
||||
for address in net_if_addrs()[interface]:
|
||||
# Don't see benefit in ipv6, so just check for v4 addresses
|
||||
if address[0] == AF_INET:
|
||||
# Mark the address for use in LAN if it is a private address
|
||||
if IPv4Address(address[1]).is_private:
|
||||
lan_ips.append(address[1])
|
||||
|
||||
# These are more likely to be actual local subnets rather than VPNs
|
||||
for ip in lan_ips:
|
||||
if '192.168' in ip:
|
||||
best_ip = ip
|
||||
break
|
||||
else:
|
||||
best_ip = lan_ips[0]
|
||||
|
||||
try:
|
||||
best_ip = lan_ips[0]
|
||||
except IndexError:
|
||||
best_ip = ""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue