def create(self, request, pk=None, **kwargs): null_response = { "status": "error", "type": "account", "currentAuthority": "guest", "token": "forguest" } # 检查是否为diypassword password = request.data.get('password', None) if password == "diypassword": context = data_return(302, BASEAUTH_MSG.get(302), null_response) return Response(context) try: serializer = AuthTokenSerializer(data=request.data) if serializer.is_valid(): token, created = Token.objects.get_or_create( user=serializer.validated_data['user']) time_now = datetime.datetime.now() if created or token.created < time_now - datetime.timedelta( minutes=EXPIRE_MINUTES): # 更新创建时间,保持token有效 token.delete() token = Token.objects.create( user=serializer.validated_data['user']) token.created = time_now token.save() null_response['status'] = 'ok' null_response[ 'currentAuthority'] = 'admin' # 当前为单用户模式,默认为admin null_response['token'] = token.key # 成功登录通知 Notice.send_info(f"{serializer.validated_data['user']} 成功登录") context = data_return(201, BASEAUTH_MSG.get(201), null_response) return Response(context) else: if Xcache.login_fail_count(): Notice.send_alert("Viper遭到暴力破解,服务器地址可能已经暴露") context = data_return(301, BASEAUTH_MSG.get(301), null_response) return Response(context) except Exception as E: logger.error(E) context = data_return(301, BASEAUTH_MSG.get(301), null_response) return Response(context)
def send_text(self, text=None): if text: pass else: return False msg = {'text': text, 'desp': text} r = requests.post(self.url, headers=self.headers, data=msg, timeout=3) if r.status_code == 200: content = json.loads(r.content.decode('utf-8', 'ignore')) if content.get('data').get('error') != "SUCCESS": logger.warning("ServerChan 消息发送失败,错误码:{} 错误消息:{}".format(content.get('code'), content.get('message'))) Notice.send_alert( "ServerChan 消息发送失败,错误码:{} 错误消息:{}".format(content.get('code'), content.get('message'))) return False else: return True else: logger.warning("ServerChan 消息发送失败,HTTP状态码:{} 结果:{}".format(r.status_code, r.content)) return False
def load_all_modules_config(): all_modules_config = [] # viper 内置模块 viper_module_count = 0 modulenames = os.listdir(os.path.join(settings.BASE_DIR, 'MODULES')) for modulename in modulenames: modulename = modulename.split(".")[0] if modulename == "__init__" or modulename == "__pycache__": # __init__.py的特殊处理 continue class_intent = importlib.import_module(f'MODULES.{modulename}') try: if isinstance(class_intent.PostModule.ATTCK, str): attck = [class_intent.PostModule.ATTCK] elif isinstance(class_intent.PostModule.ATTCK, list): attck = [class_intent.PostModule.ATTCK] else: attck = [] one_module_config = { "BROKER": class_intent.PostModule.MODULE_BROKER, # 处理器 "NAME": class_intent.PostModule.NAME, "DESC": class_intent.PostModule.DESC, "WARN": class_intent.PostModule.WARN, "AUTHOR": class_intent.PostModule.AUTHOR, "REFERENCES": class_intent.PostModule.REFERENCES, "README": class_intent.PostModule.README, "MODULETYPE": class_intent.PostModule.MODULETYPE, "OPTIONS": class_intent.PostModule.OPTIONS, "loadpath": 'MODULES.{}'.format(modulename), # post类配置 "REQUIRE_SESSION": class_intent.PostModule.REQUIRE_SESSION, "PLATFORM": class_intent.PostModule.PLATFORM, "PERMISSIONS": class_intent.PostModule.PERMISSIONS, "ATTCK": attck, # bot类配置 "SEARCH": class_intent.PostModule.SEARCH, } all_modules_config.append(one_module_config) viper_module_count += 1 except Exception as E: logger.error(E) continue logger.warning("内置模块加载完成,加载{}个模块".format(viper_module_count)) Notice.send_success(f"内置模块加载完成,加载{viper_module_count}个模块") # 自定义模块 diy_module_count = 0 modulenames = os.listdir( os.path.join(settings.BASE_DIR, 'Docker', "module")) for modulename in modulenames: modulename = modulename.split(".")[0] if modulename == "__init__" or modulename == "__pycache__": # __init__.py的特殊处理 continue try: class_intent = importlib.import_module( 'Docker.module.{}'.format(modulename)) importlib.reload(class_intent) except Exception as E: logger.exception(E) Notice.send_alert(f"加载自定义模块:{modulename} 失败") continue try: if isinstance(class_intent.PostModule.ATTCK, str): attck = [class_intent.PostModule.ATTCK] elif isinstance(class_intent.PostModule.ATTCK, list): attck = [class_intent.PostModule.ATTCK] else: attck = [] one_module_config = { "BROKER": class_intent.PostModule.MODULE_BROKER, # 处理器 "NAME": class_intent.PostModule.NAME, "DESC": class_intent.PostModule.DESC, "WARN": class_intent.PostModule.WARN, "AUTHOR": class_intent.PostModule.AUTHOR, "REFERENCES": class_intent.PostModule.REFERENCES, "README": class_intent.PostModule.README, "MODULETYPE": class_intent.PostModule.MODULETYPE, "OPTIONS": class_intent.PostModule.OPTIONS, "loadpath": 'Docker.module.{}'.format(modulename), # post类配置 "REQUIRE_SESSION": class_intent.PostModule.REQUIRE_SESSION, "PLATFORM": class_intent.PostModule.PLATFORM, "PERMISSIONS": class_intent.PostModule.PERMISSIONS, "ATTCK": attck, # bot类配置 "SEARCH": class_intent.PostModule.SEARCH, } all_modules_config.append(one_module_config) diy_module_count += 1 except Exception as E: logger.error(E) continue logger.warning("自定义模块加载完成,加载{}个模块".format(diy_module_count)) Notice.send_success(f"自定义模块加载完成,加载{diy_module_count}个模块") all_modules_config.sort(key=lambda s: (TAG2CH.get_moduletype_order( s.get('MODULETYPE')), s.get('loadpath'))) if Xcache.update_moduleconfigs(all_modules_config): return len(all_modules_config) else: return 0