Skip to content

Commit

Permalink
Add I2P Support
Browse files Browse the repository at this point in the history
Merge HelloZeroNet/ZeroNet#602

Signed-off-by: Marek Küthe <[email protected]>
  • Loading branch information
marek22k committed Oct 22, 2023
1 parent 689d930 commit fc5b0d4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
3 changes: 3 additions & 0 deletions AnnounceBitTorrent/AnnounceBitTorrentPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ def httpRequest(self, url):
handler = sockshandler.SocksiPyHandler(socks.SOCKS5, tor_manager.proxy_ip, tor_manager.proxy_port)
opener = urllib.request.build_opener(handler)
return opener.open(req, timeout=50)
elif config.trackers_proxy == "i2p":
i2p_manager = self.site.connection_server.i2p_manager
return i2p_manager.urlopen(self.site.address, req, timeout=50)
elif config.trackers_proxy == "disable":
return urllib.request.urlopen(req, timeout=25)
else:
Expand Down
30 changes: 29 additions & 1 deletion AnnounceZero/AnnounceZeroPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def processPeerRes(tracker_address, site, peers):
if site.addPeer(peer_onion, peer_port, source="tracker"):
added += 1

# I2P Destinations
found_dest = 0
for packed_address in peers["i2p"]:
found_dest += 1
peer_dest, peer_port = helper.unpackI2PAddress(packed_address)
if site.addPeer(peer_dest, peer_port):
added += 1

# Ip4
found_ipv4 = 0
peers_normal = itertools.chain(peers.get("ip4", []), peers.get("ipv4", []), peers.get("ipv6", []))
Expand Down Expand Up @@ -62,6 +70,8 @@ def announceTrackerZero(self, tracker_address, mode="start", num_want=10):
need_types += self.site.connection_server.supported_ip_types
if self.site.connection_server.tor_manager.enabled:
need_types.append("onion")
if self.site.connection_server.i2p_manager.enabled:
need_types.append("i2p")

if mode == "start" or mode == "more": # Single: Announce only this site
sites = [self.site]
Expand All @@ -77,12 +87,15 @@ def announceTrackerZero(self, tracker_address, mode="start", num_want=10):
# Create request
add_types = self.getOpenedServiceTypes()
request = {
"hashes": [], "onions": [], "port": self.fileserver_port, "need_types": need_types, "need_num": 20, "add": add_types
"hashes": [], "onions": [], "i2pdests": [], "port": self.fileserver_port, "need_types": need_types, "need_num": 20, "add": add_types
}
for site in sites:
if "onion" in add_types:
onion = self.site.connection_server.tor_manager.getOnion(site.address)
request["onions"].append(onion)
if "i2p" in add_types:
dest = self.site.connection_server.i2p_manager.getDest(site.address)
request["i2pdests"].append(dest.base64())
request["hashes"].append(site.address_hash)

# Tracker can remove sites that we don't announce
Expand Down Expand Up @@ -130,6 +143,21 @@ def announceTrackerZero(self, tracker_address, mode="start", num_want=10):
time_full_announced[tracker_address] = 0
raise AnnounceError("Announce onion address to failed: %s" % res)

if "i2p_sign_this" in res:
self.site.log.debug("Signing %s for %s to add %s I2P dests" % (res["i2p_sign_this"], tracker_address, len(sites)))
request["i2p_signs"] = {}
request["i2p_sign_this"] = res["i2p_sign_this"]
request["need_num"] = 0
for site in sites:
dest = self.connection_server.i2p_manager.getPrivateDest(site.address)
sign = dest.sign(res["i2p_sign_this"])
request["i2p_signs"][dest.base64()] = sign
res = tracker.request("announce", request)
if not res or "i2p_sign_this" in res:
if full_announce:
time_full_announced[tracker_address] = 0
raise AnnounceError("Announce I2P Destination to %s failed: %s" % (tracker_address, res))

if full_announce:
tracker_peer.remove() # Close connection, we don't need it in next 5 minute

