Ejemplo n.º 1
0
    def _request(self, method, path, service=Services.API,
                 sign=True, params={}, args={}, body={}):
        """
        Perform API request.

        :param method: HTTP method ('GET', 'POST' etc.)
        :param path: URL to retrieve (e.g. '/v2/post-list')
        :param service: :class:`.Client.Services` field, default is `API`
        :param sign: Whether to sign the request or no.
        :param args: URL arguments (converted to weird form like `/count/10/type/hot/...`)
        :param query: Query args (converted to `?foo=bar&...`)
        :param body: Request body ('POST' requests only.)
        :returns: Decoded JSON response.
        :rtype: dict
        """
        url = '/'.join([
            service.strip('/'),
            path.strip('/'),
            '/'.join('{}/{}'.format(k, v) for k, v in args.items()).strip('/')
        ])

        headers = {
            '9GAG-9GAG_TOKEN': self.token,
            '9GAG-TIMESTAMP': str(utils.get_timestamp()),
            '9GAG-APP_ID': self.app_id,
            'X-Package-ID': self.app_id,
            '9GAG-DEVICE_UUID': self.device_uuid,
            'X-Device-UUID': self.device_uuid,
            '9GAG-DEVICE_TYPE': 'android',
            '9GAG-BUCKET_NAME': 'MAIN_RELEASE',
            }

        if sign:
            headers['9GAG-REQUEST-SIGNATURE'] = utils.sign_request(
                headers['9GAG-TIMESTAMP'],
                headers['9GAG-APP_ID'],
                headers['9GAG-DEVICE_UUID']
            )

        logging.debug('{} {}: {}\n{}'.format(
            method,
            url,
            body,
            '\n'.join(['{}: {}'.format(k, v) for k, v in headers.items()])
        ))

        if method.upper() == 'GET':
            response = requests.get(url, headers=headers, params=params)
        elif method.upper() == 'POST':
            response = requests.post(url, headers=headers, data=body, params=params)
        else:
            raise NotImplementedError('Only GET and POST methods are currently implemented.')

        data = loads(response.text)

        self._validate_response(data)

        return data
Ejemplo n.º 2
0
def auth_keyboard(user_id):
    markup = InlineKeyboardMarkup()
    sign = sign_request(user_id)
    url = ('http://www.last.fm/api/auth/'
           '?api_key=330cb02fdb1e20e1ac7c99f39b923a8f&cb='
           'https://static.138.197.203.116.clients.your-server.de/'
           f'deezer/lastfm?data={user_id}AAA{sign}')
    markup.add(InlineKeyboardButton(text='Authorize', url=url))
    return markup
Ejemplo n.º 3
0
    def post_add_operator(self):
        domain_id = self.json_args.get('domain_id')
        login = self.json_args.get('login')
        user_id = self.json_args.get('user_id')
        name = self.json_args.get('name')
        role = self.json_args.get('role')

        session = self.session('repo')

        try:
            if len(name) < 2 or len(name) > MAX_NAME:
                raise ValueError('名称长度不符合要求(2-%d字符) (%s)' % (MAX_NAME, name))

            if not login_re.match(login):
                raise ValueError('登录名不符合规则:小写字母或数字长度4~12位')

            exist_user = session.query(RepoUser).filter(
                RepoUser.domain_id == domain_id).filter(
                    RepoUser.user_id == user_id).first()
            if exist_user is None:
                raise ValueError('代理商不存在 (%s)' % user_id)

            exist_operator = session.query(RepoOperator).filter(
                RepoOperator.domain_id == domain_id).filter(
                    RepoOperator.login == login).first()
            if exist_operator:
                raise ValueError('代理商登录名已经存在 (%s)' % login)

            rand_pass = ''.join(
                random.sample(string.ascii_letters + string.digits, 6))
            signed = sign_request(rand_pass.encode())

            operator = RepoOperator()
            operator.domain_id = domain_id
            operator.user_id = user_id
            operator.login = login
            operator.name = name
            operator.password = signed
            operator.role = role
            operator.status = 'enabled'
            session.add(operator)

            session.commit()
            self.finish({'status': 'ok', 'msg': '创建成功', 'password': rand_pass})

            # sync user
            yield core.sync_user(session, domain_id)

        except ValueError as ve:
            self.finish({'status': 'fail', 'msg': str(ve)})

        except Exception as ee:
            request_log.exception('ADD USER')
            self.finish({'status': 'fail', 'msg': 'EXCEPTION'})

        finally:
            session.close()
