home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / dl_daemon / natpunch.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  7.3 KB  |  324 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import socket
  5. from traceback import print_exc
  6. from subnetparse import IP_List
  7. from clock import clock
  8. from bittornadoinit import createPeerID
  9.  
  10. try:
  11.     True
  12. except:
  13.     True = 1
  14.     False = 0
  15.  
  16. DEBUG = False
  17. EXPIRE_CACHE = 30
  18. ID = 'BT-' + createPeerID()[-4:]
  19.  
  20. try:
  21.     import pythoncom
  22.     import win32com.client as win32com
  23.     _supported = 1
  24. except ImportError:
  25.     _supported = 0
  26.  
  27.  
  28. class _UPnP1:
  29.     
  30.     def __init__(self):
  31.         self.map = None
  32.         self.last_got_map = -1e+11
  33.  
  34.     
  35.     def _get_map(self):
  36.         if self.last_got_map + EXPIRE_CACHE < clock():
  37.             
  38.             try:
  39.                 dispatcher = win32com.client.Dispatch('HNetCfg.NATUPnP')
  40.                 self.map = dispatcher.StaticPortMappingCollection
  41.                 self.last_got_map = clock()
  42.             self.map = None
  43.  
  44.         
  45.         return self.map
  46.  
  47.     
  48.     def test(self):
  49.         
  50.         try:
  51.             if not self._get_map():
  52.                 raise AssertionError
  53.             success = True
  54.         except:
  55.             success = False
  56.  
  57.         return success
  58.  
  59.     
  60.     def open(self, ip, p):
  61.         map = self._get_map()
  62.         
  63.         try:
  64.             map.Add(p, 'TCP', p, ip, True, ID)
  65.             if DEBUG:
  66.                 print 'port opened: ' + ip + ':' + str(p)
  67.             
  68.             success = True
  69.         except:
  70.             if DEBUG:
  71.                 print "COULDN'T OPEN " + str(p)
  72.                 print_exc()
  73.             
  74.             success = False
  75.  
  76.         return success
  77.  
  78.     
  79.     def close(self, p):
  80.         map = self._get_map()
  81.         
  82.         try:
  83.             map.Remove(p, 'TCP')
  84.             success = True
  85.             if DEBUG:
  86.                 print 'port closed: ' + str(p)
  87.         except:
  88.             if DEBUG:
  89.                 print 'ERROR CLOSING ' + str(p)
  90.                 print_exc()
  91.             
  92.             success = False
  93.  
  94.         return success
  95.  
  96.     
  97.     def clean(self, retry = False):
  98.         if not _supported:
  99.             return None
  100.         
  101.         
  102.         try:
  103.             map = self._get_map()
  104.             ports_in_use = []
  105.             for i in xrange(len(map)):
  106.                 
  107.                 try:
  108.                     mapping = map[i]
  109.                     port = mapping.ExternalPort
  110.                     prot = str(mapping.Protocol).lower()
  111.                     desc = str(mapping.Description).lower()
  112.                 except:
  113.                     port = None
  114.  
  115.                 if port and prot == 'tcp' and desc[:3] == 'bt-':
  116.                     ports_in_use.append(port)
  117.                     continue
  118.             
  119.             success = True
  120.             for port in ports_in_use:
  121.                 
  122.                 try:
  123.                     map.Remove(port, 'TCP')
  124.                 continue
  125.                 success = False
  126.                 continue
  127.  
  128.             
  129.             if not success and not retry:
  130.                 self.clean(retry = True)
  131.         except:
  132.             pass
  133.  
  134.  
  135.  
  136.  
  137. class _UPnP2:
  138.     
  139.     def __init__(self):
  140.         self.services = None
  141.         self.last_got_services = -1e+11
  142.  
  143.     
  144.     def _get_services(self):
  145.         if not (self.services) or self.last_got_services + EXPIRE_CACHE < clock():
  146.             self.services = []
  147.             
  148.             try:
  149.                 f = win32com.client.Dispatch('UPnP.UPnPDeviceFinder')
  150.                 for t in ('urn:schemas-upnp-org:service:WANIPConnection:1', 'urn:schemas-upnp-org:service:WANPPPConnection:1'):
  151.                     
  152.                     try:
  153.                         conns = f.FindByType(t, 0)
  154.                         for c in xrange(len(conns)):
  155.                             
  156.                             try:
  157.                                 svcs = conns[c].Services
  158.                                 for s in xrange(len(svcs)):
  159.                                     
  160.                                     try:
  161.                                         self.services.append(svcs[s])
  162.                                     continue
  163.                                     continue
  164.  
  165.                             continue
  166.                             continue
  167.  
  168.                     continue
  169.                     continue
  170.  
  171.             except:
  172.                 pass
  173.  
  174.             self.last_got_services = clock()
  175.         
  176.         return self.services
  177.  
  178.     
  179.     def test(self):
  180.         
  181.         try:
  182.             if not self._get_services():
  183.                 raise AssertionError
  184.             success = True
  185.         except:
  186.             success = False
  187.  
  188.         return success
  189.  
  190.     
  191.     def open(self, ip, p):
  192.         svcs = self._get_services()
  193.         success = False
  194.         for s in svcs:
  195.             
  196.             try:
  197.                 s.InvokeAction('AddPortMapping', [
  198.                     '',
  199.                     p,
  200.                     'TCP',
  201.                     p,
  202.                     ip,
  203.                     True,
  204.                     ID,
  205.                     0], '')
  206.                 success = True
  207.             continue
  208.             continue
  209.  
  210.         
  211.         if DEBUG and not success:
  212.             print "COULDN'T OPEN " + str(p)
  213.             print_exc()
  214.         
  215.         return success
  216.  
  217.     
  218.     def close(self, p):
  219.         svcs = self._get_services()
  220.         success = False
  221.         for s in svcs:
  222.             
  223.             try:
  224.                 s.InvokeAction('DeletePortMapping', [
  225.                     '',
  226.                     p,
  227.                     'TCP'], '')
  228.                 success = True
  229.             continue
  230.             continue
  231.  
  232.         
  233.         if DEBUG and not success:
  234.             print "COULDN'T OPEN " + str(p)
  235.             print_exc()
  236.         
  237.         return success
  238.  
  239.  
  240.  
  241. class _UPnP:
  242.     
  243.     def __init__(self):
  244.         self.upnp1 = _UPnP1()
  245.         self.upnp2 = _UPnP2()
  246.         self.upnplist = (None, self.upnp1, self.upnp2)
  247.         self.upnp = None
  248.         self.local_ip = None
  249.         self.last_got_ip = -1e+11
  250.  
  251.     
  252.     def get_ip(self):
  253.         if self.last_got_ip + EXPIRE_CACHE < clock():
  254.             local_ips = IP_List()
  255.             local_ips.set_intranet_addresses()
  256.             
  257.             try:
  258.                 for info in socket.getaddrinfo(socket.gethostname(), 0):
  259.                     self.local_ip = info[4][0]
  260.                     if local_ips.includes(self.local_ip):
  261.                         self.last_got_ip = clock()
  262.                         if DEBUG:
  263.                             print 'Local IP found: ' + self.local_ip
  264.                         
  265.                         break
  266.                         continue
  267.                 else:
  268.                     raise ValueError("couldn't find intranet IP")
  269.             self.local_ip = None
  270.             if DEBUG:
  271.                 print 'Error finding local IP'
  272.                 print_exc()
  273.             
  274.  
  275.         
  276.         return self.local_ip
  277.  
  278.     
  279.     def test(self, upnp_type):
  280.         if DEBUG:
  281.             print 'testing UPnP type ' + str(upnp_type)
  282.         
  283.         if not upnp_type and not _supported or self.get_ip() is None:
  284.             if DEBUG:
  285.                 print 'not supported'
  286.             
  287.             return 0
  288.         
  289.         pythoncom.CoInitialize()
  290.         self.upnp = self.upnplist[upnp_type]
  291.         if self.upnp.test():
  292.             if DEBUG:
  293.                 print 'ok'
  294.             
  295.             return upnp_type
  296.         
  297.         if DEBUG:
  298.             print 'tested bad'
  299.         
  300.         return 0
  301.  
  302.     
  303.     def open(self, p):
  304.         if not self.upnp:
  305.             raise AssertionError, 'must run UPnP_test() with the desired UPnP access type first'
  306.         return self.upnp.open(self.get_ip(), p)
  307.  
  308.     
  309.     def close(self, p):
  310.         if not self.upnp:
  311.             raise AssertionError, 'must run UPnP_test() with the desired UPnP access type first'
  312.         return self.upnp.close(p)
  313.  
  314.     
  315.     def clean(self):
  316.         return self.upnp1.clean()
  317.  
  318.  
  319. _upnp_ = _UPnP()
  320. UPnP_test = _upnp_.test
  321. UPnP_open_port = _upnp_.open
  322. UPnP_close_port = _upnp_.close
  323. UPnP_reset = _upnp_.clean
  324.