freedombone-syncthing 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Checks for changed syncthing device IDs within user home directories
  12. # and then recreates the syncthing configuration file accordingly
  13. #
  14. # License
  15. # =======
  16. #
  17. # Copyright (C) 2016 Bob Mottram <bob@robotics.uk.to>
  18. #
  19. # This program is free software: you can redistribute it and/or modify
  20. # it under the terms of the GNU Affero General Public License as published by
  21. # the Free Software Foundation, either version 3 of the License, or
  22. # (at your option) any later version.
  23. #
  24. # This program is distributed in the hope that it will be useful,
  25. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. # GNU Affero General Public License for more details.
  28. #
  29. # You should have received a copy of the GNU Affero General Public License
  30. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. NO_OF_ARGS=$#
  32. PROJECT_NAME='freedombone'
  33. export TEXTDOMAIN=$PROJECT_NAME-syncthing
  34. export TEXTDOMAINDIR="/usr/share/locale"
  35. # File which keeps track of what has already been installed
  36. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  37. SYNCTHING_ID=
  38. SYNCTHING_CONFIG_PATH=/root/.config/syncthing
  39. SYNCTHING_CONFIG_FILE=$SYNCTHING_CONFIG_PATH/config.xml
  40. SYNCTHING_RELAY_SERVER='https://relays.syncthing.net/endpoint'
  41. SYNCTHING_RELEASES='https://api.github.com/repos/syncthing/syncthing/releases?per_page=30'
  42. SYNCTHING_PORT=22000
  43. SYNCTHING_SHARED_DATA=/var/lib/syncthing/SyncShared
  44. SYNCTHING_USER_IDS_FILE='.syncthingids'
  45. SYNCTHING_UPDATE_FILE='.syncthing-update'
  46. CHANGED=
  47. TEMP_IDS_FILE=/root/.synthingids
  48. function new_syncthing_id {
  49. for i in {1..8}
  50. do
  51. v=""
  52. for j in {1..2}
  53. do
  54. v2=`echo "obase=16;$RANDOM" | bc`
  55. v=$v$v2
  56. done
  57. v=$(echo "$v" | cut -c1-7)
  58. if [[ ${i} < 8 ]]; then
  59. v=$v"-"
  60. fi
  61. echo -n "$v"
  62. done
  63. echo "$v"
  64. }
  65. function create_syncthing_config {
  66. if grep -q "Syncthing ID" $COMPLETION_FILE; then
  67. SYNCTHING_ID=$(cat $COMPLETION_FILE | grep "Syncthing ID" | awk -F ':' '{print $2}')
  68. else
  69. if [ -f $SYNCTHING_CONFIG_FILE ]; then
  70. SYNCTHING_ID=$(cat $SYNCTHING_CONFIG_FILE | grep "device id=" | head -n 1 | awk -F '"' '{print $2}')
  71. else
  72. SYNCTHING_ID=$(new_syncthing_id)
  73. fi
  74. fi
  75. if ! grep -q "Syncthing ID" $COMPLETION_FILE; then
  76. echo "Syncthing ID:$SYNCTHING_ID" >> $COMPLETION_FILE
  77. else
  78. sed -i "s|Syncthing ID.*|Syncthing ID:$SYNCTHING_ID|g" $COMPLETION_FILE
  79. fi
  80. if [ ! -d $SYNCTHING_CONFIG_PATH ]; then
  81. mkdir -p $SYNCTHING_CONFIG_PATH
  82. fi
  83. if [ ! -d $SYNCTHING_SHARED_DATA ]; then
  84. mkdir -p $SYNCTHING_SHARED_DATA
  85. fi
  86. echo '<configuration version="12">' > $SYNCTHING_CONFIG_FILE
  87. for d in /home/*/ ; do
  88. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  89. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  90. echo " <folder id=\"$USERNAME\" path=\"/home/$USERNAME/Sync/\" ro=\"false\" rescanIntervalS=\"60\" ignorePerms=\"false\" autoNormalize=\"true\">" >> $SYNCTHING_CONFIG_FILE
  91. # include any specified device IDs for this user
  92. if [ -f /home/$USERNAME/$SYNCTHING_USER_IDS_FILE ]; then
  93. echo "" > $TEMP_IDS_FILE
  94. while read -r line || [[ -n "$line" ]]; do
  95. line2="$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  96. if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then
  97. if [ ${#line2} -gt 10 ]; then
  98. if ! grep -q "$line2" $TEMP_IDS_FILE; then
  99. echo " <device id=\"$line2\"></device>" >> $SYNCTHING_CONFIG_FILE
  100. echo "$line2" >> $TEMP_IDS_FILE
  101. fi
  102. fi
  103. fi
  104. done < /home/$USERNAME/$SYNCTHING_USER_IDS_FILE
  105. rm $TEMP_IDS_FILE
  106. fi
  107. echo " <device id=\"$SYNCTHING_ID\"></device>" >> $SYNCTHING_CONFIG_FILE
  108. echo ' <minDiskFreePct>1</minDiskFreePct>' >> $SYNCTHING_CONFIG_FILE
  109. echo ' <versioning></versioning>' >> $SYNCTHING_CONFIG_FILE
  110. echo ' <copiers>0</copiers>' >> $SYNCTHING_CONFIG_FILE
  111. echo ' <pullers>0</pullers>' >> $SYNCTHING_CONFIG_FILE
  112. echo ' <hashers>0</hashers>' >> $SYNCTHING_CONFIG_FILE
  113. echo ' <order>random</order>' >> $SYNCTHING_CONFIG_FILE
  114. echo ' <ignoreDelete>false</ignoreDelete>' >> $SYNCTHING_CONFIG_FILE
  115. echo ' <scanProgressIntervalS>0</scanProgressIntervalS>' >> $SYNCTHING_CONFIG_FILE
  116. echo ' <pullerSleepS>0</pullerSleepS>' >> $SYNCTHING_CONFIG_FILE
  117. echo ' <pullerPauseS>0</pullerPauseS>' >> $SYNCTHING_CONFIG_FILE
  118. echo ' <maxConflicts>10</maxConflicts>' >> $SYNCTHING_CONFIG_FILE
  119. echo ' <disableSparseFiles>false</disableSparseFiles>' >> $SYNCTHING_CONFIG_FILE
  120. echo ' </folder>' >> $SYNCTHING_CONFIG_FILE
  121. fi
  122. done
  123. echo " <folder id=\"shared\" path=\"$SYNCTHING_SHARED_DATA/\" ro=\"false\" rescanIntervalS=\"60\" ignorePerms=\"false\" autoNormalize=\"true\">" >> $SYNCTHING_CONFIG_FILE
  124. # all user devices may access this shared directory
  125. echo "" > $TEMP_IDS_FILE
  126. for d in /home/*/ ; do
  127. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  128. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  129. if [ -f /home/$USERNAME/$SYNCTHING_USER_IDS_FILE ]; then
  130. while read -r line || [[ -n "$line" ]]; do
  131. line2="$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  132. if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then
  133. if [ ${#line2} -gt 10 ]; then
  134. if ! grep -q "$line2" $TEMP_IDS_FILE; then
  135. echo " <device id=\"$line2\"></device>" >> $SYNCTHING_CONFIG_FILE
  136. echo "$line2" >> $TEMP_IDS_FILE
  137. fi
  138. fi
  139. fi
  140. done < /home/$USERNAME/$SYNCTHING_USER_IDS_FILE
  141. fi
  142. fi
  143. done
  144. rm $TEMP_IDS_FILE
  145. echo " <device id=\"$SYNCTHING_ID\"></device>" >> $SYNCTHING_CONFIG_FILE
  146. echo ' <minDiskFreePct>1</minDiskFreePct>' >> $SYNCTHING_CONFIG_FILE
  147. echo ' <versioning></versioning>' >> $SYNCTHING_CONFIG_FILE
  148. echo ' <copiers>0</copiers>' >> $SYNCTHING_CONFIG_FILE
  149. echo ' <pullers>0</pullers>' >> $SYNCTHING_CONFIG_FILE
  150. echo ' <hashers>0</hashers>' >> $SYNCTHING_CONFIG_FILE
  151. echo ' <order>random</order>' >> $SYNCTHING_CONFIG_FILE
  152. echo ' <ignoreDelete>false</ignoreDelete>' >> $SYNCTHING_CONFIG_FILE
  153. echo ' <scanProgressIntervalS>0</scanProgressIntervalS>' >> $SYNCTHING_CONFIG_FILE
  154. echo ' <pullerSleepS>0</pullerSleepS>' >> $SYNCTHING_CONFIG_FILE
  155. echo ' <pullerPauseS>0</pullerPauseS>' >> $SYNCTHING_CONFIG_FILE
  156. echo ' <maxConflicts>10</maxConflicts>' >> $SYNCTHING_CONFIG_FILE
  157. echo ' <disableSparseFiles>false</disableSparseFiles>' >> $SYNCTHING_CONFIG_FILE
  158. echo ' </folder>' >> $SYNCTHING_CONFIG_FILE
  159. echo " <device id=\"$SYNCTHING_ID\" name=\"${PROJECT_NAME}\" compression=\"metadata\" introducer=\"false\">" >> $SYNCTHING_CONFIG_FILE
  160. echo ' <address>dynamic</address>' >> $SYNCTHING_CONFIG_FILE
  161. echo ' </device>' >> $SYNCTHING_CONFIG_FILE
  162. echo "" > $TEMP_IDS_FILE
  163. for d in /home/*/ ; do
  164. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  165. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  166. if [ -f /home/$USERNAME/$SYNCTHING_USER_IDS_FILE ]; then
  167. while read -r line || [[ -n "$line" ]]; do
  168. line2="$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  169. if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then
  170. if [ ${#line2} -gt 10 ]; then
  171. if ! grep -q "$line2" $TEMP_IDS_FILE; then
  172. echo " <device id=\"$line2\" name=\"${USERNAME}\" compression=\"metadata\" introducer=\"false\">" >> $SYNCTHING_CONFIG_FILE
  173. echo ' <address>dynamic</address>' >> $SYNCTHING_CONFIG_FILE
  174. echo ' </device>' >> $SYNCTHING_CONFIG_FILE
  175. echo "$line2" >> $TEMP_IDS_FILE
  176. fi
  177. fi
  178. fi
  179. done < /home/$USERNAME/$SYNCTHING_USER_IDS_FILE
  180. fi
  181. fi
  182. done
  183. rm $TEMP_IDS_FILE
  184. echo ' <options>' >> $SYNCTHING_CONFIG_FILE
  185. echo " <listenAddress>tcp://0.0.0.0:$SYNCTHING_PORT</listenAddress>" >> $SYNCTHING_CONFIG_FILE
  186. echo ' <globalAnnounceServer>default</globalAnnounceServer>' >> $SYNCTHING_CONFIG_FILE
  187. echo ' <globalAnnounceEnabled>true</globalAnnounceEnabled>' >> $SYNCTHING_CONFIG_FILE
  188. echo ' <localAnnounceEnabled>true</localAnnounceEnabled>' >> $SYNCTHING_CONFIG_FILE
  189. echo ' <localAnnouncePort>21027</localAnnouncePort>' >> $SYNCTHING_CONFIG_FILE
  190. echo ' <localAnnounceMCAddr>[ff12::8384]:21027</localAnnounceMCAddr>' >> $SYNCTHING_CONFIG_FILE
  191. echo " <relayServer>dynamic+$SYNCTHING_RELAY_SERVER</relayServer>" >> $SYNCTHING_CONFIG_FILE
  192. echo ' <maxSendKbps>0</maxSendKbps>' >> $SYNCTHING_CONFIG_FILE
  193. echo ' <maxRecvKbps>0</maxRecvKbps>' >> $SYNCTHING_CONFIG_FILE
  194. echo ' <reconnectionIntervalS>60</reconnectionIntervalS>' >> $SYNCTHING_CONFIG_FILE
  195. echo ' <relaysEnabled>true</relaysEnabled>' >> $SYNCTHING_CONFIG_FILE
  196. echo ' <relayReconnectIntervalM>10</relayReconnectIntervalM>' >> $SYNCTHING_CONFIG_FILE
  197. echo ' <startBrowser>true</startBrowser>' >> $SYNCTHING_CONFIG_FILE
  198. echo ' <upnpEnabled>true</upnpEnabled>' >> $SYNCTHING_CONFIG_FILE
  199. echo ' <upnpLeaseMinutes>60</upnpLeaseMinutes>' >> $SYNCTHING_CONFIG_FILE
  200. echo ' <upnpRenewalMinutes>30</upnpRenewalMinutes>' >> $SYNCTHING_CONFIG_FILE
  201. echo ' <upnpTimeoutSeconds>10</upnpTimeoutSeconds>' >> $SYNCTHING_CONFIG_FILE
  202. echo ' <urAccepted>-1</urAccepted>' >> $SYNCTHING_CONFIG_FILE
  203. echo ' <urUniqueID></urUniqueID>' >> $SYNCTHING_CONFIG_FILE
  204. echo ' <urURL>https://data.syncthing.net/newdata</urURL>' >> $SYNCTHING_CONFIG_FILE
  205. echo ' <urPostInsecurely>false</urPostInsecurely>' >> $SYNCTHING_CONFIG_FILE
  206. echo ' <urInitialDelayS>1800</urInitialDelayS>' >> $SYNCTHING_CONFIG_FILE
  207. echo ' <restartOnWakeup>true</restartOnWakeup>' >> $SYNCTHING_CONFIG_FILE
  208. echo ' <autoUpgradeIntervalH>12</autoUpgradeIntervalH>' >> $SYNCTHING_CONFIG_FILE
  209. echo ' <keepTemporariesH>24</keepTemporariesH>' >> $SYNCTHING_CONFIG_FILE
  210. echo ' <cacheIgnoredFiles>true</cacheIgnoredFiles>' >> $SYNCTHING_CONFIG_FILE
  211. echo ' <progressUpdateIntervalS>5</progressUpdateIntervalS>' >> $SYNCTHING_CONFIG_FILE
  212. echo ' <symlinksEnabled>true</symlinksEnabled>' >> $SYNCTHING_CONFIG_FILE
  213. echo ' <limitBandwidthInLan>false</limitBandwidthInLan>' >> $SYNCTHING_CONFIG_FILE
  214. echo ' <minHomeDiskFreePct>1</minHomeDiskFreePct>' >> $SYNCTHING_CONFIG_FILE
  215. echo " <releasesURL>$SYNCTHING_RELEASES</releasesURL>" >> $SYNCTHING_CONFIG_FILE
  216. echo ' </options>' >> $SYNCTHING_CONFIG_FILE
  217. echo '</configuration>' >> $SYNCTHING_CONFIG_FILE
  218. # give each user account a file containing the device id for this server
  219. # This allows it to appear within the user control panel
  220. for d in /home/*/ ; do
  221. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  222. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  223. echo "$SYNCTHING_ID" > /home/$USERNAME/.syncthing-server-id
  224. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthing-server-id
  225. fi
  226. done
  227. }
  228. function user_devices_changed {
  229. CHANGED=
  230. if [ ! -f $SYNCTHING_CONFIG_FILE ]; then
  231. CHANGED=1
  232. return
  233. fi
  234. if ! grep -q "${PROJECT_NAME}" $SYNCTHING_CONFIG_FILE; then
  235. CHANGED=1
  236. return
  237. fi
  238. for d in /home/*/ ; do
  239. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  240. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  241. if [ -f /home/$USERNAME/$SYNCTHING_UPDATE_FILE ]; then
  242. CHANGED=1
  243. fi
  244. if [ -f /home/$USERNAME/$SYNCTHING_USER_IDS_FILE ]; then
  245. while read -r line || [[ -n "$line" ]]; do
  246. if [[ $line != *"#"* && $line != *"*"* && $line != *'/'* && $line == *"-"* ]]; then
  247. if [ ${#line} -gt 10 ]; then
  248. if ! grep -q "$line" $SYNCTHING_CONFIG_FILE; then
  249. CHANGED=1
  250. fi
  251. fi
  252. fi
  253. done < /home/$USERNAME/$SYNCTHING_USER_IDS_FILE
  254. fi
  255. fi
  256. done
  257. }
  258. user_devices_changed
  259. if [ $CHANGED ]; then
  260. create_syncthing_config
  261. systemctl restart syncthing
  262. fi
  263. exit 0