Ejemplo n.º 4
0
	def test_correct_signature(self):

		result = {  "animal" : "testanimal",
					"distribution" : "testdistr",
					"version" : "1.2.3",
					"load" : "success",
					"install" : "success",
					"check" : "failure",
					"install_log" : "pgxnclient install logfile",
					"load_log" : "pgxnclient load logfile",
					"check_log" : "pgxnclient check logfile",
					"check_diff" : "pg_regress diff file" }


		result['signature'] = sign_request(data=result, secret='testsecret')

		# list distributions (there are none)
		response = self.app.post('/api/results', data=json.dumps(result), content_type='application/json')

		self.assertEqual(response.status_code, 200)
		self.assertEqual(len(json.loads(response.data)['uuid']), 36)
Ejemplo n.º 5
0
	def test_invalid_version(self):

		result = {  "animal" : "testanimal",
					"distribution" : "testdistr",
					"version" : "1.0.0",
					"load" : "success",
					"install" : "success",
					"check" : "failure",
					"install_log" : "pgxnclient install logfile",
					"load_log" : "pgxnclient load logfile",
					"check_log" : "pgxnclient check logfile",
					"check_diff" : "pg_regress diff file" }


		result['signature'] = sign_request(data=result, secret='testsecret')

		# list distributions (there are none)
		response = self.app.post('/api/results', data=json.dumps(result), content_type='application/json')

		self.assertEqual(response.status_code, 401)
		self.assertEqual(json.loads(response.data), {'message' : 'unknown version'})
Ejemplo n.º 6
0
async def auth_redirect(request: web.Request):
    data = request.query.get('data')
    if data:
        data = data.split('AAA', 1)
        if len(data) == 2:
            user_id, sign = data
    token = request.query.get('token')
    if not token or not user_id or not sign or \
            sign != sign_request(user_id):
        return web.HTTPForbidden()

    session = await api_request('GET', 'auth.getSession', token=token)
    await set_lastfm_token(user_id, session.session.key)

    await bot.send_message(
        int(user_id),
        'You successfuly authorized to use Last.fm '
        f'as {session.session.name}, '
        'reply /lastfm_scrobble to a song to scrobble it\n'
        'reply /lastfm_love to a song to love it on your account\n'
        'send /lastfm_logout to logout from current account')
    return web.HTTPFound('tg://resolve/?domain=DeezerMusicBot')
Ejemplo n.º 7
0
					logging.info("PostgreSQL cluster started, version = %(version)s" % {'version' : pgversion})

					# run the actual test
					result = test_release(dist['name'], version['version'], version['status'], logdir=args.logdir)

					# additional info, and a random UUID for the result (we're generating it here as a protection against simple replay attacks)
					result.update({'uuid' : str(uuid.uuid4()), 'machine' : args.name, 'config' : json.dumps(pginfo), 'env' : json.dumps({})})

					# there has to be a better way ... but well, this seems to work for now
					result['install_log'] = base64.b64encode(result['install_log'].encode('utf-8'))
					result['load_log'] = base64.b64encode(result['load_log'].encode('utf-8'))
					result['check_log'] = base64.b64encode(result['check_log'].encode('utf-8'))
					result['check_diff'] = base64.b64encode(result['check_diff'].encode('utf-8'))

					# sign the request with the shared secret
					result = sign_request(result, args.secret)

					# do the POST request (if OK, status is 200)
					(status, reason) = post_results(api_host, templates, result)

					if (status == 200):
						logging.info("POST OK : UUID='%(uuid)s' install=%(install)s load=%(load)s check=%(check)s" % {'uuid' : reason['uuid'], 'install' : result['install'], 'load' : result['load'], 'check' : result['check']})
					else:
						logging.error(reason)
						logging.error("POST for %(dist)s-%(version)s failed (status = %(status)d)" % {'dist' : dist['name'], 'version' : version['version'], 'status' : status})

				except Exception as ex:

					logging.info("testing failed: %(msg)s" % {'msg' : str(ex)})
					logging.exception(ex)
