-
Notifications
You must be signed in to change notification settings - Fork 174
Feat/add webrtc transport #780
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
Open
Nkovaturient
wants to merge
11
commits into
libp2p:main
Choose a base branch
from
Nkovaturient:feat/add-webrtc-transport
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+6,972
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6920d5d
feat(webrtc):revamp-setup+implement-test-suites
Nkovaturient fd22cd6
Merge branch 'main' of https://github.com/Nkovaturient/py-libp2p into…
Nkovaturient cd019ce
fix(webrtc): resolve lint and type declaration issues
Nkovaturient c2ba14d
Merge branch 'main' into feat/add-webrtc-transport
seetadev e7c1910
Add message.proto and fix a few mypy-ci errors
sukhman-sukh 5d03f4f
Fix SDP and ICE message transfer
sukhman-sukh 3c60b92
Fix ICE candidate exchange
sukhman-sukh f15f7a2
Fix lint error in CI
sukhman-sukh 55d1de3
fix pyrefly typecheck in CI
Nkovaturient 0784f1c
Fix webrtc-direct proto and certmanager
sukhman-sukh 13378e6
Add dialer and listener to webrtc-direct
sukhman-sukh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
""" | ||
WebRTC Transport Module for py-libp2p. | ||
|
||
Provides both private-to-private and private-to-public WebRTC transport | ||
implementations. | ||
""" | ||
|
||
import sys | ||
from .private_to_private.transport import WebRTCTransport | ||
from .private_to_public.transport import WebRTCDirectTransport | ||
from .constants import ( | ||
DEFAULT_ICE_SERVERS, | ||
SIGNALING_PROTOCOL, | ||
MUXER_PROTOCOL, | ||
WebRTCError, | ||
SDPHandshakeError, | ||
ConnectionStateError, | ||
CertificateError, | ||
STUNError, | ||
CODEC_WEBRTC, | ||
CODEC_WEBRTC_DIRECT, | ||
CODEC_CERTHASH, | ||
PROTOCOL_WEBRTC, | ||
PROTOCOL_WEBRTC_DIRECT, | ||
PROTOCOL_CERTHASH, | ||
) | ||
from typing import Dict, Any, Protocol as TypingProtocol | ||
from multiaddr import protocols | ||
from multiaddr.protocols import Protocol | ||
from multiaddr import codecs | ||
|
||
|
||
class WebRTCCodec: | ||
"""Codec for WebRTC protocol (empty protocol with no value).""" | ||
SIZE = 0 | ||
IS_PATH = False | ||
|
||
@staticmethod | ||
def to_bytes(proto: Any, s: str) -> bytes: | ||
return b"" | ||
|
||
@staticmethod | ||
def to_string(proto: Any, b: bytes) -> str: | ||
return "" | ||
|
||
|
||
class WebRTCDirectCodec: | ||
"""Codec for WebRTC-Direct protocol (empty protocol with no value).""" | ||
SIZE = 0 | ||
IS_PATH = False | ||
|
||
@staticmethod | ||
def to_bytes(proto: Any, s: str) -> bytes: | ||
return b"" | ||
|
||
@staticmethod | ||
def to_string(proto: Any, b: bytes) -> str: | ||
return "" | ||
|
||
|
||
class CerthashCodec: | ||
"""Codec for certificate hash protocol (handles certificate hash encoding/decoding).""" | ||
SIZE = -1 # Variable size protocol | ||
LENGTH_PREFIXED_VAR_SIZE = -1 | ||
IS_PATH = False | ||
|
||
@staticmethod | ||
def to_bytes(proto: Any, s: str) -> bytes: | ||
if not s: | ||
return b"" | ||
# Remove multibase prefix if present | ||
if s.startswith('uEi'): | ||
s = s[3:] | ||
elif s.startswith('u'): | ||
s = s[1:] | ||
# Decode base64url encoded hash | ||
try: | ||
import base64 | ||
# Ensure s is encoded as bytes for base64 decoding | ||
s_bytes = s.encode('ascii') if isinstance(s, str) else s | ||
padding = 4 - (len(s_bytes) % 4) | ||
if padding != 4: | ||
s_bytes += b'=' * padding | ||
return base64.urlsafe_b64decode(s_bytes) | ||
except Exception: | ||
return s.encode('utf-8') | ||
Nkovaturient marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@staticmethod | ||
def to_string(proto: Any, b: bytes) -> str: | ||
if not b: | ||
return "" | ||
import base64 | ||
b64_hash = base64.urlsafe_b64encode(b).decode().rstrip('=') | ||
return f"uEi{b64_hash}" | ||
|
||
|
||
# Register WebRTC protocols with multiaddr | ||
try: | ||
|
||
# Create codec instances | ||
webrtc_codec = WebRTCCodec() | ||
webrtc_direct_codec = WebRTCDirectCodec() | ||
certhash_codec = CerthashCodec() | ||
|
||
# Register codec modules for multiaddr | ||
sys.modules['multiaddr.codecs.webrtc'] = webrtc_codec # type: ignore | ||
Nkovaturient marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sys.modules['multiaddr.codecs.webrtc_direct'] = webrtc_direct_codec # type: ignore | ||
sys.modules['multiaddr.codecs.certhash'] = certhash_codec # type: ignore | ||
|
||
setattr(codecs, 'webrtc', webrtc_codec) | ||
setattr(codecs, 'webrtc_direct', webrtc_direct_codec) | ||
setattr(codecs, 'certhash', certhash_codec) | ||
|
||
# Create Protocol objects with string codec names | ||
webrtc_protocol = Protocol( | ||
code=CODEC_WEBRTC, | ||
name=PROTOCOL_WEBRTC, | ||
codec="webrtc" | ||
) | ||
|
||
webrtc_direct_protocol = Protocol( | ||
code=CODEC_WEBRTC_DIRECT, | ||
name=PROTOCOL_WEBRTC_DIRECT, | ||
codec="webrtc_direct" | ||
) | ||
|
||
certhash_protocol = Protocol( | ||
code=CODEC_CERTHASH, | ||
name=PROTOCOL_CERTHASH, | ||
codec="certhash" | ||
) | ||
|
||
# Register protocols using the add_protocol function | ||
protocols.add_protocol(webrtc_protocol) | ||
protocols.add_protocol(webrtc_direct_protocol) | ||
protocols.add_protocol(certhash_protocol) | ||
|
||
print("✅ WebRTC protocols registered with multiaddr") | ||
Nkovaturient marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
except ImportError as e: | ||
print(f"⚠️ Failed to register WebRTC protocols: {e}") | ||
Nkovaturient marked this conversation as resolved.
Show resolved
Hide resolved
|
||
except Exception as e: | ||
print(f"⚠️ Error registering WebRTC protocols: {e}") | ||
|
||
__all__ = [ | ||
"WebRTCTransport", | ||
"WebRTCDirectTransport", | ||
"DEFAULT_ICE_SERVERS", | ||
"SIGNALING_PROTOCOL", | ||
"MUXER_PROTOCOL", | ||
"WebRTCError", | ||
"SDPHandshakeError", | ||
"ConnectionStateError", | ||
"CertificateError", | ||
"STUNError", | ||
"CODEC_WEBRTC", | ||
"CODEC_WEBRTC_DIRECT", | ||
"CODEC_CERTHASH", | ||
] | ||
|
||
|
||
def webrtc(config: dict[str, Any] | None = None) -> WebRTCTransport: | ||
"""Create a WebRTC transport instance (private-to-private).""" | ||
return WebRTCTransport(config) | ||
|
||
|
||
def webrtc_direct(config: dict[str, Any] | None = None) -> WebRTCDirectTransport: | ||
"""Create a WebRTC-Direct transport instance (private-to-public).""" | ||
return WebRTCDirectTransport(config) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.