filtron.rst 4.8 KB

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