2022-12-15 17:59:25 +08:00
|
|
|
|
# 此模块提供了维护api-key的各种功能
|
|
|
|
|
import hashlib
|
|
|
|
|
import logging
|
|
|
|
|
|
2023-01-14 22:36:48 +08:00
|
|
|
|
import pkg.plugin.host as plugin_host
|
|
|
|
|
import pkg.plugin.models as plugin_models
|
2022-12-15 17:59:25 +08:00
|
|
|
|
|
2023-03-05 15:39:13 +08:00
|
|
|
|
|
2022-12-15 17:59:25 +08:00
|
|
|
|
class KeysManager:
|
|
|
|
|
api_key = {}
|
2023-03-05 15:39:13 +08:00
|
|
|
|
"""所有api-key"""
|
2022-12-15 17:59:25 +08:00
|
|
|
|
|
|
|
|
|
using_key = ""
|
2023-03-05 15:39:13 +08:00
|
|
|
|
"""当前使用的api-key
|
|
|
|
|
"""
|
2022-12-15 17:59:25 +08:00
|
|
|
|
|
|
|
|
|
alerted = []
|
2023-03-05 15:39:13 +08:00
|
|
|
|
"""已提示过超额的key
|
|
|
|
|
|
|
|
|
|
记录在此以避免重复提示
|
|
|
|
|
"""
|
2022-12-15 17:59:25 +08:00
|
|
|
|
|
2023-01-03 00:02:18 +08:00
|
|
|
|
exceeded = []
|
2023-03-05 15:39:13 +08:00
|
|
|
|
"""已超额的key
|
|
|
|
|
|
|
|
|
|
供自动切换功能识别
|
|
|
|
|
"""
|
2023-01-03 00:02:18 +08:00
|
|
|
|
|
2022-12-15 17:59:25 +08:00
|
|
|
|
def get_using_key(self):
|
|
|
|
|
return self.using_key
|
|
|
|
|
|
2023-01-03 17:50:13 +08:00
|
|
|
|
def get_using_key_md5(self):
|
|
|
|
|
return hashlib.md5(self.using_key.encode('utf-8')).hexdigest()
|
|
|
|
|
|
2022-12-15 17:59:25 +08:00
|
|
|
|
def __init__(self, api_key):
|
2023-01-04 17:09:57 +08:00
|
|
|
|
|
2022-12-15 17:59:25 +08:00
|
|
|
|
if type(api_key) is dict:
|
|
|
|
|
self.api_key = api_key
|
|
|
|
|
elif type(api_key) is str:
|
|
|
|
|
self.api_key = {
|
|
|
|
|
"default": api_key
|
|
|
|
|
}
|
|
|
|
|
elif type(api_key) is list:
|
|
|
|
|
for i in range(len(api_key)):
|
|
|
|
|
self.api_key[str(i)] = api_key[i]
|
|
|
|
|
# 从usage中删除未加载的api-key的记录
|
|
|
|
|
# 不删了,也许会运行时添加曾经有记录的api-key
|
|
|
|
|
|
2023-01-13 15:34:28 +08:00
|
|
|
|
self.auto_switch()
|
2023-01-03 00:02:18 +08:00
|
|
|
|
|
2022-12-15 17:59:25 +08:00
|
|
|
|
def auto_switch(self) -> (bool, str):
|
2023-03-05 15:39:13 +08:00
|
|
|
|
"""尝试切换api-key
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
是否切换成功, 切换后的api-key的别名
|
|
|
|
|
"""
|
|
|
|
|
|
2022-12-15 17:59:25 +08:00
|
|
|
|
for key_name in self.api_key:
|
2023-01-03 00:02:18 +08:00
|
|
|
|
if self.api_key[key_name] not in self.exceeded:
|
2022-12-15 17:59:25 +08:00
|
|
|
|
self.using_key = self.api_key[key_name]
|
2023-01-14 22:36:48 +08:00
|
|
|
|
|
2022-12-15 17:59:25 +08:00
|
|
|
|
logging.info("使用api-key:" + key_name)
|
2023-01-14 22:36:48 +08:00
|
|
|
|
|
|
|
|
|
# 触发插件事件
|
|
|
|
|
args = {
|
|
|
|
|
"key_name": key_name,
|
|
|
|
|
"key_list": self.api_key.keys()
|
|
|
|
|
}
|
|
|
|
|
_ = plugin_host.emit(plugin_models.KeySwitched, **args)
|
|
|
|
|
|
2022-12-15 17:59:25 +08:00
|
|
|
|
return True, key_name
|
|
|
|
|
|
|
|
|
|
self.using_key = list(self.api_key.values())[0]
|
2022-12-29 11:30:43 +08:00
|
|
|
|
logging.info("使用api-key:" + list(self.api_key.keys())[0])
|
2022-12-15 17:59:25 +08:00
|
|
|
|
|
|
|
|
|
return False, ""
|
|
|
|
|
|
|
|
|
|
def add(self, key_name, key):
|
|
|
|
|
self.api_key[key_name] = key
|
|
|
|
|
|
2022-12-28 00:05:25 +08:00
|
|
|
|
def set_current_exceeded(self):
|
2023-03-05 15:39:13 +08:00
|
|
|
|
"""设置当前使用的api-key使用量超限
|
|
|
|
|
"""
|
2023-01-03 00:02:18 +08:00
|
|
|
|
self.exceeded.append(self.using_key)
|
2022-12-17 17:21:20 +08:00
|
|
|
|
|
2023-01-03 00:02:18 +08:00
|
|
|
|
def get_key_name(self, api_key):
|
|
|
|
|
"""根据api-key获取其别名"""
|
|
|
|
|
for key_name in self.api_key:
|
|
|
|
|
if self.api_key[key_name] == api_key:
|
|
|
|
|
return key_name
|
2023-03-10 23:14:32 +08:00
|
|
|
|
return ""
|