self_info.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring, missing-class-docstring
  3. from __future__ import annotations
  4. import typing
  5. import re
  6. from flask_babel import gettext
  7. from searx.botdetection._helpers import get_real_ip
  8. from searx.result_types import EngineResults
  9. from . import Plugin, PluginInfo
  10. if typing.TYPE_CHECKING:
  11. from searx.search import SearchWithPlugins
  12. from searx.extended_types import SXNG_Request
  13. class SXNGPlugin(Plugin):
  14. """Simple plugin that displays information about user's request, including
  15. the IP or HTTP User-Agent. The information is displayed in area for the
  16. "answers".
  17. """
  18. id = "self_info"
  19. default_on = True
  20. keywords = ["ip", "user-agent"]
  21. def __init__(self):
  22. super().__init__()
  23. self.ip_regex = re.compile(r"^ip", re.IGNORECASE)
  24. self.ua_regex = re.compile(r"^user-agent", re.IGNORECASE)
  25. self.info = PluginInfo(
  26. id=self.id,
  27. name=gettext("Self Information"),
  28. description=gettext(
  29. """Displays your IP if the query is "ip" and your user agent if the query is "user-agent"."""
  30. ),
  31. preference_section="query",
  32. )
  33. def post_search(self, request: "SXNG_Request", search: "SearchWithPlugins") -> EngineResults:
  34. """Returns a result list only for the first page."""
  35. results = EngineResults()
  36. if search.search_query.pageno > 1:
  37. return results
  38. if self.ip_regex.search(search.search_query.query):
  39. results.add(results.types.Answer(answer=gettext("Your IP is: ") + get_real_ip(request)))
  40. if self.ua_regex.match(search.search_query.query):
  41. results.add(results.types.Answer(answer=gettext("Your user-agent is: ") + str(request.user_agent)))
  42. return results