def contact_corp(self, corp_url): try: my_corp = Corporation.objects.mine() url = urlparse.urljoin(corp_url, '/corp/contact/') client = HttpClient() LOG.info('Sending our public info to %s...' % url) # first GET request to fetch CSRF cookie response = client.get(url) # second POST request to send our public info response = client.post(url, json.dumps(my_corp.get_public_info())) LOG.info('Fetching public info from %s...' % url) # the response should contain the corp's public info public_info = json.load(response) self.corporationID = public_info['corporationID'] self.corporationName = public_info['corporationName'] self.ticker = public_info['ticker'] self.alliance_id = public_info['alliance'] self.public_key = public_info['public_key'] self.key_fingerprint = public_info['key_fingerprint'] self.is_my_corp = False self.is_trusted = True LOG.info('Corp %s accepted our contact request.' % self.corporationName) LOG.info('Wait until they confirm that they trust us ' 'before you can exchange data with them.') except urllib2.HTTPError, e: message = 'URL: %s, Response: %s %s "%s"' % (e.url, e.code, e.reason, e.read()) LOG.exception(message) raise ValidationError(message)
def send_feedback(): """ This function will collect basic non-critical data about the current server instance and send it to eve-corp-management.org official server for usage statistics feedback. """ LOG.debug("Sending usage feedback to %r...", ECM_USAGE_FEEDBACK_URL) mycorp = Corporation.objects.mine() # fetch geolocalization info http_client = HttpClient() resp = http_client.get(url="http://freegeoip.net/json/") geoloc_info = json.loads(resp.read()) resp.close() # we only consider users that are corp members users = get_members_group().user_set.order_by("-last_login") usage_data = { "key_fingerprint": mycorp.key_fingerprint, "active_user_count": users.count(), "avg_last_visit_top10": avg_last_login(users[:10]), "avg_last_visit": avg_last_login(users), "first_installed": find_oldest_entry(), "country_code": geoloc_info.get("country_code"), "country_name": geoloc_info.get("country_name"), "city": geoloc_info.get("city"), "ecm_version": ecm.VERSION, } # send the data to the server resp = http_client.post(ECM_USAGE_FEEDBACK_URL, json.dumps(usage_data)) LOG.info("Usage feedback sent to %r. Thank you for your contribution.", ECM_USAGE_FEEDBACK_URL) new_version = resp.read().strip() old_version = ecm.VERSION if parse_version(new_version) > parse_version(old_version): LOG.info("New version of ecm is available: %r.", new_version) ctx_dict = { "host_name": settings.EXTERNAL_HOST_NAME, "use_https": settings.USE_HTTPS, "new_version": new_version, "old_version": old_version, } dummy_request = HttpRequest() dummy_request.user = AnonymousUser() subject = tr_lazy("ECM version %s is available" % new_version) msg = render_to_string("ecm/common/email/new_version.txt", ctx_dict, Ctx(dummy_request)) html = render_to_string("ecm/common/email/new_version.html", ctx_dict, Ctx(dummy_request)) mail_admins(subject=subject, message=msg, html_message=html)
def update_one_corp(corp): my_corp = Corporation.objects.mine() auth_url = urlparse.urljoin(corp.ecm_url, '/corp/auth/startsession/') client = HttpClient() LOG.debug('Establishing secure data exchange with %r...' % corp.ecm_url) response = client.get( auth_url, headers={'Authorization': 'RSA %s' % my_corp.key_fingerprint}) cipher_txt_in = response.read() # we decrypt the response with our private key session_secret = crypto.rsa_decrypt(my_corp.private_key, cipher_txt_in) # and encrypt it back with the corp's public key cipher_txt_out = crypto.rsa_encrypt(corp.public_key, session_secret) # then send it to the server client.post(auth_url, cipher_txt_out) LOG.debug('Fetching which data %r is sharing with us...' % corp) # now we fetch the urls we're allowed to pull from this corporation response = client.get( urlparse.urljoin(corp.ecm_url, '/corp/share/allowed/')) data = crypto.aes_decrypt(session_secret, response.read()) allowed_urls = json.loads(data) if not allowed_urls: LOG.warning('%r is not sharing any data with us' % corp.corporationName) for url in allowed_urls: try: shared_data = SharedData.objects.get(url=url) LOG.debug('Fetching shared data %r...' % url) response = client.get( urlparse.urljoin(corp.ecm_url, shared_data.url)) raw_data = crypto.aes_decrypt(session_secret, response.read()) if response.info().getheader( 'content-type') == 'application/gzip-compressed': raw_data = zlib.decompress(raw_data) shared_data.call_handler(corp, json.loads(raw_data)) except SharedData.DoesNotExist: LOG.error('Unknown SharedData with url=%r' % url) except: LOG.exception('') LOG.debug('Ending secure session with %r...' % corp.ecm_url) # finally destroy our session info to be sure nobody will steal it :) client.get(urlparse.urljoin(corp.ecm_url, '/corp/auth/endsession/'))
def update_one_corp(corp): my_corp = Corporation.objects.mine() auth_url = urlparse.urljoin(corp.ecm_url, '/corp/auth/startsession/') client = HttpClient() LOG.debug('Establishing secure data exchange with %r...' % corp.ecm_url) response = client.get(auth_url, headers={'Authorization': 'RSA %s' % my_corp.key_fingerprint}) cipher_txt_in = response.read() # we decrypt the response with our private key session_secret = crypto.rsa_decrypt(my_corp.private_key, cipher_txt_in) # and encrypt it back with the corp's public key cipher_txt_out = crypto.rsa_encrypt(corp.public_key, session_secret) # then send it to the server client.post(auth_url, cipher_txt_out) LOG.debug('Fetching which data %r is sharing with us...' % corp) # now we fetch the urls we're allowed to pull from this corporation response = client.get(urlparse.urljoin(corp.ecm_url, '/corp/share/allowed/')) data = crypto.aes_decrypt(session_secret, response.read()) allowed_urls = json.loads(data) if not allowed_urls: LOG.warning('%r is not sharing any data with us' % corp.corporationName) for url in allowed_urls: try: shared_data = SharedData.objects.get(url=url) LOG.debug('Fetching shared data %r...' % url) response = client.get(urlparse.urljoin(corp.ecm_url, shared_data.url)) raw_data = crypto.aes_decrypt(session_secret, response.read()) if response.info().getheader('content-type') == 'application/gzip-compressed': raw_data = zlib.decompress(raw_data) shared_data.call_handler(corp, json.loads(raw_data)) except SharedData.DoesNotExist: LOG.error('Unknown SharedData with url=%r' % url) except: LOG.exception('') LOG.debug('Ending secure session with %r...' % corp.ecm_url) # finally destroy our session info to be sure nobody will steal it :) client.get(urlparse.urljoin(corp.ecm_url, '/corp/auth/endsession/'))