2020-03-11 09:46:42 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
|
|
|
|
|
|
|
Discover and publish private-network
|
|
|
|
"""
|
|
|
|
import socket
|
|
|
|
import struct
|
2020-03-14 04:47:44 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from typing import List
|
|
|
|
from ipaddress import ip_address
|
|
|
|
from socket import SHUT_RDWR
|
2020-03-11 09:46:42 +00:00
|
|
|
|
2020-04-06 13:51:20 +00:00
|
|
|
from .getip import lan_ips, best_ip
|
2020-03-11 09:46:42 +00:00
|
|
|
from utils.bettersleep import better_sleep
|
2020-06-17 05:00:52 +00:00
|
|
|
from . import client
|
2020-03-11 09:46:42 +00:00
|
|
|
"""
|
|
|
|
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/>.
|
|
|
|
"""
|
2020-03-23 06:24:13 +00:00
|
|
|
MCAST_GRP = '224.0.0.112'
|
2020-03-11 09:46:42 +00:00
|
|
|
MCAST_PORT = 1337
|
|
|
|
IS_ALL_GROUPS = True
|
|
|
|
ANNOUNCE_LOOP_SLEEP = 30
|
|
|
|
|
|
|
|
|
2020-03-14 04:47:44 +00:00
|
|
|
|
2020-06-17 05:00:52 +00:00
|
|
|
def learn_services():
|
2020-03-14 04:47:44 +00:00
|
|
|
"""Take a list to infintely add lan service info to."""
|
|
|
|
|
2020-03-11 09:46:42 +00:00
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
if IS_ALL_GROUPS:
|
|
|
|
# on this port, receives ALL multicast groups
|
|
|
|
sock.bind(('', MCAST_PORT))
|
|
|
|
else:
|
|
|
|
# on this port, listen ONLY to MCAST_GRP
|
|
|
|
sock.bind((MCAST_GRP, MCAST_PORT))
|
|
|
|
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
|
|
|
|
|
|
|
|
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
|
|
|
|
|
|
|
|
while True:
|
2020-03-14 04:47:44 +00:00
|
|
|
service_ips = sock.recv(200).decode('utf-8')
|
|
|
|
if 'onionr' not in service_ips:
|
|
|
|
continue
|
|
|
|
service_ips = service_ips.replace('onionr-', '').split('-')
|
2020-04-15 03:40:31 +00:00
|
|
|
|
2020-03-14 04:47:44 +00:00
|
|
|
port = 0
|
|
|
|
for service in service_ips:
|
|
|
|
try:
|
|
|
|
ip_address(service)
|
2020-03-23 06:24:13 +00:00
|
|
|
if not ip_address(service).is_private: raise ValueError
|
2020-03-23 08:39:57 +00:00
|
|
|
if service in lan_ips: raise ValueError
|
2020-03-14 04:47:44 +00:00
|
|
|
except ValueError:
|
2020-04-06 13:51:20 +00:00
|
|
|
pass
|
|
|
|
else:
|
2020-06-17 05:00:52 +00:00
|
|
|
client.connect_peer(service)
|
2020-03-11 09:46:42 +00:00
|
|
|
|
|
|
|
|
2020-03-16 07:28:41 +00:00
|
|
|
def advertise_service(specific_ips=None):
|
2020-03-11 09:46:42 +00:00
|
|
|
# regarding socket.IP_MULTICAST_TTL
|
|
|
|
# ---------------------------------
|
|
|
|
# for all packets sent, after three hops on the network the packet will not
|
|
|
|
# be re-sent/broadcast (see https://www.tldp.org/HOWTO/Multicast-HOWTO-6.html)
|
|
|
|
MULTICAST_TTL = 3
|
2020-04-06 13:51:20 +00:00
|
|
|
|
|
|
|
ips = best_ip
|
2020-03-11 09:46:42 +00:00
|
|
|
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
|
|
|
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, MULTICAST_TTL)
|
|
|
|
while True:
|
2020-03-23 06:25:27 +00:00
|
|
|
sock.sendto(f"onionr-{ips}".encode('utf-8'), (MCAST_GRP, MCAST_PORT))
|
2020-03-11 09:46:42 +00:00
|
|
|
better_sleep(ANNOUNCE_LOOP_SLEEP)
|
2020-03-23 06:24:13 +00:00
|
|
|
|