File: factory.py

package info (click to toggle)
python-txi2p-tahoe 0.3.7-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 448 kB
  • sloc: python: 3,757; makefile: 163; sh: 3
file content (243 lines) | stat: -rw-r--r-- 8,792 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# Copyright (c) str4d <str4d@mail.i2p>
# See COPYING for details.

from __future__ import print_function
from builtins import object
from twisted.internet.defer import Deferred
from twisted.internet.endpoints import TCP4ClientEndpoint, TCP4ServerEndpoint
from twisted.internet.protocol import ClientFactory, Factory

from txi2p.address import I2PAddress
from txi2p.bob.protocol import (I2PClientTunnelCreatorBOBClient,
                                I2PServerTunnelCreatorBOBClient,
                                I2PTunnelRemoverBOBClient,
                                I2PClientTunnelProtocol,
                                I2PServerTunnelProtocol,
                                I2PListeningPort)


class BOBI2PClientFactory(ClientFactory):
    protocol = I2PClientTunnelCreatorBOBClient
    bobProto = None
    canceled = False
    removeTunnelWhenFinished = True

    def _cancel(self, d):
        self.bobProto.sender.transport.abortConnection()
        self.canceled = True

    def __init__(self, reactor, clientFactory, bobEndpoint, dest,
                 tunnelNick=None,
                 inhost='localhost',
                 inport=None,
                 options={}):
        self._reactor = reactor
        self._clientFactory = clientFactory
        self._bobEndpoint = bobEndpoint
        self.dest = dest
        self.tunnelNick = tunnelNick
        self.inhost = inhost
        self.inport = inport
        self.options = options
        self.deferred = Deferred(self._cancel);

    def buildProtocol(self, addr):
        proto = self.protocol()
        proto.factory = self
        self.bobProto = proto
        return proto

    def bobConnectionFailed(self, reason):
        if not self.canceled:
            self.deferred.errback(reason)

    # This method is not called if an endpoint deferred errbacks
    def clientConnectionFailed(self, connector, reason):
        self.bobConnectionFailed(reason)

    def i2pTunnelCreated(self):
        # BOB is now listening for a tunnel.
        # BOB only listens on TCP4 (for now).
        clientEndpoint = TCP4ClientEndpoint(self._reactor, self.inhost, self.inport)
        # Wrap the client Factory.
        wrappedFactory = BOBClientFactoryWrapper(self._clientFactory,
                                                 self._bobEndpoint,
                                                 I2PAddress(self.localDest),
                                                 self.tunnelNick,
                                                 self.removeTunnelWhenFinished)
        wrappedFactory.setDest(self.dest)
        d = clientEndpoint.connect(wrappedFactory)
        def checkProto(proto):
            if proto is None:
                self.deferred.cancel()
            return proto
        d.addCallback(checkProto)
        # When the Deferred returns an IProtocol, pass it on.
        d.chainDeferred(self.deferred)


class BOBI2PServerFactory(Factory):
    protocol = I2PServerTunnelCreatorBOBClient
    bobProto = None
    canceled = False
    removeTunnelWhenFinished = True

    def _cancel(self, d):
        self.bobProto.sender.transport.abortConnection()
        self.canceled = True

    def __init__(self, reactor, serverFactory, bobEndpoint, keyfile,
                 tunnelNick=None,
                 outhost='localhost',
                 outport=None,
                 options={}):
        self._reactor = reactor
        self._serverFactory = serverFactory
        self._bobEndpoint = bobEndpoint
        self._keyfile = keyfile
        self._writeKeypair = False
        self.tunnelNick = tunnelNick
        self.outhost = outhost
        self.outport = outport
        self.options = options
        self.deferred = Deferred(self._cancel)

    def startFactory(self):
        try:
            f = open(self._keyfile, 'r')
            self.keypair = f.read()
            f.close()
        except IOError:
            self.keypair = None
            self._writeKeypair = True

    def buildProtocol(self, addr):
        proto = self.protocol()
        proto.factory = self
        self.bobProto = proto
        return proto

    def bobConnectionFailed(self, reason):
        if not self.canceled:
            self.deferred.errback(reason)

    # This method is not called if an endpoint deferred errbacks
    def clientConnectionFailed(self, connector, reason):
        self.bobConnectionFailed(reason)

    def i2pTunnelCreated(self):
        if self._writeKeypair:
            try:
                f = open(self._keyfile, 'w')
                f.write(self.keypair)
                f.close()
            except IOError:
                print('Could not save keypair')
        # BOB will now forward data to a listener.
        # BOB only forwards to TCP4 (for now).
        serverEndpoint = TCP4ServerEndpoint(self._reactor, self.outport)
        # Wrap the server Factory.
        wrappedFactory = BOBServerFactoryWrapper(self._serverFactory,
                                                 self._bobEndpoint,
                                                 I2PAddress(self.localDest),
                                                 self.tunnelNick,
                                                 self.removeTunnelWhenFinished)
        d = serverEndpoint.listen(wrappedFactory)
        def handlePort(port):
            if port is None:
                self.deferred.cancel()
            serverAddr = I2PAddress(self.localDest)
            p = I2PListeningPort(port, wrappedFactory, serverAddr)
            return p
        d.addCallback(handlePort)
        # When the Deferred returns an IListeningPort, pass it on.
        d.chainDeferred(self.deferred)


