def generate_cs_keypair(keypair_filename=None, pubkey_filename=None):
    keypair = permid.generate_keypair()
    if keypair_filename:
        permid.save_keypair(keypair, keypair_filename)
    pubkey = encodestring(str(keypair.pub().get_der())).replace('\n', '')
    if pubkey_filename:
        permid.save_pub_key(keypair, pubkey_filename)
    return (keypair, pubkey)
def generate_cs_keypair(keypair_filename = None, pubkey_filename = None):
    keypair = permid.generate_keypair()
    if keypair_filename:
        permid.save_keypair(keypair, keypair_filename)
    pubkey = encodestring(str(keypair.pub().get_der())).replace('\n', '')
    if pubkey_filename:
        permid.save_pub_key(keypair, pubkey_filename)
    return (keypair, pubkey)
    def __init__(self,
                 scfg=None,
                 ignore_singleton=False,
                 on_error=lambda e: None,
                 on_stop=lambda: None,
                 app_http_handler=None,
                 network_thread_daemon=True):
        if not ignore_singleton:
            if Session.__single:
                raise RuntimeError, 'Session is singleton'
            Session.__single = self
        self.sesslock = RLock()
        self.on_error = on_error
        self.on_stop = on_stop
        self.app_http_handler = app_http_handler
        first_run = False
        if scfg is None:
            try:
                state_dir = Session.get_default_state_dir()
                cfgfilename = Session.get_default_config_filename(state_dir)
                scfg = SessionStartupConfig.load(cfgfilename)
            except:
                log_exc()
                scfg = SessionStartupConfig()

            self.sessconfig = scfg.sessconfig
        else:
            self.sessconfig = copy.copy(scfg.sessconfig)
        state_dir = self.sessconfig['state_dir']
        if state_dir is None:
            state_dir = Session.get_default_state_dir()
            self.sessconfig['state_dir'] = state_dir
        if not os.path.isdir(state_dir):
            first_run = True
            os.makedirs(state_dir)
        collected_torrent_dir = self.sessconfig['torrent_collecting_dir']
        if not collected_torrent_dir:
            collected_torrent_dir = os.path.join(self.sessconfig['state_dir'],
                                                 STATEDIR_TORRENTCOLL_DIR)
            self.sessconfig['torrent_collecting_dir'] = collected_torrent_dir
        collected_subtitles_dir = self.sessconfig.get(
            'subtitles_collecting_dir', None)
        if not collected_subtitles_dir:
            collected_subtitles_dir = os.path.join(
                self.sessconfig['state_dir'], STATEDIR_SUBSCOLL_DIR)
            self.sessconfig[
                'subtitles_collecting_dir'] = collected_subtitles_dir
        if not os.path.exists(collected_torrent_dir):
            first_run = True
            os.makedirs(collected_torrent_dir)
        buffer_dir = self.sessconfig.get('buffer_dir', None)
        if not buffer_dir:
            buffer_dir = os.path.join(self.sessconfig['state_dir'],
                                      STATEDIR_BUFFER_DIR)
            self.sessconfig['buffer_dir'] = buffer_dir
        if not os.path.exists(buffer_dir):
            first_run = True
            os.makedirs(buffer_dir)
        ads_dir = self.sessconfig.get('ads_dir', None)
        if not ads_dir:
            ads_dir = os.path.join(self.sessconfig['state_dir'],
                                   STATEDIR_ADS_DIR)
            self.sessconfig['ads_dir'] = ads_dir
        if not os.path.exists(ads_dir):
            first_run = True
            os.makedirs(ads_dir)
        if 'ts_login' in self.sessconfig:
            if first_run and len(self.sessconfig['ts_login']) == 0:
                self.sessconfig['ts_login'] = '******'
        else:
            self.sessconfig['ts_login'] = sessdefaults['ts_login']
        if 'ts_password' in self.sessconfig:
            if first_run and len(self.sessconfig['ts_password']) == 0:
                self.sessconfig['ts_password'] = '******'
        else:
            self.sessconfig['ts_password'] = sessdefaults['ts_password']
        if 'ts_user_key' not in self.sessconfig:
            self.sessconfig['ts_user_key'] = sessdefaults['ts_user_key']
        if 'max_socket_connects' not in self.sessconfig:
            self.sessconfig['max_socket_connects'] = sessdefaults[
                'max_socket_connects']
        if not self.sessconfig['peer_icon_path']:
            self.sessconfig['peer_icon_path'] = os.path.join(
                self.sessconfig['state_dir'], STATEDIR_PEERICON_DIR)
        if GOTM2CRYPTO:
            permidmod.init()
            pairfilename = os.path.join(self.sessconfig['state_dir'], 'ec.pem')
            if self.sessconfig['eckeypairfilename'] is None:
                self.sessconfig['eckeypairfilename'] = pairfilename
            if os.access(self.sessconfig['eckeypairfilename'], os.F_OK):
                self.keypair = permidmod.read_keypair(
                    self.sessconfig['eckeypairfilename'])
            else:
                self.keypair = permidmod.generate_keypair()
                pubfilename = os.path.join(self.sessconfig['state_dir'],
                                           'ecpub.pem')
                permidmod.save_keypair(self.keypair, pairfilename)
                permidmod.save_pub_key(self.keypair, pubfilename)
        else:
            self.keypair = None
        dlpstatedir = os.path.join(self.sessconfig['state_dir'],
                                   STATEDIR_DLPSTATE_DIR)
        if not os.path.isdir(dlpstatedir):
            os.mkdir(dlpstatedir)
        dl_direct_pstatedir = os.path.join(self.sessconfig['state_dir'],
                                           STATEDIR_DLDIRECT_PSTATE_DIR)
        if not os.path.isdir(dl_direct_pstatedir):
            os.mkdir(dl_direct_pstatedir)
        trackerdir = self.get_internal_tracker_dir()
        if not os.path.isdir(trackerdir):
            os.mkdir(trackerdir)
        if self.sessconfig['tracker_dfile'] is None:
            self.sessconfig['tracker_dfile'] = os.path.join(
                trackerdir, 'tracker.db')
        if self.sessconfig['tracker_allowed_dir'] is None:
            self.sessconfig['tracker_allowed_dir'] = trackerdir
        if self.sessconfig['tracker_logfile'] is None:
            if sys.platform == 'win32':
                sink = 'nul'
            else:
                sink = '/dev/null'
            self.sessconfig['tracker_logfile'] = sink
        if self.sessconfig['superpeer_file'] is None:
            self.sessconfig['superpeer_file'] = os.path.join(
                self.sessconfig['install_dir'], LIBRARYNAME, 'Core',
                'superpeer.txt')
        if 'crawler_file' not in self.sessconfig or self.sessconfig[
                'crawler_file'] is None:
            self.sessconfig['crawler_file'] = os.path.join(
                self.sessconfig['install_dir'], LIBRARYNAME, 'Core',
                'Statistics', 'crawler.txt')
        if self.sessconfig['overlay'] and self.sessconfig['download_help']:
            if self.sessconfig['download_help_dir'] is None:
                self.sessconfig['download_help_dir'] = os.path.join(
                    get_default_dest_dir(), DESTDIR_COOPDOWNLOAD)
            if not os.path.isdir(self.sessconfig['download_help_dir']):
                os.makedirs(self.sessconfig['download_help_dir'])
        if self.sessconfig['peer_icon_path'] is None:
            self.sessconfig['peer_icon_path'] = os.path.join(
                self.sessconfig['state_dir'], STATEDIR_PEERICON_DIR)
            if not os.path.isdir(self.sessconfig['peer_icon_path']):
                os.mkdir(self.sessconfig['peer_icon_path'])
        for key, defvalue in sessdefaults.iteritems():
            if key not in self.sessconfig:
                self.sessconfig[key] = defvalue

        if 'live_aux_seeders' not in self.sessconfig:
            self.sessconfig['live_aux_seeders'] = sessdefaults[
                'live_aux_seeders']
        if 'nat_detect' not in self.sessconfig:
            self.sessconfig['nat_detect'] = sessdefaults['nat_detect']
        if 'puncturing_internal_port' not in self.sessconfig:
            self.sessconfig['puncturing_internal_port'] = sessdefaults[
                'puncturing_internal_port']
        if 'stun_servers' not in self.sessconfig:
            self.sessconfig['stun_servers'] = sessdefaults['stun_servers']
        if 'pingback_servers' not in self.sessconfig:
            self.sessconfig['pingback_servers'] = sessdefaults[
                'pingback_servers']
        if 'mainline_dht' not in self.sessconfig:
            self.sessconfig['mainline_dht'] = sessdefaults['mainline_dht']
        self.http_seeds = {}
        self.save_pstate_sessconfig()
        self.uch = UserCallbackHandler(self)
        self.lm = ACEStreamLaunchMany(network_thread_daemon)
        self.lm.register(self, self.sesslock)
        self.lm.start()
