def verify_login_rand_code(): req = request.get_json(True) phone_number = str(req.get('phone_number') or '') rand_code = str(req.get('code') or '') r = common.get_redis_conn() cached_key = common.make_login_cached_key(phone_number) cached_str = r.eval(LUA_CODE_FETCH_AND_DELETE,1, cached_key) if cached_str is None: return jsonify(code = common.ERROR_EXPIRED_LOGIN, message = 'expiry') cached_info = json.loads(cached_str) phone_number_id = cached_info.get('phone_number_id') uid = cached_info['uid'] if rand_code != cached_info.get('rand_code'): current_app.logger.debug('unmatched rand code<%s,%s>' % \ (rand_code, cached_info.get('rand_code'))) return jsonify(code=common.ERROR_RANDCODE_UNMATCHED) if phone_number != cached_info.get('phone_number'): current_app.logger.error('unmatched phone number<%s> <%s>' % \ (str(phone_number),cached_info.get('phone_number'))) return jsonify(code=common.ERROR_RANDCODE_EXCEPTION) if not isinstance(uid,(int,long)): current_app.logger.error('invalid uid<%s>' % str(uid)) return jsonify(code=common.ERROR_UNKNOWN_EXCEPTION) key_token = third_login.create_phone_key_token(phone_number_id, uid) return jsonify(code = 0, uid = uid, key_token = key_token)
def get_login_rand_code(): req = request.get_json(True) phone_number = str(req.get('phone_number') or '') if len(phone_number) < 3: current_app.logger.debug('invalid phone number <%s>' % phone_number) return jsonify(code=common.ERROR_REG_FAILED_TO_INVALID_PHONE) #check if this phone number has already been used or send a code to user's phone phone_number_id = common.make_phone_number_id(phone_number) user = User.query.filter_by(global_id=phone_number_id).first() if user is None: return jsonify(code=common.ERROR_PHONE_BEEN_EXISTING) uid = user.uid r = common.get_redis_conn() #check if that phone is already in checking cached_key = common.make_login_cached_key(phone_number) if r.get(cached_key): return jsonify(code=common.ERROR_RANDCODE_BEEN_SENT) #ok,send a random code in text message to user's phone rand_code = create_and_send_rand_code(phone_number) phone_number_id = common.make_phone_number_id(phone_number) cached_info = { 'phone_number' : phone_number, 'rand_code' : rand_code, 'phone_number_id': phone_number_id, 'uid' : uid, } #cache it r.setex(cached_key,json.dumps(cached_info),VERIFYING_RAND_CODE_LIVE_TIME) return jsonify(code = 0, message = 'please check your text message')
def get_user_info(): req = request.get_json(True) uid = int(req.get('uid')) user_info_key = common.make_user_info_key(uid) r = common.get_redis_conn() data = r.get(user_info_key) or '{}' return data
def create_phone_key_token(global_id, uid): r = common.get_redis_conn() raw_key = common.create_raw_key(global_id) cached_key = common.wrap_token(CHANNEL_PHONE, raw_key) cached_value = {'uid': uid, 'global_id': global_id} r.setex(cached_key, json.dumps(cached_value), SESSION_ALIVE_TIME) return cached_key
def verify_wechat(uid, code): current_app.logger.debug('verify_wechat=======<%d><%s>', uid, code) URL = WECHAT_LOGIN_URL % (WECHAT_APP_ID, WECHAT_APP_SECRET, code) current_app.logger.debug('before verify=======<%d><%s>,URL<%s>,<%s>', uid, code, URL, get_proxies()) r = requests.get(URL, proxies=get_proxies(), verify=False) r.encoding = 'utf-8' current_app.logger.debug('after verify=======<%d><%s>', uid, code) rsp = r.json() if rsp.has_key('errcode'): current_app.logger.error('got an error[%d] while checking code <%s>', (rsp.get('errcode'), code)) return { 'code': common.ERROR_UNEXPECTED_VERIFYING, 'message': 'failed to check' } access_token = rsp.get('access_token') refresh_token = rsp.get('refresh_token') open_id = rsp.get('openid') expires_in = rsp.get('expires_in') global_id = common.make_3rd_global_id(CHANNEL_WECHAT, open_id) new_uid = create_or_fetch_user(global_id) raw_key = common.create_raw_key(global_id) cached_key = common.wrap_token(CHANNEL_WECHAT, raw_key) cached_value = { 'access_token': access_token, 'refresh_token': refresh_token, 'global_id': global_id, 'uid': new_uid, 'expires_at': int(time.time()) + expires_in, 'open_id': open_id, } user_info_key = common.make_user_info_key(new_uid) user_info = {} try: user_info = update_wechat_user_info(access_token, open_id) except Exception as e: current_app.logger.error( 'failed to get user_info access_token<%s>, openid<%s>', access_token, open_id) current_app.logger.exception(e) r = common.get_redis_conn() r.eval(LUA_CODE_UPDATE_SESSION_AND_INFO, 2, cached_key, user_info_key, json.dumps(cached_value), json.dumps(user_info), SESSION_ALIVE_TIME) return {'code': 0, 'uid': new_uid, 'key_token': cached_key}
def register_phone_number(): req = request.get_json(True) phone_number = str(req.get('phone_number') or '') tourist_key_token = req.get('tourist_key_token') tourist_id = None if len(phone_number) < 3: current_app.logger.error('invalid phone number <%s>' % phone_number) return jsonify(code=common.ERROR_REG_FAILED_TO_INVALID_PHONE) if tourist_key_token is not None: tourist_id = common.get_tourist_id_from_token(tourist_key_token) if not common.is_tourist_id(tourist_id): current_app.logger.error('invalid tourist_id <%s>' % tourist_id) return jsonify(code=common.ERROR_REG_FAILED_TO_BIND_NOT_TOURIST) tourist_user = User.query.filter_by(global_id=tourist_id).first() if tourist_user is None: current_app.logger.error('no such tourist user <%s>' % tourist_id) return jsonify(code=common.ERROR_TOURIST_NO_FOUND) #check if this phone number has already been used or send a code to user's phone phone_number_id = common.make_phone_number_id(phone_number) record = User.query.filter_by(global_id=phone_number_id).first() if record is not None: return jsonify(code=common.ERROR_REG_FAILED_TO_PHONE_EXISTING) r = common.get_redis_conn() #check if that phone is already in checking cached_key = common.make_register_cached_key(phone_number) if r.get(cached_key): return jsonify(code=common.ERROR_RANDCODE_BEEN_SENT) r.delete(cached_key) #ok,send a random code in text message to user's phone rand_code = create_and_send_rand_code(phone_number) phone_number_id = common.make_phone_number_id(phone_number) cached_info = { 'phone_number' : phone_number, 'rand_code' : rand_code, 'phone_number_id': phone_number_id, 'tourist_key_token' : tourist_key_token } if tourist_id is not None: cached_info['tourist_id'] = tourist_id #cache it r.setex(cached_key,json.dumps(cached_info),VERIFYING_RAND_CODE_LIVE_TIME) return jsonify(code = 0, message = 'please check your text message')
def check_phone(uid, token): r = common.get_redis_conn() v = r.get(token) if v == None: return { 'code': common.ERROR_EXPIRED_LOGIN, 'message': 'relogin please' } cached_value = json.loads(v) if uid != cached_value.get('uid'): return { 'code': common.ERROR_UNMATCHED_UID, 'message': 'relogin please' } r.expire(token, SESSION_ALIVE_TIME) return {'code': 0, 'uid': uid, 'key_token': token}
def verify_register_rand_code(): req = request.get_json(True) phone_number = str(req.get('phone_number') or '') rand_code = str(req.get('code') or '') r = common.get_redis_conn() cached_key = common.make_register_cached_key(phone_number) cached_str = r.eval(LUA_CODE_FETCH_AND_DELETE,1, cached_key) if cached_str is None: return jsonify(code=common.ERROR_RANDCODE_BEEN_EXPIRED) r.delete(cached_key) cached_info = json.loads(cached_str) phone_number_id = cached_info.get('phone_number_id') if rand_code != cached_info.get('rand_code'): return jsonify(code=common.ERROR_RANDCODE_UNMATCHED) if phone_number != cached_info.get('phone_number'): return jsonify(code=common.ERROR_RANDCODE_EXCEPTION) #now insert of update the phone number tourist_id = cached_info.get('tourist_id') uid = None if tourist_id is not None: #update ... tourist_user = User.query.filter_by(global_id=tourist_id).first() if tourist_user is None: return jsonify(code=common.ERROR_TOURIST_NO_FOUND) tourist_user.global_id = phone_number_id db.session.add(tourist_user) db.session.commit() uid = tourist_user.uid else: #register now. user = User(global_id=phone_number_id, phone=phone_number, register_time=int(time.time())) db.session.add(user) db.session.commit() uid = user.uid key_token = third_login.create_phone_key_token(phone_number_id, uid) tourist_key_token = cached_info.get('tourist_key_token') or '' return jsonify(code = 0, uid = uid, key_token = key_token, tourist_key_token=tourist_key_token)
def check_tourist(uid, token): tourist_id = common.get_tourist_id_from_token(token) cached_key = common.wrap_token(CHANNEL_TOURIST, tourist_id) r = common.get_redis_conn() real_uid = r.get(cached_key) if real_uid is None: user = User.query.filter_by(global_id=tourist_id).first() if user is None: return { 'code': common.ERROR_TOURIST_NO_FOUND, 'message': 'no such tourist' } real_uid = user.uid r.setex(cached_key, real_uid, SESSION_ALIVE_TIME) else: real_uid = int(real_uid) if uid != real_uid: return {'code': common.ERROR_UNMATCHED_UID, 'message': 'unmatched uid'} return {'code': 0, 'uid': uid, 'key_token': token}
total = sum(rpc.getblock(rpc.getblockhash(i))["difficulty"] for i in range(lowest, blocks)) total /= (blocks - lowest) + 1 return round(total, 2) while True: try: # postgresql setup conn = get_postgres_conn() cur = conn.cursor() logger.info("Connected to postgresql") # set up redis red = get_redis_conn() red.ping() logger.info("Connected to redis") # rpc rpc = bitcoinrpc.connect_to_remote( config["dogecoindUser"], config["dogecoindPass"], config["dogecoindHost"], config["dogecoindPort"] ) # insert shares from redis to postgresql pubsub = red.pubsub() pubsub.subscribe("shares") for item in pubsub.listen(): if type(item["data"]) is bytes: data = json.loads(item["data"].decode("utf-8"))
def check_wechat(uid, token): r = common.get_redis_conn() v = r.get(token) if v == None: return { 'code': common.ERROR_EXPIRED_LOGIN, 'message': 'relogin please' } cached_value = json.loads(v) access_token = cached_value.get('access_token') refresh_token = cached_value.get('refresh_token') global_id = cached_value.get('global_id') uid = cached_value.get('uid') expires_at = cached_value.get('expires_at') open_id = cached_value.get('open_id') if int(time.time()) < expires_at: return {'code': 0, 'uid': uid, 'key_token': token} URL = WECHAT_REFRESH_URL % (WECHAT_APP_ID, refresh_token) #remote check or refresh the token... r = requests.get(URL, proxies=get_proxies(), verify=False) r.encoding = 'utf-8' rsp = r.json() if rsp.has_key('errcode'): current_app.logger.error('got an error[%d] while checking token <%s>', (rsp.get('errcode'), token)) return { 'code': common.ERROR_UNEXPECTED_VERIFYING, 'message': 'failed to refresh' } new_access_token = rsp.get('access_token') new_refresh_token = rsp.get('refresh_token') expires_in = rsp.get('expires_in') new_openid = rsp.get('openid') new_global_id = common.make_3rd_global_id(CHANNEL_WECHAT, new_openid) if new_global_id != global_id: return { 'code': common.ERROR_UNMATCHED_GID, 'message': 'failed to refresh' } cached_value['access_token'] = new_access_token cached_value['refresh_token'] = new_refresh_token cached_value['expires_at'] = int(time.time()) + expires_in user_info_key = common.make_user_info_key(uid) user_info = {} try: user_info = update_wechat_user_info(access_token, open_id) except Exception as e: current_app.logger.error( 'failed to get user_info access_token<%s>, openid<%s>', access_token, open_id) current_app.logger.exception(e) r = common.get_redis_conn() r.eval(LUA_CODE_UPDATE_SESSION_AND_INFO, 2, token, user_info_key, json.dumps(cached_value), json.dumps(user_info), SESSION_ALIVE_TIME) return {'code': 0, 'uid': uid, 'key_token': token}
def redis_proc(): logger = get_logger('db') while True: try: red = get_redis_conn() red.ping() logger.info('Connected to redis') pubsub = red.pubsub() pubsub.subscribe('shares') last_active_update = 0 active = {} for item in pubsub.listen(): if type(item['data']) is bytes: # sub, srv, diff, valid data = json.loads(item['data'].decode('utf-8')) # update server stats now = time.time() active[data['sub']] = now if now - last_active_update > 5 * 60: last_active_update = now for k, v in list(active.items()): if now - v > 5 * 60: del active[k] red.hset('stats:server', 'active', len(active)) red.hincrbyfloat('stats:server', 'shares', data['diff'] * 65536) stats_key = 'stats:' + data['sub'] hashrate_key = 'hashrate:' + data['sub'] def initalise_key(): pipe = red.pipeline() pipe.hset(stats_key, 'shares', 0) pipe.hset(stats_key, 'invalid_shares', 0) pipe.hset(stats_key, 'start_time', now) pipe.execute() start_time = now if not red.exists(stats_key): # initalise the data initalise_key() red.hset(stats_key, 'session_start_time', now) else: start_time = float(red.hget(stats_key, 'start_time')) time_diff = now - start_time # collected more than 5 minutes if time_diff > 5 * 60: pipe = red.pipeline() pipe.hget(stats_key, 'shares') pipe.hget(stats_key, 'invalid_shares') shares, invalid_shares = pipe.execute() shares, invalid_shares = float(shares), float(invalid_shares) pipe = red.pipeline() pipe.hset(stats_key, 'average_valid', shares / time_diff) pipe.hset(stats_key, 'average_invalid', invalid_shares / time_diff) pipe.hincrbyfloat(stats_key, 'total_shares', shares) pipe.hincrbyfloat(stats_key, 'total_invalid_shares', invalid_shares) pipe.lpush(hashrate_key, ','.join(map(str, [shares / time_diff, invalid_shares / time_diff, now]))) pipe.ltrim(hashrate_key, 0, 300) pipe.expire(hashrate_key, 3600 * 24) pipe.execute() initalise_key() else: if data['valid']: red.hincrbyfloat(stats_key, 'shares', data['diff'] * 65536) else: red.hincrbyfloat(stats_key, 'invalid_shares', data['diff'] * 65536) # 5 minute expiry red.expire(stats_key, 5 * 60) except Exception as e: logger.warning(e) logger.info('Exception: retrying in 5 seconds') time.sleep(5)
def __init__(self): self._red = get_redis_conn() self._pg = get_postgres_conn() self._logger = get_logger('web')
for i in range(lowest, blocks)) total /= (blocks - lowest) + 1 return round(total, 2) while True: try: # postgresql setup conn = get_postgres_conn() cur = conn.cursor() logger.info('Connected to postgresql') # set up redis red = get_redis_conn() red.ping() logger.info('Connected to redis') # rpc rpc = bitcoinrpc.connect_to_remote(config['dogecoindUser'], config['dogecoindPass'], config['dogecoindHost'], config['dogecoindPort']) # insert shares from redis to postgresql pubsub = red.pubsub() pubsub.subscribe('shares') for item in pubsub.listen(): if type(item['data']) is bytes:
def redis_proc(): logger = get_logger('db') while True: try: red = get_redis_conn() red.ping() logger.info('Connected to redis') pubsub = red.pubsub() pubsub.subscribe('shares') last_active_update = 0 active = {} for item in pubsub.listen(): if type(item['data']) is bytes: # sub, srv, diff, valid data = json.loads(item['data'].decode('utf-8')) # update server stats now = time.time() active[data['sub']] = now if now - last_active_update > 5 * 60: last_active_update = now for k, v in list(active.items()): if now - v > 5 * 60: del active[k] red.hset('stats:server', 'active', len(active)) red.hincrbyfloat('stats:server', 'shares', data['diff'] * 65536) stats_key = 'stats:' + data['sub'] hashrate_key = 'hashrate:' + data['sub'] def initalise_key(): pipe = red.pipeline() pipe.hset(stats_key, 'shares', 0) pipe.hset(stats_key, 'invalid_shares', 0) pipe.hset(stats_key, 'start_time', now) pipe.execute() start_time = now if not red.exists(stats_key): # initalise the data initalise_key() red.hset(stats_key, 'session_start_time', now) else: start_time = float(red.hget(stats_key, 'start_time')) time_diff = now - start_time # collected more than 5 minutes if time_diff > 5 * 60: pipe = red.pipeline() pipe.hget(stats_key, 'shares') pipe.hget(stats_key, 'invalid_shares') shares, invalid_shares = pipe.execute() shares, invalid_shares = float(shares), float( invalid_shares) pipe = red.pipeline() pipe.hset(stats_key, 'average_valid', shares / time_diff) pipe.hset(stats_key, 'average_invalid', invalid_shares / time_diff) pipe.hincrbyfloat(stats_key, 'total_shares', shares) pipe.hincrbyfloat(stats_key, 'total_invalid_shares', invalid_shares) pipe.lpush( hashrate_key, ','.join( map(str, [ shares / time_diff, invalid_shares / time_diff, now ]))) pipe.ltrim(hashrate_key, 0, 300) pipe.expire(hashrate_key, 3600 * 24) pipe.execute() initalise_key() else: if data['valid']: red.hincrbyfloat(stats_key, 'shares', data['diff'] * 65536) else: red.hincrbyfloat(stats_key, 'invalid_shares', data['diff'] * 65536) # 5 minute expiry red.expire(stats_key, 5 * 60) except Exception as e: logger.warning(e) logger.info('Exception: retrying in 5 seconds') time.sleep(5)