def is_key_verified(orig_key: str) -> Optional[Key]: key_index = utils.naive_hash(orig_key) key = keys_table.select_by_primary_key(key_index) if key is None: return None try: password_hasher.verify(key.key_value, orig_key) except VerifyMismatchError: return None return key
def verify_key(self, orig_key: str) -> str: key_index = utils.naive_hash(orig_key) key = sql.select_by_primary_key(key_index) # 非空且未过期 if key is not None and \ not (key.key_created_time and key.key_expired_time < utils.curr_time() and key.key_expired_time): try: self._ph.verify(key.key_value, orig_key) except VerifyMismatchError: raise KeyCheckVerificationError() if not key.key_created_time: print(f'正在激活 {key_index[:5]}***') sql.activate( key_index) # 只更新 creat 和 expire 时间,后面的返回不需要这么多东西,这里就不需要刷新了 if self._receivers.count_user_by_key( key.key_index) < key.key_max_users: # 连接数量限制 return key_index raise KeyCheckMaxError() raise KeyCheckVerificationError()
def _create_key(self, max_users: int = 3, available_days: int = 30) -> str: while True: orig_key = ''.join(random.choices(self._key_seed, k=16)) # 100^16 别想着暴力了,各位 encrypted_key = base64.b64encode( rsa.encrypt(orig_key.encode('utf8'), self._super_admin_pubkey)).decode('utf8') hashed_key: str = self._ph.hash(orig_key) naive_hashed_key: str = utils.naive_hash(orig_key) if sql.is_key_addable(key_index=naive_hashed_key, key_value=hashed_key): curr_time = utils.curr_time() expired_time = 0 if not available_days else curr_time + available_days * 3600 * 24 sql.insert_element( sql.Key(key_index=naive_hashed_key, key_value=hashed_key, key_created_time=curr_time, key_max_users=max_users, key_expired_time=expired_time)) info(f'创建了一个新的KEY(MAX人数{max_users:^5}): {orig_key}') return encrypted_key
def create_key(self, max_users: int, available_days: int) -> str: while True: orig_key = ''.join(random.choices(self._key_seed, k=16)) # 100^16 别想着暴力了,各位 hashed_key: str = self._ph.hash(orig_key) naive_hashed_key: str = utils.naive_hash(orig_key) if sql.is_key_addable(key_index=naive_hashed_key, key_value=hashed_key): key_created_time = 0 expired_time = 0 if not available_days else key_created_time + available_days * 3600 * 24 sql.insert_element( sql.Key(key_index=naive_hashed_key, key_value=hashed_key, key_created_time=key_created_time, key_max_users=max_users, key_expired_time=expired_time)) print( f'创建了一个新的KEY(MAX人数为{max_users:^5}人, 可用天数为{available_days:^5}天): {orig_key}' ) return orig_key
"""用于检查服务器状态,比如有多少人在监听服务器、推送者有哪些等等 """ import rsa import utils import global_var name = 'bbbn' # 用户名没必要改 key = 'V_F<Nv)!WeK]irWY' # 检测 key 是否存在以及相关信息 with open(f'{global_var.KEY_PATH}/admin_privkey.pem', 'rb') as f: admin_privkey = rsa.PrivateKey.load_pkcs1(f.read()) dict_signature = utils.make_signature(name, admin_privkey, need_name=True) data = {} if key: naive_hashed_key = utils.naive_hash(key) data['naive_hashed_key'] = naive_hashed_key data = { 'code': 0, 'type': 'raffle', 'data': data, 'verification': dict_signature, } json_rsp = utils.request_json('GET', f'{global_var.URL}/check', json=data) print('JSON结果:', json_rsp)