gucontent_proxy.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/python3
  2. # Copyright (C) 2017 Farooq Karimi Zadeh <fkz@riseup.net>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in all
  12. # copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. import socket
  22. import threading
  23. import requests
  24. import gzip
  25. PORT = 5433
  26. class sockthread(threading.Thread):
  27. def __init__(self, csock):
  28. threading.Thread.__init__(self)
  29. self.csock = csock # client's socket
  30. def run(self):
  31. msg = self.csock.recv(1024).decode()
  32. if msg.startswith("GET"):
  33. url = msg.split(maxsplit=2)[1]
  34. if url.startswith("http"):
  35. api_url = "https://feedback.googleusercontent.com/gadgets/proxy"
  36. args = {"url": url, "container": "fbk"}
  37. req = requests.get(api_url, params=args, stream=True)
  38. bytes_ = req.raw.read()
  39. if bytes_[0] == 0x1f and bytes_[1] == 0x8b:
  40. self.csock.send(gzip.decompress(bytes_))
  41. else:
  42. self.csock.send(bytes_)
  43. else:
  44. self.csock.send(bytes("", "ascii"))
  45. else:
  46. self.csock.send(bytes("", "ascii"))
  47. self.csock.close()
  48. sock = socket.socket()
  49. sock.bind(("", PORT))
  50. sock.listen()
  51. try:
  52. while 1:
  53. sockthread(sock.accept()[0]).start()
  54. except KeyboardInterrupt:
  55. print("Waiting for threads to terminate...")
  56. while len(threading.enumerate()) > 1:
  57. pass