Esempio n. 1
0
 def get(self):
     user = get_current_user(self)
     if not user:
         user = self.get_argument("user", None)  # mock user since node client has no cookie
     if user:
         try:
             user=int(user)
         except ValueError:
             err = json.dumps({"err": 5, "what": "invalid uid"})
             self.write(err)
             return
         ip = self.get_argument("ip", None)
         port = self.get_argument("port", None)
         if ip and port:
             if port == '8884' and IP.is_valid_ipv4_address(ip):
                 HttpServerInfoCache.update_ipv4_address(user, ip)
                 ok = json.dumps({"err": 0})
                 self.write(ok)
             elif port == '8886' and IP.is_valid_ipv6_address(ip):
                 HttpServerInfoCache.update_ipv6_address(user, ip)
                 ok = json.dumps({"err": 0})
                 self.write(ok)
             else:
                 err = json.dumps({"err": 2, "what": "port number err or ip address format err. ip:"+ip+" port:"+port})
                 self.write(err)
         else:
             err = json.dumps({"err": 3, "what": "no ip or port"})
             self.write(err)
     else:
         err = json.dumps({"err": 1, "what": "login first"})
         self.write(err)
Esempio n. 2
0
 def get(self):
     if self.get_argument("key", "") == "fbt":
         http_server_info = HttpServerInfoCache.get_server_info()
         if not http_server_info:
             return
         self.render("http_server_info.html", server_info=http_server_info)
     else:
         raise tornado.web.HTTPError(404)
Esempio n. 3
0
 def get(self):
     if self.get_argument("key", "") == "fbt":
         http_server_info = HttpServerInfoCache.get_server_info()
         if not http_server_info:
             return
         self.render("http_server_info.html", server_info=http_server_info)
     else:
         raise tornado.web.HTTPError(404)
Esempio n. 4
0
 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 []
Esempio n. 5
0
    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"]})
Esempio n. 6
0
    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"]})
Esempio n. 7
0
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()
Esempio n. 8
0
 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"]}
Esempio n. 9
0
 def get(self):
     http_server_info = HttpServerInfoCache.get_server_info()
     self.render("http_server_info.html", server_info=http_server_info)
Esempio n. 10
0
 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"]}