def getRegistrationsForProxiedPath(self, userFacingUrlPrefix): self._logger.info( 'thrift interface received a call to getRegistrationsForProxiedPath with a path value of %s' % userFacingUrlPrefix) prefix = zbase62.b2a(userFacingUrlPrefix) + "_" with self.lock: return self.configurer.getAllRegistrationsWihPrefix( zbase62.b2a(userFacingUrlPrefix) + '_')
def POST(self): web.header('Access-Control-Allow-Origin', '*') try: data = web.input() except ValueError: web.header("Content-Type", "application/json") return json.dumps({"status": "File is too large"}) if not 'encrypted' in data: web.header("Content-Type", "application/json") return json.dumps({"status": "Wrong parameters"}) try: json.loads(data.encrypted) if 'thumb' in data: json.loads(data.thumb) except ValueError: web.header("Content-Type", "application/json") return json.dumps({"status": "This is not JSON"}) salt = r_server.get('salt') if salt is None: salt = zbase62.b2a(M2Crypto.m2.rand_bytes(25)) r_server.set('salt', salt) r_server.expire('salt', 86400) hashedip = hashlib.sha3_512(web.ctx.ip.encode('utf-8') + salt).hexdigest() redisip = r_server.get('ip:' + hashedip) if redisip is not None and int(redisip) > 100: web.header("Content-Type", "application/json") return json.dumps({"status": "Too much uploads from your IP"}) fileid = ''.join( random.choice(string.letters + string.digits) for x in range(7)) while r_server.get('file:' + fileid): fileid = ''.join( random.choice(string.letters + string.digits) for x in range(7)) password = zbase62.b2a(M2Crypto.m2.rand_bytes(20)) hashed = bcrypt.hashpw(password, bcrypt.gensalt()) f = open(upload_dir + '/' + fileid, 'w') f.write(data.encrypted) f.close() if 'thumb' in data: f = open(upload_dir + '/thumb/' + fileid, 'w') f.write(data.thumb) f.close() if 'expire' in data and int(data.expire) != 0: try: delta = timedelta(days=int(data.expire)) except: return json.dumps({"status": "Wrong expire date"}) r_server.set('file:expire:' + fileid, (datetime.now() + delta).strftime('%Y-%m-%d %H:%M:%S')) r_server.set('file:' + fileid, hashed) r_server.incr('ip:' + hashedip) r_server.expire('ip:' + hashedip, 86400) web.header("Content-Type", "application/json") return json.dumps({"id": fileid, "pass": password, "status": "OK"})
def getNodeName(registration): # The node name encodes the UserFacingUrlPrefix, # the UpstreamPath, and the UpstreamHostAandPort in base62 # and separates them with a _ so that the information # can be encoded in a file name in zookeeper filename = \ zbase62.b2a(registration.UserFacingUrlPrefix)+ "_" +\ zbase62.b2a(registration.UpstreamPath) + "_" +\ zbase62.b2a(registration.UpstreamHostAndPort) return filename
def POST(self): web.header('Access-Control-Allow-Origin', '*') try: data = web.input() except ValueError: web.header("Content-Type", "application/json") return json.dumps({"status": "File is too large"}) if not 'encrypted' in data: web.header("Content-Type", "application/json") return json.dumps({"status": "Wrong parameters"}) try: json.loads(data.encrypted) if 'thumb' in data: json.loads(data.thumb) except ValueError: web.header("Content-Type", "application/json") return json.dumps({"status": "This is not JSON"}) salt = r_server.get('salt') if salt is None: salt = zbase62.b2a(M2Crypto.m2.rand_bytes(25)) r_server.set('salt', salt) r_server.expire('salt',86400) hashedip = hashlib.sha3_512(web.ctx.ip.encode('utf-8') + salt).hexdigest() redisip = r_server.get('ip:' + hashedip) if redisip is not None and int(redisip) > 100: web.header("Content-Type", "application/json") return json.dumps({"status": "Too much uploads from your IP"}) fileid = ''.join(random.choice(string.letters + string.digits) for x in range(7)) while r_server.get('file:' + fileid): fileid = ''.join(random.choice(string.letters + string.digits) for x in range(7)) password = zbase62.b2a(M2Crypto.m2.rand_bytes(20)) hashed = bcrypt.hashpw(password, bcrypt.gensalt()) f = open(upload_dir + '/' + fileid,'w') f.write(data.encrypted) f.close() if 'thumb' in data: f = open(upload_dir + '/thumb/' + fileid,'w') f.write(data.thumb) f.close() if 'expire' in data and int(data.expire) != 0: try: delta = timedelta(days=int(data.expire)) except: return json.dumps({"status": "Wrong expire date"}) r_server.set('file:expire:' + fileid, (datetime.now() + delta).strftime('%Y-%m-%d %H:%M:%S')) r_server.set('file:' + fileid, hashed) r_server.incr('ip:' + hashedip) r_server.expire('ip:' + hashedip,86400) web.header("Content-Type", "application/json") return json.dumps({"id": fileid, "pass": password, "status": "OK"})
def save(self, *args, **kwargs): old_id = self.id # Don't cascade saves by default. kwargs['cascade'] = kwargs.get('cascade', False) try: if not self.id: self.id = u'%s_%s' % (self.get_pk_prefix(), zbase62.b2a(os.urandom(32))) # Throw an exception if another object with this id already exists. kwargs['force_insert'] = True # But don't do that when cascading. kwargs['cascade_kwargs'] = {'force_insert': False} return super(RandomPKDocument, self).save(*args, **kwargs) except OperationError, err: self.id = old_id # Use "startswith" instead of "in". Otherwise, if a free form # StringField had a unique constraint someone could inject that # string into the error message. if unicode(err).startswith(u'Tried to save duplicate unique keys (E11000 duplicate key error index: %s.%s.$_id_ ' % (self._get_db().name, self._get_collection_name())): return self.save(*args, **kwargs) else: raise
def save(self, *args, **kwargs): old_id = self.id # Don't cascade saves by default. kwargs['cascade'] = kwargs.get('cascade', False) try: if not self.id: self.id = u'%s_%s' % (self.get_pk_prefix(), zbase62.b2a(os.urandom(32))) # Throw an exception if another object with this id already exists. kwargs['force_insert'] = True # But don't do that when cascading. kwargs['cascade_kwargs'] = { 'force_insert': False } return super(RandomPKDocument, self).save(*args, **kwargs) except OperationError, err: self.id = old_id # Use "startswith" instead of "in". Otherwise, if a free form # StringField had a unique constraint someone could inject that # string into the error message. if unicode(err).startswith(u'Tried to save duplicate unique keys (E11000 duplicate key error index: %s.%s.$_id_ ' % (self._get_db().name, self._get_collection_name())): return self.save(*args, **kwargs) else: raise
def isReverseProxiedPathRegistered(self, userFacingUrlPrefix): #string self._logger.info("thrift interface received a call to isReverseProxiedPathRegistered with a path value of %s" % (userFacingUrlPrefix)) prefix = zbase62.b2a(userFacingUrlPrefix)+ "_" with self.lock: allNodes = self.kz.get_children(ezRPKazoo.KZCONFLOC) matchingFilenames = [x for x in allNodes if x.startswith(prefix)] if len(matchingFilenames): return True return False
def isReverseProxiedPathRegistered(self, userFacingUrlPrefix): #string self._logger.info( "thrift interface received a call to isReverseProxiedPathRegistered with a path value of %s" % (userFacingUrlPrefix)) prefix = zbase62.b2a(userFacingUrlPrefix) + "_" with self.lock: allNodes = self.kz.get_children(ezRPKazoo.KZCONFLOC) matchingFilenames = [x for x in allNodes if x.startswith(prefix)] if len(matchingFilenames): return True return False
def _test_ende(self, bs): alphas = zbase62.b2a(bs) bs2 = zbase62.a2b(alphas) assert bs2 == bs, "bs2: %s:%s, bs: %s:%s, alphas: %s:%s" % ( len(bs2), repr(bs2), len(bs), repr(bs), len(alphas), repr(alphas), )
def GET(self): data = web.input() if not 'email' in data: return 'Wrong parameters' if not re.match(r'[^@]+@[^@]+\.[^@]+', data.email): return 'This is not email' if r_server.sismember('nonregistred', data.email): password = zbase62.b2a(M2Crypto.m2.rand_bytes(20)) hashed = bcrypt.hashpw(password, bcrypt.gensalt()) r_server.set('user:'******'nonregistred', data.email) return render.register(password, cgi.escape(data.email)) else: return 'No such email'
def GET(self): data = web.input() if not 'email' in data: return 'Wrong parameters' if not re.match(r'[^@]+@[^@]+\.[^@]+', data.email): return 'This is not email' if r_server.sismember('nonregistred',data.email): password = zbase62.b2a(M2Crypto.m2.rand_bytes(20)) hashed = bcrypt.hashpw(password, bcrypt.gensalt()) r_server.set('user:'******'nonregistred',data.email) return render.register(password,cgi.escape(data.email)) else: return 'No such email'
def removeReverseProxiedPath(self, userFacingUrlPrefix): """ Parameters: - userFacingUrlPrefix """ self._logger.info("thrift interface received a call to removeReverseProxiedPath with a path value of %s" % userFacingUrlPrefix) prefixToRemove = zbase62.b2a(userFacingUrlPrefix)+ "_" with self.lock: allNodes = self.kz.get_children(ezRPKazoo.KZCONFLOC) fileNamesToRemove = [x for x in allNodes if x.startswith(prefixToRemove)] for fn in fileNamesToRemove: self._logger.info("removeReverseProxiedPath is deleting %s from zookeeper" % (ezRPKazoo.KZCONFLOC+'/'+fn)) self.kz.delete(ezRPKazoo.KZCONFLOC+'/'+fn) if len(fileNamesToRemove) > 0: self.kz.set(ezRPKazoo.KZWATCHLOC, str(gConfig.current_milli_time()))
def base62_text_gen(text_length=None, try_times=100): """ Generate a random sequence of bytes, encode it in base62. :param try_times: number of times to try. :return: the base62-encoded text. """ # Tried times tried_times = 0 # Start to generate while True: # Increment tried times tried_times += 1 # If have tried enough times if tried_times > try_times: # Raise error raise ValueError('Tried {} times'.format(try_times)) # Generate random bytes byte_s = os.urandom(16) # Encode the random bytes using base62 text = zbase62.b2a(byte_s) # If "text_length" is given if text_length is not None: # Limit to the length text = text[0:text_length] # Convert to uppercase text = text.upper() # If the text is ok to use if base62_text_is_ok(text): # Return return text # If the text is not ok to use else: # Re-generate in the next loop continue
def test_odd_sizes(self): for j in range(2**6): lib = random.randrange(1, 2**8) numos = div_ceil(lib, 8) bs = random_bytes(numos) # zero-out unused least-sig bits if lib % 8: b = ord(bs[-1]) if IS_PY2 else bs[-1] b = b >> (8 - (lib % 8)) b = b << (8 - (lib % 8)) bs = bs[:-1] + (chr(b) if IS_PY2 else bytes([b])) asl = zbase62.b2a(bs) assert len( asl) == zbase62.num_chars_that_this_many_octets_encode_to( numos ) # the size of the base-62 encoding must be just right bs2l = zbase62.a2b_l(asl, lib) assert len( bs2l) == numos # the size of the result must be just right assert bs == bs2l
def removeReverseProxiedPath(self, userFacingUrlPrefix): """ Parameters: - userFacingUrlPrefix """ self._logger.info( "thrift interface received a call to removeReverseProxiedPath with a path value of %s" % userFacingUrlPrefix) prefixToRemove = zbase62.b2a(userFacingUrlPrefix) + "_" with self.lock: allNodes = self.kz.get_children(ezRPKazoo.KZCONFLOC) fileNamesToRemove = [ x for x in allNodes if x.startswith(prefixToRemove) ] for fn in fileNamesToRemove: self._logger.info( "removeReverseProxiedPath is deleting %s from zookeeper" % (ezRPKazoo.KZCONFLOC + '/' + fn)) self.kz.delete(ezRPKazoo.KZCONFLOC + '/' + fn) if len(fileNamesToRemove) > 0: self.kz.set(ezRPKazoo.KZWATCHLOC, str(gConfig.current_milli_time()))
def uuid_to_id(uuid_obj, prefix): return '{}_{}'.format(prefix, zbase62.b2a(uuid_obj.bytes))
def _test_ende(self, bs): alphas=zbase62.b2a(bs) bs2=zbase62.a2b(alphas) assert bs2 == bs, "bs2: %s:%s, bs: %s:%s, alphas: %s:%s" % (len(bs2), `bs2`, len(bs), `bs`, len(alphas), `alphas`)
def _generate_unique_slug(self): """Generate unique identifier for this slug.""" unique_id = zbase62.b2a(urandom(24)) while exists(join(self.storage_dir, unique_id)): unique_id = zbase62.b2a(urandom(24)) return unique_id
def getRegistrationsForProxiedPath(self, userFacingUrlPrefix): self._logger.info('thrift interface received a call to getRegistrationsForProxiedPath with a path value of %s' % userFacingUrlPrefix) prefix = zbase62.b2a(userFacingUrlPrefix)+ "_" with self.lock: return self.configurer.getAllRegistrationsWihPrefix(zbase62.b2a(userFacingUrlPrefix)+ '_')
def test_types(self): assert type(zbase62.a2b(u"x")) == bytes assert type(zbase62.a2b(b"x")) == bytes assert type(zbase62.b2a(u"x")) == unicode assert type(zbase62.b2a(b"x")) == unicode
def _generate_pk(cls): return '%s_%s' % (cls.get_pk_prefix(), zbase62.b2a(os.urandom(32)))