def main(): args = parser.parse_args() socket_host, socket_port = args.http_host_port.split(':') if os.path.isdir(args.data_dir): print('Using existing profile directory: %s' % args.data_dir) else: mkdir(args.data_dir) p1 = Process( target=httpsrv.main, kwargs=kwargs(socket_host=socket_host, socket_port=int(socket_port), http_webdir=config.http_webdir, profile_dir=args.data_dir), ) p2 = Process(target=mockproc.main, args=('p2', 1)) p3 = Process(target=crypto.test_rsa_generic) p3.start() p1.start() p2.start() p3.join() p1.join() p2.join() return 0
def main_profile( http_webdir, files_dir_name, cdn_host, cdn_port, ): http_webdir = os.path.abspath(http_webdir) files_dir = os.path.abspath('%s/%s' % (profile_dir, files_dir_name)) if not os.path.isdir(files_dir): print('Creating dir for static files') mkdir(files_dir) profile_dir_abs = os.path.abspath(profile_dir) ert_profile_ctl = EthearnalProfileController(data_dir=profile_dir_abs, cdn_bootstrap_host=cdn_host, cdn_bootstrap_port=cdn_port) return ert_profile_ctl
def main_http( http_webdir: str = config.http_webdir, socket_host: str = config.http_socket_host, socket_port: int = config.http_socket_port, ert_profile_ctl: EthearnalProfileController = None, # ert_profile_view: EthearnalProfileView = None, files_dir_name: str = config.static_files, interactive: bool = config.interactive, dht_=None, dht_facade_=None): site_conf = { '/': { 'tools.sessions.on': True, 'tools.staticdir.root': os.path.abspath(os.getcwd()) }, '/ui': { 'tools.staticdir.on': True, 'tools.staticdir.dir': http_webdir, 'tools.staticdir.index': 'index.html', }, '/ui/network': { 'tools.staticdir.on': True, 'tools.staticdir.dir': http_webdir, 'tools.staticdir.index': 'network.html', }, '/ui/profile': { 'tools.staticdir.on': True, 'tools.staticdir.dir': http_webdir, 'tools.staticdir.index': 'profile.html', }, '/ui/profiles': { 'tools.staticdir.on': True, 'tools.staticdir.dir': http_webdir, 'tools.staticdir.index': 'profiles.html', }, # '/ui/files': { # 'tools.staticdir.on': True, # 'tools.staticdir.root': ert_profile_ctl.data_dir, # 'tools.staticdir.dir': files_dir_name, # }, '/api/ui': { 'tools.staticdir.on': True, 'tools.staticdir.dir': 'apidef/swagger', 'tools.staticdir.index': 'index.html', } } cherrypy.server.socket_host = socket_host cherrypy.server.socket_port = socket_port # Cache-Control:public, max-age=5 # in seconds cherrypy.response.headers['Cache-Control'] = 'public, max-age=5' cherrypy.tree.mount(EthearnalSite(), '/', site_conf) cherrypy.config.update({ 'global': { 'engine.autoreload.on': False, 'server.thread_pool': 120, } }) hfs_dir = '%s/%s' % (ert.data_dir, config.hfs_dir) mkdir(hfs_dir) print('HFS_DIR: %s' % hfs_dir) idx = Indexer(ert=ert, dhf=dht_facade_) # select and save CDN service hfs_guids = AutoDirHfs(hfs_dir, 'guids_hfs') dkv = DhtKv(d) dkv.set(WebDhtCdnSelector.K_SELECTED_CDN, "%s:%s" % (ert.cdn_host, ert.cdn_port)) cdn_select = WebDhtCdnSelector(dkv) from webfacades.dht_kv import WebCdnClusterTrackerClient, WebCdnClusterTracker wtrack_cli = WebCdnClusterTrackerClient( dhf=d, http_host_port=config.ert_default_tracker) cdn_list = WebDhtCdnList(dkv=dkv, wtrack_cli=wtrack_cli) knownguids = WebDHTKnownGuids(cherry=cherrypy, dhtf=dht_facade_, dkv=dkv, hfs=hfs_guids, mount_point='/api/v1/dht/guids') dht_get_hk = DhtGetByHkeyWebAPI( cherry=cherrypy, dhf=dht_facade_, ) dht_gigs_hk = DhtGigsHkeysWebAPI(cherry=cherrypy, dhf=dht_facade_, me_owner=OwnerGuidHashIO( ert_profile_ctl.rsa_guid_hex)) # don't delete this # events = DhtEventsHkeysWebAPI( # cherry=cherrypy, # dhf=dht_facade_, # me_owner=OwnerGuidHashIO(ert_profile_ctl.rsa_guid_hex), # ) # # dht_portfolios_hk = DhtPortfoliosWebAPI( # cherry=cherrypy, # dhf=dht_facade_, # me_owner=OwnerGuidHashIO(ert_profile_ctl.rsa_guid_hex) # ) dht_ip4 = WebDHTKnownPeers( cherry=cherrypy, dhf=dht_facade_, ) dht_profile = WebDHTProfileKeyVal( cherry=cherrypy, dhf=dht_facade_, ) dht_node = WebDHTAboutNode( cherry=cherrypy, dhf=dht_facade_, ) from webfacades.dht_peers import WebDhtPeers rel_urls = None if args.http_config_url: from toolkit.tools import get_http_peers_from_http_tracker from toolkit.tools import boot_peers_from_http_tracker # url = 'http://159.65.56.140:8080/cluster.json' relays = get_http_peers_from_http_tracker(args.http_config_url) rel_urls = [ 'http://%s/api/cdn/v1/resource' % k for k in relays if ip not in k ] # self.relays = set(rurl) boot_peers_from_http_tracker(d, args.http_config_url) peers = PeersInfo(dhf=d, geo=FsCachedGeoIp(AutoDirHfs(hfs_dir, 'geo_hfs')), hfs=AutoDirHfs(hfs_dir, 'peers_hfs')) web_peers = WebDhtPeers(peers=peers, cherry=cherrypy) from webdht.bundle import DocumentCollectionCRD, DHTEventHandler, DocModelIndexQuery # from webdht.test_bndle_gig import Gigs as test_gig_data # nes bundle c = DocumentCollectionCRD( 'Event', dhf=dht_facade_, own_guid_hex=ert.rsa_guid_hex, key_composer=None, ) # from apifacades.events import SelfEvent # se = SelfEvent(c) # evt = DHTEventHandler(dht_facade_.dht.storage, data_dir=ert_profile_ctl.personal_dir) # todo add support for instance id db files # qidx = DocModelIndexQuery(evt.doc_indexers.MODEL_INDEXERS['.Gig.model']) # eidx = DocModelIndexQuery(evt.doc_indexers.MODEL_INDEXERS['.Event.model']) # qpro = DocModelIndexQuery(evt.doc_indexers.MODEL_INDEXERS['.Profile.key']) cherrypy.engine.start() print('WEBUI DIR:', http_webdir) print('PROFILE DIR:', ert_profile_ctl.data_dir) cherrypy.engine.exit = on_hook(target=tear_down_udp, target_args=(dht_, ), target_kwargs={})(cherrypy.engine.exit) # import asyncio # import datetime # import random # import websockets # try: # async def time(websocket, path): # while True: # now = datetime.datetime.utcnow().isoformat() + 'Z' # await websocket.send(now) # await asyncio.sleep(random.random() * 3) # # start_server = websockets.serve(time, '127.0.0.1', 6789) # asyncio.get_event_loop().run_until_complete(start_server) # asyncio.get_event_loop().run_forever() # except Exception as e: # print(str(e)) # sys.exit() if not interactive: cherrypy.engine.block() else: try: from IPython import embed embed() except: pass
seed_port = None upnp_attempt = args.upnp_attempt # cdn_selected = args.cdn_bootstrap_host_port boot_cdn_host, boot_cdn_port = args.cdn_bootstrap_host_port.split(':') json_data_to_profile = args.json_data_to_profile print('boot-cdn', boot_cdn_host, boot_cdn_port) if args.udp_seed_host_port: seed_host, seed_port = args.udp_seed_host_port.split(':') seed_port = int(seed_port) if os.path.isdir(args.data_dir): print('Using existing profile directory: %s' % profile_dir) else: mkdir(profile_dir) dht = None d = None dl = None try: ert_profile_ctl = main_profile( http_webdir=http_webdir, files_dir_name=config.static_files, cdn_host=boot_cdn_host, cdn_port=boot_cdn_port, shared_profile_instance_id=args.shared_profile_instance_id) ert = ert_profile_ctl if args.upnp_attempt: upnp.punch_port(udp_port, udp_port, args.if_name)
def __init__( self, data_dir=config.data_dir, personal_dir=None, # files_dir=None, cdn_bootstrap_host=None, cdn_bootstrap_port=None, cdn_service_node=False, ): self.cdn_service_node = cdn_service_node self.my_wan_ip = ipgetter.myip() self.my_lan_ip = None self.cdn_service_http_url = None # self.my_wan_port = 0 self.cdx = cdx self.cdn_host = cdn_bootstrap_host self.cdn_port = cdn_bootstrap_port if not self.cdn_service_node: if not cdn_bootstrap_host and not cdn_bootstrap_port: raise ValueError('bootstrap to ertcdn service is required') # self.cdn_gigs = CdnBinResourceBsonApiClientRequests(endpoint_host=cdn_bootstrap_host, # endpoint_port=cdn_bootstrap_port, # endpoint_path='/api/v1/gig') # # self.cdn_imgs = CdnBinResourceBsonApiClientRequests(endpoint_host=cdn_bootstrap_host, # endpoint_port=cdn_bootstrap_port, # endpoint_path='/api/v1/img') self.data_dir = os.path.abspath(data_dir) if personal_dir: self.personal_dir = os.path.abspath(personal_dir) else: self.personal_dir = os.path.abspath( '%s/%s' % (self.data_dir, self.PERSONAL_DIRECTORY_NAME)) tools.mkdir(self.personal_dir) # if files_dir: # self.files_dir = os.path.abspath(files_dir) # else: # self.files_dir = os.path.abspath('%s/%s' % (self.data_dir, config.static_files)) self.profile_json_file_name = '%s/%s' % (self.personal_dir, self.PROFILE_JSON_FILE_NAME) self.profile_html_file_name = '%s/%s' % (self.personal_dir, self.PROFILE_HTML_FILE_NAME) self.profile_image_file_name = '%s/%s' % (self.personal_dir, self.PROFILE_IMAGE_FILE_NAME) self.rsa_prv_fn = '%s/%s' % (self.personal_dir, self.RSA_PRV) self.rsa_pub_fn = '%s/%s' % (self.personal_dir, self.RSA_PUB) self.dht_fb_fn = '%s/%s' % (self.personal_dir, self.PROFILE_DHT_SQLITE) self.dht_ref_pubkeys_fn = '%s/%s' % (self.personal_dir, self.PROFILE_DHT_REF_PUBKEYS) self.db_gigs_index_fn = '%s/%s' % (self.personal_dir, self.DB_GIGS_IDX) # self.job_post_json_store_fn = '%s/%s' % (self.personal_dir, self.JOB_POSTS_JSON_FILE_NAME) # self.db_plain_text = '%s/%s' % (self.personal_dir, self.PROFILE_PLAIN_UTF8_TEXTS) # self.db_plain_text_inv = '%s/%s' % (self.personal_dir, self.PROFILE_PREFIXES_IDX) # # self.db_gigs = '%s/%s' % (self.personal_dir, self.PROFILE_GIGS_DB) # self.db_imgs = '%s/%s' % (self.personal_dir, self.PROFILE_IMGS_DB) # self.db_my_gigs_idx = '%s/%s' % (self.personal_dir, self.PROFILE_MY_GIGS_INDEX) # self.model = EthearnalProfileModel() self.dht_store = ErtDHTSQLite(self.dht_fb_fn) self.dht_pubkeys = ErtREFSQLite(self.dht_ref_pubkeys_fn) # create empty profile if not found # if not os.path.isfile(self.profile_json_file_name): # self.model.to_json_file(self.profile_json_file_name) # else: # self.model.from_json_file(self.profile_json_file_name) # create empty profile html if not found # if not os.path.isfile(self.profile_html_file_name): # # todo # pass # else: # # todo # pass # create generated avatar if not found if not os.path.isfile(self.profile_image_file_name): self.generate_random_avatar(filename=self.profile_image_file_name) # create rsa keys if not present # todo win/ux chmod 400 secure keys self.rsa_keys() # init local signer self.rsa_signer = LocalRsaSigner(self.rsa_prv_der, self.rsa_pub_der)
print('UDP %s:%s' % (udp_host, udp_port)) print('HTTP %s:%s' % (host, port)) udp_port = int(udp_port) cdn_profile_dir = args.profile_data_dir cdn_files_dir = args.data_dir cherrypy.server.socket_host = host cherrypy.server.socket_port = int(port) cherrypy.tree.mount(WebCDNSite(), '/', site_conf) cdn_profile_dir = os.path.abspath(cdn_profile_dir) cdn_files_dir = os.path.abspath(cdn_files_dir) # main data if os.path.isdir(cdn_profile_dir): print('Using existing profile directory: %s' % cdn_profile_dir) else: mkdir(cdn_profile_dir) # uploaded files if os.path.isdir(cdn_files_dir): print('Using existing profile directory: %s' % cdn_files_dir) else: mkdir(cdn_files_dir) from ert_profile import EthearnalProfileController from kadem.kad import DHT from kadem.kad import DHTFacade from toolkit import store_handler from ert import tear_down_udp, punch_dht_udp_hole from webdht.wdht import OwnerGuidHashIO, WebDHTKnownGuids from webdht.wdht_ertapi import WebDHTKnownPeers, WebDHTProfileKeyVal, WebDHTAboutNode from webdht.wdht_ertapi import DhtGigsHkeysWebAPI, DhtGetByHkeyWebAPI, DhtPortfoliosWebAPI, Indexer from toolkit import upnp
def AutoDirHfs(base_dir: str, name: str) -> FileSystemHashStore: dir_name = '%s/%s' % (os.path.abspath(base_dir), name) mkdir(dir_name) return FileSystemHashStore(dir_name)