-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,43 @@ | ||
import re | ||
from twisted.internet.endpoints import clientFromString | ||
from twisted.internet.interfaces import IStreamClientEndpoint | ||
from txi2p.sam import SAMI2PStreamClientEndpoint | ||
from zope.interface import implementer | ||
|
||
from foolscap.ipb import IConnectionHintHandler, InvalidHintError | ||
|
||
HINT_RE=re.compile(r"^i2p:([A-Za-z.0-9\-]+)(:(\d+){1,5})?$") | ||
|
||
@implementer(IConnectionHintHandler) | ||
class _RunningI2P: | ||
def __init__(self, sam_endpoint): | ||
assert IStreamClientEndpoint.providedBy(sam_endpoint) | ||
self._sam_endpoint = sam_endpoint | ||
|
||
def hint_to_endpoint(self, hint, reactor): | ||
# Return (endpoint, hostname), where "hostname" is what we pass to the | ||
# HTTP "Host:" header so a dumb HTTP server can be used to redirect us. | ||
mo = HINT_RE.search(hint) | ||
if not mo: | ||
raise InvalidHintError("unrecognized I2P hint") | ||
host, port = mo.group(1), int(mo.group(2)) if mo.group(2) else None | ||
return SAMI2PStreamClientEndpoint.new(self._sam_endpoint, host, port), host | ||
|
||
def default_sam_port(reactor): | ||
"""Return a handler which connects to a pre-existing I2P process on the | ||
default SAM port. | ||
""" | ||
return _RunningI2P(clientFromString(reactor, 'tcp:127.0.0.1:7656')) | ||
|
||
def with_sam_port(sam_endpoint): | ||
"""Return a handler which connects to a pre-existing I2P process on the | ||
given SAM port. | ||
- sam_endpoint: a ClientEndpoint which points at the SAM API | ||
""" | ||
return _RunningI2P(sam_endpoint) | ||
|
||
def with_local_i2p(reactor, i2p_configdir=None): | ||
raise NotImplementedError | ||
|
||
def launch_local_i2p(reactor, i2p_binary=None, i2p_configdir=None): | ||
raise NotImplementedError |