enen92 / screensaver.kaster

A chromecast-like screensaver for Kodi
GNU General Public License v2.0
25 stars 13 forks source link

Bad image URLs cause excessive hits on Google servers #22

Closed delboy711 closed 3 years ago

delboy711 commented 3 years ago

After setting up a caching DNS server I was surprised to see in the logs I was getting hundreds of thousands of hits per day on google image servers, as well as issues with not being able to wake Kodi up when Kaster was running on occasion.

I was also seeing Kodi crash logs with entries like these:-

2020-12-03 02:11:10.523 T:2041557200 ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--

2020-12-10 00:41:14.044 T:2097148112 ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--

The two URLs shown in this log https://lh3.googleusercontent.com/-zabx6mtDOEg/UtrJztwa_YI/AAAAAAAAX3M/Z7BuWJ7hkzQ/s1920-w1920-h1080-c/Mysore+Palace.jpg https://lh3.googleusercontent.com/mbbapEbFH7QmPoDJv1k6XkJZsshrYb-N7YeyvRthEKkbZlMAuh49Zw=s1280-w1280-h720-p-k-no-nd-mv

are listed in the chromecast.json file, but are not present on the Google servers and return a 404 error. The second one appears to be malformed since it does not end in .jpg.

As a workaround I have removed those URLs from chromecast.json, and to prevent future problems with 404 errors causing DNS storms have edited screensaver.py to force a delay after any result other than 200

            # if it is a google image....
            if "private" not in self.images[rand_index]:
                req = requests.head(url=self.images[rand_index]["url"])
                if req.status_code != 200:
                    # sleep for a bit to avoid 429 (too many requests)
                    #if req.status_code == 429:
                    #    self.exit_monitor.waitForAbort(5)
                    self.exit_monitor.waitForAbort(5)
                    continue
delboy711 commented 3 years ago

Looking deeper into this I notice that an awful lot of the URLs in chromecast.json are invalid. In fact only 455 out of 785 are valid.

I have made a little script to extract the valid URLs. A revised chromecast.json is attached chromecast_json.zip

delboy711 commented 3 years ago

I have scraped the Chromecast gallery page curated by Alex Meub at https://chromecastbg.alexmeub.com/ and produced a new chromecast.json file containing 851 chromecast screensavers. This file includes the existing screensavers plus a whole lot of newer ones. All the images are stored on Google servers so none of Alex's bandwidth is used. All the images currently download OK.

The attached zip file has two versions, 'Better' at 1920x1200 resolution, and 'Best' at 2560x1440 resolution. new_chromecast_json.zip

enen92 commented 3 years ago

Thanks for the contribution @delboy711 I'll submit a new update to the repo

Yaff1e commented 3 years ago

I'm on version 1.3.5 and seem to be having the same problems. Sometimes Kodi doesn't respond to remote control input when Kaster is active. And below is the activity of one of my PiHoles with the black bar being Vero activity. It seems odd in that the Vero is on permanently overnight but the DNS activity was normal until just after 7am and then it goes crazy. It seems to be the same every time it goes crazy. It's definitely Kaster causing it. Kaster

delboy711 commented 3 years ago

I have also seen another instance of a DNS storm with 1.3.5. To try to fix it I edited the file ~/.kodi/addons/screensaver.kaster/resources/lib/screensaver.py

On line 70. Add a timeout to the image fetch req = requests.head(url=self.images[rand_index]["url"], timeout=2)

Comment out line 73 to sleep after any fetch failure instead of just error 429 #if req.status_code == 429:

Reduce indentation of lines 74 and 75 to the comment lines above (Sorry I have never worked out how to paste code in github without losing all the indentation)

I made this change about 2 weeks ago and have not had a DNS storm since, but was waiting to confirm it was actually fixed before reporting it.

Yaff1e commented 3 years ago

Thanks for that. I've made those changes and will give it a try.

Yaff1e commented 3 years ago

It seems to have worked for me thanks. It definitely would have done it within 24 hours previously.