def on_close(self): user_to_del = None for user, connection in FbtWebSocketHandler.waiters.iteritems(): if connection == self: user_to_del = user logging.info("detect websocket close:" + str(user_to_del)) break if user_to_del: del FbtWebSocketHandler.waiters[user_to_del] UserIPCache.delete_my_ip(user_to_del)
def test_download_ipv6(self): client = redis.StrictRedis() client.flushdb() r = RedisProxy(redis_client=client) UserIPCache.set_cache(r) UserIPCache.reset() online_users = [] # user offline my_uid = 12345 ret = DownloadMedium.get_matched_online_owners(my_uid, online_users, False, True) self.assertEqual(ret, {'owners': [], 'download_type': DownloadMedium.download_type["None"]}) # user online but has no owners ip = "1.1.1.1" UserIPCache.update_my_ip(my_uid, ip) ret = DownloadMedium.get_matched_online_owners(my_uid, online_users, False, True) self.assertEqual(ret, {'owners': [], 'download_type': DownloadMedium.download_type["None"]}) # user just see himself online_users = [my_uid, ] v6_ip = "2001::1" port = 8885 HttpServerInfoCache.update_ipv6_address(my_uid, v6_ip, port) ret = DownloadMedium.get_matched_online_owners(my_uid, online_users, False, False) self.assertEqual(ret, {'owners': [{'host': v6_ip, 'uid': my_uid, 'port': port}], 'download_type': DownloadMedium.download_type["V6"]}) ret = DownloadMedium.get_matched_online_owners(my_uid, online_users, False, True) self.assertEqual(ret, {'owners': [], 'download_type': DownloadMedium.download_type["None"]}) his_uid = 54321 his_ip = '2.2.2.2' UserIPCache.update_my_ip(his_uid, his_ip) his_v6_ip = "2001::2" his_port = 8886 HttpServerInfoCache.update_ipv6_address(his_uid, his_v6_ip, his_port) online_users.append(his_uid) ret = DownloadMedium.get_matched_online_owners(my_uid, online_users, False, True) self.assertEqual(ret, {'owners': [{'host': his_v6_ip, 'uid': his_uid, 'port': his_port}], 'download_type': DownloadMedium.download_type["V6"]}) her_uid = 56789 her_ip = '3.3.3.3' UserIPCache.update_my_ip(her_uid, her_ip) her_v6_ip = "2001::3" her_port = 8885 HttpServerInfoCache.update_ipv6_address(her_uid, her_v6_ip, her_port) online_users.append(her_uid) ret = DownloadMedium.get_matched_online_owners(my_uid, online_users, False, True) self.assertEqual(ret, {'owners': [{'host': his_v6_ip, 'uid': his_uid, 'port': his_port}, {'host': her_v6_ip, 'uid': her_uid, 'port': her_port}, ], 'download_type': DownloadMedium.download_type["V6"]})
def on_message(self, message): logging.info("got message %r", message) json_data = json.loads(message) if "uid" in json_data: user = json_data["uid"] logging.info("got message from uid:%r", user) try: user=int(user) UserIPCache.update_my_ip(user, self.request.remote_ip) #!!! record the user IP FbtWebSocketHandler.waiters[user] = self self.write_message(json.dumps({"err": 0})) except ValueError: self.write_message(json.dumps({"err": 2, "what": "uid is not number"})) else: self.write_message(json.dumps({"err": 1, "what": "json format error"}))
def test_download_ipv4_lan(self): client = redis.StrictRedis() client.flushdb() r = RedisProxy(redis_client=client) UserIPCache.set_cache(r) UserIPCache.reset() online_users = [] # user offline my_uid = 1234 ret = DownloadMedium.get_matched_online_owners(my_uid, online_users, False, True) self.assertEqual(ret, {'owners': [], 'download_type': DownloadMedium.download_type["None"]}) # user online but has no owners ip = "1.1.1.1" UserIPCache.update_my_ip(my_uid, ip) ret = DownloadMedium.get_matched_online_owners(my_uid, online_users, False, True) self.assertEqual(ret, {'owners': [], 'download_type': DownloadMedium.download_type["None"]}) # user just see himself online_users = [my_uid, ] lan_ip = "192.168.0.1" port = 8885 HttpServerInfoCache.update_ipv4_address(my_uid, lan_ip, port) ret = DownloadMedium.get_matched_online_owners(my_uid, online_users, False, False) self.assertEqual(ret, {'owners': [{'host': lan_ip, 'uid': my_uid, 'port': port}], 'download_type': DownloadMedium.download_type["V4_LAN"]}) ret = DownloadMedium.get_matched_online_owners(my_uid, online_users, False, True) self.assertEqual(ret, {'owners': [], 'download_type': DownloadMedium.download_type["None"]}) his_uid = 4321 UserIPCache.update_my_ip(his_uid, ip) his_lan_ip = "192.168.0.2" his_port = 8884 HttpServerInfoCache.update_ipv4_address(his_uid, his_lan_ip, his_port) online_users.append(his_uid) ret = DownloadMedium.get_matched_online_owners(my_uid, online_users, False, True) self.assertEqual(ret, {'owners': [{'host': his_lan_ip, 'uid': his_uid, 'port': his_port}], 'download_type': DownloadMedium.download_type["V4_LAN"]}) her_uid = 5678 UserIPCache.update_my_ip(her_uid, ip) her_lan_ip = "192.168.0.1" her_port = 8885 HttpServerInfoCache.update_ipv4_address(her_uid, her_lan_ip, her_port) online_users.append(her_uid) ret = DownloadMedium.get_matched_online_owners(my_uid, online_users, False, True) self.assertEqual(ret, {'owners': [{'host': his_lan_ip, 'uid': his_uid, 'port': his_port}, {'host': her_lan_ip, 'uid': her_uid, 'port': her_port}, ], 'download_type': DownloadMedium.download_type["V4_LAN"]})
def get(self): user_ip_list = UserIPCache.get_user_ip_list() if self.request.remote_ip in user_ip_list.values(): self.render( "user_ip_list.html", title="user ip list", items=user_ip_list) else: self.write("login with websocket first")
def get(self): if self.get_argument("key", "") == "fbt": user_ip_list = UserIPCache.get_user_ip_list() if not user_ip_list: return self.render("user_ip_list.html", title="user ip list", items=user_ip_list, online_cnt=len(user_ip_list)) else: raise tornado.web.HTTPError(404)
def get_online_file_owner(cls, my_uid, file_hash): assert len(file_hash) > 0 assert my_uid >= 0 online_owners = filter(UserIPCache.user_online, ResourcesCache.get_resource_owners(file_hash)) # if online_owners.count(my_uid): # remove myself # online_owners.remove(my_uid) if (UserIPCache.user_online(my_uid)): my_ip = UserIPCache.get_user_ip(my_uid) is_ipv4 = IP.is_valid_ipv4_address(my_ip) if is_ipv4: same_ip_users = filter(lambda user: UserIPCache.get_user_ip(user) == my_ip, online_owners) v4_owners = [user for user in same_ip_users if IP.is_valid_ipv4_address(HttpServerInfoCache.get_user_ipv4(user))] grep_owners = [{"uid": user, "host": HttpServerInfoCache.get_user_ipv4(user), "port": 8884} for user in v4_owners] return grep_owners else: v6_owners = [user for user in online_owners if IP.is_valid_ipv6_address(HttpServerInfoCache.get_user_ipv6(user))] grep_owners = [{"uid": user, "host": HttpServerInfoCache.get_user_ipv6(user), "port": 8886} for user in v6_owners] return grep_owners else: return []
def test_download(): UserIPCache.update_my_ip(1,"1.1.1.1") UserIPCache.update_my_ip(2,"2.2.2.2") UserIPCache.update_my_ip(3,"2.2.2.2") UserIPCache.update_my_ip(4,"2.2.2.2") #2,3,4 are the same IP UserIPCache.update_my_ip(5,"2:2:2::2") UserIPCache.update_my_ip(6,"2:2:2::3") UserIPCache.update_my_ip(7,"5:2:2::3") UserIPCache.update_my_ip(8,"5:4:2::3") HttpServerInfoCache.update_ipv4_address(1,'192.168.1.101') HttpServerInfoCache.update_ipv4_address(2,'192.168.1.103') HttpServerInfoCache.update_ipv4_address(3,'192.168.1.104') HttpServerInfoCache.update_ipv4_address(4,'192.168.1.105') HttpServerInfoCache.update_ipv6_address(5,'2:2:2::2') HttpServerInfoCache.update_ipv6_address(6,'2:2:2::3') HttpServerInfoCache.update_ipv6_address(7,"5:2:2::3") #7,8 are the same IP HttpServerInfoCache.update_ipv6_address(8,"5:4:2::3") # user 2,3,4 uploaded the same file ResourcesCache.user_upload_resource(2,"user2","file_hash2","test2.txt",1023,1,"test",0,1,3,"user2 uploaded file") ResourcesCache.user_upload_resource(3,"user3","file_hash2","test2.txt",1023,1,"test",0,1,3,"user3 uploaded file") ResourcesCache.user_upload_resource(4,"user4","file_hash2","test2.txt",1023,1,"test",0,1,3,"user4 uploaded file") # user 3,4 uploaded the same file ResourcesCache.user_upload_resource(3,"user3","file_hash3","test4.txt",1023,1,"test",0,1,3,"user3 uploaded file") ResourcesCache.user_upload_resource(4,"user4","file_hash3","test4.txt",1023,1,"test",0,1,3,"user4 uploaded file") # user 5,6,7 uploaded the same file ResourcesCache.user_upload_resource(5,"user5","file_hash5","test5.txt",10123,1,"test",0,1,3,"user5 uploaded file") ResourcesCache.user_upload_resource(6,"user6","file_hash5","test5.txt",10123,1,"test",0,1,3,"user6 uploaded file") ResourcesCache.user_upload_resource(7,"user7","file_hash5","test5.txt",10123,1,"test",0,1,3,"user7 uploaded file") assert DownloadMedium.get_online_file_owner(1,"file_hash2") == [] assert DownloadMedium.get_online_file_owner(1,"file_hash5") == [] assert DownloadMedium.get_online_file_owner(1,"file_hash_not_exist") == [] assert DownloadMedium.get_online_file_owner(8,"file_hash_not_exist") == [] assert DownloadMedium.get_online_file_owner(8,"file_hash2") == [] assert DownloadMedium.get_online_file_owner(2,"file_hash3") == [{"uid": 3, "host": '192.168.1.104', "port":8884},{"uid": 4, "host": '192.168.1.105', "port":8884}] assert DownloadMedium.get_online_file_owner(8,"file_hash5") == [{"uid": 5, "host": '2:2:2::2', "port":8886},{"uid": 6, "host": '2:2:2::3', "port":8886},{"uid": 7, "host": "5:2:2::3", "port":8886}] #if __name__ == "__main__": # test_download()
def get_matched_online_owners(cls, my_uid, res_users, allowV4Download=False, need_romove_self=False): assert my_uid > 0 online_owners = filter(UserIPCache.user_online, res_users) if need_romove_self: if online_owners.count(my_uid): # remove myself online_owners.remove(my_uid) if len(online_owners): if (UserIPCache.user_online(my_uid)): shouldCheckV4 = True my_ipv6_addrs = HttpServerInfoCache.get_user_ipv6(my_uid) if isinstance(my_ipv6_addrs, set): assert len(my_ipv6_addrs) > 0 v6_owners = [] for user in online_owners: v6_addrs = HttpServerInfoCache.get_user_ipv6(user) if isinstance(v6_addrs, set): for addr in v6_addrs: if isinstance(addr, Address): assert IP.is_valid_ipv6_address( addr.get_host()) v6_owners.append({ "uid": user, "host": addr.get_host(), "port": addr.get_port() }) else: assert IP.is_valid_ipv6_address(addr) v6_owners.append({ "uid": user, "host": addr, "port": 8886 }) if len(v6_owners): return { "owners": v6_owners, "download_type": cls.download_type["V6"] } elif not allowV4Download: shouldCheckV4 = False if shouldCheckV4: my_ipv4 = UserIPCache.get_user_ip(my_uid) assert IP.is_valid_ipv4_address(my_ipv4) same_ip_users = filter( lambda user: UserIPCache.get_user_ip(user) == my_ipv4, online_owners) if len(same_ip_users): # LAN USER v4_owners = [] for user in same_ip_users: addr = HttpServerInfoCache.get_user_ipv4(user) if addr is not None: if isinstance(addr, Address): if IP.is_valid_ipv4_address( addr.get_host()): v4_owners.append(user) else: if IP.is_valid_ipv4_address(addr): v4_owners.append(user) grep_owners = [] for user in v4_owners: addr = HttpServerInfoCache.get_user_ipv4(user) if addr is not None: if isinstance(addr, Address): grep_owners.append({ "uid": user, "host": addr.get_host(), "port": addr.get_port() }) else: grep_owners.append({ "uid": user, "host": addr, "port": 8884 }) return { "owners": grep_owners, "download_type": cls.download_type["V4_LAN"] } else: # NAT USER #V4 user cant download v6's resource return { "owners": [], "download_type": cls.download_type["None"] } # TODO # valid_v4_owners = [user for user in online_owners if # IP.is_valid_ipv4_address(UserIPCache.get_user_ip( # user))] #and not isinstance(HttpServerInfoCache.get_user_ipv6(user),set)] # grep_owners = [{"uid": user, "host": UserIPCache.get_user_ip(user), "port": 8884} for user in # valid_v4_owners] #CAUTION: if user has a external IP, he cant open local LAN server. SO there must use get_user_ip, but not get_user_ipv4 # return {"owners": grep_owners, "download_type": cls.download_type["V4_NAT"]} else: return { "owners": [], "download_type": cls.download_type["V4_NOT_ALLOW"] } else: return { "owners": [], "download_type": cls.download_type["None"] } else: return {"owners": [], "download_type": cls.download_type["None"]}
def get_matched_online_owners2(cls, my_uid, res_users, allowV4Download=False, need_romove_self=False): assert my_uid > 0 online_owners = filter(UserIPCache.user_online, res_users) if need_romove_self: if online_owners.count(my_uid): # remove myself online_owners.remove(my_uid) if len(online_owners): #if (UserIPCache.user_online(my_uid)): shouldCheckV4 = True # my_ipv6_addrs = HttpServerInfoCache.get_user_ipv6(my_uid) # if isinstance(my_ipv6_addrs, set): # assert len(my_ipv6_addrs) > 0 v6_owners = [] for user in online_owners: v6_addrs = HttpServerInfoCache.get_user_ipv6(user) if isinstance(v6_addrs, set): for addr in v6_addrs: if isinstance(addr, Address): assert IP.is_valid_ipv6_address(addr.get_host()) v6_owners.append({"uid": user, "host": addr.get_host(), "port": addr.get_port()}) else: assert IP.is_valid_ipv6_address(addr) v6_owners.append({"uid": user, "host": addr, "port": 8886}) if len(v6_owners): return {"owners": v6_owners, "download_type": cls.download_type["V6"]} elif not allowV4Download: shouldCheckV4 = False if shouldCheckV4: my_ipv4 = UserIPCache.get_user_ip(my_uid) assert IP.is_valid_ipv4_address(my_ipv4) same_ip_users = filter(lambda user: UserIPCache.get_user_ip(user) == my_ipv4, online_owners) if len(same_ip_users): # LAN USER v4_owners = [] for user in same_ip_users: addr = HttpServerInfoCache.get_user_ipv4(user) if addr is not None: if isinstance(addr, Address): if IP.is_valid_ipv4_address(addr.get_host()): v4_owners.append(user) else: if IP.is_valid_ipv4_address(addr): v4_owners.append(user) grep_owners = [] for user in v4_owners: addr = HttpServerInfoCache.get_user_ipv4(user) if addr is not None: if isinstance(addr, Address): grep_owners.append({"uid": user, "host": addr.get_host(), "port": addr.get_port()}) else: grep_owners.append({"uid": user, "host": addr, "port": 8884}) return {"owners": grep_owners, "download_type": cls.download_type["V4_LAN"]} else: # NAT USER #V4 user cant download v6's resource return {"owners": [], "download_type": cls.download_type["None"]} # TODO # valid_v4_owners = [user for user in online_owners if # IP.is_valid_ipv4_address(UserIPCache.get_user_ip( # user))] #and not isinstance(HttpServerInfoCache.get_user_ipv6(user),set)] # grep_owners = [{"uid": user, "host": UserIPCache.get_user_ip(user), "port": 8884} for user in # valid_v4_owners] #CAUTION: if user has a external IP, he cant open local LAN server. SO there must use get_user_ip, but not get_user_ipv4 # return {"owners": grep_owners, "download_type": cls.download_type["V4_NAT"]} else: return {"owners": [], "download_type": cls.download_type["V4_NOT_ALLOW"]} #else: # return {"owners": [], "download_type": cls.download_type["None"]} else: return {"owners": [], "download_type": cls.download_type["None"]}
def test_user_ip_cache(self): client = redis.StrictRedis() client.flushdb() r = RedisProxy(redis_client=client) user_ip_cache = UserIPCache() user_ip_cache.set_cache(r) user_ip_cache.reset() user = 1234 ip = "1.2.3.4" ip2 = user_ip_cache.get_user_ip(user) self.assertEqual(ip2, None) self.assertEqual(len(user_ip_cache.get_all_users()), 0) user_ip_cache.load() user_ip_list = user_ip_cache.get_user_ip_list() self.assertEqual(len(user_ip_list), 0) user_ip_cache.update_my_ip(user, ip) ip2 = user_ip_cache.get_user_ip(user) self.assertEqual(ip2, ip) self.assertEqual(len(user_ip_cache.get_all_users()), 1) user222 = 4321 ip222 = "43.21.21.12" user_ip_cache.update_my_ip(user222, ip222) self.assertEqual(len(user_ip_cache.get_all_users()), 2) user_ip_cache.delete_my_ip(user) ip2 = user_ip_cache.get_user_ip(user) self.assertEqual(ip2, None) self.assertEqual(len(user_ip_cache.get_all_users()), 1) user_ip_cache.load() user_ip_list = user_ip_cache.get_user_ip_list() self.assertEqual(len(user_ip_list), 1) user = 12345 ip = "1.2.3.5" user_ip_cache.update_my_ip(user, ip) user_ip_cache.reset() user_ip_cache.load() user_ip_list = user_ip_cache.get_user_ip_list() self.assertEqual(len(user_ip_list), 2)