Ejemplo n.º 1
0
    def run(self):
        ec = ec_generate_key(u"low")
        self._my_member = Member.get_instance(ec_to_public_bin(ec),
                                              ec_to_private_bin(ec),
                                              sync_with_database=True)

        self.caller(self.test_incoming_barter_record)
def create_bc3_community(dispersy):
    ec = ec_generate_key(u"very-low")
    private_key = ec_to_private_bin(ec)
    public_key = ec_to_public_bin(ec)

    my_member = dispersy.get_member(public_key, private_key)
    c = BarterCommunity.create_community(dispersy, my_member, DummySwiftProcess())
    logger.info("created community with cid " + c.cid.encode('hex'))
    
    with open(os.path.join(os.environ["PROJECTROOT"], "tmp", "bc3_master"), "w") as f:
        print >>f, c._master_member.public_key.encode("hex"), c._master_member.mid.encode("hex")
Ejemplo n.º 3
0
 def load_community(cls, master):
     dispersy = Dispersy.get_instance()
     try:
         # test if this community already exists
         classification, = next(dispersy.database.execute(u"SELECT classification FROM community WHERE master = ?", (master.database_id,)))
     except StopIteration:
         # join the community with a new my_member, using a cheap cryptography key
         ec = ec_generate_key(u"NID_secp160r1")
         return cls.join_community(master, Member(ec_to_public_bin(ec), ec_to_private_bin(ec)))
     else:
         if classification == cls.get_classification():
             return super(EffortCommunity, cls).load_community(master)
         else:
             raise RuntimeError("Unable to load an EffortCommunity that has been killed")
Ejemplo n.º 4
0
 def load_community(cls, master, swift_process):
     dispersy = Dispersy.get_instance()
     try:
         # test if this community already exists
         classification, = next(dispersy.database.execute(u"SELECT classification FROM community WHERE master = ?", (master.database_id,)))
     except StopIteration:
         # join the community with a new my_member, using a cheap cryptography key
         ec = ec_generate_key(u"NID_secp160r1")
         return cls.join_community(master, Member(ec_to_public_bin(ec), ec_to_private_bin(ec)), swift_process)
     else:
         if classification == cls.get_classification():
             return super(EffortCommunity, cls).load_community(master, swift_process)
         else:
             raise RuntimeError("Unable to load an EffortCommunity that has been killed")
Ejemplo n.º 5
0
def setup_peer_dir(peers_directory, peer_count):
    for i in range(peer_count):
        td = peers_directory+"/"+"%05d" %(i+1)
        if not isdir(td): makedirs(td)
    with open(peers_directory + "/peer.count", "w") as f:
        f.write("%d\n" %(peer_count))
    with open(peers_directory + "/peer-keys", 'w') as f:
        for i in range(peer_count):
            rsa = ec_generate_key(u"low")
            f.write("%(id)s %(ip)s %(port)d %(public_key)s %(private_key)s\n" % \
                          {'id': i+1, #'peer-%05d' % i,
                           'ip': '0.0.0.0',
                           'port': 12000 + i,
                           'public_key': ec_to_public_bin(rsa).encode("HEX"),
                           'private_key': ec_to_private_bin(rsa).encode("HEX")
                           }
                          )
    for node in get_nodes(peers_directory):
        open(node+"/data/bartercast.log", "w").close()
        open(node+"/data/availability.log", "w").close()
Ejemplo n.º 6
0
def setup_peer_dir(peers_directory, peer_count):
    for i in range(peer_count):
        td = peers_directory + "/" + "%05d" % (i + 1)
        if not isdir(td): makedirs(td)
    with open(peers_directory + "/peer.count", "w") as f:
        f.write("%d\n" % (peer_count))
    with open(peers_directory + "/peer-keys", 'w') as f:
        for i in range(peer_count):
            rsa = ec_generate_key(u"low")
            f.write("%(id)s %(ip)s %(port)d %(public_key)s %(private_key)s\n" % \
                          {'id': i+1, #'peer-%05d' % i,
                           'ip': '0.0.0.0',
                           'port': 12000 + i,
                           'public_key': ec_to_public_bin(rsa).encode("HEX"),
                           'private_key': ec_to_private_bin(rsa).encode("HEX")
                           }
                          )
    for node in get_nodes(peers_directory):
        open(node + "/data/bartercast.log", "w").close()
        open(node + "/data/availability.log", "w").close()