Ejemplo n.º 8
0
    def post_add_user(self):
        domain_id = self.json_args.get('domain_id')
        template_id = self.json_args.get('template_id')
        user_name = self.json_args.get('name')
        # request_id = self.json_args.get('request_id')
        login = self.json_args.get('login')
        level = self.json_args.get('plevel', '1')
        notes = self.json_args.get('notes', '')
        qq = self.json_args.get('qq', '')
        mobile = self.json_args.get('mobile', '')

        session = self.session('repo')

        try:
            if len(user_name) < 2 or len(user_name) > MAX_NAME:
                raise ValueError('代理商名称长度不符合要求(2-%d字符) (%s)' %
                                 (MAX_NAME, user_name))

            if not login_re.match(login):
                raise ValueError('登录名不符合规则:小写字母或数字长度4~12位')

            exist_user = session.query(RepoUser).filter(
                RepoUser.domain_id == domain_id).filter(
                    RepoUser.name == user_name).first()
            if exist_user:
                raise ValueError('代理商用户名已经存在 (%s)' % user_name)

            exist_operator = session.query(RepoOperator).filter(
                RepoOperator.domain_id == domain_id).filter(
                    RepoOperator.login == login).first()
            if exist_operator:
                raise ValueError('代理商登录名已经存在 (%s)' % login)

            q = session.query(RepoTemplate).filter(
                RepoTemplate.domain_id == domain_id).filter(
                    RepoTemplate.template_id == template_id)

            user_template = q.one()
            if user_template is None:
                raise ValueError('代理商模板异常')

            # Next UserID
            latest_user = session.query(RepoUser).filter(
                RepoUser.user_id >= user_template.user_id_start).filter(
                    RepoUser.user_id <= user_template.user_id_end).order_by(
                        desc(RepoUser.user_id)).first()

            if latest_user:
                if latest_user == user_template.user_id_end:
                    raise ValueError('用户数已超出限额,请联系开发人员!')

                user_id = str(int(latest_user.user_id) + 1)
            else:
                user_id = user_template.user_id_start

            user = RepoUser()
            user.user_id = user_id
            user.domain_id = domain_id
            user.name = user_name
            user.master_id = user_template.master_id or user_id
            user.shard_id = user_template.shard_id or user_id
            user.type = user_template.type
            user.password = gen_key(
                16, string.ascii_uppercase + string.ascii_lowercase +
                string.digits)
            user.secret = gen_key(
                32, string.ascii_uppercase + string.ascii_lowercase +
                string.digits)
            user.iv = gen_key(16, string.digits)
            user.back_url = user_template.back_url
            user.tags = get_initial(user_name)
            user.level = level
            user.prefix = user_template.prefix
            user.status = user_template.status
            user.create_time = dt.now()
            user.qq = qq
            user.mobile = mobile
            user.notes = notes
            user.services = user_template.services

            session.add(user)

            rand_pass = ''.join(
                random.sample(string.ascii_letters + string.digits, 6))
            signed = sign_request(rand_pass.encode())

            operator = RepoOperator()
            operator.domain_id = domain_id
            operator.user_id = user_id
            operator.login = login
            operator.name = user_name
            operator.password = signed
            operator.role = user_template.role
            operator.status = user_template.status
            session.add(operator)

            session.commit()
            self.finish({'status': 'ok', 'msg': 'OK', 'password': rand_pass})

            # sync user
            yield core.sync_user(session, domain_id)
            # sync pricing
            # yield core.sync_pricing(session, domain_id, filter_user=user_id)
            self.master.lpush(
                'list:sync:pricing',
                '{domain_id},{product_id},{user_id}'.format(
                    domain_id=domain_id, product_id='', user_id=user_id))

        except ValueError as ve:
            self.finish({'status': 'fail', 'msg': str(ve)})

        except Exception as ee:
            request_log.exception('ADD USER')
            self.finish({'status': 'fail', 'msg': 'EXCEPTION'})

        finally:
            session.close()
