freedombone-utils-onion 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Onion functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. # The maximum amount of traffic per day in gigabytes
  29. TOR_MAX_TRAFFIC_PER_MONTH_GB=10
  30. USE_V2_ONION_ADDRESS=
  31. HIDDEN_SERVICE_PATH='/var/lib/tor/hidden_service_'
  32. ONION_SERVICES_FILE=/etc/torrc.d/${PROJECT_NAME}
  33. function torrc_migrate {
  34. if [ -f "$ONION_SERVICES_FILE" ]; then
  35. if grep -q "#%include /etc/torrc.d" /etc/tor/torrc; then
  36. sed -i 's|#%include /etc/torrc.d|%include /etc/torrc.d|g' /etc/tor/torrc
  37. systemctl restart tor
  38. fi
  39. return
  40. fi
  41. systemctl stop tor
  42. mkdir /etc/torrc.d
  43. grep "HiddenServiceDir\\|HiddenServiceVersion\\|HiddenServicePort" /etc/tor/torrc | grep -v "#HiddenServiceDir" >> "$ONION_SERVICES_FILE"
  44. if ! grep "HiddenServiceVersion" "$ONION_SERVICES_FILE"; then
  45. systemctl restart tor
  46. return
  47. fi
  48. if grep -q "#%include /etc/torrc.d" /etc/tor/torrc; then
  49. sed -i 's|#%include /etc/torrc.d|%include /etc/torrc.d|g' /etc/tor/torrc
  50. else
  51. echo "%include /etc/torrc.d" >> /etc/tor/torrc
  52. fi
  53. { echo 'DNSPort 5300';
  54. echo 'DNSListenAddress 127.0.0.1';
  55. echo 'AutomapHostsOnResolve 1'; } > /etc/torrc.d/dns
  56. sed -i '/DNSPort 5300/d' /etc/tor/torrc
  57. sed -i '/DNSListenAddress 127.0.0./d' /etc/tor/torrc
  58. sed -i '/AutomapHostsOnResolve 1/d' /etc/tor/torrc
  59. sed -i '/HiddenServiceDir/d' /etc/tor/torrc
  60. sed -i '/HiddenServiceVersion/d' /etc/tor/torrc
  61. sed -i '/HiddenServicePort/d' /etc/tor/torrc
  62. systemctl restart tor
  63. }
  64. function add_email_hostname {
  65. extra_email_hostname="$1"
  66. email_hostnames=$(grep "dc_other_hostnames" /etc/exim4/update-exim4.conf.conf | awk -F "'" '{print $2}')
  67. if [[ "$email_hostnames" != *"$extra_email_hostname"* ]]; then
  68. sed -i "s|dc_other_hostnames=.*|dc_other_hostnames='$email_hostnames;$extra_email_hostname'|g" /etc/exim4/update-exim4.conf.conf
  69. update-exim4.conf
  70. dpkg-reconfigure --frontend noninteractive exim4-config
  71. systemctl restart saslauthd
  72. fi
  73. }
  74. function onion_update {
  75. # update so that new onion services appear
  76. systemctl restart tor
  77. }
  78. function wait_for_onion_service_base {
  79. onion_service_name="$1"
  80. sleep_ctr=0
  81. while [ ! -f "${HIDDEN_SERVICE_PATH}${onion_service_name}/hostname" ]; do
  82. sleep 1
  83. sleep_ctr=$((sleep_ctr + 1))
  84. if [ $sleep_ctr -gt 10 ]; then
  85. break
  86. fi
  87. done
  88. }
  89. function wait_for_onion_service {
  90. onion_service_name="$1"
  91. wait_for_onion_service_base "${onion_service_name}"
  92. if [ ! -f "${HIDDEN_SERVICE_PATH}${onion_service_name}/hostname" ]; then
  93. # try a second time
  94. onion_update
  95. wait_for_onion_service_base "${onion_service_name}"
  96. fi
  97. sync
  98. }
  99. function remove_onion_service {
  100. onion_service_name="$1"
  101. onion_service_port_to=$2
  102. nick="$3"
  103. if [ ${#nick} -gt 0 ]; then
  104. sed -i "/stealth ${nick}/d" "$ONION_SERVICES_FILE"
  105. fi
  106. sed -i "/hidden_service_${onion_service_name}/,+1 d" "$ONION_SERVICES_FILE"
  107. sed -i "/hidden_service_${onion_service_name}_mobile/,+1 d" "$ONION_SERVICES_FILE"
  108. sed -i "/127.0.0.1:${onion_service_port_to}/d" "$ONION_SERVICES_FILE"
  109. if [ "$3" ]; then
  110. sed -i "/127.0.0.1:${3}/d" "$ONION_SERVICES_FILE"
  111. if [ "$4" ]; then
  112. sed -i "/127.0.0.1:${4}/d" "$ONION_SERVICES_FILE"
  113. if [ "$5" ]; then
  114. sed -i "/127.0.0.1:${5}/d" "$ONION_SERVICES_FILE"
  115. fi
  116. fi
  117. fi
  118. if [ -d "${HIDDEN_SERVICE_PATH}${onion_service_name}" ]; then
  119. shred -zu "${HIDDEN_SERVICE_PATH}${onion_service_name}/"*
  120. rm -rf "${HIDDEN_SERVICE_PATH}${onion_service_name}"
  121. fi
  122. if [ -d "${HIDDEN_SERVICE_PATH}${onion_service_name}_mobile" ]; then
  123. shred -zu "${HIDDEN_SERVICE_PATH}${onion_service_name}_mobile/"*
  124. rm -rf "${HIDDEN_SERVICE_PATH}${onion_service_name}_mobile"
  125. fi
  126. remove_completion_param "${onion_service_name} onion domain"
  127. onion_update
  128. }
  129. function add_onion_service {
  130. onion_service_name="$1"
  131. onion_service_port_from=$2
  132. onion_service_port_to=$3
  133. onion_stealth_name="$4"
  134. if [ -f "${HIDDEN_SERVICE_PATH}${onion_service_name}/hostname" ]; then
  135. cat "${HIDDEN_SERVICE_PATH}${onion_service_name}/hostname"
  136. USE_V2_ONION_ADDRESS=
  137. return
  138. fi
  139. if [ ! -d /var/lib/tor ]; then
  140. echo $"No Tor installation found. ${onion_service_name} onion site cannot be configured."
  141. USE_V2_ONION_ADDRESS=
  142. exit 877367
  143. fi
  144. if ! grep -q "hidden_service_${onion_service_name}" "$ONION_SERVICES_FILE"; then
  145. echo "HiddenServiceDir ${HIDDEN_SERVICE_PATH}${onion_service_name}/" >> "$ONION_SERVICES_FILE"
  146. if [ ! $USE_V2_ONION_ADDRESS ]; then
  147. echo 'HiddenServiceVersion 3' >> "$ONION_SERVICES_FILE"
  148. else
  149. echo 'HiddenServiceVersion 2' >> "$ONION_SERVICES_FILE"
  150. fi
  151. echo "HiddenServicePort ${onion_service_port_from} 127.0.0.1:${onion_service_port_to}" >> "$ONION_SERVICES_FILE"
  152. if [ ${#onion_stealth_name} -gt 0 ]; then
  153. echo "HiddenServiceAuthorizeClient stealth ${onion_stealth_name}" >> "$ONION_SERVICES_FILE"
  154. fi
  155. fi
  156. USE_V2_ONION_ADDRESS=
  157. onion_update
  158. function_check wait_for_onion_service
  159. wait_for_onion_service "${onion_service_name}"
  160. if [ ! -f "${HIDDEN_SERVICE_PATH}${onion_service_name}/hostname" ]; then
  161. ls -lh "${HIDDEN_SERVICE_PATH}${onion_service_name}/hostname"
  162. echo $"${onion_service_name} onion site hostname not found"
  163. exit 763624
  164. fi
  165. onion_address=$(cat "${HIDDEN_SERVICE_PATH}${onion_service_name}/hostname")
  166. # Record the domain in the completion file
  167. set_completion_param "${onion_service_name} onion domain" "${onion_address}"
  168. echo "$onion_address"
  169. }
  170. function set_default_onion_domains {
  171. # If sites are only visible via Tor then for installation
  172. # purposes assign them some default domain names
  173. if [[ $ONION_ONLY == "no" ]]; then
  174. return
  175. fi
  176. POSTACTIV_DOMAIN_NAME='postactiv.local'
  177. GNUSOCIAL_DOMAIN_NAME='gnusocial.local'
  178. HTMLY_DOMAIN_NAME='htmly.local'
  179. BLUDIT_DOMAIN_NAME='bludit.local'
  180. DOKUWIKI_DOMAIN_NAME='dokuwiki.local'
  181. DEFAULT_DOMAIN_NAME="${LOCAL_NAME}.local"
  182. GOGS_DOMAIN_NAME='gogs.local'
  183. }
  184. function create_avahi_onion_domains {
  185. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  186. return
  187. fi
  188. if [ ! -d /etc/avahi/services ]; then
  189. return
  190. fi
  191. if [ $GNUSOCIAL_DOMAIN_NAME ]; then
  192. function_check create_avahi_service
  193. create_avahi_service gnusocial http tcp "$GNUSOCIAL_ONION_PORT"
  194. fi
  195. if [ $HTMLY_DOMAIN_NAME ]; then
  196. function_check create_avahi_service
  197. create_avahi_service blog http tcp "$HTMLY_ONION_PORT"
  198. fi
  199. if [ $GOGS_DOMAIN_NAME ]; then
  200. function_check create_avahi_service
  201. create_avahi_service git http tcp "$GIT_ONION_PORT"
  202. fi
  203. if [ $DOKUWIKI_DOMAIN_NAME ]; then
  204. function_check create_avahi_service
  205. create_avahi_service dokuwiki http tcp "$DOKUWIKI_ONION_PORT"
  206. fi
  207. }
  208. function allow_ssh_to_onion_address {
  209. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  210. return
  211. fi
  212. if [ ! -d "/home/$MY_USERNAME/.ssh" ]; then
  213. mkdir "/home/$MY_USERNAME/.ssh"
  214. fi
  215. if [ ! -d /etc/tor ]; then
  216. echo $'Tor not found when updating ssh'
  217. exit 528257
  218. fi
  219. if ! grep -q "onion" "/home/$MY_USERNAME/.ssh/config"; then
  220. echo 'Host *.onion' >> "/home/$MY_USERNAME/.ssh/config"
  221. echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> "/home/$MY_USERNAME/.ssh/config"
  222. fi
  223. }
  224. function enable_ssh_via_onion {
  225. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  226. return
  227. fi
  228. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  229. return
  230. fi
  231. echo 'N' | apt-get -yq -t stretch-backports install tor
  232. apt-get -yq install connect-proxy
  233. if ! grep -q 'Host *.onion' "/home/$MY_USERNAME/.ssh/config"; then
  234. if [ ! -d "/home/$MY_USERNAME/.ssh" ]; then
  235. mkdir "/home/$MY_USERNAME/.ssh"
  236. fi
  237. echo 'Host *.onion' >> "/home/$MY_USERNAME/.ssh/config"
  238. echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> "/home/$MY_USERNAME/.ssh/config"
  239. chown "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.ssh"
  240. chown "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.ssh/config"
  241. fi
  242. if ! grep -q 'Host *.onion' /root/.ssh/config; then
  243. if [ ! -d /root/.ssh ]; then
  244. mkdir /root/.ssh
  245. fi
  246. echo 'Host *.onion' >> /root/.ssh/config
  247. echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /root/.ssh/config
  248. fi
  249. if ! grep -q 'Host *.onion' /etc/skel/.ssh/config; then
  250. if [ ! -d /etc/skel/.ssh ]; then
  251. mkdir /etc/skel/.ssh
  252. fi
  253. echo 'Host *.onion' >> /etc/skel/.ssh/config
  254. echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /etc/skel/.ssh/config
  255. fi
  256. mark_completed "${FUNCNAME[0]}"
  257. }
  258. function configure_ssh_onion {
  259. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  260. return
  261. fi
  262. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  263. return
  264. fi
  265. SSH_ONION_HOSTNAME=$(add_onion_service ssh "${SSH_PORT}" "${SSH_PORT}")
  266. if [[ "$SSH_ONION_HOSTNAME" != *'.onion' ]]; then
  267. echo $'ssh onion site not generated'
  268. exit 624128
  269. fi
  270. set_completion_param "ssh onion domain" "${SSH_ONION_HOSTNAME}"
  271. add_email_hostname "${SSH_ONION_HOSTNAME}"
  272. mark_completed "${FUNCNAME[0]}"
  273. }
  274. function check_tor_health {
  275. { echo '#!/bin/bash';
  276. echo "status=\$(${PROJECT_NAME}-tor-health)";
  277. echo "ADMIN_USER=\$(grep \"MY_USERNAME=\" ~/${PROJECT_NAME}.cfg | awk -F '=' '{print \$2}')";
  278. echo "if [[ \"\$status\" == 'G'* ]]; then";
  279. echo ' if [ -f /tmp/.torfailed ]; then';
  280. echo ' rm /tmp/.torfailed';
  281. echo " tail -n 3 /var/log/tor/notices.log | mail -s \"[${PROJECT_NAME}] Tor status is now \$status\" \$ADMIN_USER@\$HOSTNAME";
  282. echo ' fi';
  283. echo ' exit 0';
  284. echo 'fi';
  285. echo 'if [ ! -f /tmp/.torfailed ]; then';
  286. echo " tail -n 3 /var/log/tor/notices.log | mail -s \"[${PROJECT_NAME}] Tor status is \$status\" \$ADMIN_USER@\$HOSTNAME";
  287. echo " echo \"\$status\" > /tmp/.torfailed";
  288. echo 'else';
  289. echo " prev_status=\$(cat /tmp/.torfailed)";
  290. echo " if [[ \"\$prev_status\" != \"\$status\" ]]; then";
  291. echo " tail -n 3 /var/log/tor/notices.log | mail -s \"[${PROJECT_NAME}] Tor status is \$status\" \$ADMIN_USER@\$HOSTNAME";
  292. echo " echo \"\$status\" > /tmp/.torfailed";
  293. echo ' fi';
  294. echo 'fi'; } > /usr/bin/check_tor_health
  295. chmod +x /usr/bin/check_tor_health
  296. if ! grep -q 'check_tor_health' /etc/crontab; then
  297. cron_add_mins 10 "/usr/bin/check_tor_health"
  298. fi
  299. }
  300. function install_tor {
  301. if [[ $SYSTEM_TYPE == "mesh*" ]]; then
  302. return
  303. fi
  304. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  305. return
  306. fi
  307. apt-get -yq -t stretch-backports install tor
  308. if [ ! -f /etc/tor/torrc ]; then
  309. echo 'Tor failed to install'
  310. exit 38259
  311. fi
  312. # For torify
  313. apt-get -yq install torsocks
  314. if [ ! -d /etc/torrc.d ]; then
  315. mkdir /etc/torrc.d
  316. fi
  317. sed -i 's|#%include /etc/torrc.d|%include /etc/torrc.d|g' /etc/tor/torrc
  318. if ! grep -q '%include /etc/torrc.d' /etc/tor/torrc; then
  319. echo '%include /etc/torrc.d' >> /etc/tor/torrc
  320. fi
  321. echo 'Log notice file /var/log/tor/notices.log' > /etc/torrc.d/logging
  322. echo "AccountingMax $TOR_MAX_TRAFFIC_PER_MONTH_GB GBytes" > /etc/torrc.d/maxtraffic
  323. mark_completed "${FUNCNAME[0]}"
  324. }
  325. # see https://trac.torproject.org/projects/tor/wiki/doc/TransparentProxy
  326. # Local Redirection and Anonymizing Middlebox
  327. function route_outgoing_traffic_through_tor {
  328. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  329. return
  330. fi
  331. if [[ $ROUTE_THROUGH_TOR != "yes" ]]; then
  332. return
  333. fi
  334. echo 'N' | apt-get -yq -t stretch-backports install tor
  335. echo 'N' | apt-get -yq -t stretch-backports install tor-arm
  336. ### set variables
  337. # Destinations you don't want routed through Tor
  338. _non_tor="192.168.1.0/24 192.168.0.0/24"
  339. # The user that Tor runs as
  340. _tor_uid="debian-tor"
  341. # Tor's TransPort
  342. _trans_port="9040"
  343. # Your internal interface
  344. _int_if="eth0"
  345. ### Set iptables *nat
  346. iptables -t nat -A OUTPUT -o lo -j RETURN
  347. iptables -t nat -A OUTPUT -m owner --uid-owner $_tor_uid -j RETURN
  348. iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-ports 53
  349. # Allow clearnet access for hosts in $_non_tor
  350. for _clearnet in $_non_tor; do
  351. iptables -t nat -A OUTPUT -d "$_clearnet" -j RETURN
  352. iptables -t nat -A PREROUTING -i $_int_if -d "$_clearnet" -j RETURN
  353. done
  354. # Redirect all other pre-routing and output to Tor
  355. iptables -t nat -A OUTPUT -p tcp --syn -j REDIRECT --to-ports $_trans_port
  356. iptables -t nat -A PREROUTING -i $_int_if -p udp --dport 53 -j REDIRECT --to-ports 53
  357. iptables -t nat -A PREROUTING -i $_int_if -p tcp --syn -j REDIRECT --to-ports $_trans_port
  358. ### set iptables *filter
  359. iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  360. # Allow clearnet access for hosts in $_non_tor
  361. for _clearnet in $_non_tor 127.0.0.0/8; do
  362. iptables -A OUTPUT -d "$_clearnet" -j ACCEPT
  363. done
  364. # Allow only Tor output
  365. iptables -A OUTPUT -m owner --uid-owner $_tor_uid -j ACCEPT
  366. iptables -A OUTPUT -j REJECT
  367. function_check save_firewall_settings
  368. save_firewall_settings
  369. if ! grep -q "fs.file-max" /etc/sysctl.conf; then
  370. echo "fs.file-max=100000" >> /etc/sysctl.conf
  371. /sbin/sysctl -p -q
  372. fi
  373. resolvconf=/etc/resolvconf/resolv.conf.d/head
  374. echo 'domain localdomain' > $resolvconf
  375. echo 'search localdomain' >> $resolvconf
  376. echo 'nameserver 127.0.0.1' >> $resolvconf
  377. resolvconf -u
  378. if ! grep -q "VirtualAddrNetworkIPv4" /etc/tor/torrc; then
  379. echo 'VirtualAddrNetworkIPv4 10.192.0.0/10' >> /etc/tor/torrc
  380. fi
  381. if ! grep -q "AutomapHostsOnResolve" /etc/tor/torrc; then
  382. echo 'AutomapHostsOnResolve 1' >> /etc/tor/torrc
  383. fi
  384. if ! grep -q "TransPort" /etc/tor/torrc; then
  385. echo 'TransPort 9040' >> /etc/tor/torrc
  386. fi
  387. if ! grep -q "TransListenAddress 127.0.0.1" /etc/tor/torrc; then
  388. echo 'TransListenAddress 127.0.0.1' >> /etc/tor/torrc
  389. fi
  390. if ! grep -q "TransListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" /etc/tor/torrc; then
  391. echo "TransListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" >> /etc/tor/torrc
  392. fi
  393. if ! grep -q "DNSPort" /etc/tor/torrc; then
  394. echo 'DNSPort 53' >> /etc/tor/torrc
  395. fi
  396. if ! grep -q "DNSListenAddress 127.0.0.1" /etc/tor/torrc; then
  397. echo 'DNSListenAddress 127.0.0.1' >> /etc/tor/torrc
  398. fi
  399. if ! grep -q "DNSListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" /etc/tor/torrc; then
  400. echo "DNSListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" >> /etc/tor/torrc
  401. fi
  402. mark_completed "${FUNCNAME[0]}"
  403. }
  404. function get_app_onion_address {
  405. app_name="$1"
  406. mobilestr="$2"
  407. if [ ${#mobilestr} -gt 0 ]; then
  408. app_name="mobile${app_name}"
  409. fi
  410. if grep -q "${app_name} onion domain" "$COMPLETION_FILE"; then
  411. if grep -q "${app_name} onion domain" "$COMPLETION_FILE"; then
  412. grep "${app_name} onion domain" "${COMPLETION_FILE}" | head -n 1 | awk -F ':' '{print $2}'
  413. return
  414. fi
  415. fi
  416. echo ""
  417. }
  418. function tor_add_bridge {
  419. bridge_ip_address="$1"
  420. bridge_port="$2"
  421. bridge_key="$3"
  422. bridge_type='obfs4'
  423. if [[ "$bridge_ip_address" != *"."* ]]; then
  424. return
  425. fi
  426. if [ ${#bridge_port} -eq 0 ]; then
  427. return
  428. fi
  429. if [ ${#bridge_key} -eq 0 ]; then
  430. return
  431. fi
  432. apt-get -yq install obfs4proxy
  433. if [ ! -f /etc/torrc.d/bridges ]; then
  434. { echo 'ClientTransportPlugin obfs4 exec /usr/bin/obfs4proxy managed';
  435. echo 'UseBridges 1';
  436. echo "Bridge $bridge_type ${bridge_ip_address}:${bridge_port} ${bridge_key}"; } > /etc/torrc.d/bridges
  437. else
  438. if ! grep -q "Bridge $bridge_type ${bridge_ip_address}:${bridge_port} ${bridge_key}" /etc/torrc.d/bridges; then
  439. echo "Bridge $bridge_type ${bridge_ip_address}:${bridge_port} ${bridge_key}" >> /etc/torrc.d/bridges
  440. fi
  441. fi
  442. systemctl restart tor
  443. }
  444. function tor_remove_bridge {
  445. bridge_ip_address="$1"
  446. bridge_type='obfs4'
  447. if [[ "$bridge_ip_address" == *"."* ]]; then
  448. bridge_str="Bridge $bridge_type ${bridge_ip_address}"
  449. else
  450. if grep -q " ${bridge_ip_address}" /etc/torrc.d/bridges; then
  451. bridge_str=" ${bridge_ip_address}"
  452. else
  453. return
  454. fi
  455. fi
  456. if grep -q "${bridge_str}" /etc/torrc.d/bridges; then
  457. sed -i "/${bridge_str}/d" /etc/torrc.d/bridges
  458. fi
  459. # If there are no bridges remaining then remove the file
  460. if ! grep -q "Bridge " /etc/torrc.d/bridges; then
  461. rm /etc/torrc.d/bridges
  462. fi
  463. systemctl restart tor
  464. }
  465. function tor_create_bridge_relay {
  466. read_config_param 'TOR_BRIDGE_PORT'
  467. read_config_param 'TOR_BRIDGE_NICKNAME'
  468. read_config_param 'MY_EMAIL_ADDRESS'
  469. if [ ! "$TOR_BRIDGE_PORT" ]; then
  470. return
  471. fi
  472. if [ ${#TOR_BRIDGE_PORT} -eq 0 ]; then
  473. return
  474. fi
  475. if [ ${#TOR_BRIDGE_NICKNAME} -eq 0 ]; then
  476. return
  477. fi
  478. apt-get -yq install obfs4proxy
  479. { echo 'BridgeRelay 1';
  480. echo 'ServerTransportPlugin obfs4 exec /usr/bin/obfs4proxy';
  481. echo "ExtORPort $TOR_BRIDGE_PORT";
  482. echo "ContactInfo $MY_EMAIL_ADDRESS";
  483. echo "Nickname $TOR_BRIDGE_NICKNAME"; } > /etc/torrc.d/bridgerelay
  484. firewall_add tor_bridge "$TOR_BRIDGE_PORT" tcp
  485. systemctl restart tor
  486. }
  487. function tor_remove_bridge_relay {
  488. if [ -f /etc/torrc.d/bridgerelay ]; then
  489. rm /etc/torrc.d/bridgerelay
  490. fi
  491. read_config_param 'TOR_BRIDGE_PORT'
  492. firewall_remove "$TOR_BRIDGE_PORT" tcp
  493. systemctl restart tor
  494. }
  495. # NOTE: deliberately no exit 0