freedombone-app-synapse 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # synapse matrix server
  12. #
  13. # https://matrix.org/blog/2016/02/10/advanced-synapse-setup-with-lets-encrypt/
  14. #
  15. # Had no success in configuring this with Riot, and the identity server
  16. # turned out to be centralised.
  17. # https://github.com/matrix-org/matrix-doc/issues/712
  18. # Possibly it might become decentralised in future.
  19. #
  20. # License
  21. # =======
  22. #
  23. # Copyright (C) 2016 Bob Mottram <bob@freedombone.net>
  24. #
  25. # This program is free software: you can redistribute it and/or modify
  26. # it under the terms of the GNU Affero General Public License as published by
  27. # the Free Software Foundation, either version 3 of the License, or
  28. # (at your option) any later version.
  29. #
  30. # This program is distributed in the hope that it will be useful,
  31. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. # GNU Affero General Public License for more details.
  34. #
  35. # You should have received a copy of the GNU Affero General Public License
  36. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  37. VARIANTS=''
  38. IN_DEFAULT_INSTALL=0
  39. SHOW_ON_ABOUT=0
  40. SYNAPSE_PORT=8448
  41. SYNAPSE_PASSWORD=
  42. SYNAPSE_DIR=/etc/matrix-synapse
  43. MATRIX_IRC_REPO="https://github.com/matrix-org/matrix-appservice-irc"
  44. synapse_variables=(ONION_ONLY
  45. MY_USERNAME
  46. SYNAPSE_PASSWORD
  47. DEFAULT_DOMAIN_NAME)
  48. function install_matrix_irc_bridge {
  49. if [ ! -d /etc/matrix-synapse ]; then
  50. return
  51. fi
  52. if [ ! -d /etc/ngircd ]; then
  53. return
  54. fi
  55. function_check install_nodejs
  56. install_nodejs synapse
  57. if [ ! -d $INSTALL_DIR ]; then
  58. mkdir -p $INSTALL_DIR
  59. fi
  60. cd $INSTALL_DIR
  61. git_clone $MATRIX_IRC_REPO $INSTALL_DIR/matrix_irc_bridge
  62. cd $INSTALL_DIR/matrix_irc_bridge
  63. npm install
  64. if [ ! -f config.sample.yaml ]; then
  65. exit 623835
  66. fi
  67. IRC_PASSWORD=$(cat /etc/ngircd/ngircd.conf | grep "Password =" | head -n 1 | awk -F '=' '{print $2}')
  68. echo 'homeserver:' > $INSTALL_DIR/matrix_irc_bridge/config.yaml
  69. echo ' url: "http://localhost:8008"' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  70. echo ' domain: "localhost"' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  71. echo 'ircService:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  72. echo ' servers:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  73. echo " localhost:" >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  74. echo " name: \"${PROJECT_NAME} IRC\"" >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  75. echo " port: ${IRC_PORT}" >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  76. echo ' ssl: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  77. echo ' sslselfsign: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  78. echo ' password: "${IRC_PASSWORD}"' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  79. echo ' sendConnectionMessages: true' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  80. echo ' quitDebounce:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  81. echo ' enabled: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  82. echo ' quitsPerSecond: 5' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  83. echo ' delayMs: 3600000 # 1h' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  84. echo ' botConfig:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  85. echo ' enabled: true' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  86. echo ' nick: "MatrixBot"' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  87. echo ' joinChannelsIfNoUsers: true' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  88. echo ' privateMessages:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  89. echo ' enabled: true' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  90. echo ' federate: true' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  91. echo ' dynamicChannels:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  92. echo ' enabled: true' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  93. echo ' createAlias: true' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  94. echo ' published: true' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  95. echo ' joinRule: public' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  96. echo ' federate: true' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  97. echo ' aliasTemplate: "#irc_$CHANNEL"' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  98. echo ' membershipLists:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  99. echo ' enabled: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  100. echo ' floodDelayMs: 10000' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  101. echo ' global:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  102. echo ' ircToMatrix:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  103. echo ' initial: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  104. echo ' incremental: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  105. echo ' matrixToIrc:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  106. echo ' initial: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  107. echo ' incremental: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  108. echo ' rooms:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  109. echo ' - room: "!fuasirouddJoxtwfge:localhost"' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  110. echo ' matrixToIrc:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  111. echo ' initial: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  112. echo ' incremental: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  113. echo ' channels:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  114. echo " - channel: \"#${PROJECT_NAME}\"" >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  115. echo ' ircToMatrix:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  116. echo ' initial: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  117. echo ' incremental: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  118. echo ' mappings:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  119. echo ' "#thepub": ["!kieouiJuedJoxtVdaG:localhost"]' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  120. echo ' matrixClients:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  121. echo ' userTemplate: "@irc_$NICK"' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  122. echo ' displayName: "$NICK (IRC)"' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  123. echo ' ircClients:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  124. echo ' nickTemplate: "$DISPLAY[m]"' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  125. echo ' allowNickChanges: true' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  126. echo ' maxClients: 30' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  127. echo ' idleTimeout: 10800' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  128. echo ' reconnectIntervalMs: 5000' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  129. echo ' lineLimit: 3' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  130. echo ' ident:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  131. echo ' enabled: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  132. echo ' port: 1113' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  133. echo ' logging:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  134. echo ' level: "debug"' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  135. echo ' logfile: "debug.log"' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  136. echo ' errfile: "errors.log"' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  137. echo ' toConsole: true' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  138. echo ' maxFileSizeBytes: 134217728 # 128 MB' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  139. echo ' maxFiles: 5' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  140. echo ' databaseUri: "nedb://data"' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  141. echo ' debugApi:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  142. echo ' enabled: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  143. echo ' port: 11100' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  144. echo ' provisioning:' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  145. echo ' enabled: false' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  146. echo ' requestTimeoutSeconds: 300' >> $INSTALL_DIR/matrix_irc_bridge/config.yaml
  147. /usr/bin/node $INSTALL_DIR/matrix_irc_bridge/app.js -r -f /etc/matrix-synapse/my_registration_file.yaml -u "http://${DEFAULT_DOMAIN_NAME}:9999" -c $INSTALL_DIR/matrix_irc_bridge/config.yaml -l my_bot
  148. if ! grep "my_registration_file.yaml" homeserver.yaml; then
  149. echo 'app_service_config_files: ["my_registration_file.yaml"]' >> homeserver.yaml
  150. fi
  151. echo '[Unit]' > /etc/systemd/system/matrix_irc_bridge.service
  152. echo 'Description=Matrix IRC Bridge' >> /etc/systemd/system/matrix_irc_bridge.service
  153. echo '' >> /etc/systemd/system/matrix_irc_bridge.service
  154. echo '[Service]' >> /etc/systemd/system/matrix_irc_bridge.service
  155. echo 'Type=simple' >> /etc/systemd/system/matrix_irc_bridge.service
  156. echo 'User=matrix-synapse' >> /etc/systemd/system/matrix_irc_bridge.service
  157. echo "WorkingDirectory=$INSTALL_DIR/matrix_irc_bridge" >> /etc/systemd/system/matrix_irc_bridge.service
  158. echo "ExecStart=/usr/bin/node $INSTALL_DIR/matrix_irc_bridge/app.js -c $INSTALL_DIR/matrix_irc_bridge/config.yaml -f /etc/matrix-synapse/my_registration_file.yaml -p 9999" >> /etc/systemd/system/matrix_irc_bridge.service
  159. echo 'Restart=always' >> /etc/systemd/system/matrix_irc_bridge.service
  160. echo 'RestartSec=3' >> /etc/systemd/system/matrix_irc_bridge.service
  161. echo '' >> /etc/systemd/system/matrix_irc_bridge.service
  162. echo '[Install]' >> /etc/systemd/system/matrix_irc_bridge.service
  163. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/matrix_irc_bridge.service
  164. systemctl enable matrix_irc_bridge
  165. systemctl start matrix_irc_bridge
  166. }
  167. function can_install_synapse {
  168. check_architecture=$(uname -a)
  169. if [[ "$check_architecture" == *"amd64"* || "$check_architecture" == *"i386"* ]]; then
  170. echo "1"
  171. else
  172. echo "0"
  173. fi
  174. }
  175. function remove_user_synapse {
  176. remove_username="$1"
  177. ${PROJECT_NAME}-pass -u $remove_username --rmapp synapse
  178. # TODO
  179. }
  180. function add_user_synapse {
  181. new_username="$1"
  182. new_user_password="$2"
  183. ${PROJECT_NAME}-pass -u $new_username -a synapse -p "$new_user_password"
  184. cd $SYNAPSE_DIR
  185. register_new_matrix_user -c homeserver.yaml https://localhost:${SYNAPSE_PORT} -u "${new_username}" -p "${new_user_password}" -a
  186. echo '0'
  187. }
  188. function install_interactive_synapse {
  189. echo -n ''
  190. APP_INSTALLED=1
  191. }
  192. function change_password_synapse {
  193. curr_username="$1"
  194. new_user_password="$2"
  195. #${PROJECT_NAME}-pass -u "$curr_username" -a synapse -p "$new_user_password"
  196. }
  197. function reconfigure_synapse {
  198. echo -n ''
  199. }
  200. function upgrade_synapse {
  201. echo -n ''
  202. }
  203. function backup_local_synapse {
  204. source_directory=$SYNAPSE_DIR
  205. if [ -d $source_directory ]; then
  206. systemctl stop synapse
  207. function_check backup_directory_to_usb
  208. backup_directory_to_usb $source_directory synapse
  209. source_directory=/var/lib/matrix-synapse
  210. if [ -d $source_directory ]; then
  211. backup_directory_to_usb $source_directory synapsedata
  212. fi
  213. systemctl start synapse
  214. fi
  215. }
  216. function restore_local_synapse {
  217. if [ -d $SYNAPSE_DIR ]; then
  218. systemctl stop synapse
  219. temp_restore_dir=/root/tempsynapse
  220. function_check restore_directory_from_usb
  221. restore_directory_from_usb $temp_restore_dir synapse
  222. cp -r $temp_restore_dir$SYNAPSE_DIR/* $SYNAPSE_DIR
  223. if [ ! "$?" = "0" ]; then
  224. function_check backup_unmount_drive
  225. backup_unmount_drive
  226. exit 725
  227. fi
  228. rm -rf $temp_restore_dir
  229. chown -R synapse:synapse $SYNAPSE_DIR
  230. temp_restore_dir=/root/tempsynapsedata
  231. restore_directory_from_usb $temp_restore_dir synapsedata
  232. cp -r $temp_restore_dir/var/lib/matrix-synapse/* /var/lib/matrix-synapse
  233. if [ ! "$?" = "0" ]; then
  234. function_check backup_unmount_drive
  235. backup_unmount_drive
  236. exit 36872
  237. fi
  238. rm -rf $temp_restore_dir
  239. chown -R synapse:synapse /var/lib/matrix-synapse
  240. systemctl start synapse
  241. fi
  242. }
  243. function backup_remote_synapse {
  244. source_directory=$SYNAPSE_DIR
  245. if [ -d $source_directory ]; then
  246. systemctl stop synapse
  247. function_check backup_directory_to_friend
  248. backup_directory_to_friend $source_directory synapse
  249. source_directory=/var/lib/matrix-synapse
  250. if [ -d $source_directory ]; then
  251. backup_directory_to_friend $source_directory synapsedata
  252. fi
  253. systemctl start synapse
  254. fi
  255. }
  256. function restore_remote_synapse {
  257. if [ -d $SYNAPSE_DIR ]; then
  258. systemctl stop synapse
  259. temp_restore_dir=/root/tempsynapse
  260. function_check restore_directory_from_friend
  261. restore_directory_from_friend $temp_restore_dir synapse
  262. cp -r $temp_restore_dir$SYNAPSE_DIR/* $SYNAPSE_DIR
  263. if [ ! "$?" = "0" ]; then
  264. exit 59262
  265. fi
  266. rm -rf $temp_restore_dir
  267. chown -R synapse:synapse $SYNAPSE_DIR
  268. temp_restore_dir=/root/tempsynapsedata
  269. restore_directory_from_friend $temp_restore_dir synapsedata
  270. cp -r $temp_restore_dir/var/lib/matrix-synapse/* /var/lib/matrix-synapse
  271. if [ ! "$?" = "0" ]; then
  272. exit 78335
  273. fi
  274. rm -rf $temp_restore_dir
  275. chown -R synapse:synapse /var/lib/matrix-synapse
  276. systemctl start synapse
  277. fi
  278. }
  279. function remove_synapse {
  280. systemctl stop matrix-synapse
  281. firewall_remove ${SYNAPSE_PORT}
  282. rm -rf $SYNAPSE_DIR
  283. apt-get -yq remove --purge matrix-synapse matrix-synapse-angular-client
  284. apt-get -yq autoremove
  285. rm /etc/apt/sources.list.d/synapse.list
  286. apt-get update
  287. if [ -f /etc/systemd/system/matrix_irc_bridge.service ]; then
  288. systemctl stop matrix_irc_bridge
  289. systemctl disable matrix_irc_bridge
  290. rm /etc/systemd/system/matrix_irc_bridge.service
  291. remove_nodejs synapse
  292. rm -rf $INSTALL_DIR/matrix_irc_bridge
  293. fi
  294. remove_completion_param install_synapse
  295. sed -i '/synapse/d' $COMPLETION_FILE
  296. }
  297. function install_synapse {
  298. if [[ "$(can_install_synapse)" == "0" ]]; then
  299. echo $'Matrix/Synapse can only be installed on i386 or amd64 architectures'
  300. exit 36734
  301. fi
  302. if [[ ${ONION_ONLY} == 'no' ]]; then
  303. # obtain a cert for the default domain
  304. if [[ "$(cert_exists ${DEFAULT_DOMAIN_NAME} pem)" == "0" ]]; then
  305. echo $'Obtaining certificate for the main domain'
  306. create_site_certificate ${DEFAULT_DOMAIN_NAME} 'yes'
  307. fi
  308. fi
  309. if [ ! -d /etc/prosody ]; then
  310. echo $'xmpp should be installed first'
  311. exit 67382
  312. fi
  313. apt-get -yq install build-essential python2.7-dev libffi-dev \
  314. python-pip python-setuptools sqlite3 \
  315. libssl-dev libjpeg-dev libxslt1-dev python-virtualenv curl
  316. curl -s https://matrix.org/packages/debian/repo-key.asc | apt-key add -
  317. echo "deb https://matrix.org/packages/debian/ ${DEBIAN_VERSION} main" | tee /etc/apt/sources.list.d/synapse.list
  318. apt-get update
  319. apt-get -yq install python-cffi
  320. apt-get -yq install python-nacl
  321. apt-get -yq install python-signedjson
  322. debconf-set-selections <<< "matrix-synapse matrix-synapse/server-name string $DEFAULT_DOMAIN_NAME"
  323. debconf-set-selections <<< "matrix-synapse matrix-synapse/server_name string $DEFAULT_DOMAIN_NAME"
  324. debconf-set-selections <<< "matrix-synapse matrix-synapse/report-stats boolean false"
  325. apt-get -yq install matrix-synapse
  326. if [ ! -d /etc/matrix-synapse ]; then
  327. exit 653835
  328. fi
  329. #apt-get -yq install matrix-synapse-angular-client
  330. #cp -av /etc/matrix-synapse/conf.d/webclient.yaml \
  331. #/etc/matrix-synapse/conf.d/webclient.yaml.orig
  332. #sed -e 's|,$||g' -i /etc/matrix-synapse/conf.d/webclient.yaml
  333. #cp -av /etc/init.d/matrix-synapse /etc/init.d/matrix-synapse.orig
  334. #sed -e 's|chown $USER:nogroup $PIDFILE.*|chown $USER:nogroup $PIDFILE /var/lib/$NAME/media|g' -i /etc/init.d/matrix-synapse
  335. #diff -u /etc/init.d/matrix-synapse.orig /etc/init.d/matrix-synapse
  336. #cp -av /etc/matrix-synapse/homeserver.yaml /etc/matrix-synapse/homeserver.yaml.orig
  337. #sed -i 's|enable_registration: False|enable_registration: True|g' -i /etc/matrix-synapse/homeserver.yaml
  338. #rm -f /etc/matrix-synapse/conf.d/*.orig /etc/matrix-synapse/conf.d/*~
  339. update_default_domain
  340. systemctl stop matrix-synapse
  341. systemctl start matrix-synapse
  342. firewall_add synapse ${SYNAPSE_PORT}
  343. SYNAPSE_ONION_HOSTNAME=$(add_onion_service synapse ${SYNAPSE_PORT} ${SYNAPSE_PORT})
  344. if [ ! ${SYNAPSE_PASSWORD} ]; then
  345. if [ -f ${IMAGE_PASSWORD_FILE} ]; then
  346. SYNAPSE_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  347. else
  348. SYNAPSE_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  349. fi
  350. fi
  351. add_user_synapse "${MY_USERNAME}" "${SYNAPSE_PASSWORD}"
  352. #install_matrix_irc_bridge
  353. APP_INSTALLED=1
  354. }
  355. # NOTE: deliberately no exit 0