class BOBFactoryWrapperCommon(object):
    def __init__(self, wrappedFactory,
                       bobEndpoint,
                       localAddr,
                       tunnelNick,
                       removeTunnelWhenFinished):
        self.w = wrappedFactory
        self.bobEndpoint = bobEndpoint
        self.localAddr = localAddr
        self.tunnelNick = tunnelNick
        self.removeTunnelWhenFinished = removeTunnelWhenFinished

    def __getattr__(self, attr):
        return getattr(self.w, attr)


class BOBClientFactoryWrapper(BOBFactoryWrapperCommon):
    protocol = I2PClientTunnelProtocol

    def setDest(self, dest):
        self.dest = dest

    def buildProtocol(self, addr):
        wrappedProto = self.w.buildProtocol(addr)
        proto = self.protocol(wrappedProto, self.localAddr, self.dest)
        proto.factory = self
        return proto

    def i2pConnectionLost(self, wrappedProto, reason):
        if self.removeTunnelWhenFinished:
            # Notify the underlying Protocol once the tunnel has
            # been removed, in case they stop the reactor.
            rmTunnelFac = BOBI2PClientTunnelRemoverFactory(self.tunnelNick,
                                                           wrappedProto,
                                                           reason)
            self.bobEndpoint.connect(rmTunnelFac)
        else:
            # Notify the underlying Protocol now.
            wrappedProto.connectionLost(reason)


class BOBServerFactoryWrapper(BOBFactoryWrapperCommon):
    protocol = I2PServerTunnelProtocol

    def buildProtocol(self, addr):
        wrappedProto = self.w.buildProtocol(addr)
        proto = self.protocol(wrappedProto, self.localAddr)
        proto.factory = self
        return proto

    def stopListening(self, wrappedPort):
        if self.removeTunnelWhenFinished:
            # Notify the underlying ListeningPort once the tunnel
            # has been removed, in case they stop the reactor.
            rmTunnelFac = BOBI2PServerTunnelRemoverFactory(self.tunnelNick,
                                                           wrappedPort)
            self.bobEndpoint.connect(rmTunnelFac)
        else:
            # Notify the underlying ListeningPort now.
            wrappedPort.stopListening()


class BOBI2PClientTunnelRemoverFactory(ClientFactory):
    protocol = I2PTunnelRemoverBOBClient

    def __init__(self, tunnelNick, protoToNotify, reason):
        self.tunnelNick = tunnelNick
        self._protoToNotify = protoToNotify
        self._reason = reason

    def i2pTunnelRemoved(self):
        # Now that the I2P tunnel has been removed,
        # notify the underlying Protocol.
        self._protoToNotify.connectionLost(self._reason)


class BOBI2PServerTunnelRemoverFactory(ClientFactory):
    protocol = I2PTunnelRemoverBOBClient

    def __init__(self, tunnelNick, portToNotify):
        self.tunnelNick = tunnelNick
        self._portToNotify = portToNotify

    def i2pTunnelRemoved(self):
        # Now that the I2P tunnel has been removed,
        # notify the underlying ListeningPort.
        self._portToNotify.stopListening()