async def post(self, request): email = request.form.get('email', '').strip().lower() passwd = request.form.get('passwd', '') repasswd = request.form.get('repasswd', '') res = {'ret': 0} if not re.compile(r'^[a-z_0-9.-]{1,64}@([a-z0-9-]{1,200}.){1,5}[a-z]{1,6}$').match(email): res['msg'] = '邮箱格式不正确' return json(res) if passwd != repasswd: res['msg'] = '两次密码不一致' return json(res) if not 6 <= len(passwd) <= 16: res['msg'] = '密码长度 6 ~ 16 位' return json(res) users = await User.objects.execute( User.select().where(User.email == email).limit(1) ) if len(users) > 0: res['msg'] = '邮箱已经被注册了' return json(res) max_port = 1024 try: users = await User.objects.execute( User.select().order_by(User.port.desc()).limit(1) ) if users: max_port = users[0].port except User.DoesNotExist: pass await User.objects.create( User, user_name='', email=email, password=User.hash_password(passwd), passwd=tools.random_string(6), port=max_port + 1, t=0, u=0, d=0, transfer_enable=tools.gb_to_byte(app.config.DEFAULT_TRAFFIC), invite_num=app.config.INVITE_NUM, ref_by=0, is_admin=0, reg_ip=request.ip[0] ) return json({'ret': 1, 'msg': '注册成功'})
async def index_view(request): user = request['user'] analytics = { 'total_user': await User.objects.count(User.select()), 'checkin_user': await User.objects.count(User.select().where(User.last_check_in_time > 0)), 'traffic_usage': tools.flow_auto_show(await User.objects.scalar( User.select(fn.Sum(User.u + User.d)))), 'online_user': await User.objects.count(User.select().where(User.t > 3600)), 'total_node': await SS_Node.objects.count(SS_Node.select()), } return render('admin_panel/index.html', request, user=user, **analytics)
async def users_view(request): user = request['user'] total = await User.objects.count(User.select()) page, per_page, offset = Pagination.get_page_args(request) users = await User.objects.execute(User.select().order_by( User.id.asc()).paginate(page, per_page)) Pagination._per_page = 20 pagination = Pagination(request, total=total, record_name='users') return render('admin_panel/user/index.html', request, user=user, users=users, pagination=pagination)
def changepassword(): """change admin account's password""" user = None while user is None: email = input('Email: ').strip() if email == '': continue try: user = User.get(User.email == email) except User.DoesNotExist: print(RED_COLOR + "Error: '%s' does not exist" % email + DEFAULT_COLOR, file=sys.stderr) print("Changing password for user '%s'" % user.email) password = None while password is None: password = getpass.getpass() password2 = getpass.getpass('Password (again): ') if password != password2: print(RED_COLOR + "Error: Your passwords didn't match." + DEFAULT_COLOR, file=sys.stderr) password = None # Don't validate passwords that don't match. continue if password.strip() == '': print(RED_COLOR + "Error: Blank passwords aren't allowed." + DEFAULT_COLOR, file=sys.stderr) password = None # Don't validate blank passwords. continue user.password = user.hash_password(password) try: user.save() except Exception as e: print(RED_COLOR + "Error: %s" % e + DEFAULT_COLOR, file=sys.stderr) else: print(GREEN_COLOR + "Password changed successfully." + DEFAULT_COLOR)
def createadmin(): """create admin account""" email = None while email is None: email = input('Email: ').strip() if email == '': email = None continue if User.select().where(User.email == email).exists(): print(RED_COLOR + "Error: '%s' is already exists." % email + DEFAULT_COLOR, file=sys.stderr) email = None password = None while password is None: password = getpass.getpass() password2 = getpass.getpass('Password (again): ') if password != password2: print(RED_COLOR + "Error: Your passwords didn't match." + DEFAULT_COLOR, file=sys.stderr) password = None # Don't validate passwords that don't match. continue if password.strip() == '': print(RED_COLOR + "Error: Blank passwords aren't allowed." + DEFAULT_COLOR, file=sys.stderr) password = None # Don't validate blank passwords. continue try: max_port = User.select().order_by(User.port.desc()).get().port except User.DoesNotExist: max_port = 1024 user = User() user.user_name = '' user.email = email user.password = user.hash_password(password) user.passwd = tools.random_string(6) user.port = max_port + 1 user.t = 0 user.u = 0 user.d = 0 user.transfer_enable = tools.gb_to_byte(app.config.DEFAULT_TRAFFIC) user.invite_num = app.config.INVITE_NUM user.ref_by = 0 user.is_admin = 1 try: user.save() except Exception as e: print(RED_COLOR + "Error: %s" % e + DEFAULT_COLOR, file=sys.stderr) else: print(GREEN_COLOR + "admin account created successfully." + DEFAULT_COLOR)