Ejemplo n.º 9
0
    def post_add_domain(self):
        domain_id = self.json_args.get('domain_id')
        domain_name = self.json_args.get('domain_name')
        title = self.json_args.get('title')
        hosts = self.json_args.get('hosts')
        up_user = self.json_args.get('up_user', None)
        id_start = self.json_args.get('id_start', None)
        id_end = self.json_args.get('id_end', None)
        template_list = self.json_args.get('template_list')
        up_domain_id = None
        db_up_user = None

        status = "enabled"

        session = self.session('repo')

        try:
            if up_user:
                # sub-domain
                db_up_user = session.query(RepoUser).filter(
                    RepoUser.user_id == up_user).first()
                if db_up_user is None:
                    raise RuntimeError('没有找到指定的上游用户 %s' % up_user)

                up_domain_id = db_up_user.domain_id

                # check start-end in up_domain
                db_up_domain = session.query(RepoDomain).filter(
                    RepoDomain.domain_id == up_domain_id).one()

                if id_start < db_up_domain.id_start or id_end > db_up_domain.id_end:
                    raise RuntimeError('子平台用户段需要在上级平台用户端内')

                # 用户不可重叠
                exist_domain = session.query(RepoDomain).filter(
                    RepoDomain.up_domain == up_domain_id).filter(
                        or_(between(RepoDomain.id_start, id_start, id_end),
                            between(RepoDomain.id_end, id_start,
                                    id_end))).first()

                if exist_domain:
                    raise RuntimeError(
                        '与同级平台的用户段重叠 %s(%s)' %
                        (exist_domain.domain_name, exist_domain.domain_id))

            else:
                raise RuntimeError('请选择一个上级用户')  # 暂时关闭顶级平台

                # top-level domain
                exist_domain = session.query(RepoDomain).filter(
                    or_(between(RepoDomain.id_start, id_start, id_end),
                        between(RepoDomain.id_end, id_start, id_end))).first()

                if exist_domain:
                    raise RuntimeError('用户段重叠 %s' % exist_domain.domain_id)

            domain = RepoDomain()
            domain.domain_id = domain_id
            domain.domain_name = domain_name
            domain.title = title
            domain.hosts = hosts
            domain.up_domain = up_domain_id
            domain.up_user = up_user
            domain.status = status
            domain.id_start = id_start
            domain.id_end = id_end
            domain.create_time = dt.now()

            session.add(domain)

            # add route/interface

            route = RepoRouteSupply()
            route.domain_id = domain_id
            route.name = '内部路由'
            route.area = 'CN'
            route.adapt_flag = 'yes'
            route.interfaces = 'quxun'
            route.status = 'enabled'
            route.create_time = dt.now()
            session.add(route)

            interface = RepoRouteInterface()
            interface.domain_id = domain_id
            interface.interface_id = 'quxun'
            interface.name = '<趣讯上游>'
            interface.carrier = '1,2,3'
            interface.area = 'CN'
            interface.create_time = dt.now()
            session.add(interface)

            # add user/operator

            user_id = id_start

            user = RepoUser()
            user.domain_id = domain_id
            user.user_id = user_id
            user.name = domain_name
            user.master_id = user_id
            user.shard_id = user_id
            user.type = db_up_user.type
            user.password = gen_key(
                16, string.ascii_uppercase + string.ascii_lowercase +
                string.digits)
            user.secret = gen_key(
                32, string.ascii_uppercase + string.ascii_lowercase +
                string.digits)
            user.iv = gen_key(16, string.digits)
            user.back_url = db_up_user.back_url
            user.tags = get_initial(domain_name)
            user.level = 1
            user.prefix = 'TB'
            user.status = 'enabled'
            user.services = db_up_user.services
            user.create_time = dt.now()

            session.add(user)

            rand_pass = ''.join(
                random.sample(string.ascii_letters + string.digits, 6))
            signed = sign_request(rand_pass.encode())

            admin_login = '******' + user_id

            operator = RepoOperator()
            operator.domain_id = domain_id
            operator.user_id = user_id
            operator.login = admin_login
            operator.name = domain_name + '管理员'
            operator.password = signed
            operator.role = 'down_admin'
            operator.status = 'enabled'

            session.add(operator)
            session.commit()

            # copy template
            if template_list:
                q = session.query(RepoTemplate).filter(
                    RepoTemplate.domain_id == up_domain_id)
                for template in q.all():
                    if template.template_id in template_list:
                        copy_template = RepoTemplate()
                        copy_template.domain_id = domain_id
                        copy_template.template_id = template.template_id
                        copy_template.template_name = template.template_name
                        copy_template.user_id_start = id_start
                        copy_template.user_id_end = id_end
                        copy_template.shard_id = user_id
                        copy_template.type = template.type
                        copy_template.back_url = template.back_url
                        copy_template.level = template.level
                        copy_template.prefix = template.prefix
                        copy_template.status = template.status
                        copy_template.services = template.services
                        copy_template.role = template.role
                        session.add(copy_template)

                session.commit()

            self.finish({
                'status':
                'ok',
                'msg': ('创建成功\n'
                        '管理用户名:%s\n'
                        '密码:%s\n'
                        '(!!这里是唯一记录密码的机会!!)') % (admin_login, rand_pass)
            })

        except Exception as e:
            self.finish({'status': 'fail', 'msg': '创建失败' + str(e)})

        finally:
            session.close()