Exemple #4
0
    def __init__(
        self,
        scfg=None,
        ignore_singleton=False,
        on_error=lambda e: None,
        on_stop=lambda: None,
        app_http_handler=None,
        network_thread_daemon=True,
    ):
        if not ignore_singleton:
            if Session.__single:
                raise RuntimeError, "Session is singleton"
            Session.__single = self
        self.sesslock = RLock()
        self.on_error = on_error
        self.on_stop = on_stop
        self.app_http_handler = app_http_handler
        first_run = False
        if scfg is None:
            try:
                state_dir = Session.get_default_state_dir()
                cfgfilename = Session.get_default_config_filename(state_dir)
                scfg = SessionStartupConfig.load(cfgfilename)
            except:
                log_exc()
                scfg = SessionStartupConfig()

            self.sessconfig = scfg.sessconfig
        else:
            self.sessconfig = copy.copy(scfg.sessconfig)
        state_dir = self.sessconfig["state_dir"]
        if state_dir is None:
            state_dir = Session.get_default_state_dir()
            self.sessconfig["state_dir"] = state_dir
        if not os.path.isdir(state_dir):
            first_run = True
            os.makedirs(state_dir)
        collected_torrent_dir = self.sessconfig["torrent_collecting_dir"]
        if not collected_torrent_dir:
            collected_torrent_dir = os.path.join(self.sessconfig["state_dir"], STATEDIR_TORRENTCOLL_DIR)
            self.sessconfig["torrent_collecting_dir"] = collected_torrent_dir
        collected_subtitles_dir = self.sessconfig.get("subtitles_collecting_dir", None)
        if not collected_subtitles_dir:
            collected_subtitles_dir = os.path.join(self.sessconfig["state_dir"], STATEDIR_SUBSCOLL_DIR)
            self.sessconfig["subtitles_collecting_dir"] = collected_subtitles_dir
        if not os.path.exists(collected_torrent_dir):
            first_run = True
            os.makedirs(collected_torrent_dir)
        buffer_dir = self.sessconfig.get("buffer_dir", None)
        if not buffer_dir:
            buffer_dir = os.path.join(self.sessconfig["state_dir"], STATEDIR_BUFFER_DIR)
            self.sessconfig["buffer_dir"] = buffer_dir
        if not os.path.exists(buffer_dir):
            first_run = True
            os.makedirs(buffer_dir)
        ads_dir = self.sessconfig.get("ads_dir", None)
        if not ads_dir:
            ads_dir = os.path.join(self.sessconfig["state_dir"], STATEDIR_ADS_DIR)
            self.sessconfig["ads_dir"] = ads_dir
        if not os.path.exists(ads_dir):
            first_run = True
            os.makedirs(ads_dir)
        if "ts_login" in self.sessconfig:
            if first_run and len(self.sessconfig["ts_login"]) == 0:
                self.sessconfig["ts_login"] = "******"
        else:
            self.sessconfig["ts_login"] = sessdefaults["ts_login"]
        if "ts_password" in self.sessconfig:
            if first_run and len(self.sessconfig["ts_password"]) == 0:
                self.sessconfig["ts_password"] = "******"
        else:
            self.sessconfig["ts_password"] = sessdefaults["ts_password"]
        if "ts_user_key" not in self.sessconfig:
            self.sessconfig["ts_user_key"] = sessdefaults["ts_user_key"]
        if "max_socket_connects" not in self.sessconfig:
            self.sessconfig["max_socket_connects"] = sessdefaults["max_socket_connects"]
        if not self.sessconfig["peer_icon_path"]:
            self.sessconfig["peer_icon_path"] = os.path.join(self.sessconfig["state_dir"], STATEDIR_PEERICON_DIR)
        if GOTM2CRYPTO:
            permidmod.init()
            pairfilename = os.path.join(self.sessconfig["state_dir"], "ec.pem")
            if self.sessconfig["eckeypairfilename"] is None:
                self.sessconfig["eckeypairfilename"] = pairfilename
            if os.access(self.sessconfig["eckeypairfilename"], os.F_OK):
                self.keypair = permidmod.read_keypair(self.sessconfig["eckeypairfilename"])
            else:
                self.keypair = permidmod.generate_keypair()
                pubfilename = os.path.join(self.sessconfig["state_dir"], "ecpub.pem")
                permidmod.save_keypair(self.keypair, pairfilename)
                permidmod.save_pub_key(self.keypair, pubfilename)
        else:
            self.keypair = None
        dlpstatedir = os.path.join(self.sessconfig["state_dir"], STATEDIR_DLPSTATE_DIR)
        if not os.path.isdir(dlpstatedir):
            os.mkdir(dlpstatedir)
        dl_direct_pstatedir = os.path.join(self.sessconfig["state_dir"], STATEDIR_DLDIRECT_PSTATE_DIR)
        if not os.path.isdir(dl_direct_pstatedir):
            os.mkdir(dl_direct_pstatedir)
        trackerdir = self.get_internal_tracker_dir()
        if not os.path.isdir(trackerdir):
            os.mkdir(trackerdir)
        if self.sessconfig["tracker_dfile"] is None:
            self.sessconfig["tracker_dfile"] = os.path.join(trackerdir, "tracker.db")
        if self.sessconfig["tracker_allowed_dir"] is None:
            self.sessconfig["tracker_allowed_dir"] = trackerdir
        if self.sessconfig["tracker_logfile"] is None:
            if sys.platform == "win32":
                sink = "nul"
            else:
                sink = "/dev/null"
            self.sessconfig["tracker_logfile"] = sink
        if self.sessconfig["superpeer_file"] is None:
            self.sessconfig["superpeer_file"] = os.path.join(
                self.sessconfig["install_dir"], LIBRARYNAME, "Core", "superpeer.txt"
            )
        if "crawler_file" not in self.sessconfig or self.sessconfig["crawler_file"] is None:
            self.sessconfig["crawler_file"] = os.path.join(
                self.sessconfig["install_dir"], LIBRARYNAME, "Core", "Statistics", "crawler.txt"
            )
        if self.sessconfig["overlay"] and self.sessconfig["download_help"]:
            if self.sessconfig["download_help_dir"] is None:
                self.sessconfig["download_help_dir"] = os.path.join(get_default_dest_dir(), DESTDIR_COOPDOWNLOAD)
            if not os.path.isdir(self.sessconfig["download_help_dir"]):
                os.makedirs(self.sessconfig["download_help_dir"])
        if self.sessconfig["peer_icon_path"] is None:
            self.sessconfig["peer_icon_path"] = os.path.join(self.sessconfig["state_dir"], STATEDIR_PEERICON_DIR)
            if not os.path.isdir(self.sessconfig["peer_icon_path"]):
                os.mkdir(self.sessconfig["peer_icon_path"])
        for key, defvalue in sessdefaults.iteritems():
            if key not in self.sessconfig:
                self.sessconfig[key] = defvalue

        if "live_aux_seeders" not in self.sessconfig:
            self.sessconfig["live_aux_seeders"] = sessdefaults["live_aux_seeders"]
        if "nat_detect" not in self.sessconfig:
            self.sessconfig["nat_detect"] = sessdefaults["nat_detect"]
        if "puncturing_internal_port" not in self.sessconfig:
            self.sessconfig["puncturing_internal_port"] = sessdefaults["puncturing_internal_port"]
        if "stun_servers" not in self.sessconfig:
            self.sessconfig["stun_servers"] = sessdefaults["stun_servers"]
        if "pingback_servers" not in self.sessconfig:
            self.sessconfig["pingback_servers"] = sessdefaults["pingback_servers"]
        if "mainline_dht" not in self.sessconfig:
            self.sessconfig["mainline_dht"] = sessdefaults["mainline_dht"]
        self.http_seeds = {}
        self.save_pstate_sessconfig()
        self.uch = UserCallbackHandler(self)
        self.lm = ACEStreamLaunchMany(network_thread_daemon)
        self.lm.register(self, self.sesslock)
        self.lm.start()