filtron.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. .. _searx filtron:
  2. ==========================
  3. How to protect an instance
  4. ==========================
  5. .. sidebar:: further reading
  6. - :ref:`filtron.sh`
  7. - :ref:`nginx searx site`
  8. .. contents:: Contents
  9. :depth: 2
  10. :local:
  11. :backlinks: entry
  12. .. _filtron: https://github.com/asciimoo/filtron
  13. Searx depends on external search services. To avoid the abuse of these services
  14. it is advised to limit the number of requests processed by searx.
  15. An application firewall, filtron_ solves exactly this problem. Filtron is just
  16. a middleware between your web server (nginx, apache, ...) and searx, we describe
  17. such infratructures in chapter: :ref:`architecture`.
  18. filtron & go
  19. ============
  20. .. _Go: https://golang.org/
  21. .. _filtron README: https://github.com/asciimoo/filtron/blob/master/README.md
  22. Filtron needs Go_ installed. If Go_ is preinstalled, filtron_ is simply
  23. installed by ``go get`` package management (see `filtron README`_). If you use
  24. filtron as middleware, a more isolated setup is recommended. To simplify such
  25. an installation and the maintenance of, use our script :ref:`filtron.sh`.
  26. .. _Sample configuration of filtron:
  27. Sample configuration of filtron
  28. ===============================
  29. .. sidebar:: Tooling box
  30. - :origin:`/etc/filtron/rules.json <utils/templates/etc/filtron/rules.json>`
  31. An example configuration can be find below. This configuration limits the access
  32. of:
  33. - scripts or applications (roboagent limit)
  34. - webcrawlers (botlimit)
  35. - IPs which send too many requests (IP limit)
  36. - too many json, csv, etc. requests (rss/json limit)
  37. - the same UserAgent of if too many requests (useragent limit)
  38. .. code:: json
  39. [
  40. { "name": "search request",
  41. "filters": [
  42. "Param:q",
  43. "Path=^(/|/search)$"
  44. ],
  45. "interval": "<time-interval-in-sec (int)>",
  46. "limit": "<max-request-number-in-interval (int)>",
  47. "subrules": [
  48. {
  49. "name": "roboagent limit",
  50. "interval": "<time-interval-in-sec (int)>",
  51. "limit": "<max-request-number-in-interval (int)>",
  52. "filters": [
  53. "Header:User-Agent=(curl|cURL|Wget|python-requests|Scrapy|FeedFetcher|Go-http-client)"
  54. ],
  55. "actions": [
  56. { "name": "log"},
  57. { "name": "block",
  58. "params": {
  59. "message": "Rate limit exceeded"
  60. }
  61. }
  62. ]
  63. },
  64. {
  65. "name": "botlimit",
  66. "limit": 0,
  67. "stop": true,
  68. "filters": [
  69. "Header:User-Agent=(Googlebot|bingbot|Baiduspider|yacybot|YandexMobileBot|YandexBot|Yahoo! Slurp|MJ12bot|AhrefsBot|archive.org_bot|msnbot|MJ12bot|SeznamBot|linkdexbot|Netvibes|SMTBot|zgrab|James BOT)"
  70. ],
  71. "actions": [
  72. { "name": "log"},
  73. { "name": "block",
  74. "params": {
  75. "message": "Rate limit exceeded"
  76. }
  77. }
  78. ]
  79. },
  80. {
  81. "name": "IP limit",
  82. "interval": "<time-interval-in-sec (int)>",
  83. "limit": "<max-request-number-in-interval (int)>",
  84. "stop": true,
  85. "aggregations": [
  86. "Header:X-Forwarded-For"
  87. ],
  88. "actions": [
  89. { "name": "log"},
  90. { "name": "block",
  91. "params": {
  92. "message": "Rate limit exceeded"
  93. }
  94. }
  95. ]
  96. },
  97. {
  98. "name": "rss/json limit",
  99. "interval": "<time-interval-in-sec (int)>",
  100. "limit": "<max-request-number-in-interval (int)>",
  101. "stop": true,
  102. "filters": [
  103. "Param:format=(csv|json|rss)"
  104. ],
  105. "actions": [
  106. { "name": "log"},
  107. { "name": "block",
  108. "params": {
  109. "message": "Rate limit exceeded"
  110. }
  111. }
  112. ]
  113. },
  114. {
  115. "name": "useragent limit",
  116. "interval": "<time-interval-in-sec (int)>",
  117. "limit": "<max-request-number-in-interval (int)>",
  118. "aggregations": [
  119. "Header:User-Agent"
  120. ],
  121. "actions": [
  122. { "name": "log"},
  123. { "name": "block",
  124. "params": {
  125. "message": "Rate limit exceeded"
  126. }
  127. }
  128. ]
  129. }
  130. ]
  131. }
  132. ]
  133. .. _filtron route request:
  134. Route request through filtron
  135. =============================
  136. Filtron can be started using the following command:
  137. .. code:: sh
  138. $ filtron -rules rules.json
  139. It listens on ``127.0.0.1:4004`` and forwards filtered requests to
  140. ``127.0.0.1:8888`` by default.
  141. Use it along with ``nginx`` with the following example configuration.
  142. .. code:: nginx
  143. location / {
  144. proxy_pass http://127.0.0.1:4004/;
  145. proxy_set_header Host $http_host;
  146. proxy_set_header X-Real-IP $remote_addr;
  147. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  148. proxy_set_header X-Scheme $scheme;
  149. }
  150. Requests are coming from port 4004 going through filtron and then forwarded to
  151. port 8888 where a searx is being run. For a complete setup see: :ref:`nginx
  152. searx site`.