freedombone-app-syncthing 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Syncthing application
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. VARIANTS='full full-vim cloud'
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=0
  33. SYNCTHING_ID=
  34. SYNCTHING_CONFIG_PATH=/root/.config/syncthing
  35. SYNCTHING_CONFIG_FILE=$SYNCTHING_CONFIG_PATH/config.xml
  36. SYNCTHING_RELAY_SERVER='https://relays.syncthing.net/endpoint'
  37. SYNCTHING_RELEASES='https://api.github.com/repos/syncthing/syncthing/releases?per_page=30'
  38. SYNCTHING_PORT=22000
  39. SYNCTHING_SHARED_DATA=/var/lib/syncthing/SyncShared
  40. SYNCTHING_USER_IDS_FILE='.syncthingids'
  41. syncthing_variables=(SYNCTHING_ID
  42. SYNCTHING_CONFIG_PATH
  43. SYNCTHING_CONFIG_FILE
  44. SYNCTHING_RELAY_SERVER
  45. SYNCTHING_RELEASES
  46. SYNCTHING_PORT
  47. SYNCTHING_SHARED_DATA
  48. USB_MOUNT)
  49. function logging_on_syncthing {
  50. echo -n ''
  51. }
  52. function logging_off_syncthing {
  53. echo -n ''
  54. }
  55. function syncthing_create_ids_file {
  56. if [ ! -f ~/.syncthing-server-id ]; then
  57. return
  58. fi
  59. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  60. if [ ! -f $SYNCTHING_CONFIG_FILE ]; then
  61. { echo $'# Your syncthing configuration file';
  62. echo '#';
  63. echo $"# The ${PROJECT_NAME} syncthing ID is: $SYNCTHING_ID";
  64. echo '#';
  65. echo '# Paste the IDs of your devices below';
  66. echo '#'; } > $SYNCTHING_CONFIG_FILE
  67. fi
  68. }
  69. function syncthing_manual_edit {
  70. if [ ! -f ~/.syncthing-server-id ]; then
  71. return
  72. fi
  73. syncthing_create_ids_file
  74. editor $SYNCTHING_CONFIG_FILE
  75. # force an update of the configuration
  76. touch ~/.syncthing-update
  77. }
  78. function syncthing_show_id {
  79. if [ ! -f ~/.syncthing-server-id ]; then
  80. return
  81. fi
  82. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  83. dialog --title $"Device ID for ${PROJECT_NAME}" \
  84. --backtitle $"Freedombone User Control Panel" \
  85. --msgbox $"In a desktop terminal press shift and select the ID below,\\nthen right click and copy.\\n\\nWithin Connectbot select Menu/Copy and then highlight the ID below\\n\\n$SYNCTHING_ID\\n\\nAlternatively press Enter to display a QR code which can be scanned." 13 78
  86. clear
  87. echo $'Your Syncthing ID code'
  88. echo ''
  89. echo -n "$SYNCTHING_ID" | qrencode -t UTF8
  90. echo ''
  91. # shellcheck disable=SC2034
  92. read -n1 -rsp $"Press any key to continue..." key
  93. }
  94. function syncthing_add_id {
  95. if [ ! -f ~/.syncthing-server-id ]; then
  96. return
  97. fi
  98. syncthing_create_ids_file
  99. data=$(mktemp 2>/dev/null)
  100. dialog --backtitle $"Freedombone User Control Panel" \
  101. --title $"Add a Syncthing device ID" \
  102. --form $"Paste the device ID for your laptop/desktop/netbook/phone/tablet below" 9 80 2 \
  103. $"Device ID:" 1 1 "" 1 26 80 80 \
  104. $"Description (optional):" 2 1 "" 2 26 80 80 \
  105. 2> "$data"
  106. sel=$?
  107. case $sel in
  108. 1) rm -f "$data"
  109. return;;
  110. 255) rm -f "$data"
  111. return;;
  112. esac
  113. SYNCTHING_DEVICE_ID=$(sed -n 1p < "$data")
  114. SYNCTHING_DESCRIPTION=$(sed -n 2p < "$data")
  115. rm -f "$data"
  116. if [ ${#SYNCTHING_DEVICE_ID} -lt 10 ]; then
  117. return
  118. fi
  119. if [[ $SYNCTHING_DEVICE_ID == *"#"* || $SYNCTHING_DEVICE_ID == *"*"* || $SYNCTHING_DEVICE_ID == *'/'* || $SYNCTHING_DEVICE_ID != *"-"* ]]; then
  120. dialog --title $"Add a Syncthing device ID" \
  121. --backtitle $"Freedombone User Control Panel" \
  122. --msgbox $"That doesn't look like a device ID" 6 50
  123. return
  124. fi
  125. if grep -q "$SYNCTHING_DEVICE_ID" $SYNCTHING_CONFIG_FILE; then
  126. dialog --title $"Add a Syncthing device ID" \
  127. --backtitle $"Freedombone User Control Panel" \
  128. --msgbox $"That ID has already been added" 6 50
  129. return
  130. fi
  131. if [ ${#SYNCTHING_DESCRIPTION} -gt 0 ]; then
  132. echo "# $SYNCTHING_DESCRIPTION" >> $SYNCTHING_CONFIG_FILE
  133. fi
  134. echo "$SYNCTHING_DEVICE_ID" >> $SYNCTHING_CONFIG_FILE
  135. # force an update of the configuration
  136. touch ~/.syncthing-update
  137. dialog --title $"Add a Syncthing device ID" \
  138. --backtitle $"Freedombone User Control Panel" \
  139. --msgbox $"The ID was added" 6 50
  140. }
  141. function syncthing_remove_id {
  142. if [ ! -f ~/.syncthing-server-id ]; then
  143. return
  144. fi
  145. syncthing_create_ids_file
  146. data=$(mktemp 2>/dev/null)
  147. dialog --backtitle $"Freedombone User Control Panel" \
  148. --title $"Remove a Syncthing device ID" \
  149. --form $"Paste the device ID which is to be removed below" 8 80 1 \
  150. $"Device ID:" 1 1 "" 1 14 80 80 \
  151. 2> "$data"
  152. sel=$?
  153. case $sel in
  154. 1) rm -f "$data"
  155. return;;
  156. 255) rm -f "$data"
  157. return;;
  158. esac
  159. SYNCTHING_DEVICE_ID=$(sed -n 1p < "$data")
  160. rm -f "$data"
  161. if [ ${#SYNCTHING_DEVICE_ID} -lt 10 ]; then
  162. return
  163. fi
  164. if [[ $SYNCTHING_DEVICE_ID == *"#"* || $SYNCTHING_DEVICE_ID == *"*"* || $SYNCTHING_DEVICE_ID == *'/'* || $SYNCTHING_DEVICE_ID != *"-"* ]]; then
  165. dialog --title $"Remove a Syncthing device ID" \
  166. --backtitle $"Freedombone User Control Panel" \
  167. --msgbox $"That doesn't look like a device ID" 6 50
  168. return
  169. fi
  170. if ! grep -q "$SYNCTHING_DEVICE_ID" $SYNCTHING_CONFIG_FILE; then
  171. dialog --title $"Remove a Syncthing device ID" \
  172. --backtitle $"Freedombone User Control Panel" \
  173. --msgbox $"That ID wasn't registered anyway" 6 50
  174. return
  175. fi
  176. sed -i "/$SYNCTHING_DEVICE_ID/d" $SYNCTHING_CONFIG_FILE
  177. # force an update of the configuration
  178. touch ~/.syncthing-update
  179. dialog --title $"Remove a Syncthing device ID" \
  180. --backtitle $"Freedombone User Control Panel" \
  181. --msgbox $"The ID was removed" 6 50
  182. }
  183. function run_client_syncthing {
  184. SYNCTHING_CONFIG_FILE=~/.syncthingids
  185. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  186. while true
  187. do
  188. data=$(mktemp 2>/dev/null)
  189. dialog --backtitle $"Freedombone User Control Panel" \
  190. --title $"File Synchronization" \
  191. --radiolist $"Choose an operation:" 12 70 6 \
  192. 1 $"Show device ID for ${PROJECT_NAME}" off \
  193. 2 $"Add an ID for another machine or device" off \
  194. 3 $"Remove an ID for another machine or device" off \
  195. 4 $"Manually edit device IDs" off \
  196. 5 $"Back to main menu" on 2> "$data"
  197. sel=$?
  198. case $sel in
  199. 1) rm -f "$data"
  200. break;;
  201. 255) rm -f "$data"
  202. break;;
  203. esac
  204. case $(cat "$data") in
  205. 1) syncthing_show_id;;
  206. 2) syncthing_add_id;;
  207. 3) syncthing_remove_id;;
  208. 4) syncthing_manual_edit;;
  209. 5) rm -f "$data"
  210. break;;
  211. esac
  212. rm -f "$data"
  213. done
  214. }
  215. function install_interactive_syncthing {
  216. echo -n ''
  217. APP_INSTALLED=1
  218. }
  219. function reconfigure_syncthing {
  220. echo -n ''
  221. }
  222. function upgrade_syncthing {
  223. echo -n ''
  224. }
  225. function backup_local_syncthing {
  226. if [ -d $SYNCTHING_SHARED_DATA ]; then
  227. function_check backup_directory_to_usb
  228. backup_directory_to_usb $SYNCTHING_SHARED_DATA syncthingshared
  229. backup_directory_to_usb $SYNCTHING_CONFIG_PATH syncthingconfig
  230. fi
  231. for d in /home/*/ ; do
  232. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  233. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  234. if [ -d "/home/$USERNAME/Sync" ]; then
  235. echo $"Backing up syncthing files for $USERNAME"
  236. backup_directory_to_usb "/home/$USERNAME/Sync" "syncthing/$USERNAME"
  237. # ensure that device IDs will be backed up as part of user config settings
  238. if [ ! -d "/home/$USERNAME/.config/syncthing" ]; then
  239. mkdir -p "/home/$USERNAME/.config/syncthing"
  240. chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/.config"
  241. fi
  242. if [ -f "/home/$USERNAME/.syncthing-server-id" ]; then
  243. cp "/home/$USERNAME/.syncthing-server-id" "/home/$USERNAME/.config/syncthing"
  244. chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/.config"
  245. fi
  246. if [ -f "/home/$USERNAME/.syncthingids" ]; then
  247. cp "/home/$USERNAME/.syncthingids" "/home/$USERNAME/.config/syncthing"
  248. chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/.config"
  249. fi
  250. fi
  251. fi
  252. done
  253. }
  254. function restore_local_syncthing {
  255. if [ -f /etc/systemd/system/syncthing.service ]; then
  256. systemctl stop syncthing
  257. systemctl stop cron
  258. fi
  259. temp_restore_dir=/root/tempsyncthing
  260. if [ -d "$USB_MOUNT/backup/syncthingconfig" ]; then
  261. echo $"Restoring syncthing configuration"
  262. function_check restore_directory_from_usb
  263. restore_directory_from_usb ${temp_restore_dir}config syncthingconfig
  264. #cp -r ${temp_restore_dir}config/* /
  265. if [ ! -d $SYNCTHING_CONFIG_PATH ]; then
  266. mkdir -p $SYNCTHING_CONFIG_PATH
  267. fi
  268. if ! cp -r ${temp_restore_dir}config/* $SYNCTHING_CONFIG_PATH/; then
  269. set_user_permissions
  270. backup_unmount_drive
  271. systemctl start syncthing
  272. systemctl start cron
  273. exit 6833
  274. fi
  275. rm -rf ${temp_restore_dir}config
  276. fi
  277. if [ -d "$USB_MOUNT/backup/syncthingshared" ]; then
  278. echo $"Restoring syncthing shared files"
  279. restore_directory_from_usb ${temp_restore_dir}shared syncthingshared
  280. #cp -r ${temp_restore_dir}shared/* /
  281. if [ ! -d $SYNCTHING_SHARED_DATA ]; then
  282. mkdir -p $SYNCTHING_SHARED_DATA
  283. fi
  284. cp -r ${temp_restore_dir}shared/* $SYNCTHING_SHARED_DATA/
  285. rm -rf ${temp_restore_dir}shared
  286. fi
  287. if [ -d "$USB_MOUNT/backup/syncthing" ]; then
  288. for d in $USB_MOUNT/backup/syncthing/*/ ; do
  289. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  290. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  291. if [ ! -d "/home/$USERNAME" ]; then
  292. "${PROJECT_NAME}-adduser" "$USERNAME"
  293. fi
  294. echo $"Restoring syncthing files for $USERNAME"
  295. restore_directory_from_usb "${temp_restore_dir}" "syncthing/$USERNAME"
  296. if [ -d "${temp_restore_dir}/home/$USERNAME/Sync" ]; then
  297. cp -r "${temp_restore_dir}/home/$USERNAME/Sync" "/home/$USERNAME/"
  298. else
  299. if [ ! -d "/home/$USERNAME/Sync" ]; then
  300. mkdir "/home/$USERNAME/Sync"
  301. fi
  302. if [ -d /root/Sync ]; then
  303. cp -r /root/Sync/* "/home/$USERNAME/Sync/"
  304. rm -rf /root/Sync
  305. else
  306. cp -r "${temp_restore_dir}/"* "/home/$USERNAME/Sync/"
  307. fi
  308. fi
  309. # shellcheck disable=SC2181
  310. if [ ! "$?" = "0" ]; then
  311. rm -rf ${temp_restore_dir}
  312. set_user_permissions
  313. backup_unmount_drive
  314. systemctl start syncthing
  315. systemctl start cron
  316. exit 68438
  317. fi
  318. rm -rf ${temp_restore_dir}
  319. # restore device IDs from config settings
  320. if [ -f "/home/$USERNAME/.config/syncthing/.syncthing-server-id" ]; then
  321. cp "/home/$USERNAME/.config/syncthing/.syncthing-server-id" "/home/$USERNAME/.syncthing-server-id"
  322. chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.syncthing-server-id"
  323. fi
  324. if [ -f "/home/$USERNAME/.config/syncthing/.syncthingids" ]; then
  325. cp "/home/$USERNAME/.config/syncthing/.syncthingids" "/home/$USERNAME/.syncthingids"
  326. chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.syncthingids"
  327. fi
  328. fi
  329. done
  330. fi
  331. if [ -f /etc/systemd/system/syncthing.service ]; then
  332. systemctl start syncthing
  333. systemctl start cron
  334. fi
  335. }
  336. function backup_remote_syncthing {
  337. if [ -d $SYNCTHING_CONFIG_PATH ]; then
  338. echo $"Backing up syncthing configuration"
  339. function_check backup_directory_to_friend
  340. backup_directory_to_friend $SYNCTHING_CONFIG_PATH syncthingconfig
  341. echo $"Backup of syncthing configuration complete"
  342. fi
  343. if [ -d $SYNCTHING_SHARED_DATA ]; then
  344. echo $"Backing up syncthing shared files"
  345. function_check backup_directory_to_friend
  346. backup_directory_to_friend $SYNCTHING_SHARED_DATA syncthingshared
  347. echo $"Backup of syncthing shared files complete"
  348. fi
  349. for d in /home/*/ ; do
  350. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  351. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  352. if [ -d "/home/$USERNAME/Sync" ]; then
  353. echo $"Backing up syncthing files for $USERNAME"
  354. backup_directory_to_friend "/home/$USERNAME/Sync" "syncthing/$USERNAME"
  355. # ensure that device IDs will be backed up as part of user config settings
  356. if [ ! -d "/home/$USERNAME/.config/syncthing" ]; then
  357. mkdir -p "/home/$USERNAME/.config/syncthing"
  358. chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/.config"
  359. fi
  360. if [ -f "/home/$USERNAME/.syncthing-server-id" ]; then
  361. cp "/home/$USERNAME/.syncthing-server-id" "/home/$USERNAME/.config/syncthing"
  362. chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/.config"
  363. fi
  364. if [ -f "/home/$USERNAME/.syncthingids" ]; then
  365. cp "/home/$USERNAME/.syncthingids" "/home/$USERNAME/.config/syncthing"
  366. chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/.config"
  367. fi
  368. fi
  369. fi
  370. done
  371. }
  372. function restore_remote_syncthing {
  373. if [ -f /etc/systemd/system/syncthing.service ]; then
  374. systemctl stop syncthing
  375. systemctl stop cron
  376. fi
  377. if [ -d "$SERVER_DIRECTORY/backup/syncthingconfig" ]; then
  378. echo $"Restoring syncthing configuration"
  379. temp_restore_dir=/root/tempsyncthingconfig
  380. function_check restore_directory_from_friend
  381. restore_directory_from_friend $temp_restore_dir syncthingconfig
  382. #cp -r $temp_restore_dir/* /
  383. if [ ! -d $SYNCTHING_CONFIG_PATH ]; then
  384. mkdir -p $SYNCTHING_CONFIG_PATH
  385. fi
  386. if ! cp -r ${temp_restore_dir}/* $SYNCTHING_CONFIG_PATH/; then
  387. systemctl start syncthing
  388. systemctl start cron
  389. exit 6833
  390. fi
  391. rm -rf $temp_restore_dir
  392. fi
  393. if [ -d "$SERVER_DIRECTORY/backup/syncthingshared" ]; then
  394. echo $"Restoring syncthing shared files"
  395. temp_restore_dir=/root/tempsyncthingshared
  396. function_check restore_directory_from_friend
  397. restore_directory_from_friend $temp_restore_dir syncthingshared
  398. if [ ! -d $SYNCTHING_SHARED_DATA ]; then
  399. mkdir -p $SYNCTHING_SHARED_DATA
  400. fi
  401. cp -r ${temp_restore_dir}/* $SYNCTHING_SHARED_DATA/
  402. rm -rf ${temp_restore_dir}
  403. fi
  404. if [ -d "$SERVER_DIRECTORY/backup/syncthing" ]; then
  405. for d in $SERVER_DIRECTORY/backup/syncthing/*/ ; do
  406. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  407. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  408. if [ ! -d "/home/$USERNAME" ]; then
  409. "${PROJECT_NAME}-adduser" "$USERNAME"
  410. fi
  411. echo $"Restoring syncthing files for $USERNAME"
  412. temp_restore_dir=/root/tempsyncthing
  413. function_check restore_directory_from_friend
  414. restore_directory_from_friend "$temp_restore_dir" "syncthing/$USERNAME"
  415. if [ -d "$temp_restore_dir/home/$USERNAME/Sync" ]; then
  416. cp -r "$temp_restore_dir/home/$USERNAME/Sync" "/home/$USERNAME/"
  417. else
  418. if [ ! -d "/home/$USERNAME/Sync" ]; then
  419. mkdir "/home/$USERNAME/Sync"
  420. fi
  421. if [ -d /root/Sync ]; then
  422. cp -r /root/Sync/* "/home/$USERNAME/Sync/"
  423. rm -rf /root/Sync
  424. else
  425. cp -r "${temp_restore_dir}/"* "/home/$USERNAME/Sync/"
  426. fi
  427. fi
  428. # shellcheck disable=SC2181
  429. if [ ! "$?" = "0" ]; then
  430. rm -rf $temp_restore_dir
  431. systemctl start syncthing
  432. systemctl start cron
  433. exit 68438
  434. fi
  435. rm -rf $temp_restore_dir
  436. # restore device IDs from config settings
  437. if [ -f "/home/$USERNAME/.config/syncthing/.syncthing-server-id" ]; then
  438. cp "/home/$USERNAME/.config/syncthing/.syncthing-server-id" "/home/$USERNAME/.syncthing-server-id"
  439. chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.syncthing-server-id"
  440. fi
  441. if [ -f "/home/$USERNAME/.config/syncthing/.syncthingids" ]; then
  442. cp "/home/$USERNAME/.config/syncthing/.syncthingids" "/home/$USERNAME/.syncthingids"
  443. chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.syncthingids"
  444. fi
  445. echo $"Restore of syncthing files for $USERNAME complete"
  446. fi
  447. done
  448. fi
  449. if [ -f /etc/systemd/system/syncthing.service ]; then
  450. systemctl start syncthing
  451. systemctl start cron
  452. fi
  453. }
  454. function remove_syncthing {
  455. firewall_remove ${SYNCTHING_PORT}
  456. systemctl stop syncthing
  457. systemctl disable syncthing
  458. rm /etc/systemd/system/syncthing.service
  459. systemctl daemon-reload
  460. apt-get -yq remove --purge syncthing
  461. sed -i "/${PROJECT_NAME}-syncthing/d" /etc/crontab
  462. remove_completion_param install_syncthing
  463. remove_completion_param configure_firewall_for_syncthing
  464. systemctl restart cron
  465. }
  466. function configure_firewall_for_syncthing {
  467. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  468. return
  469. fi
  470. firewall_add Syncthing ${SYNCTHING_PORT}
  471. mark_completed "${FUNCNAME[0]}"
  472. }
  473. function install_syncthing_repo {
  474. if [ -f /etc/apt/sources.list.d/syncthing.list ]; then
  475. return
  476. fi
  477. apt-get -yq install curl
  478. curl -s https://syncthing.net/release-key.txt | apt-key add -
  479. echo "deb http://apt.syncthing.net/ syncthing release" | tee /etc/apt/sources.list.d/syncthing.list
  480. apt-get update
  481. }
  482. function install_syncthing {
  483. install_syncthing_repo
  484. apt-get -yq install syncthing
  485. # This probably does need to run as root so that it can access the Sync directories
  486. # in each user's home directory
  487. { echo '[Unit]';
  488. echo 'Description=Syncthing - Open Source Continuous File Synchronization';
  489. echo 'Documentation=man:syncthing(1)';
  490. echo 'After=network.target';
  491. echo 'Wants=syncthing-inotify@.service';
  492. echo '';
  493. echo '[Service]';
  494. echo 'User=root';
  495. echo "Environment='all_proxy=socks5://localhost:9050'";
  496. echo 'ExecStart=/usr/bin/syncthing -no-browser -no-restart -logflags=0';
  497. echo 'Restart=on-failure';
  498. echo 'SuccessExitStatus=3 4';
  499. echo 'RestartForceExitStatus=3 4';
  500. echo '';
  501. echo '[Install]';
  502. echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/syncthing.service
  503. systemctl enable syncthing
  504. systemctl daemon-reload
  505. systemctl start syncthing
  506. function_check cron_add_mins
  507. cron_add_mins 1 "/usr/local/bin/${PROJECT_NAME}-syncthing > /dev/null"
  508. function_check configure_firewall_for_syncthing
  509. configure_firewall_for_syncthing
  510. APP_INSTALLED=1
  511. }
  512. # NOTE: deliberately no exit 0