freedombone-utils-onion 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. GIT_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 [ $GIT_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 install_tor {
  275. if [[ $SYSTEM_TYPE == "mesh*" ]]; then
  276. return
  277. fi
  278. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  279. return
  280. fi
  281. apt-get -yq -t stretch-backports install tor
  282. if [ ! -f /etc/tor/torrc ]; then
  283. echo 'Tor failed to install'
  284. exit 38259
  285. fi
  286. # For torify
  287. apt-get -yq install torsocks
  288. if [ ! -d /etc/torrc.d ]; then
  289. mkdir /etc/torrc.d
  290. fi
  291. sed -i 's|#%include /etc/torrc.d|%include /etc/torrc.d|g' /etc/tor/torrc
  292. if ! grep -q '%include /etc/torrc.d' /etc/tor/torrc; then
  293. echo '%include /etc/torrc.d' >> /etc/tor/torrc
  294. fi
  295. echo 'Log notice file /dev/null' > /etc/torrc.d/logging
  296. echo "AccountingMax $TOR_MAX_TRAFFIC_PER_MONTH_GB GBytes" > /etc/torrc.d/maxtraffic
  297. mark_completed "${FUNCNAME[0]}"
  298. }
  299. # see https://trac.torproject.org/projects/tor/wiki/doc/TransparentProxy
  300. # Local Redirection and Anonymizing Middlebox
  301. function route_outgoing_traffic_through_tor {
  302. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  303. return
  304. fi
  305. if [[ $ROUTE_THROUGH_TOR != "yes" ]]; then
  306. return
  307. fi
  308. echo 'N' | apt-get -yq -t stretch-backports install tor
  309. echo 'N' | apt-get -yq -t stretch-backports install tor-arm
  310. ### set variables
  311. # Destinations you don't want routed through Tor
  312. _non_tor="192.168.1.0/24 192.168.0.0/24"
  313. # The user that Tor runs as
  314. _tor_uid="debian-tor"
  315. # Tor's TransPort
  316. _trans_port="9040"
  317. # Your internal interface
  318. _int_if="eth0"
  319. ### Set iptables *nat
  320. iptables -t nat -A OUTPUT -o lo -j RETURN
  321. iptables -t nat -A OUTPUT -m owner --uid-owner $_tor_uid -j RETURN
  322. iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-ports 53
  323. # Allow clearnet access for hosts in $_non_tor
  324. for _clearnet in $_non_tor; do
  325. iptables -t nat -A OUTPUT -d "$_clearnet" -j RETURN
  326. iptables -t nat -A PREROUTING -i $_int_if -d "$_clearnet" -j RETURN
  327. done
  328. # Redirect all other pre-routing and output to Tor
  329. iptables -t nat -A OUTPUT -p tcp --syn -j REDIRECT --to-ports $_trans_port
  330. iptables -t nat -A PREROUTING -i $_int_if -p udp --dport 53 -j REDIRECT --to-ports 53
  331. iptables -t nat -A PREROUTING -i $_int_if -p tcp --syn -j REDIRECT --to-ports $_trans_port
  332. ### set iptables *filter
  333. iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  334. # Allow clearnet access for hosts in $_non_tor
  335. for _clearnet in $_non_tor 127.0.0.0/8; do
  336. iptables -A OUTPUT -d "$_clearnet" -j ACCEPT
  337. done
  338. # Allow only Tor output
  339. iptables -A OUTPUT -m owner --uid-owner $_tor_uid -j ACCEPT
  340. iptables -A OUTPUT -j REJECT
  341. function_check save_firewall_settings
  342. save_firewall_settings
  343. if ! grep -q "fs.file-max" /etc/sysctl.conf; then
  344. echo "fs.file-max=100000" >> /etc/sysctl.conf
  345. /sbin/sysctl -p -q
  346. fi
  347. resolvconf=/etc/resolvconf/resolv.conf.d/head
  348. echo 'domain localdomain' > $resolvconf
  349. echo 'search localdomain' >> $resolvconf
  350. echo 'nameserver 127.0.0.1' >> $resolvconf
  351. resolvconf -u
  352. if ! grep -q "VirtualAddrNetworkIPv4" /etc/tor/torrc; then
  353. echo 'VirtualAddrNetworkIPv4 10.192.0.0/10' >> /etc/tor/torrc
  354. fi
  355. if ! grep -q "AutomapHostsOnResolve" /etc/tor/torrc; then
  356. echo 'AutomapHostsOnResolve 1' >> /etc/tor/torrc
  357. fi
  358. if ! grep -q "TransPort" /etc/tor/torrc; then
  359. echo 'TransPort 9040' >> /etc/tor/torrc
  360. fi
  361. if ! grep -q "TransListenAddress 127.0.0.1" /etc/tor/torrc; then
  362. echo 'TransListenAddress 127.0.0.1' >> /etc/tor/torrc
  363. fi
  364. if ! grep -q "TransListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" /etc/tor/torrc; then
  365. echo "TransListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" >> /etc/tor/torrc
  366. fi
  367. if ! grep -q "DNSPort" /etc/tor/torrc; then
  368. echo 'DNSPort 53' >> /etc/tor/torrc
  369. fi
  370. if ! grep -q "DNSListenAddress 127.0.0.1" /etc/tor/torrc; then
  371. echo 'DNSListenAddress 127.0.0.1' >> /etc/tor/torrc
  372. fi
  373. if ! grep -q "DNSListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" /etc/tor/torrc; then
  374. echo "DNSListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" >> /etc/tor/torrc
  375. fi
  376. mark_completed "${FUNCNAME[0]}"
  377. }
  378. function get_app_onion_address {
  379. app_name="$1"
  380. mobilestr="$2"
  381. if [ ${#mobilestr} -gt 0 ]; then
  382. app_name="mobile${app_name}"
  383. fi
  384. if grep -q "${app_name} onion domain" "$COMPLETION_FILE"; then
  385. if grep -q "${app_name} onion domain" "$COMPLETION_FILE"; then
  386. grep "${app_name} onion domain" "${COMPLETION_FILE}" | head -n 1 | awk -F ':' '{print $2}'
  387. return
  388. fi
  389. fi
  390. echo ""
  391. }
  392. function tor_add_bridge {
  393. bridge_ip_address="$1"
  394. bridge_port="$2"
  395. bridge_key="$3"
  396. bridge_type='obfs4'
  397. if [[ "$bridge_ip_address" != *"."* ]]; then
  398. return
  399. fi
  400. if [ ${#bridge_port} -eq 0 ]; then
  401. return
  402. fi
  403. if [ ${#bridge_key} -eq 0 ]; then
  404. return
  405. fi
  406. apt-get -yq install obfs4proxy
  407. if [ ! -f /etc/torrc.d/bridges ]; then
  408. { echo 'ClientTransportPlugin obfs4 exec /usr/bin/obfs4proxy managed';
  409. echo 'UseBridges 1';
  410. echo "Bridge $bridge_type ${bridge_ip_address}:${bridge_port} ${bridge_key}"; } > /etc/torrc.d/bridges
  411. else
  412. if ! grep -q "Bridge $bridge_type ${bridge_ip_address}:${bridge_port} ${bridge_key}" /etc/torrc.d/bridges; then
  413. echo "Bridge $bridge_type ${bridge_ip_address}:${bridge_port} ${bridge_key}" >> /etc/torrc.d/bridges
  414. fi
  415. fi
  416. systemctl restart tor
  417. }
  418. function tor_remove_bridge {
  419. bridge_ip_address="$1"
  420. bridge_type='obfs4'
  421. if [[ "$bridge_ip_address" == *"."* ]]; then
  422. bridge_str="Bridge $bridge_type ${bridge_ip_address}"
  423. else
  424. if grep -q " ${bridge_ip_address}" /etc/torrc.d/bridges; then
  425. bridge_str=" ${bridge_ip_address}"
  426. else
  427. return
  428. fi
  429. fi
  430. if grep -q "${bridge_str}" /etc/torrc.d/bridges; then
  431. sed -i "/${bridge_str}/d" /etc/torrc.d/bridges
  432. fi
  433. # If there are no bridges remaining then remove the file
  434. if ! grep -q "Bridge " /etc/torrc.d/bridges; then
  435. rm /etc/torrc.d/bridges
  436. fi
  437. systemctl restart tor
  438. }
  439. function tor_create_bridge_relay {
  440. read_config_param 'TOR_BRIDGE_PORT'
  441. read_config_param 'TOR_BRIDGE_NICKNAME'
  442. read_config_param 'MY_EMAIL_ADDRESS'
  443. if [ ! "$TOR_BRIDGE_PORT" ]; then
  444. return
  445. fi
  446. if [ ${#TOR_BRIDGE_PORT} -eq 0 ]; then
  447. return
  448. fi
  449. if [ ${#TOR_BRIDGE_NICKNAME} -eq 0 ]; then
  450. return
  451. fi
  452. apt-get -yq install obfs4proxy
  453. { echo 'BridgeRelay 1';
  454. echo 'ServerTransportPlugin obfs4 exec /usr/bin/obfs4proxy';
  455. echo "ExtORPort $TOR_BRIDGE_PORT";
  456. echo "ContactInfo $MY_EMAIL_ADDRESS";
  457. echo "Nickname $TOR_BRIDGE_NICKNAME"; } > /etc/torrc.d/bridgerelay
  458. firewall_add tor_bridge "$TOR_BRIDGE_PORT" tcp
  459. systemctl restart tor
  460. }
  461. function tor_remove_bridge_relay {
  462. if [ -f /etc/torrc.d/bridgerelay ]; then
  463. rm /etc/torrc.d/bridgerelay
  464. fi
  465. read_config_param 'TOR_BRIDGE_PORT'
  466. firewall_remove "$TOR_BRIDGE_PORT" tcp
  467. systemctl restart tor
  468. }
  469. # NOTE: deliberately no exit 0