Skip to content

Commit 8724ca0

Browse files
authored
Merge branch 'main' into metrics
2 parents 58cf912 + e4ca2e1 commit 8724ca0

File tree

103 files changed

+12419
-1231
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+12419
-1231
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,7 @@ tests/interop/js_libp2p/js_node/src/package-lock.json
193193
# Sphinx documentation build
194194
_build/
195195

196+
# Attack simulation test results
197+
tests/security/attack_simulation/results/
196198
libp2p-forge
197199
libp2p-metrics

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
[![Python versions](https://img.shields.io/pypi/pyversions/libp2p.svg)](https://pypi.python.org/pypi/libp2p)
1212
[![Build Status](https://img.shields.io/github/actions/workflow/status/libp2p/py-libp2p/tox.yml?branch=main&label=build%20status)](https://github.com/libp2p/py-libp2p/actions/workflows/tox.yml)
1313
[![Docs build](https://readthedocs.org/projects/py-libp2p/badge/?version=latest)](http://py-libp2p.readthedocs.io/en/latest/?badge=latest)
14-
<a href="https://filecoin.drips.network/app/projects/github/libp2p/py-libp2p" target="_blank"><img src="https://filecoin.drips.network/api/embed/project/https%3A%2F%2Fgithub.com%2Flibp2p%2Fpy-libp2p/support.png?background=light&style=drips&text=project&stat=support" alt="Support py-libp2p on drips.network" height="32"></a>
1514

1615
> py-libp2p has moved beyond its experimental roots and is steadily progressing toward production readiness. The core features are stable, and we’re focused on refining performance, expanding protocol support, and ensuring smooth interop with other libp2p implementations. We welcome contributions and real-world usage feedback to help us reach full production maturity.
1716
@@ -140,3 +139,7 @@ _(non-normative, useful for team notes, not a reference)_
140139
**Communication over one connection with multiple protocols**: X and Y can communicate over the same connection using different protocols and the multiplexer will appropriately route messages for a given protocol to a particular handler function for that protocol, which allows for each host to handle different protocols with separate functions. Furthermore, we can use multiple streams for a given protocol that allow for the same protocol and same underlying connection to be used for communication about separate topics between nodes X and Y.
141140

142141
**Why use multiple streams?**: The purpose of using the same connection for multiple streams to communicate over is to avoid the overhead of having multiple connections between X and Y. In order for X and Y to differentiate between messages on different streams and different protocols, a multiplexer is used to encode the messages when a message will be sent and decode a message when a message is received. The multiplexer encodes the message by adding a header to the beginning of any message to be sent that contains the stream id (along with some other info). Then, the message is sent across the raw connection and the receiving host will use its multiplexer to decode the message, i.e. determine which stream id the message should be routed to.
142+
143+
### Support
144+
145+
<a href="https://filecoin.drips.network/app/projects/github/libp2p/py-libp2p" target="_blank"><img src="https://filecoin.drips.network/api/embed/project/https%3A%2F%2Fgithub.com%2Flibp2p%2Fpy-libp2p/support.png?background=light&style=drips&text=project&stat=support" alt="Support py-libp2p on drips.network" height="32"></a>

docs/examples.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Examples
2121
examples.rendezvous
2222
examples.random_walk
2323
examples.multiple_connections
24+
tls-support
25+
gossipsub-1.2
2426
examples.websocket
2527
examples.tls
2628
examples.autotls

docs/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ The Python implementation of the libp2p networking stack
1818

1919
Examples <examples>
2020
API <libp2p>
21-
GossipSub 1.2 <gossipsub-1.2>
2221

2322
.. toctree::
2423
:maxdepth: 1

docs/libp2p.utils.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ libp2p.utils.logging module
1212
:undoc-members:
1313
:show-inheritance:
1414

15+
libp2p.utils.multiaddr_utils module
16+
-------------------------------------
17+
18+
.. automodule:: libp2p.utils.multiaddr_utils
19+
:members:
20+
:undoc-members:
21+
:show-inheritance:
22+
1523
libp2p.utils.varint module
1624
--------------------------
1725

docs/tls-support.rst

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
Py-libp2p – TLS Support Documentation
2+
======================================================
3+
4+
.. contents::
5+
:depth: 2
6+
:local:
7+
8+
Overview of TLS in Libp2p
9+
-------------------------
10+
11+
**Purpose of TLS in P2P networking**
12+
13+
- Encrypts data between peers.
14+
- Authenticates peer identity using certificates.
15+
- Prevents man-in-the-middle attacks.
16+
17+
**Integration in libp2p security modules**
18+
19+
- TLS is one of the supported secure channel protocols (alongside Noise).
20+
- Negotiated during connection setup.
21+
22+
**Current status**
23+
24+
- **py-libp2p**: Experimental, usable for local and interop tests.
25+
- **go-libp2p / js-libp2p**: Stable and production-ready.
26+
27+
Installation Requirements
28+
-------------------------
29+
30+
**Additional dependencies**
31+
32+
Ubuntu / Debian:
33+
34+
.. code-block:: bash
35+
36+
sudo apt install build-essential python3-dev libffi-dev libssl-dev
37+
38+
macOS:
39+
40+
.. code-block:: bash
41+
42+
brew install openssl
43+
44+
Enabling TLS in py-libp2p
45+
-------------------------
46+
47+
**Working example – Listener and Dialer**
48+
49+
Listener node:
50+
51+
.. code-block:: python
52+
53+
import trio
54+
import multiaddr
55+
from libp2p import new_host
56+
from libp2p.crypto.secp256k1 import create_new_key_pair
57+
from libp2p.security.tls.transport import PROTOCOL_ID, TLSTransport
58+
59+
async def main():
60+
key_pair = create_new_key_pair(secret=None)
61+
tls_transport = TLSTransport(libp2p_keypair=key_pair)
62+
sec_opt = {PROTOCOL_ID: tls_transport}
63+
host = new_host(key_pair=key_pair, sec_opt=sec_opt)
64+
listen_addr = multiaddr.Multiaddr("/ip4/0.0.0.0/tcp/8000")
65+
async with host.run(listen_addrs=[listen_addr]):
66+
while not host.get_addrs():
67+
await trio.sleep(0.1)
68+
addrs = host.get_addrs()
69+
peer_id = host.get_id()
70+
print("TLS-enabled listener at:", addrs[0] if addrs else "No addresses")
71+
print("Peer ID:", peer_id)
72+
print("\nUse this address with the dialer:")
73+
print(f" /ip4/127.0.0.1/tcp/8000/p2p/{peer_id}")
74+
await trio.sleep_forever()
75+
76+
if __name__ == "__main__":
77+
trio.run(main)
78+
79+
Dialer node:
80+
81+
.. code-block:: python
82+
83+
import trio
84+
import multiaddr
85+
from libp2p import new_host
86+
from libp2p.crypto.secp256k1 import create_new_key_pair
87+
from libp2p.security.tls.transport import PROTOCOL_ID, TLSTransport
88+
from libp2p.peer.peerinfo import info_from_p2p_addr
89+
90+
async def main():
91+
key_pair = create_new_key_pair(secret=None)
92+
tls_transport = TLSTransport(libp2p_keypair=key_pair)
93+
sec_opt = {PROTOCOL_ID: tls_transport}
94+
host = new_host(key_pair=key_pair, sec_opt=sec_opt)
95+
96+
addr = "/ip4/127.0.0.1/tcp/8000/p2p/16Uiu2HAm3hATVnBDT13acn2utRJXsFa2LRRGrZwDsosJ1mFZsM2Q"
97+
maddr = multiaddr.Multiaddr(addr)
98+
peer_info = info_from_p2p_addr(maddr)
99+
100+
async with host.run(listen_addrs=[]):
101+
await trio.sleep(0.5)
102+
host.peerstore.add_addrs(peer_info.peer_id, peer_info.addrs, 120)
103+
104+
try:
105+
await host.connect(peer_info)
106+
print("Connected securely to", peer_info.peer_id)
107+
await trio.sleep(1)
108+
except Exception as e:
109+
print(f"Connection failed: {e}")
110+
raise
111+
112+
if __name__ == "__main__":
113+
trio.run(main)
114+
115+
**Defaults if no configuration is provided**
116+
117+
- Generates a self-signed certificate automatically.
118+
119+
**Note for testing with self-signed certificates**
120+
121+
When testing with self-signed certificates, peers need to trust each other's certificates.
122+
You can do this by calling ``trust_peer_cert_pem()`` on the TLS transport before creating the host:
123+
124+
.. code-block:: python
125+
126+
# For testing: trust peer certificates
127+
listener_tls.trust_peer_cert_pem(dialer_tls.get_certificate_pem())
128+
dialer_tls.trust_peer_cert_pem(listener_tls.get_certificate_pem())
129+
130+
Certificate Management
131+
----------------------
132+
133+
**Generate a development certificate**
134+
135+
.. code-block:: bash
136+
137+
openssl req -x509 -newkey rsa:2048 \
138+
-keyout key.pem -out cert.pem \
139+
-days 365 -nodes -subj "/CN=py-libp2p"
140+
141+
- Store keys outside version control.
142+
- Rotate certificates every 90 days in production.
143+
144+
Testing TLS Connections
145+
-----------------------
146+
147+
**Local test steps**
148+
149+
1. Run the listener example.
150+
2. Start the dialer with the listener's multiaddress.
151+
3. Confirm the secure connection in logs.
152+
153+
**Interop testing**
154+
155+
- Ensure both nodes advertise `/tls/1.0.0`.
156+
- Peer IDs must match certificate public keys.
157+
158+
Security Considerations
159+
-----------------------
160+
161+
- Never disable certificate verification in production.
162+
- Use TLS 1.3 or later.
163+
- Pin certificates for critical peers.
164+
165+
Troubleshooting
166+
---------------
167+
168+
.. list-table::
169+
:header-rows: 1
170+
:widths: 30 30 40
171+
172+
* - Problem
173+
- Cause
174+
- Solution
175+
* - Certificate not trusted
176+
- Self-signed without trust store entry
177+
- Add cert to local trust store or disable verification **only** in testing.
178+
* - Protocol negotiation failed
179+
- One peer does not support `/tls/1.0.0`
180+
- Enable TLS on both peers or use Noise.
181+
* - SSL handshake failure
182+
- TLS version mismatch or clock skew
183+
- Enforce TLS 1.3, sync system clock.
184+
* - Connection refused
185+
- Port blocked or listener not running
186+
- Check firewall rules and listener status.

0 commit comments

Comments
 (0)