def modify_nginx_conf(request): from .file_config import NginxWAFConfFile data = request.GET if request.method == "GET" else request.data out_settings = origin_cfg.copy() if "init" in data.keys(): get_command("mv -f {nginx_conf} {nginx_conf}.bak").format( nginx_conf=NginxWAFConfFile) with open(NginxWAFConfFile, "w+", encoding="utf-8") as f: f.write(nginx_inital_conf()) f.close() return Response({"stat": "初始化成功", "reason": "初始化Nginx配置"}) ## 默认未开启ssl if "ssl" in data.keys(): out_settings["ssl"] = "" out_settings["no_ssl"] = "#" ## 默认未开启ssl if "no_ssl" in data.keys(): out_settings["ssl"] = "#" out_settings["no_ssl"] = "" ## 默认未开启防盗链 if "fdl" in data.keys(): out_settings["start_fdl"] = False ## 默认未开启 ddos if "ddos" in data.keys(): out_settings["limit_per_second"] = True ## 默认模板种类选择为2 if "tid" in data.keys(): out_settings["tid"] = int(data["tid"]) ## WAF 防护端口设置 if "local_server_port" in data.keys(): out_settings["local_server_port"] = int(data["local_server_port"]) ## 路径指向配置; 需严格安装 config 规定来 if "locations" in data.keys() and request.method == "POST": for x in data["locations"]: out_settings["location_cfgs"].append(x) try: return Response({"stat": "修改Nginx配置成功, 请稍后重启引擎使其生效", "params": data}) finally: with open(NginxWAFConfFile, "w+", encoding="utf-8") as f: f.write(nginx_inital_conf(origin_cfg=out_settings)) f.close()
def resave_scanner_data(res_datas): from wafmanage.dprocess import get_command res_str_eof = "&&&".join([ "# " + str(x[0]) if x[1] == False else str(x[0]) for x in res_datas ]) # get_command("cat > {file} <<-EOF\n{str}\nEOF ".format(file=File, str=res_str_eof)) # resave_command = "cat > {file} <<-EOF\n{str}".format(file=File, str=res_str_eof) resave_command = "echo '{str}' | sed 's/&&&//\/\n/g' > {file}".format( file=File, str=res_str_eof) get_command(resave_command) import logging logger = logging.getLogger('collect') logger.info("复位文件记录命令进行测试") logger.warn(resave_command)
def upgrade_view(request): data = request.GET if request.GET else request.data saved_path_filename = data["filename"] saved_file_path = os.path.join(FileUploadSaveDir, saved_path_filename) replace_and_upgrade_shell = os.path.join(UpgradShellDir, "upgrade.sh") version = data["version"] if "version" in data.keys() else VERSION cfg_type = data["cfg_type"] if "cfg_type" in data.keys() else "patch_rule" _local_params = upgrade_cfgs[cfg_type] # params = [saved_file_path, _local_params["gaim_path"], version, 1, "echo ok"] params = [ saved_file_path, _local_params["gaim_path"], version, '1', _local_params["sync_shell"] ] current_shell_cmd = "/bin/bash {bin_file} {params}".format( bin_file=replace_and_upgrade_shell, params=" ".join(params)) responsed_stdout = get_command(current_shell_cmd) response = dict( current_shell_cmd=current_shell_cmd, responsed_stdout=responsed_stdout, ) try: return Response(dict(response, stat=True)) except: return Response(dict(response, stat=False), status=400)
def delete_file_by_port(instance): pre_delete_filepath1 = os.path.join(NginxBaseDir, "vhost", str(instance.server_port)+".conf") from wafmanage.dprocess import get_command try: stdout = get_command("rm {path1}; echo 'delete {path1} ok';".format(path1 = pre_delete_filepath1) ) except: return 'file_not_found' return stdout
def update_hw_dt(request): ## {"systime":"2018-11-12 16:25:33"} if request.method == 'POST': str_dt = request.data["systime"] stats_str = [] import re datas_matched = re.match("(.*?)\s(.*)", str_dt) if datas_matched: date = datas_matched.group(1) time = datas_matched.group(2) get_command(modify_sys_datetime.format(date=date, time=time)) stats_str.append({"update_dt": date + " " + time}) else: return Response({"reson": "验证传入参数是否是 YYYY-MM-DD HHmmss 格式"}) return Response({"stat": "修改成功", "datas": stats_str}) if request.method == 'GET': return Response({"hw": get_command(hk_dt_show)})
def get_scanner_datas(): from wafmanage.dprocess import get_command lines = get_command("cat {}".format(File)).split("\r\n") res_datas = [] for line in lines: false_matched = re.match("\#\s(.*)", line) true_matched = re.match("(^[0-9a-zA-z].*)", line) if false_matched: res_datas.append((false_matched.group(1), False)) elif true_matched: res_datas.append((true_matched.group(1), True)) else: res_datas.append((None, False)) return res_datas
def test_file(request, File): # data = json.loads(request.body.decode()) def get_scanner_datas(): with open(File, "r+", encoding="utf-8") as f: lines = f.read().split("\n") f.close() res_datas = [] for line in lines: false_matched = re.match("\#\s(.*)", line) true_matched = re.match("(.*)", line) if false_matched: res_datas.append((false_matched.group(1), False)) elif true_matched: res_datas.append((true_matched.group(1), True)) else: res_datas.append((None, False)) return res_datas def resave_scanner_data(res_datas): with open(File, "w+", encoding="utf-8") as f: f.write("\n".join([ str("# " + x[0]) if x[1] == False else str(x[0]) for x in res_datas ])) f.close() if request.method == "GET": res_datas = get_scanner_datas() action_type = request.GET["type"] if "type" in request.GET.keys( ) else None if action_type: res_datas = get_scanner_datas() ## 增加元素 if action_type == "add": res_datas.append((request.GET["scanner"], True)) # stats = [res_datas.remove(x) for x in res_datas if x[0]["scanner"] == request.GET["scanner"] ] ## 初始化文件对象 if action_type == "inital": from wafmanage.dprocess import get_command get_command("rm -f {path} && mv {path}.bak {path}".format( path=ScriptUADatasFile)) return JsonResponse({"stat": True, "reason": "复位成功"}) ## 在列表中删除这个元素 if action_type == "delete": if "scanner" in request.GET.keys(): [ res_datas.remove(x) for x in res_datas if x[0] == request.GET["scanner"] ] ## 修改为真和假 if action_type == "update": gaim_stat = True if "retrue" in request.GET.keys() else False _new_datas = [] if "scanner" in request.GET.keys(): for x in res_datas: if x[0] != request.GET["scanner"]: _new_datas.append(x) else: _new_datas.append((x[0], gaim_stat)) res_datas = _new_datas if action_type == "download": return JsonResponse({"reson": "此处不提供下载;下载规则模板即可查看"}) try: return JsonResponse( {"datas": [x for x in res_datas if x[0] != ""]}) finally: resave_scanner_data(res_datas)
def test_file(request, File, CAT=True): def get_scanner_datas(): from wafmanage.dprocess import get_command lines = get_command("cat {}".format(File)).split("\r\n") res_datas = [] for line in lines: false_matched = re.match("\#\s(.*)", line) true_matched = re.match("(^[0-9a-zA-z].*)", line) if false_matched: res_datas.append((false_matched.group(1), False)) elif true_matched: res_datas.append((true_matched.group(1), True)) else: res_datas.append((None, False)) return res_datas def resave_scanner_data(res_datas): from wafmanage.dprocess import get_command res_str_eof = "&&&".join([ "# " + str(x[0]) if x[1] == False else str(x[0]) for x in res_datas ]) # get_command("cat > {file} <<-EOF\n{str}\nEOF ".format(file=File, str=res_str_eof)) # resave_command = "cat > {file} <<-EOF\n{str}".format(file=File, str=res_str_eof) resave_command = "echo '{str}' | sed 's/&&&//\/\n/g' > {file}".format( file=File, str=res_str_eof) get_command(resave_command) import logging logger = logging.getLogger('collect') logger.info("复位文件记录命令进行测试") logger.warn(resave_command) res_datas = get_scanner_datas() request_data = request.GET if request.method == "GET" else request.data action_type = request_data["type"] if "type" in request_data.keys( ) else None if action_type: # res_datas = get_scanner_datas() ## 增加元素 if action_type == "add": res_datas.append((request_data["scanner"], True)) # stats = [res_datas.remove(x) for x in res_datas if x[0]["scanner"] == request.GET["scanner"] ] ## 初始化文件对象 if action_type == "inital": from wafmanage.dprocess import get_command # get_command("rm -f {path} && mv {path}.bak {path}".format(path=File) ) inintal_file = os.path.join(DataInItalDir, str(File).split(SplitSigal)[-1]) get_command("rm -f {path} && cp {inintal_file} {path}".format( path=File, inintal_file=inintal_file)) return Response({"stat": True, "reason": "复位成功"}) ## 在列表中删除这个元素 if action_type == "delete": if "scanner" in request_data.keys(): [ res_datas.remove(x) for x in res_datas if x[0] == request_data["scanner"] ] ## 修改为真和假 if action_type == "update": gaim_stat = True if "retrue" in request_data.keys() else False _new_datas = [] if "scanner" in request_data.keys(): for x in res_datas: if x[0] != request_data["scanner"]: _new_datas.append(x) else: _new_datas.append((x[0], gaim_stat)) res_datas = _new_datas if action_type == "download": return Response({"reason": "此处不提供下载;下载规则模板即可查看"}) try: return Response({ "datas": [x for x in res_datas if x[0] != ""], "request_data": request_data }) finally: resave_scanner_data(res_datas)