blocky-install.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2021-2023 tteck
  3. # Author: tteck (tteckster)
  4. # License: MIT
  5. # https://github.com/tteck/Proxmox/raw/main/LICENSE
  6. source /dev/stdin <<< "$FUNCTIONS_FILE_PATH"
  7. color
  8. verb_ip6
  9. catch_errors
  10. setting_up_container
  11. network_check
  12. update_os
  13. msg_info "Installing Dependencies"
  14. $STD apt-get install -y curl
  15. $STD apt-get install -y sudo
  16. $STD apt-get install -y mc
  17. msg_ok "Installed Dependencies"
  18. msg_info "Installing Blocky"
  19. systemctl stop systemd-resolved
  20. $STD systemctl disable systemd-resolved.service
  21. RELEASE=0.20 #$(curl -s https://api.github.com/repos/0xERR0R/blocky/releases/latest | grep "tag_name" | awk '{print substr($2, 3, length($2)-4) }')
  22. wget -q https://github.com/0xERR0R/blocky/releases/download/v$RELEASE/blocky_${RELEASE}_Linux_x86_64.tar.gz
  23. mkdir -p /opt/blocky
  24. tar -xf blocky_${RELEASE}_Linux_x86_64.tar.gz -C /opt/blocky
  25. rm -rf blocky_${RELEASE}_Linux_x86_64.tar.gz
  26. cat <<EOF >/opt/blocky/config.yml
  27. upstream:
  28. # these external DNS resolvers will be used. Blocky picks 2 random resolvers from the list for each query
  29. # format for resolver: [net:]host:[port][/path]. net could be empty (default, shortcut for tcp+udp), tcp+udp, tcp, udp, tcp-tls or https (DoH). If port is empty, default port will be used (53 for udp and tcp, 853 for tcp-tls, 443 for https (Doh))
  30. # this configuration is mandatory, please define at least one external DNS resolver
  31. default:
  32. # example for tcp+udp IPv4 server (https://digitalcourage.de/)
  33. #- 5.9.164.112
  34. # Cloudflare
  35. - 1.1.1.1
  36. # example for DNS-over-TLS server (DoT)
  37. #- tcp-tls:fdns1.dismail.de:853
  38. # example for DNS-over-HTTPS (DoH)
  39. #- https://dns.digitale-gesellschaft.ch/dns-query
  40. # optional: use client name (with wildcard support: * - sequence of any characters, [0-9] - range)
  41. # or single ip address / client subnet as CIDR notation
  42. #laptop*:
  43. #- 123.123.123.123
  44. # optional: timeout to query the upstream resolver. Default: 2s
  45. #upstreamTimeout: 2s
  46. # optional: If true, blocky will fail to start unless at least one upstream server per group is reachable. Default: false
  47. #startVerifyUpstream: true
  48. # optional: Determines how blocky will create outgoing connections. This impacts both upstreams, and lists.
  49. # accepted: dual, v4, v6
  50. # default: dual
  51. #connectIPVersion: dual
  52. # optional: custom IP address(es) for domain name (with all sub-domains). Multiple addresses must be separated by a comma
  53. # example: query "printer.lan" or "my.printer.lan" will return 192.168.178.3
  54. #customDNS:
  55. #customTTL: 1h
  56. # optional: if true (default), return empty result for unmapped query types (for example TXT, MX or AAAA if only IPv4 address is defined).
  57. # if false, queries with unmapped types will be forwarded to the upstream resolver
  58. #filterUnmappedTypes: true
  59. # optional: replace domain in the query with other domain before resolver lookup in the mapping
  60. #rewrite:
  61. #example.com: printer.lan
  62. #mapping:
  63. #printer.lan: 192.168.178.3,2001:0db8:85a3:08d3:1319:8a2e:0370:7344
  64. # optional: definition, which DNS resolver(s) should be used for queries to the domain (with all sub-domains). Multiple resolvers must be separated by a comma
  65. # Example: Query client.fritz.box will ask DNS server 192.168.178.1. This is necessary for local network, to resolve clients by host name
  66. #conditional:
  67. # optional: if false (default), return empty result if after rewrite, the mapped resolver returned an empty answer. If true, the original query will be sent to the upstream resolver
  68. # Example: The query "blog.example.com" will be rewritten to "blog.fritz.box" and also redirected to the resolver at 192.168.178.1. If not found and if was set to , the original query "blog.example.com" will be sent upstream.
  69. # Usage: One usecase when having split DNS for internal and external (internet facing) users, but not all subdomains are listed in the internal domain.
  70. #fallbackUpstream: false
  71. # optional: replace domain in the query with other domain before resolver lookup in the mapping
  72. #rewrite:
  73. #example.com: fritz.box
  74. #mapping:
  75. #fritz.box: 192.168.178.1
  76. #lan.net: 192.168.178.1,192.168.178.2
  77. # optional: use black and white lists to block queries (for example ads, trackers, adult pages etc.)
  78. blocking:
  79. # definition of blacklist groups. Can be external link (http/https) or local file
  80. blackLists:
  81. ads:
  82. - https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt
  83. - https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
  84. - http://sysctl.org/cameleon/hosts
  85. - https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt
  86. - |
  87. # inline definition with YAML literal block scalar style
  88. # hosts format
  89. someadsdomain.com
  90. special:
  91. - https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews/hosts
  92. # definition of whitelist groups. Attention: if the same group has black and whitelists, whitelists will be used to disable particular blacklist entries. If a group has only whitelist entries -> this means only domains from this list are allowed, all other domains will be blocked
  93. whiteLists:
  94. ads:
  95. - whitelist.txt
  96. - |
  97. # inline definition with YAML literal block scalar style
  98. # hosts format
  99. whitelistdomain.com
  100. # this is a regex
  101. /^banners?[_.-]/
  102. # definition: which groups should be applied for which client
  103. clientGroupsBlock:
  104. # default will be used, if no special definition for a client name exists
  105. default:
  106. - ads
  107. - special
  108. # use client name (with wildcard support: * - sequence of any characters, [0-9] - range)
  109. # or single ip address / client subnet as CIDR notation
  110. #laptop*:
  111. #- ads
  112. #192.168.178.1/24:
  113. #- special
  114. # which response will be sent, if query is blocked:
  115. # zeroIp: 0.0.0.0 will be returned (default)
  116. # nxDomain: return NXDOMAIN as return code
  117. # comma separated list of destination IP addresses (for example: 192.100.100.15, 2001:0db8:85a3:08d3:1319:8a2e:0370:7344). Should contain ipv4 and ipv6 to cover all query types. Useful with running web server on this address to display the "blocked" page.
  118. blockType: zeroIp
  119. # optional: TTL for answers to blocked domains
  120. # default: 6h
  121. blockTTL: 1m
  122. # optional: automatically list refresh period (in duration format). Default: 4h.
  123. # Negative value -> deactivate automatically refresh.
  124. # 0 value -> use default
  125. refreshPeriod: 4h
  126. # optional: timeout for list download (each url). Default: 60s. Use large values for big lists or slow internet connections
  127. downloadTimeout: 4m
  128. # optional: Download attempt timeout. Default: 60s
  129. downloadAttempts: 5
  130. # optional: Time between the download attempts. Default: 1s
  131. downloadCooldown: 10s
  132. # optional: if failOnError, application startup will fail if at least one list can't be downloaded / opened. Default: blocking
  133. #startStrategy: failOnError
  134. # optional: configuration for caching of DNS responses
  135. caching:
  136. # duration how long a response must be cached (min value).
  137. # If <=0, use response's TTL, if >0 use this value, if TTL is smaller
  138. # Default: 0
  139. minTime: 5m
  140. # duration how long a response must be cached (max value).
  141. # If <0, do not cache responses
  142. # If 0, use TTL
  143. # If > 0, use this value, if TTL is greater
  144. # Default: 0
  145. maxTime: 30m
  146. # Max number of cache entries (responses) to be kept in cache (soft limit). Useful on systems with limited amount of RAM.
  147. # Default (0): unlimited
  148. maxItemsCount: 0
  149. # if true, will preload DNS results for often used queries (default: names queried more than 5 times in a 2-hour time window)
  150. # this improves the response time for often used queries, but significantly increases external traffic
  151. # default: false
  152. prefetching: true
  153. # prefetch track time window (in duration format)
  154. # default: 120
  155. prefetchExpires: 2h
  156. # name queries threshold for prefetch
  157. # default: 5
  158. prefetchThreshold: 5
  159. # Max number of domains to be kept in cache for prefetching (soft limit). Useful on systems with limited amount of RAM.
  160. # Default (0): unlimited
  161. #prefetchMaxItemsCount: 0
  162. # optional: configuration of client name resolution
  163. clientLookup:
  164. # optional: this DNS resolver will be used to perform reverse DNS lookup (typically local router)
  165. #upstream: 192.168.178.1
  166. # optional: some routers return multiple names for client (host name and user defined name). Define which single name should be used.
  167. # Example: take second name if present, if not take first name
  168. #singleNameOrder:
  169. #- 2
  170. #- 1
  171. # optional: custom mapping of client name to IP addresses. Useful if reverse DNS does not work properly or just to have custom client names.
  172. #clients:
  173. #laptop:
  174. #- 192.168.178.29
  175. # optional: configuration for prometheus metrics endpoint
  176. prometheus:
  177. # enabled if true
  178. #enable: true
  179. # url path, optional (default '/metrics')
  180. #path: /metrics
  181. # optional: write query information (question, answer, client, duration etc.) to daily csv file
  182. queryLog:
  183. # optional one of: mysql, postgresql, csv, csv-client. If empty, log to console
  184. #type: mysql
  185. # directory (should be mounted as volume in docker) for csv, db connection string for mysql/postgresql
  186. #target: db_user:db_password@tcp(db_host_or_ip:3306)/db_name?charset=utf8mb4&parseTime=True&loc=Local
  187. #postgresql target: postgres://user:password@db_host_or_ip:5432/db_name
  188. # if > 0, deletes log files which are older than ... days
  189. #logRetentionDays: 7
  190. # optional: Max attempts to create specific query log writer, default: 3
  191. #creationAttempts: 1
  192. # optional: Time between the creation attempts, default: 2s
  193. #creationCooldown: 2s
  194. # optional: Blocky can synchronize its cache and blocking state between multiple instances through redis.
  195. redis:
  196. # Server address and port
  197. #address: redis:6379
  198. # Password if necessary
  199. #password: passwd
  200. # Database, default: 0
  201. #database: 2
  202. # Connection is required for blocky to start. Default: false
  203. #required: true
  204. # Max connection attempts, default: 3
  205. #connectionAttempts: 10
  206. # Time between the connection attempts, default: 1s
  207. #connectionCooldown: 3s
  208. # optional: DNS listener port(s) and bind ip address(es), default 53 (UDP and TCP). Example: 53, :53, "127.0.0.1:5353,[::1]:5353"
  209. port: 553
  210. # optional: Port(s) and bind ip address(es) for DoT (DNS-over-TLS) listener. Example: 853, 127.0.0.1:853
  211. #tlsPort: 853
  212. # optional: HTTPS listener port(s) and bind ip address(es), default empty = no http listener. If > 0, will be used for prometheus metrics, pprof, REST API, DoH... Example: 443, :443, 127.0.0.1:443
  213. #httpPort: 4000
  214. #httpsPort: 443
  215. # optional: Mininal TLS version that the DoH and DoT server will use
  216. #minTlsServeVersion: 1.3
  217. # if https port > 0: path to cert and key file for SSL encryption. if not set, self-signed certificate will be generated
  218. #certFile: server.crt
  219. #keyFile: server.key
  220. # optional: use this DNS server to resolve blacklist urls and upstream DNS servers. Useful if no DNS resolver is configured and blocky needs to resolve a host name. Format net:IP:port, net must be udp or tcp
  221. #bootstrapDns: tcp+udp:1.1.1.1
  222. filtering:
  223. # optional: drop all queries with following query types. Default: empty
  224. #queryTypes:
  225. #- AAAA
  226. # optional: if path defined, use this file for query resolution (A, AAAA and rDNS). Default: empty
  227. hostsFile:
  228. # optional: Path to hosts file (e.g. /etc/hosts on Linux)
  229. #filePath: /etc/hosts
  230. # optional: TTL, default: 1h
  231. #hostsTTL: 60m
  232. # optional: Time between hosts file refresh, default: 1h
  233. #refreshPeriod: 30m
  234. # optional: Whether loopback hosts addresses (127.0.0.0/8 and ::1) should be filtered or not, default: false
  235. #filterLoopback: true
  236. # optional: Log level (one from debug, info, warn, error). Default: info
  237. #logLevel: info
  238. # optional: Log format (text or json). Default: text
  239. #logFormat: text
  240. # optional: log timestamps. Default: true
  241. #logTimestamp: true
  242. # optional: obfuscate log output (replace all alphanumeric characters with *) for user sensitive data like request domains or responses to increase privacy. Default: false
  243. #logPrivacy: false
  244. # optional: add EDE error codes to dns response
  245. #ede:
  246. # enabled if true, Default: false
  247. #enable: true
  248. EOF
  249. msg_ok "Installed Blocky"
  250. msg_info "Creating Service"
  251. cat <<EOF >/etc/systemd/system/blocky.service
  252. [Unit]
  253. Description=Blocky
  254. After=network.target
  255. [Service]
  256. User=root
  257. WorkingDirectory=/opt/blocky
  258. ExecStart=/opt/blocky/./blocky --config config.yml
  259. [Install]
  260. WantedBy=multi-user.target
  261. EOF
  262. $STD systemctl enable --now blocky
  263. msg_ok "Created Service"
  264. motd_ssh
  265. customize
  266. msg_info "Cleaning up"
  267. $STD apt-get autoremove
  268. $STD apt-get autoclean
  269. msg_ok "Cleaned"