Ejemplo n.º 7
0
    def register(self, session, sesslock):
        if not self.registered:
            self.registered = True

            self.session = session
            self.sesslock = sesslock

            self.downloads = {}
            config = session.sessconfig  # Should be safe at startup

            self.upnp_ports = []

            # Orig
            self.sessdoneflag = Event()

            self.rawserver = RawServer(self.sessdoneflag,
                                       config['timeout_check_interval'],
                                       config['timeout'],
                                       ipv6_enable=config['ipv6_enabled'],
                                       failfunc=self.rawserver_fatalerrorfunc,
                                       errorfunc=self.rawserver_nonfatalerrorfunc)
            self.rawserver.add_task(self.rawserver_keepalive, 1)
            self.listen_port = config['minport']
            self.shutdownstarttime = None

            self.multihandler = MultiHandler(self.rawserver, self.sessdoneflag)

            # SWIFTPROC
            swift_exists = config['swiftproc'] and (os.path.exists(config['swiftpath']) or os.path.exists(config['swiftpath'] + '.exe'))
            if swift_exists:
                from Tribler.Core.Swift.SwiftProcessMgr import SwiftProcessMgr

                self.spm = SwiftProcessMgr(config['swiftpath'], config['swiftcmdlistenport'], config['swiftdlsperproc'], self.session.get_swift_tunnel_listen_port(), self.sesslock)
                try:
                    self.swift_process = self.spm.get_or_create_sp(self.session.get_swift_working_dir(), self.session.get_torrent_collecting_dir(), self.session.get_swift_tunnel_listen_port(), self.session.get_swift_tunnel_httpgw_listen_port(), self.session.get_swift_tunnel_cmdgw_listen_port())
                    self.upnp_ports.append((self.session.get_swift_tunnel_listen_port(), 'UDP'))

                except OSError:
                    # could not find/run swift
                    print >> sys.stderr, "lmc: could not start a swift process"

            else:
                self.spm = None
                self.swift_process = None

            # Dispersy
            self.session.dispersy_member = None
            if config['dispersy']:
                from Tribler.dispersy.callback import Callback
                from Tribler.dispersy.dispersy import Dispersy
                from Tribler.dispersy.endpoint import RawserverEndpoint, TunnelEndpoint
                from Tribler.dispersy.community import HardKilledCommunity

                # set communication endpoint
                if config['dispersy-tunnel-over-swift'] and self.swift_process:
                    endpoint = TunnelEndpoint(self.swift_process)
                else:
                    endpoint = RawserverEndpoint(self.rawserver, config['dispersy_port'])

                callback = Callback("Dispersy")  # WARNING NAME SIGNIFICANT
                working_directory = unicode(config['state_dir'])

                self.dispersy = Dispersy(callback, endpoint, working_directory)

                # TODO: see if we can postpone dispersy.start to improve GUI responsiveness.
                # However, for now we must start self.dispersy.callback before running
                # try_register(nocachedb, self.database_thread)!

                self.dispersy.start()

                print >> sys.stderr, "lmc: Dispersy is listening on port", self.dispersy.wan_address[1], "using", endpoint
                self.upnp_ports.append((self.dispersy.wan_address[1], 'UDP'))

                self.dispersy.callback.call(self.dispersy.define_auto_load, args=(HardKilledCommunity,), kargs={'load': True})

                # notify dispersy finished loading
                self.session.uch.notify(NTFY_DISPERSY, NTFY_STARTED, None)

                from Tribler.Core.permid import read_keypair
                from Tribler.dispersy.crypto import ec_to_public_bin, ec_to_private_bin
                keypair = read_keypair(self.session.get_permid_keypair_filename())
                self.session.dispersy_member = callback.call(self.dispersy.get_member, (ec_to_public_bin(keypair), ec_to_private_bin(keypair)))

                self.database_thread = callback
            else:
                class FakeCallback():
                    def __init__(self):
                        from Tribler.Utilities.TimedTaskQueue import TimedTaskQueue
                        self.queue = TimedTaskQueue("FakeCallback")

                    def register(self, call, args=(), kargs=None, delay=0.0, priority=0, id_=u"", callback=None, callback_args=(), callback_kargs=None, include_id=False):
                        def do_task():
                            if kargs:
                                call(*args, **kargs)
                            else:
                                call(*args)

                            if callback:
                                if callback_kargs:
                                    callback(*callback_args, **callback_kargs)
                                else:
                                    callback(*callback_args)
                        self.queue.add_task(do_task, t=delay)

                    def shutdown(self, immediately=False):
                        self.queue.shutdown(immediately)

                self.database_thread = FakeCallback()

            if config['megacache']:
                import Tribler.Core.CacheDB.cachedb as cachedb
                from Tribler.Core.CacheDB.SqliteCacheDBHandler import PeerDBHandler, TorrentDBHandler, MyPreferenceDBHandler, VoteCastDBHandler, ChannelCastDBHandler, NetworkBuzzDBHandler, UserEventLogDBHandler
                from Tribler.Category.Category import Category
                from Tribler.Core.Tag.Extraction import TermExtraction
                from Tribler.Core.CacheDB.sqlitecachedb import try_register

                if DEBUG:
                    print >> sys.stderr, 'tlm: Reading Session state from', config['state_dir']

                nocachedb = cachedb.init(config, self.rawserver_fatalerrorfunc)
                try_register(nocachedb, self.database_thread)

                self.cat = Category.getInstance(config['install_dir'])
                self.term = TermExtraction.getInstance(config['install_dir'])

                self.peer_db = PeerDBHandler.getInstance()
                self.peer_db.registerConnectionUpdater(self.session)

                self.torrent_db = TorrentDBHandler.getInstance()
                self.torrent_db.register(os.path.abspath(config['torrent_collecting_dir']))
                self.mypref_db = MyPreferenceDBHandler.getInstance()
                self.votecast_db = VoteCastDBHandler.getInstance()
                self.votecast_db.registerSession(self.session)
                self.channelcast_db = ChannelCastDBHandler.getInstance()
                self.channelcast_db.registerSession(self.session)
                self.nb_db = NetworkBuzzDBHandler.getInstance()
                self.ue_db = UserEventLogDBHandler.getInstance()

                if self.dispersy:
                    self.dispersy.database.attach_commit_callback(self.channelcast_db._db.commitNow)
            else:
                config['torrent_checking'] = 0

            self.rtorrent_handler = None
            if config['torrent_collecting']:
                from Tribler.Core.RemoteTorrentHandler import RemoteTorrentHandler
                self.rtorrent_handler = RemoteTorrentHandler()
