Skip to content

LibP2PStreamMounting Functions #305

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
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ipfshttpclient/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from . import miscellaneous
from . import name
from . import object
from . import p2p
from . import pin
from . import pubsub
from . import repo
Expand Down Expand Up @@ -185,6 +186,7 @@ def close(self): # Call this when you're done
key = base.SectionProperty(key.Section)
name = base.SectionProperty(name.Section)
object = base.SectionProperty(object.Section)
p2p = base.SectionProperty(p2p.Section)
pin = base.SectionProperty(pin.Section)
pubsub = base.SectionProperty(pubsub.Section)
repo = base.SectionProperty(repo.Section)
Expand Down Expand Up @@ -330,4 +332,4 @@ def get_json(self, cid, **kwargs):
object
Deserialized IPFS JSON object value
"""
return self.cat(cid, decoder='json', **kwargs)
return self.cat(cid, decoder='json', **kwargs)
105 changes: 105 additions & 0 deletions ipfshttpclient/client/p2p.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import typing as ty

from . import base


class Section(base.SectionBase):
@base.returns_no_item
def forward(self, protocol: str, peer_id: str, port: str, **kwargs: base.CommonArgs):
"""Forward connections to libp2p service

Forward connections made to the specified port to another IPFS node.

.. code-block:: python

# forwards connections made to port 8888 to 'QmHash' as protocol '/x/testproto'
>>> client.p2p.forward('/x/testproto', 'QmHash', 8888)
[]

Parameters
----------
protocol
specifies the libp2p protocol name to use for libp2p connections and/or handlers. It must be prefixed with '/x/'.
PeerID
Target endpoint
port
Listening endpoint

Returns
-------
list
An empty list
"""
args = (protocol, peer_id, port)
return self._client.request('/p2p/forward', args, decoder='json', **kwargs)

@base.returns_no_item
def listen(self, protocol: str, port: str, **kwargs: base.CommonArgs):
"""Create libp2p service to forward IPFS connections to port

Creates a libp2p service that forwards IPFS connections to it
to the specified port on the local computer.


.. code-block:: python

# listens for connections of protocol '/x/testproto' and forwards them to port 8888
>>> client.p2p.listen('/x/testproto', 8888)
[]

Parameters
----------
protocol
specifies the libp2p handler name. It must be prefixed with '/x/'.
port
Listener port to which to forward incoming connections


Returns
-------
list
An empty list
"""
args = (protocol, port)
return self._client.request('/p2p/listen', args, decoder='json', **kwargs)

# @base.returns_single_item(base.ResponseBase)
def close(self, all: bool = False, protocol: str = None, listenaddress: str = None, targetaddress: str = None, **kwargs: base.CommonArgs):
"""Stop listening for new connections to forward.

Stops all forwarding and listening libp2p services that match the input arguments.

.. code-block:: python

# Close listening and forwarding connections of protocol '/x/testproto' and port 8888.
>>> client.p2p.close(protocol='/x/testproto', port='8888')
[]

Parameters
----------
protocol
specifies the libp2p handler name. It must be prefixed with '/x/'.
port
Listener port to which to forward incoming connections


Returns
-------
list
An empty list
"""

opts = {}
if all is not None:
opts.update({"all": all})
if protocol is not None:
opts.update({"protocol": str(protocol)})
if listenaddress is not None:
opts.update({"listen-address": str(listenaddress)})
if targetaddress is not None:
opts.update({"target-address": str(targetaddress)})

kwargs.setdefault("opts", {}).update(opts)
args = (all,) # if all is not None else ()
return self._client.request('/p2p/close', decoder='json', **kwargs)