NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

sites.py (16542B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\carbonui\control\browser\sites.py
      2 import cPickle
      3 import hashlib
      4 import urlparse
      5 import urllib
      6 import os
      7 from eveexceptions import UserError
      8 from fnmatch import fnmatch
      9 import blue
     10 import log
     11 import utillib
     12 from carbon.common.script.sys.service import Service
     13 
     14 class SitesSvc(Service):
     15     __guid__ = 'svc.sites'
     16     __exportedcalls__ = {'GetBookmarks': [],
     17      'AddBookmark': [],
     18      'RemoveBookmark': [],
     19      'GetTrustedSites': [],
     20      'AddTrustedSite': [],
     21      'RemoveTrustedSite': [],
     22      'AddIgnoredSite': [],
     23      'IsTrusted': [],
     24      'IsIgnored': [],
     25      'OnBrowserLockdownChange': [],
     26      'GetBrowserWhitelist': [],
     27      'GetBrowserBlacklist': [],
     28      'GetBannedInChatList': [],
     29      'OnFlaggedListsChange': []}
     30     __notifyevents__ = ['OnBrowserLockdownChange', 'OnFlaggedListsChange']
     31     __dependencies__ = ['objectCaching']
     32 
     33     def Run(self, *etc):
     34         Service.Run(self, *etc)
     35         browserSettingsPath = blue.paths.ResolvePathForWriting(u'settings:/Browser/')
     36         if not os.path.exists(browserSettingsPath):
     37             try:
     38                 os.makedirs(browserSettingsPath)
     39             except OSError as e:
     40                 self.LogError('SitesSvc.Run: Error creating the browser settings folder')
     41 
     42         self.isBrowserInLockdown = None
     43         self.bookmarksFile = browserSettingsPath + 'bookmarks.dat'
     44         self.trustedFile = browserSettingsPath + 'trustedSites.dat'
     45         self.ignoredFile = browserSettingsPath + 'ignoredSites.dat'
     46         self.flaggedSitesFile = blue.paths.ResolvePathForWriting(u'cache:/Browser/flaggedSites.dat')
     47         self.systemLists = None
     48         self.trustedSites = None
     49         self.BLACKLIST_FLAG = 1
     50         self.WHITELIST_FLAG = 2
     51         self.COMMUNITY_FLAG = 4
     52         self.AUTOTRUSTED_FLAG = 8
     53         self.LoadBookmarks()
     54 
     55     def IsTrusted(self, url):
     56         splitUrl = urlparse.urlsplit(url)
     57         urlProtocol = splitUrl.scheme
     58         urlDomain = splitUrl.netloc
     59         urlPath = splitUrl.path
     60         trustedList = self.GetTrustedSiteList()
     61         for trustUrl, trustData in trustedList.iteritems():
     62             if trustData.trusted and self.Matches(trustData.protocol, trustData.hostname, trustData.path, urlProtocol, urlDomain, urlPath):
     63                 return True
     64 
     65         return False
     66 
     67     def Matches(self, trustProtocol, trustDomain, trustPath, urlProtocol, urlDomain, urlPath):
     68         if trustProtocol and trustProtocol != urlProtocol:
     69             return False
     70         if not fnmatch(urlPath, trustPath):
     71             return False
     72         if not fnmatch(urlDomain, trustDomain):
     73             if not (trustDomain.startswith('*.') and fnmatch(urlDomain, trustDomain[2:])):
     74                 return False
     75         return True
     76 
     77     def IsIgnored(self, url):
     78         splitUrl = urlparse.urlsplit(url)
     79         urlProtocol = splitUrl.scheme
     80         urlDomain = splitUrl.hostname
     81         urlPath = splitUrl.path
     82         trustedList = self.GetTrustedSiteList()
     83         for trustUrl, trustData in trustedList.iteritems():
     84             if trustData.trusted == 0 and self.Matches(trustData.protocol, trustData.hostname, trustData.path, urlProtocol, urlDomain, urlPath):
     85                 return True
     86 
     87         return False
     88 
     89     def SaveSiteList(self, fileName, siteList):
     90         f = blue.ResFile()
     91         try:
     92             f.Create(fileName)
     93             f.Write(cPickle.dumps(siteList))
     94             f.Close()
     95         except:
     96             self.LogError('SitesSvc.SaveSiteList: Error saving to ', fileName)
     97 
     98     def GetBookmarks(self):
     99         return self.bookmarkedSites
    100 
    101     def MakeBookmarkEntry(self, name, url):
    102         return utillib.KeyVal(name=name, url=url)
    103 
    104     def RemoveBookmark(self, urlKV):
    105         self.bookmarkedSites.remove(urlKV)
    106         self.SaveBookmarks()
    107 
    108     def AddBookmark(self, name, url):
    109         self.bookmarkedSites.append(self.MakeBookmarkEntry(name, url))
    110         self.SaveBookmarks()
    111 
    112     def EditBookmark(self, oldEntry, name, url):
    113         for each in self.bookmarkedSites:
    114             if each == oldEntry:
    115                 oldEntry.name = name
    116                 oldEntry.url = url
    117 
    118         self.SaveBookmarks()
    119 
    120     def LoadBookmarks(self):
    121         self.bookmarkedSites = None
    122         f = blue.ResFile()
    123         if f.Open(self.bookmarksFile):
    124             self.bookmarkedSites = cPickle.loads(f.Read())
    125             f.Close()
    126         if not self.bookmarkedSites:
    127             self.bookmarkedSites = []
    128 
    129     def SaveBookmarks(self):
    130         self.SaveSiteList(self.bookmarksFile, self.bookmarkedSites)
    131 
    132     def ReformatUrlForTrustList(self, url):
    133         if not url:
    134             return None
    135         splitUrl = urlparse.urlsplit(url)
    136         cleanedUrl = ''
    137         scheme = urllib.quote(splitUrl.scheme.strip())
    138         if scheme == 'http' or scheme == 'https':
    139             cleanedUrl = '%s://' % scheme
    140         elif scheme:
    141             return None
    142         netloc = splitUrl.netloc.strip()
    143         try:
    144             netloc = str(netloc)
    145         except UnicodeEncodeError:
    146             netloc = urllib.quote(netloc, safe=':@*')
    147 
    148         path = splitUrl.path.strip()
    149         path = urllib.quote(path, safe='/*')
    150         if netloc == '' and path != '':
    151             if path.startswith('.'):
    152                 cleanedUrl += '*'
    153             cleanedUrl += path
    154             if not cleanedUrl.endswith('/') and not cleanedUrl.endswith('/*'):
    155                 cleanedUrl += '/'
    156         else:
    157             if netloc.startswith('.'):
    158                 cleanedUrl += '*'
    159             cleanedUrl += netloc
    160             if path == '':
    161                 cleanedUrl += '/'
    162             else:
    163                 cleanedUrl += path
    164         if cleanedUrl.endswith('/'):
    165             cleanedUrl += '*'
    166         return cleanedUrl
    167 
    168     def GetTrustedSites(self):
    169         trusted = {}
    170         trustedList = self.GetTrustedSiteList()
    171         for site, siteData in trustedList.iteritems():
    172             if siteData.trusted:
    173                 trusted[site] = siteData
    174 
    175         return trusted
    176 
    177     def GetIgnoredSites(self):
    178         ignored = {}
    179         trustedList = self.GetTrustedSiteList()
    180         for site, siteData in trustedList.iteritems():
    181             if not siteData.trusted:
    182                 ignored[site] = siteData
    183 
    184         return ignored
    185 
    186     def AddTrustedSite(self, s, store = 1):
    187         if not s:
    188             return
    189         s = self.ReformatUrlForTrustList(s)
    190         if s is None:
    191             raise UserError('CannotTrustInvalidUrl')
    192         temp_s = s
    193         checkProtocol = True
    194         if s.find('://') == -1:
    195             temp_s = 'http://' + s
    196             checkProtocol = False
    197         splitUrl = urlparse.urlsplit(temp_s)
    198         for trustUrl, trustData in self.trustedSites.iteritems():
    199             if trustData.auto and self.Matches(trustData.protocol, trustData.hostname, trustData.path, splitUrl.scheme, splitUrl.netloc, splitUrl.path):
    200                 raise UserError('CannotTrustCCPTrusted')
    201 
    202         val = self.GetSiteKey()
    203         val.trusted = 1
    204         val.temporary = store ^ 1
    205         val.protocol = splitUrl.scheme if checkProtocol else ''
    206         val.hostname = splitUrl.netloc
    207         val.path = splitUrl.path
    208         self.trustedSites[s] = val
    209         sm.ScatterEvent('OnTrustedSitesChange')
    210         if store:
    211             self.SaveTrusted()
    212 
    213     def RemoveTrustedSite(self, s):
    214         if s in self.trustedSites:
    215             if self.trustedSites[s].auto:
    216                 return
    217             del self.trustedSites[s]
    218             self.SaveTrusted()
    219             sm.ScatterEvent('OnTrustedSitesChange')
    220 
    221     def ToggleTrust(self, s):
    222         if s in self.trustedSites:
    223             self.trustedSites[s].trusted ^= 1
    224             sm.ScatterEvent('OnTrustedSitesChange')
    225             self.SaveTrusted()
    226 
    227     def AddIgnoredSite(self, s):
    228         if not s:
    229             return
    230         s = self.ReformatUrlForTrustList(s)
    231         if not s:
    232             raise UserError('CannotTrustInvalidUrl')
    233         splitUrl = urlparse.urlsplit(s)
    234         for trustUrl, trustData in self.trustedSites.iteritems():
    235             if trustData.auto and self.Matches(trustData.protocol, trustData.hostname, trustData.path, splitUrl.scheme, splitUrl.netloc, splitUrl.path):
    236                 raise UserError('CannotIgnoreCCPTrusted')
    237 
    238         val = self.GetSiteKey()
    239         val.protocol = splitUrl.scheme
    240         val.hostname = splitUrl.netloc
    241         val.path = splitUrl.path
    242         self.trustedSites[s] = val
    243         self.SaveTrusted()
    244         sm.ScatterEvent('OnTrustedSitesChange')
    245 
    246     def GetSiteKey(self):
    247         return utillib.KeyVal(trusted=0, temporary=0, auto=0, community=0, protocol='', hostname='', path='')
    248 
    249     def GetTrustedSiteList(self):
    250         if self.trustedSites is None:
    251             ret = {}
    252             trusted = []
    253             ignored = []
    254             f = blue.ResFile()
    255             if f.Open(self.trustedFile):
    256                 trusted = cPickle.loads(f.Read())
    257                 f.Close()
    258             if f.Open(self.ignoredFile):
    259                 ignored = cPickle.loads(f.Read())
    260                 f.Close()
    261 
    262             def SplitSiteURL(key):
    263                 checkProtocol = True
    264                 if key.find('://') == -1:
    265                     key = 'http://' + key
    266                     checkProtocol = False
    267                 splitUrl = urlparse.urlsplit(key)
    268                 protocol = splitUrl.scheme if checkProtocol else ''
    269                 return (protocol, splitUrl.netloc, splitUrl.path)
    270 
    271             for key in self.GetAutoTrustedSites():
    272                 val = self.GetSiteKey()
    273                 val.trusted = 1
    274                 val.auto = 1
    275                 val.protocol, val.hostname, val.path = SplitSiteURL(key)
    276                 ret[key] = val
    277 
    278             for key in self.GetCommunitySites():
    279                 if key in ret:
    280                     self.LogWarn('GetTrustedSiteList: Skipping community Autosite because it was already listed as a CCP site', key)
    281                     continue
    282                 val = self.GetSiteKey()
    283                 val.trusted = 1
    284                 val.auto = 1
    285                 val.community = 1
    286                 val.protocol, val.hostname, val.path = SplitSiteURL(key)
    287                 ret[key] = val
    288 
    289             for key in trusted:
    290                 if key in ret:
    291                     self.LogWarn('GetTrustedSiteList: Skipping user-trusted site because it was already listed as a CCP site', key)
    292                     continue
    293                 cleanedUrl = self.ReformatUrlForTrustList(key)
    294                 if cleanedUrl is None:
    295                     self.LogInfo('SitesSvc.LoadTrusted rejecting trusted url', key, 'due to failed sanitization')
    296                     continue
    297                 if cleanedUrl not in ret:
    298                     val = self.GetSiteKey()
    299                     val.trusted = 1
    300                     val.protocol, val.hostname, val.path = SplitSiteURL(key)
    301                     ret[cleanedUrl] = val
    302 
    303             for key in ignored:
    304                 if key in ret:
    305                     self.LogWarn('GetTrustedSiteList: Skipping user-trusted site because it was already listed as a CCP or user-trusted site', key)
    306                     continue
    307                 cleanedUrl = self.ReformatUrlForTrustList(key)
    308                 if cleanedUrl is None:
    309                     self.LogInfo('SitesSvc.LoadTrusted rejecting ignored url', key, 'due to failed sanitization')
    310                     continue
    311                 if cleanedUrl not in ret:
    312                     val = self.GetSiteKey()
    313                     val.protocol, val.hostname, val.path = SplitSiteURL(key)
    314                     ret[cleanedUrl] = val
    315 
    316             self.trustedSites = ret
    317         return self.trustedSites
    318 
    319     def SaveTrusted(self):
    320         trustedList = self.GetTrustedSiteList()
    321         trusted = [ key for key, value in trustedList.iteritems() if value.auto == 0 and value.temporary == 0 and value.trusted == 1 ]
    322         ignored = [ key for key, value in trustedList.iteritems() if value.auto == 0 and value.temporary == 0 and value.trusted == 0 ]
    323         f = blue.ResFile()
    324         self.SaveSiteList(self.trustedFile, trusted)
    325         self.SaveSiteList(self.ignoredFile, ignored)
    326 
    327     def GetDefaultHomePage(self):
    328         return sm.RemoteSvc('browserLockdownSvc').GetDefaultHomePage()
    329 
    330     def IsBrowserInLockdown(self):
    331         if self.isBrowserInLockdown is None:
    332             self.isBrowserInLockdown = sm.RemoteSvc('browserLockdownSvc').IsBrowserInLockdown()
    333         return self.isBrowserInLockdown
    334 
    335     def OnBrowserLockdownChange(self, newValue):
    336         self.isBrowserInLockdown = newValue
    337         self.objectCaching.InvalidateCachedMethodCall('browserLockdownSvc', 'IsBrowserInLockdown')
    338         sm.ScatterEvent('OnClientBrowserLockdownChange')
    339 
    340     def GetBrowserBlacklist(self):
    341         if self.systemLists is None:
    342             self.__PopulateSystemLists()
    343         if self.systemLists is None:
    344             return []
    345         return self.systemLists.blacklist
    346 
    347     def GetBannedInChatList(self):
    348         if self.systemLists is None:
    349             self.__PopulateSystemLists()
    350         if self.systemLists is None:
    351             return []
    352         return self.systemLists.bannedInChat
    353 
    354     def GetBrowserWhitelist(self):
    355         if self.systemLists is None:
    356             self.__PopulateSystemLists()
    357         if self.systemLists is None:
    358             return []
    359         return self.systemLists.whitelist
    360 
    361     def GetCommunitySites(self):
    362         if self.systemLists is None:
    363             self.__PopulateSystemLists()
    364         if self.systemLists is None:
    365             return []
    366         return self.systemLists.community
    367 
    368     def GetAutoTrustedSites(self):
    369         if self.systemLists is None:
    370             self.__PopulateSystemLists()
    371         if self.systemLists is None:
    372             return []
    373         return self.systemLists.autotrusted
    374 
    375     def __PopulateSystemLists(self):
    376         flaggedSites = []
    377         try:
    378             f = blue.ResFile()
    379             if f.Open(self.flaggedSitesFile):
    380                 flaggedSites = cPickle.loads(f.Read())
    381         except:
    382             log.LogException('Error reading file from disk')
    383         finally:
    384             f.Close()
    385 
    386         try:
    387             m = hashlib.md5()
    388             m.update(str(flaggedSites))
    389             if m.hexdigest() != sm.RemoteSvc('browserLockdownSvc').GetFlaggedSitesHash():
    390                 self.LogInfo('FlaggedSites: Download full list from server')
    391                 flaggedSites = sm.RemoteSvc('browserLockdownSvc').GetFlaggedSitesList()
    392                 try:
    393                     f = blue.ResFile()
    394                     f.Create(self.flaggedSitesFile)
    395                     f.Write(cPickle.dumps(flaggedSites))
    396                 except:
    397                     log.LogException('Error writing flaggedSites list to disk')
    398                 finally:
    399                     f.Close()
    400 
    401         except:
    402             log.LogException('Error connecting to database')
    403             return
    404 
    405         self.systemLists = utillib.KeyVal(blacklist=[], bannedInChat=[], whitelist=['about:blank'], community=[], autotrusted=[])
    406 
    407         def GetDomainOnly(uStr):
    408             if uStr.find(u'//') != -1:
    409                 uStr = uStr.split(u'//')[1]
    410             if uStr.startswith(u'*'):
    411                 uStr = uStr[1:]
    412             if uStr.startswith(u'.'):
    413                 uStr = uStr[1:]
    414             if uStr.endswith(u'/*'):
    415                 uStr = uStr[:-2]
    416             return uStr
    417 
    418         for flaggedSiteRow in flaggedSites:
    419             if flaggedSiteRow['siteFlag'] & self.BLACKLIST_FLAG:
    420                 self.systemLists.blacklist.append(flaggedSiteRow['siteUrl'])
    421                 uStr = GetDomainOnly(unicode(flaggedSiteRow['siteUrl'])).strip()
    422                 if uStr:
    423                     self.systemLists.bannedInChat.append(uStr)
    424             if flaggedSiteRow['siteFlag'] & self.WHITELIST_FLAG:
    425                 self.systemLists.whitelist.append(flaggedSiteRow['siteUrl'])
    426             if flaggedSiteRow['siteFlag'] & self.COMMUNITY_FLAG:
    427                 self.systemLists.community.append(flaggedSiteRow['siteUrl'])
    428             if flaggedSiteRow['siteFlag'] & self.AUTOTRUSTED_FLAG:
    429                 self.systemLists.autotrusted.append(flaggedSiteRow['siteUrl'])
    430 
    431     def OnFlaggedListsChange(self):
    432         self.objectCaching.InvalidateCachedMethodCall('browserLockdownSvc', 'GetFlaggedSitesList')
    433         self.objectCaching.InvalidateCachedMethodCall('browserLockdownSvc', 'GetFlaggedSitesHash')
    434         self.systemLists = None
    435         self.trustedSites = None
    436         sm.ScatterEvent('OnClientFlaggedListsChange')
    437 
    438 
    439 exports = {'svc.sites': SitesSvc}