Expand Down
10 changes: 8 additions & 2 deletions Sidebar/SidebarPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def sidebarRenderPeerStats(self, body, site):
connected = len([peer for peer in list(site.peers.values()) if peer.connection and peer.connection.connected])
connectable = len([peer_id for peer_id in list(site.peers.keys()) if not peer_id.endswith(":0")])
onion = len([peer_id for peer_id in list(site.peers.keys()) if ".onion" in peer_id])
i2p = len([peer_id for peer_id in list(site.peers.keys()) if ".i2p" in peer_id])
local = len([peer for peer in list(site.peers.values()) if helper.isPrivateIp(peer.ip)])
peers_total = len(site.peers)

Expand All @@ -101,21 +102,24 @@ def sidebarRenderPeerStats(self, body, site):
connectable += 1
if site.connection_server.tor_manager.start_onions:
onion += 1
if site.connection_server.i2p_manager.start_dests:
i2p += 1

if peers_total:
percent_connected = float(connected) / peers_total
percent_connectable = float(connectable) / peers_total
percent_onion = float(onion) / peers_total
percent_i2p = float(i2p) / peers_total
else:
percent_connectable = percent_connected = percent_onion = 0
percent_connectable = percent_connected = percent_onion = percent_i2p = 0

if local:
local_html = _("<li class='color-yellow'><span>{_[Local]}:</span><b>{local}</b></li>")
else:
local_html = ""

peer_ips = [peer.key for peer in site.getConnectablePeers(20, allow_private=False)]
peer_ips.sort(key=lambda peer_ip: ".onion:" in peer_ip)
peer_ips.sort(key=lambda peer_ip: ".onion:" in peer_ip or ".i2p" in peer_ip)
copy_link = "http://127.0.0.1:43110/%s/?zeronet_peers=%s" % (
site.content_manager.contents.get("content.json", {}).get("domain", site.address),
",".join(peer_ips)
Expand All @@ -131,12 +135,14 @@ def sidebarRenderPeerStats(self, body, site):
<li style='width: 100%' class='total back-black' title="{_[Total peers]}"></li>
<li style='width: {percent_connectable:.0%}' class='connectable back-blue' title='{_[Connectable peers]}'></li>
<li style='width: {percent_onion:.0%}' class='connected back-purple' title='{_[Onion]}'></li>
<li style='width: {percent_i2p:.0%}' class='connected back-purple' title='{_[I2P]}'></li>
<li style='width: {percent_connected:.0%}' class='connected back-green' title='{_[Connected peers]}'></li>
</ul>
<ul class='graph-legend'>
<li class='color-green'><span>{_[Connected]}:</span><b>{connected}</b></li>
<li class='color-blue'><span>{_[Connectable]}:</span><b>{connectable}</b></li>
<li class='color-purple'><span>{_[Onion]}:</span><b>{onion}</b></li>
<li class='color-purple'><span>{_[I2P]}:</span><b>{i2p}</b></li>
{local_html}
<li class='color-black'><span>{_[Total]}:</span><b>{peers_total}</b></li>
</ul>
Expand Down
7 changes: 7 additions & 0 deletions Stats/StatsPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ def renderTor(self):
for site_address, onion in list(main.file_server.tor_manager.site_onions.items()):
yield "- %-34s: %s<br>" % (site_address, onion)

def renderI2P(self):
import main
yield "<br><br><b>I2P Destinations (status: %s):</b><br>" % main.file_server.i2p_manager.status
for site_address, dest in list(main.file_server.i2p_manager.site_dests.items()):
yield "- %-34s: %s<br>" % (site_address, dest.base32())

def renderDbStats(self):
yield "<br><br><b>Db</b>:<br>"
for db in Db.opened_dbs:
Expand Down Expand Up @@ -406,6 +412,7 @@ def actionStats(self):
self.renderConnectionsTable(),
self.renderTrackers(),
self.renderTor(),
self.renderI2P(),
self.renderDbStats(),
self.renderSites(),
self.renderBigfiles(),
Expand Down

0 comments on commit fc5b0d4

Please sign in to comment.