Ejemplo n.º 8
0
    def run(self):
        ec = ec_generate_key(u"low")                    
        self._my_member = Member.get_instance(ec_to_public_bin(ec), ec_to_private_bin(ec), sync_with_database=True)

        self.caller(self.test_incoming_channel_propagate)
        self.caller(self.test_outgoing_channel_propagate)
Ejemplo n.º 9
0
    def register(self, session, sesslock):
        if not self.registered:
            self.registered = True

            self.session = session
            self.sesslock = sesslock

            self.downloads = {}
            config = session.sessconfig  # Should be safe at startup

            self.upnp_ports = []

            # Orig
            self.sessdoneflag = Event()

            self.rawserver = RawServer(
                self.sessdoneflag,
                config['timeout_check_interval'],
                config['timeout'],
                ipv6_enable=config['ipv6_enabled'],
                failfunc=self.rawserver_fatalerrorfunc,
                errorfunc=self.rawserver_nonfatalerrorfunc)
            self.rawserver.add_task(self.rawserver_keepalive, 1)
            self.listen_port = config['minport']
            self.shutdownstarttime = None

            self.multihandler = MultiHandler(self.rawserver, self.sessdoneflag)

            # SWIFTPROC
            swift_exists = config['swiftproc'] and (
                os.path.exists(config['swiftpath'])
                or os.path.exists(config['swiftpath'] + '.exe'))
            if swift_exists:
                from Tribler.Core.Swift.SwiftProcessMgr import SwiftProcessMgr

                self.spm = SwiftProcessMgr(
                    config['swiftpath'], config['swiftcmdlistenport'],
                    config['swiftdlsperproc'],
                    self.session.get_swift_tunnel_listen_port(), self.sesslock)
                try:
                    self.swift_process = self.spm.get_or_create_sp(
                        self.session.get_swift_working_dir(),
                        self.session.get_torrent_collecting_dir(),
                        self.session.get_swift_tunnel_listen_port(),
                        self.session.get_swift_tunnel_httpgw_listen_port(),
                        self.session.get_swift_tunnel_cmdgw_listen_port())
                    self.upnp_ports.append(
                        (self.session.get_swift_tunnel_listen_port(), 'UDP'))

                except OSError:
                    # could not find/run swift
                    print >> sys.stderr, "lmc: could not start a swift process"

            else:
                self.spm = None
                self.swift_process = None

            # Dispersy
            self.session.dispersy_member = None
            if config['dispersy']:
                from Tribler.dispersy.callback import Callback
                from Tribler.dispersy.dispersy import Dispersy
                from Tribler.dispersy.endpoint import RawserverEndpoint, TunnelEndpoint
                from Tribler.dispersy.community import HardKilledCommunity

                # set communication endpoint
                if config['dispersy-tunnel-over-swift'] and self.swift_process:
                    endpoint = TunnelEndpoint(self.swift_process)
                else:
                    endpoint = RawserverEndpoint(self.rawserver,
                                                 config['dispersy_port'])

                callback = Callback("Dispersy")  # WARNING NAME SIGNIFICANT
                working_directory = unicode(config['state_dir'])

                self.dispersy = Dispersy(callback, endpoint, working_directory)

                # TODO: see if we can postpone dispersy.start to improve GUI responsiveness.
                # However, for now we must start self.dispersy.callback before running
                # try_register(nocachedb, self.database_thread)!

                self.dispersy.start()

                print >> sys.stderr, "lmc: Dispersy is listening on port", self.dispersy.wan_address[
                    1], "using", endpoint
                self.upnp_ports.append((self.dispersy.wan_address[1], 'UDP'))

                self.dispersy.callback.call(self.dispersy.define_auto_load,
                                            args=(HardKilledCommunity, ),
                                            kargs={'load': True})

                # notify dispersy finished loading
                self.session.uch.notify(NTFY_DISPERSY, NTFY_STARTED, None)

                from Tribler.Core.permid import read_keypair
                from Tribler.dispersy.crypto import ec_to_public_bin, ec_to_private_bin
                keypair = read_keypair(
                    self.session.get_permid_keypair_filename())
                self.session.dispersy_member = callback.call(
                    self.dispersy.get_member,
                    (ec_to_public_bin(keypair), ec_to_private_bin(keypair)))

                self.database_thread = callback
            else:

                class FakeCallback():
                    def __init__(self):
                        from Tribler.Utilities.TimedTaskQueue import TimedTaskQueue
                        self.queue = TimedTaskQueue("FakeCallback")

                    def register(self,
                                 call,
                                 args=(),
                                 kargs=None,
                                 delay=0.0,
                                 priority=0,
                                 id_=u"",
                                 callback=None,
                                 callback_args=(),
                                 callback_kargs=None,
                                 include_id=False):
                        def do_task():
                            if kargs:
                                call(*args, **kargs)
                            else:
                                call(*args)

                            if callback:
                                if callback_kargs:
                                    callback(*callback_args, **callback_kargs)
                                else:
                                    callback(*callback_args)

                        self.queue.add_task(do_task, t=delay)

                    def shutdown(self, immediately=False):
                        self.queue.shutdown(immediately)

                self.database_thread = FakeCallback()

            if config['megacache']:
                import Tribler.Core.CacheDB.cachedb as cachedb
                from Tribler.Core.CacheDB.SqliteCacheDBHandler import PeerDBHandler, TorrentDBHandler, MyPreferenceDBHandler, VoteCastDBHandler, ChannelCastDBHandler, NetworkBuzzDBHandler, UserEventLogDBHandler
                from Tribler.Category.Category import Category
                from Tribler.Core.Tag.Extraction import TermExtraction
                from Tribler.Core.CacheDB.sqlitecachedb import try_register

                if DEBUG:
                    print >> sys.stderr, 'tlm: Reading Session state from', config[
                        'state_dir']

                nocachedb = cachedb.init(config, self.rawserver_fatalerrorfunc)
                try_register(nocachedb, self.database_thread)

                self.cat = Category.getInstance(config['install_dir'])
                self.term = TermExtraction.getInstance(config['install_dir'])

                self.peer_db = PeerDBHandler.getInstance()
                self.peer_db.registerConnectionUpdater(self.session)

                self.torrent_db = TorrentDBHandler.getInstance()
                self.torrent_db.register(
                    os.path.abspath(config['torrent_collecting_dir']))
                self.mypref_db = MyPreferenceDBHandler.getInstance()
                self.votecast_db = VoteCastDBHandler.getInstance()
                self.votecast_db.registerSession(self.session)
                self.channelcast_db = ChannelCastDBHandler.getInstance()
                self.channelcast_db.registerSession(self.session)
                self.nb_db = NetworkBuzzDBHandler.getInstance()
                self.ue_db = UserEventLogDBHandler.getInstance()

                if self.dispersy:
                    self.dispersy.database.attach_commit_callback(
                        self.channelcast_db._db.commitNow)
            else:
                config['torrent_checking'] = 0

            self.rtorrent_handler = None
            if config['torrent_collecting']:
                from Tribler.Core.RemoteTorrentHandler import RemoteTorrentHandler
                self.rtorrent_handler = RemoteTorrentHandler()
