def post(self, request): username = request.POST.get("username") password = request.POST.get("password") sex = request.POST.get("sex") rept_password = request.POST.get("rept_password") remark = request.POST.get("remark") if password != rept_password: data = {"status": 403, "msg": "两个密码不一致"} return JsonResponse(data, safe=False) if Users.objects.filter(username=username).count() != 0: data = {"status": 403, "msg": "用户已存在"} return JsonResponse(data, safe=False) if sex == "1": sex = "男" elif sex == "0": sex = "女" Users.objects.create_user(id=get_uuid(), username=username, password=password, is_superuser=True, sex=sex, remark=remark) data = {"status": 200, "msg": "用户创建成功"} return JsonResponse(data, safe=False)
def post(self, request): category = request.POST.get("category", "") if len(category) == 0: data = {"status": 403, "msg": "请提供标签名"} return JsonResponse(data, safe=False) if Category.objects.filter(category_name=category).count() != 0: data = {"status": 403, "msg": "标签已存在"} return JsonResponse(data, safe=False) Category.objects.create(id=get_uuid(), category_name=category) data = {"status": 200, "msg": "标签创建成功"} return JsonResponse(data, safe=False)
class Users(AbstractUser): id = models.CharField(max_length=64, primary_key=True, default=get_uuid()) create_date = models.DateTimeField('创建时间', auto_now_add=True, null=True) nickname = models.CharField(max_length=32, blank=True, null=True, verbose_name='昵称') sex = models.CharField(max_length=8, default='男') is_superuser = models.BooleanField() remark = models.CharField(max_length=32, blank=True, null=True, verbose_name='备注') class Meta: verbose_name = '用户' verbose_name_plural = verbose_name ordering = ['-id'] def __str__(self): return f"<{self.username}> Model"
def post(self, request): username = request.POST.get("username") password = request.POST.get("password") rept_password = request.POST.get("rept_password") address = request.POST.get("school") if password != rept_password: data = {"status": 403, "msg": "两个密码不一致"} return JsonResponse(data, safe=False) if Players.objects.filter(username=username).count() > 0: data = {"status": 403, "msg": "用户已存在"} return JsonResponse(data, safe=False) Players.objects.create(id=get_uuid(), username=username, password=make_password(password), address=address) data = {"status": 200, "msg": "用户创建成功"} return JsonResponse(data, safe=False)
def post(request): username = request.POST.get("username", "") password = request.POST.get("password", "") confirm_password = request.POST.get("confirm_password", "") address = request.POST.get("school", "") captcha = request.POST.get("captcha", "") if len(captcha) == 0 or captcha.upper() != request.session.get( "valid_code", "").upper(): data = {"status": 403, "msg": "验证码错误"} return JsonResponse(data, safe=False) # 无论数据提交是否成功,都要在服务器端刷新一遍验证码 # Refresh(request) if len(username) == 0 or len(password) == 0 or len( confirm_password) == 0 or len(address) == 0: data = {"status": 403, "msg": "数据不能为空"} return JsonResponse(data) if Players.objects.filter(username=username).count() > 0: data = {"status": 403, "msg": "用户已存在"} return JsonResponse(data, safe=False) if password != confirm_password: data = {"status": 403, "msg": "密码不一致"} return JsonResponse(data, safe=False) Players.objects.create(id=get_uuid(), username=username, address=address, password=make_password(password=password)) data = {"status": 200, "msg": "注册成功"} return JsonResponse(data, safe=False)
def post(request): topic_title = request.POST.get("title", "") topic_image = request.POST.get("tag", "") topic_desc = request.POST.get("remark", "") topic_display = request.POST.get("display", "1") topic_category = request.POST.get("category", "") topic_score = request.POST.get("score", "") topic_flag = request.POST.get("flag", "") topic_ports = request.POST.get("ports", "") filename = request.POST.get("name", "") score_method = request.POST.get("method", "1") flag_method = request.POST.get("key", "1") # flag是动态还是静态 if topic_display == "1": display = True else: display = False if flag_method == "1": key = "static" else: key = "dynamic" # 分数是一成不变还是根据答题人数动态变换 if score_method == "1": method = True else: method = False if Category.objects.filter(id=topic_category).count() == 0: data = {"status": 403, "msg": "标签不存在"} return JsonResponse(data, safe=False) if TopicName.objects.filter(topic_name=topic_title).count() != 0: data = {"status": 403, "msg": "题目已存在"} return JsonResponse(data, safe=False) if TopicName.objects.filter(flag_string=topic_flag).count() != 0: data = {"status": 403, "msg": "flag已存在"} return JsonResponse(data, safe=False) if key == "static" and len(topic_flag) == 0: data = {"status": 403, "msg": "flag是静态必须提供flag字符串"} return JsonResponse(data, safe=False) category = Category.objects.filter(id=topic_category).first() if len(filename) == 0: file_path = "s1riu5" else: if filename[0:7] != "/static": data = {"status": 403, "msg": "文件路径错误"} return JsonResponse(data, safe=False) file_path = filename if len(topic_image) > 0: # 如果提供了镜像名称 TopicName.objects.create(id=get_uuid(), topic_name=topic_title, topic_description=topic_desc, image_tag=topic_image, display=display, score=topic_score, flag_string=topic_flag, inside_port=topic_ports, category=category, user=request.user, pull_status="Running", flag_type=key, score_type=method, upload_file=file_path) docker = DockerDeploy(name=topic_image) docker.start() else: TopicName.objects.create(id=get_uuid(), topic_name=topic_title, topic_description=topic_desc, display=display, score=topic_score, flag_string=topic_flag, inside_port=topic_ports, category=category, user=request.user, pull_status="Complete", flag_type=key, score_type=method, upload_file=file_path) data = {"status": 200, "msg": "创建成功"} return JsonResponse(data, safe=False)
def post(request): action = request.POST.get("action") images_id = request.POST.get("images_id") try: topic = TopicName.objects.get(id=images_id) except ObjectDoesNotExist: data = {"status": 403, "msg": "找不到数据"} return JsonResponse(data, safe=False) search_dict = {} if action == "start": if "," in topic.inside_port: port_list = [] port_pattern = re.compile("([0-9]+)") ports = port_pattern.findall(topic.inside_port) for i in ports: port = str(str2int(i)) if str2int(i) != 0 else "80" port_list.append(port) port_list = port_list else: port = str(str2int(topic.inside_port)) if str2int( topic.inside_port) != 0 else "80" port_list = [port] port_dict = {} out_list = [] for i in port_list: port_get = PORT_QUEUE.get() port_dict[i] = port_get out_list.append(str(port_get)) outside = ",".join(out_list) info = TopicName.objects.filter(image_tag=topic.image_tag).first() if info is None: data = {"status": 403, "msg": "无此镜像"} return JsonResponse(data, safe=False) user = str(request.user.username) if str(request.user.username) \ else request.session.get("user_name", "") search_dict['username'] = user search_dict['status'] = "Running" # 状态检查 result = Containers.objects.filter(**search_dict).count() if result > 0: run_list = [] result = Containers.objects.filter(**search_dict).all() for i in result: run_list.append(i.topic_name) result1 = ",".join(run_list) data = { "status": 403, "msg": "题目:{}已启动,请先关闭再启动此镜像".format(result1) } return JsonResponse(data, safe=False) try: if topic.flag_type == "status": # 如果flag是静态 con = DOCKER_CLIENT.containers.run(image=topic.image_tag, ports=port_dict, detach=True) Containers.objects.create(id=get_uuid(), username=user, contain=con.id, inside_port=topic.inside_port, outside_port=outside, topic_name=topic.topic_name, image_tag=topic.image_tag, status="Running") else: # 如果flag是动态 con = DOCKER_CLIENT.containers.run(image=topic.image_tag, ports=port_dict, detach=True) flag = generate_flag(user) contain_id = con.id # 运行容器中根目录下的start.sh文件修改或生成flag docker_container = DOCKER_CLIENT.containers.get(contain_id) command = "/bin/bash /start.sh '{}'".format(flag) docker_container.exec_run(cmd=command, detach=True) Containers.objects.create(id=get_uuid(), username=user, contain=contain_id, inside_port=topic.inside_port, outside_port=outside, topic_name=topic.topic_name, image_tag=topic.image_tag, status="Running", flag_string=flag) data = {"status": 200, "msg": str(port_dict)} return JsonResponse(data, safe=False) except Exception as e: data = {"status": 403, "msg": f"镜像启动失败,请稍后再试,{str(e)}"} return JsonResponse(data, safe=False) elif action == "destroy": search_dict = dict() search_dict['username'] = request.session.get( "user_name", "") if request.session.get( "user_name", "") else request.user.username search_dict['status'] = "Running" obj = TopicName.objects.filter(id=images_id).first().image_tag search_dict["image_tag"] = obj search_result = Containers.objects.filter(**search_dict).all() if len(search_result) > 0: two_result = Containers.objects.filter(**search_dict).first() two_result.status = "Stop" two_result.save() if "," in two_result.outside_port: outside = two_result.outside_port.split(",") else: outside = [two_result.outside_port] for i in outside: PORT_QUEUE.put_nowait(int(i)) # 启用子线程关闭容器 obj = ImageStop(two_result.contain) obj.start() data = {"status": 200, "msg": "容器已销毁"} return JsonResponse(data, safe=False) else: data = {"status": 403, "msg": "无启动容器"} return JsonResponse(data, safe=False)