Ejemplo n.º 10
0
    def run(self):
        ec = ec_generate_key(u"low")
        self._my_member = Member.get_instance(ec_to_public_bin(ec), ec_to_private_bin(ec), sync_with_database=True)

        self.caller(self.test_incoming_barter_record)
Ejemplo n.º 11
0
if __name__ == "__main__":
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option(
        "--total-peers",
        help=
        "Total number of peers to generate ip:port and key combinations. Default 50",
        type=int,
        default=50)
    parser.add_option(
        "--port-start",
        help="Port number to start assigning from. Default 12000",
        default=12000)
    parser.add_option("-o",
                      help="Output file. Default \"../peers/peer-keys\"",
                      default="../peers/peer-keys")
    options, _ = parser.parse_args()

    with open(options.o, 'wb') as nodefp:
        for i in xrange(0, options.total_peers):
            rsa = ec_generate_key(u"low")
            nodefp.write("%(id)s %(ip)s %(port)d %(public_key)s %(private_key)s\n" % \
                          {'id': i+1, #'peer-%05d' % i,
                           'ip': '0.0.0.0',
                           'port': options.port_start + i,
                           'public_key': ec_to_public_bin(rsa).encode("HEX"),
                           'private_key': ec_to_private_bin(rsa).encode("HEX")
                           }
                          )
Ejemplo n.º 12
0
# The list is in the following format:
#
# <peername> <ip> <port> <public key> <private key>
#
# The public and private keys come from an eleptic curve
# key, generated during runtime for each peer
#
# Use ./ec-generator.py --help for help on the parameters
#
from Tribler.dispersy.crypto import ec_generate_key, ec_to_public_bin, ec_to_private_bin

if __name__ == "__main__":
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("--total-peers", help="Total number of peers to generate ip:port and key combinations. Default 50", type=int, default=50)
    parser.add_option("--port-start", help="Port number to start assigning from. Default 12000", default=12000)
    parser.add_option("-o", help="Output file. Default \"../peers/peer-keys\"", default="../peers/peer-keys")
    options, _ = parser.parse_args()

    with open(options.o, 'wb') as nodefp:
        for i in xrange(0, options.total_peers):
            rsa = ec_generate_key(u"low")
            nodefp.write("%(id)s %(ip)s %(port)d %(public_key)s %(private_key)s\n" % \
                          {'id': i+1, #'peer-%05d' % i,
                           'ip': '0.0.0.0',
                           'port': options.port_start + i,
                           'public_key': ec_to_public_bin(rsa).encode("HEX"),
                           'private_key': ec_to_private_bin(rsa).encode("HEX")
                           }
                          )