core_dl.sh 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. #!/bin/bash
  2. # LinuxGSM core_dl.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Deals with all downloads for LinuxGSM.
  7. # remote_fileurl: The URL of the file: http://example.com/dl/File.tar.bz2
  8. # local_filedir: location the file is to be saved: /home/server/lgsm/tmp
  9. # local_filename: name of file (this can be different from the url name): file.tar.bz2
  10. # chmodx: Optional, set to "chmodx" to make file executable using chmod +x
  11. # run: Optional, set run to execute the file after download
  12. # forcedl: Optional, force re-download of file even if exists
  13. # hash: Optional, set an hash sum and will compare it against the file.
  14. #
  15. # Downloads can be defined in code like so:
  16. # fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  17. # fn_fetch_file "http://example.com/file.tar.bz2" "http://example.com/file2.tar.bz2" "file.tar.bz2" "file2.tar.bz2" "/some/dir" "file.tar.bz2" "chmodx" "run" "forcedl" "10cd7353aa9d758a075c600a6dd193fd"
  18. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  19. # Removes consecutive duplicate lines from SteamCMD output without buffering.
  20. fn_dl_steamcmd_output() {
  21. local line=""
  22. local previousline=""
  23. local firstline="1"
  24. while IFS= read -r line || [ -n "${line}" ]; do
  25. if [ -n "${firstline}" ] || [ "${line}" != "${previousline}" ]; then
  26. printf '%s\n' "${line}"
  27. fi
  28. previousline="${line}"
  29. firstline=""
  30. done
  31. }
  32. fn_dl_steamcmd() {
  33. remotelocation="SteamCMD"
  34. fn_print_start_nl "${remotelocation}"
  35. fn_script_log_info "${commandaction} ${selfname}: ${remotelocation}"
  36. if [ -n "${branch}" ]; then
  37. echo -e "Branch: ${branch}"
  38. fn_script_log_info "Branch: ${branch}"
  39. fi
  40. if [ -n "${betapassword}" ]; then
  41. echo -e "Branch password: ${betapassword}"
  42. fn_script_log_info "Branch password: ${betapassword}"
  43. fi
  44. if [ -d "${steamcmddir}" ]; then
  45. cd "${steamcmddir}" || exit
  46. fi
  47. # Unbuffer will allow the output of steamcmd not buffer allowing a smooth output.
  48. # unbuffer us part of the expect package.
  49. if [ "$(command -v unbuffer 2> /dev/null)" ]; then
  50. unbuffer="unbuffer"
  51. fi
  52. # Validate will be added as a parameter if required.
  53. if [ "${commandname}" == "VALIDATE" ] || [ "${commandname}" == "INSTALL" ]; then
  54. validate="validate"
  55. fi
  56. # Wrap steamcmdcommand as a single-element array to avoid word-splitting
  57. # on paths/commands that should not be tokenised.
  58. steamcmdcommandarray=("${steamcmdcommand}")
  59. unbuffercommand=()
  60. if [ -n "${unbuffer}" ]; then
  61. unbuffercommand=("${unbuffer}")
  62. fi
  63. validateparam=()
  64. if [ -n "${validate}" ]; then
  65. validateparam=("${validate}")
  66. fi
  67. # To do error checking for SteamCMD the output of steamcmd will be saved to a log.
  68. steamcmdlog="${lgsmlogdir}/${selfname}-steamcmd.log"
  69. local invalidplatformworkaround=""
  70. counter=0
  71. while [ "${counter}" -eq 0 ] || [ "${exitcode}" -ne 0 ]; do
  72. counter=$((counter + 1))
  73. # clear previous steamcmd log
  74. rm -f "${steamcmdlog:?}"
  75. # Select SteamCMD parameters
  76. # If GoldSrc (appid 90) servers. GoldSrc (appid 90) require extra commands.
  77. if [ "${appid}" == "90" ]; then
  78. # If using a specific branch.
  79. if [ -n "${branch}" ] && [ -n "${betapassword}" ]; then
  80. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_set_config 90 mod "${appidmod}" +app_update "${appid}" -beta "${branch}" -betapassword "${betapassword}" "${validateparam[@]}" +quit | fn_dl_steamcmd_output | tee -a "${lgsmlog}" "${steamcmdlog}"
  81. exitcode="${PIPESTATUS[0]}"
  82. elif [ -n "${branch}" ]; then
  83. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_set_config 90 mod "${appidmod}" +app_update "${appid}" -beta "${branch}" "${validateparam[@]}" +quit | fn_dl_steamcmd_output | tee -a "${lgsmlog}" "${steamcmdlog}"
  84. exitcode="${PIPESTATUS[0]}"
  85. else
  86. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_set_config 90 mod "${appidmod}" +app_update "${appid}" "${validateparam[@]}" +quit | fn_dl_steamcmd_output | tee -a "${lgsmlog}" "${steamcmdlog}"
  87. exitcode="${PIPESTATUS[0]}"
  88. fi
  89. # Force Windows Platform type.
  90. elif [ "${steamcmdforcewindows}" == "yes" ]; then
  91. # If a base app is required, install it first.
  92. if [ -n "${baseappid}" ]; then
  93. if [ -z "${supportdir}" ]; then
  94. fn_print_failure_nl "${commandaction} ${selfname}: baseappid is set but supportdir is not defined"
  95. fn_script_log_fail "${commandaction} ${selfname}: baseappid is set but supportdir is not defined"
  96. core_exit.sh
  97. fi
  98. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +@sSteamCmdForcePlatformType windows +force_install_dir "${supportdir}" +login "${steamuser}" "${steampass}" +app_update "${baseappid}" "${validateparam[@]}" +quit | fn_dl_steamcmd_output | tee -a "${lgsmlog}" "${steamcmdlog}"
  99. exitcode="${PIPESTATUS[0]}"
  100. fi
  101. if [ -z "${baseappid}" ] || [ "${exitcode}" -eq 0 ]; then
  102. if [ -n "${branch}" ] && [ -n "${betapassword}" ]; then
  103. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +@sSteamCmdForcePlatformType windows +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_update "${appid}" -beta "${branch}" -betapassword "${betapassword}" "${validateparam[@]}" +quit | fn_dl_steamcmd_output | tee -a "${lgsmlog}" "${steamcmdlog}"
  104. exitcode="${PIPESTATUS[0]}"
  105. elif [ -n "${branch}" ]; then
  106. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +@sSteamCmdForcePlatformType windows +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_update "${appid}" -beta "${branch}" "${validateparam[@]}" +quit | fn_dl_steamcmd_output | tee -a "${lgsmlog}" "${steamcmdlog}"
  107. exitcode="${PIPESTATUS[0]}"
  108. else
  109. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +@sSteamCmdForcePlatformType windows +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_update "${appid}" "${validateparam[@]}" +quit | fn_dl_steamcmd_output | tee -a "${lgsmlog}" "${steamcmdlog}"
  110. exitcode="${PIPESTATUS[0]}"
  111. fi
  112. fi
  113. # All other servers.
  114. else
  115. # If a base app is required, install it first.
  116. if [ -n "${baseappid}" ]; then
  117. if [ -z "${supportdir}" ]; then
  118. fn_print_failure_nl "${commandaction} ${selfname}: baseappid is set but supportdir is not defined"
  119. fn_script_log_fail "${commandaction} ${selfname}: baseappid is set but supportdir is not defined"
  120. core_exit.sh
  121. fi
  122. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +force_install_dir "${supportdir}" +login "${steamuser}" "${steampass}" +app_update "${baseappid}" "${validateparam[@]}" +quit | fn_dl_steamcmd_output | tee -a "${lgsmlog}" "${steamcmdlog}"
  123. exitcode="${PIPESTATUS[0]}"
  124. fi
  125. if [ -z "${baseappid}" ] || [ "${exitcode}" -eq 0 ]; then
  126. if [ -n "${branch}" ] && [ -n "${betapassword}" ]; then
  127. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_update "${appid}" -beta "${branch}" -betapassword "${betapassword}" "${validateparam[@]}" +quit | fn_dl_steamcmd_output | tee -a "${lgsmlog}" "${steamcmdlog}"
  128. exitcode="${PIPESTATUS[0]}"
  129. elif [ -n "${branch}" ]; then
  130. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_update "${appid}" -beta "${branch}" "${validateparam[@]}" +quit | fn_dl_steamcmd_output | tee -a "${lgsmlog}" "${steamcmdlog}"
  131. exitcode="${PIPESTATUS[0]}"
  132. else
  133. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_update "${appid}" "${validateparam[@]}" +quit | fn_dl_steamcmd_output | tee -a "${lgsmlog}" "${steamcmdlog}"
  134. exitcode="${PIPESTATUS[0]}"
  135. fi
  136. fi
  137. fi
  138. # Error checking for SteamCMD. Some errors will loop to try again and some will just exit.
  139. if grep -qi "Error!" "${steamcmdlog}"; then
  140. # Not enough space.
  141. if [ -n "$(grep "0x202" "${steamcmdlog}" | tail -1)" ]; then
  142. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Not enough disk space to download server files"
  143. fn_script_log_fail "${commandaction} ${selfname}: ${remotelocation}: Not enough disk space to download server files"
  144. core_exit.sh
  145. # Not enough space.
  146. elif [ -n "$(grep "0x212" "${steamcmdlog}" | tail -1)" ]; then
  147. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Not enough disk space to download server files"
  148. fn_script_log_fail "${commandaction} ${selfname}: ${remotelocation}: Not enough disk space to download server files"
  149. core_exit.sh
  150. # Invalid platform (SteamCMD bug, e.g. Left 4 Dead 2): retry once after a Windows platform download.
  151. elif [ -n "$(grep -i "Invalid platform" "${steamcmdlog}" | tail -1)" ] && [ "${steamcmdforcewindows}" != "yes" ] && [ -z "${invalidplatformworkaround}" ]; then
  152. invalidplatformworkaround="1"
  153. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Invalid platform - applying SteamCMD Windows platform workaround"
  154. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Invalid platform - applying SteamCMD Windows platform workaround"
  155. "${unbuffercommand[@]}" "${steamcmdcommandarray[@]}" +@sSteamCmdForcePlatformType windows +force_install_dir "${serverfiles}" +login "${steamuser}" "${steampass}" +app_update "${appid}" +quit | fn_dl_steamcmd_output | tee -a "${lgsmlog}" "${steamcmdlog}"
  156. validateparam=("validate")
  157. rm -f "${steamcmdlog:?}"
  158. counter=0
  159. # Invalid platform for app/update request (after the workaround, or when forcing Windows).
  160. elif [ -n "$(grep -i "Invalid platform" "${steamcmdlog}" | tail -1)" ]; then
  161. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Invalid platform for AppID ${appid}"
  162. fn_print_nl "Check steamcmdforcewindows setting and system architecture (x86_64 required for most servers)"
  163. fn_script_log_fail "${commandaction} ${selfname}: ${remotelocation}: Invalid platform for AppID ${appid}"
  164. core_exit.sh
  165. # Need tp purchase game.
  166. elif [ -n "$(grep "No subscription" "${steamcmdlog}" | tail -1)" ]; then
  167. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Steam account does not have a license for the required game"
  168. fn_script_log_fail "${commandaction} ${selfname}: ${remotelocation}: Steam account does not have a license for the required game"
  169. core_exit.sh
  170. # Two-factor authentication failure
  171. elif [ -n "$(grep "Two-factor code mismatch" "${steamcmdlog}" | tail -1)" ]; then
  172. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Two-factor authentication failure"
  173. fn_script_log_fail "${commandaction} ${selfname}: ${remotelocation}: Two-factor authentication failure"
  174. core_exit.sh
  175. # Incorrect Branch password
  176. elif [ -n "$(grep "Password check for AppId" "${steamcmdlog}" | tail -1)" ]; then
  177. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: betapassword is incorrect"
  178. fn_script_log_fail "${commandaction} ${selfname}: ${remotelocation}: betapassword is incorrect"
  179. core_exit.sh
  180. # Update did not finish.
  181. elif [ -n "$(grep "0x402" "${steamcmdlog}" | tail -1)" ] || [ -n "$(grep "0x602" "${steamcmdlog}" | tail -1)" ]; then
  182. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Update required but not completed - check network"
  183. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Update required but not completed - check network"
  184. # Disk write failure.
  185. elif [ -n "$(grep "0x606" "${steamcmdlog}" | tail -1)" ] || [ -n "$(grep "0x602" "${steamcmdlog}" | tail -1)" ]; then
  186. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Disk write failure"
  187. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Disk write failure"
  188. # Missing update files.
  189. elif [ -n "$(grep "0x626" "${steamcmdlog}" | tail -1)" ] || [ -n "$(grep "0x626" "${steamcmdlog}" | tail -1)" ]; then
  190. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Missing update files"
  191. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Missing update files"
  192. elif [ -n "$(grep "0x6A6" "${steamcmdlog}" | tail -1)" ]; then
  193. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Corrupt update files"
  194. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Corrupt update files"
  195. # Steam app metadata unavailable.
  196. elif [ -n "$(grep -i "Missing configuration" "${steamcmdlog}" | tail -1)" ]; then
  197. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Missing configuration - Steam app metadata unavailable for AppID ${appid}"
  198. fn_print_nl "Please provide content log to LinuxGSM developers https://linuxgsm.com/steamcmd-error"
  199. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Missing configuration - Steam app metadata unavailable for AppID ${appid}"
  200. else
  201. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Unknown error occurred"
  202. fn_print_nl "Please provide content log to LinuxGSM developers https://linuxgsm.com/steamcmd-error"
  203. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Unknown error occurred"
  204. fi
  205. elif [ "${exitcode}" -ne 0 ]; then
  206. fn_print_error2_nl "${commandaction} ${selfname}: ${remotelocation}: Exit code: ${exitcode}"
  207. fn_script_log_error "${commandaction} ${selfname}: ${remotelocation}: Exit code: ${exitcode}"
  208. else
  209. fn_print_success_nl "${commandaction} ${selfname}: ${remotelocation}"
  210. fn_script_log_pass "${commandaction} ${selfname}: ${remotelocation}"
  211. fi
  212. if [ "${counter}" -gt "10" ]; then
  213. fn_print_failure_nl "${commandaction} ${selfname}: ${remotelocation}: Did not complete the download, too many retrys"
  214. fn_script_log_fail "${commandaction} ${selfname}: ${remotelocation}: Did not complete the download, too many retrys"
  215. core_exit.sh
  216. fi
  217. done
  218. }
  219. # Emptys contents of the LinuxGSM tmpdir.
  220. fn_clear_tmp() {
  221. echo -en "clearing tmp directory [ ${italic}${tmpdir}${default} ]"
  222. if [ -d "${tmpdir}" ]; then
  223. rm -rf "${tmpdir:?}/"*
  224. exitcode=$?
  225. if [ "${exitcode}" -ne 0 ]; then
  226. fn_print_error_eol_nl
  227. fn_script_log_error "clearing tmp directory ${tmpdir}"
  228. else
  229. fn_print_ok_eol_nl
  230. fn_script_log_pass "clearing tmp directory ${tmpdir}"
  231. fi
  232. fi
  233. }
  234. fn_dl_hash() {
  235. # Runs Hash Check if available.
  236. if [ "${hash}" != "0" ] && [ "${hash}" != "nohash" ] && [ "${hash}" != "nomd5" ]; then
  237. # MD5
  238. if [ "${#hash}" == "32" ]; then
  239. hashbin="md5sum"
  240. hashtype="MD5"
  241. # SHA1
  242. elif [ "${#hash}" == "40" ]; then
  243. hashbin="sha1sum"
  244. hashtype="SHA1"
  245. # SHA256
  246. elif [ "${#hash}" == "64" ]; then
  247. hashbin="sha256sum"
  248. hashtype="SHA256"
  249. # SHA512
  250. elif [ "${#hash}" == "128" ]; then
  251. hashbin="sha512sum"
  252. hashtype="SHA512"
  253. else
  254. fn_script_log_error "hash length not known for hash type"
  255. fn_print_error_nl "hash length not known for hash type"
  256. core_exit.sh
  257. fi
  258. echo -en "verifying ${local_filename} with ${hashtype}"
  259. fn_sleep_time
  260. hashsumcmd=$(${hashbin} "${local_filedir}/${local_filename}" | awk '{print $1}')
  261. if [ "${hashsumcmd}" != "${hash}" ]; then
  262. fn_print_fail_eol_nl
  263. echo -e "${local_filename} returned ${hashtype} checksum: ${hashsumcmd}"
  264. echo -e "expected ${hashtype} checksum: ${hash}"
  265. fn_script_log_fail "Verifying ${local_filename} with ${hashtype}"
  266. fn_script_log_info "${local_filename} returned ${hashtype} checksum: ${hashsumcmd}"
  267. fn_script_log_info "Expected ${hashtype} checksum: ${hash}"
  268. core_exit.sh
  269. else
  270. fn_print_ok_eol_nl
  271. fn_script_log_pass "Verifying ${local_filename} with ${hashtype}"
  272. fn_script_log_info "${local_filename} returned ${hashtype} checksum: ${hashsumcmd}"
  273. fn_script_log_info "Expected ${hashtype} checksum: ${hash}"
  274. fi
  275. fi
  276. }
  277. # Extracts gzip, bzip2, xz or zip files. Any other file type fails.
  278. # Extracts can be defined in code like so:
  279. # fn_dl_extract "${local_filedir}" "${local_filename}" "${extractdest}" "${extractsrc}"
  280. # fn_dl_extract "/home/gameserver/lgsm/tmp" "file.tar.bz2" "/home/gamserver/serverfiles"
  281. fn_dl_extract() {
  282. local_filedir="${1}"
  283. local_filename="${2}"
  284. extractdest="${3}"
  285. extractsrc="${4}"
  286. # Extracts archives.
  287. echo -en "extracting ${local_filename}..."
  288. if [ ! -d "${extractdest}" ]; then
  289. mkdir "${extractdest}"
  290. fi
  291. if [ ! -f "${local_filedir}/${local_filename}" ]; then
  292. fn_print_fail_eol_nl
  293. echo -en "file ${local_filedir}/${local_filename} not found"
  294. fn_script_log_fail "Extracting ${local_filename}"
  295. fn_script_log_fail "File ${local_filedir}/${local_filename} not found"
  296. core_exit.sh
  297. return 1
  298. fi
  299. mime=$(file -b --mime-type "${local_filedir}/${local_filename}")
  300. if [ "${mime}" == "application/gzip" ] || [ "${mime}" == "application/x-gzip" ]; then
  301. if [ -n "${extractsrc}" ]; then
  302. extractcmd=$(tar -zxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}" 2>&1)
  303. else
  304. extractcmd=$(tar -zxf "${local_filedir}/${local_filename}" -C "${extractdest}" 2>&1)
  305. fi
  306. elif [ "${mime}" == "application/x-bzip2" ]; then
  307. if [ -n "${extractsrc}" ]; then
  308. extractcmd=$(tar -jxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}" 2>&1)
  309. else
  310. extractcmd=$(tar -jxf "${local_filedir}/${local_filename}" -C "${extractdest}" 2>&1)
  311. fi
  312. elif [ "${mime}" == "application/x-xz" ]; then
  313. if [ -n "${extractsrc}" ]; then
  314. extractcmd=$(tar -Jxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}" 2>&1)
  315. else
  316. extractcmd=$(tar -Jxf "${local_filedir}/${local_filename}" -C "${extractdest}" 2>&1)
  317. fi
  318. elif [ "${mime}" == "application/zip" ]; then
  319. if [ -n "${extractsrc}" ]; then
  320. temp_extractdir="${tmpdir}/${extractsrc}"
  321. extractcmd=$(unzip -qo "${local_filedir}/${local_filename}" "${extractsrc}/*" -d "${temp_extractdir}" 2>&1 && cp -a "${temp_extractdir}/${extractsrc}/." "${extractdest}/" 2>&1)
  322. else
  323. extractcmd=$(unzip -qo -d "${extractdest}" "${local_filedir}/${local_filename}" 2>&1)
  324. fi
  325. else
  326. extractcmd="Unsupported archive type: ${mime}"
  327. false
  328. fi
  329. exitcode=$?
  330. # Clean up after capturing the exit code so cleanup cannot mask a failed extraction or copy.
  331. if [ -n "${temp_extractdir}" ]; then
  332. rm -rf "${temp_extractdir}"
  333. unset temp_extractdir
  334. fi
  335. if [ "${exitcode}" -ne 0 ]; then
  336. fn_print_fail_eol_nl
  337. fn_script_log_fail "Extracting ${local_filename}"
  338. if [ -f "${lgsmlog}" ]; then
  339. echo -e "${extractcmd}" >> "${lgsmlog}"
  340. fi
  341. echo -e "${extractcmd}"
  342. core_exit.sh
  343. return 1
  344. else
  345. fn_print_ok_eol_nl
  346. fn_script_log_pass "Extracting ${local_filename}"
  347. fi
  348. }
  349. # Trap to remove file download if canceled before completed.
  350. fn_fetch_trap() {
  351. echo -e ""
  352. echo -en "downloading ${local_filename}"
  353. fn_print_canceled_eol_nl
  354. fn_script_log_info "Downloading ${local_filename}...CANCELED"
  355. rm -f "${local_filedir:?}/${local_filename}"
  356. echo -en "downloading ${local_filename}"
  357. fn_print_removed_eol_nl
  358. fn_script_log_info "Downloading ${local_filename}...REMOVED"
  359. if [ "${local_filename}" == "core_exit.sh" ]; then
  360. exit 130
  361. fi
  362. core_exit.sh
  363. }
  364. # Will check a file exists and download it. Will not exit if fails to download.
  365. fn_check_file() {
  366. remote_fileurl="${1}"
  367. remote_fileurl_backup="${2}"
  368. remote_fileurl_name="${3}"
  369. remote_fileurl_backup_name="${4}"
  370. remote_filename="${5}"
  371. # If backup fileurl exists include it.
  372. if [ -n "${remote_fileurl_backup}" ]; then
  373. # counter set to 0 to allow second try
  374. counter=0
  375. remote_fileurls_array=(remote_fileurl remote_fileurl_backup)
  376. else
  377. # counter set to 1 to not allow second try
  378. counter=1
  379. remote_fileurls_array=(remote_fileurl)
  380. fi
  381. for remote_fileurl_array in "${remote_fileurls_array[@]}"; do
  382. if [ "${remote_fileurl_array}" == "remote_fileurl" ]; then
  383. fileurl="${remote_fileurl}"
  384. fileurl_name="${remote_fileurl_name}"
  385. elif [ "${remote_fileurl_array}" == "remote_fileurl_backup" ]; then
  386. fileurl="${remote_fileurl_backup}"
  387. fileurl_name="${remote_fileurl_backup_name}"
  388. fi
  389. counter=$((counter + 1))
  390. echo -e "checking ${fileurl_name} ${remote_filename}\c"
  391. curlcmd=$(curl --output /dev/null --silent --head --fail "${fileurl}" 2>&1)
  392. exitcode=$?
  393. # On first try will error. On second try will fail.
  394. if [ "${exitcode}" -ne 0 ]; then
  395. if [ ${counter} -ge 2 ]; then
  396. fn_print_fail_eol_nl
  397. if [ -f "${lgsmlog}" ]; then
  398. fn_script_log_fail "Checking ${remote_filename}"
  399. fn_script_log_fail "${fileurl}"
  400. checkflag=1
  401. fi
  402. else
  403. fn_print_error_eol_nl
  404. if [ -f "${lgsmlog}" ]; then
  405. fn_script_log_error "Checking ${remote_filename}"
  406. fn_script_log_error "${fileurl}"
  407. checkflag=2
  408. fi
  409. fi
  410. else
  411. if [ -n "${plainoutput}" ]; then
  412. fn_print_ok_eol_nl
  413. else
  414. fn_print_ok_eol
  415. echo -en "\033[2K\\r"
  416. fi
  417. if [ -f "${lgsmlog}" ]; then
  418. fn_script_log_pass "Checking ${remote_filename}"
  419. checkflag=0
  420. fi
  421. break
  422. fi
  423. done
  424. if [ -f "${local_filedir}/${local_filename}" ]; then
  425. fn_dl_hash
  426. # Execute file if run is set.
  427. if [ "${run}" == "run" ]; then
  428. # shellcheck source=/dev/null
  429. source "${local_filedir}/${local_filename}"
  430. fi
  431. fi
  432. }
  433. fn_fetch_file() {
  434. remote_fileurl="${1}"
  435. remote_fileurl_backup="${2}"
  436. remote_fileurl_name="${3}"
  437. remote_fileurl_backup_name="${4}"
  438. local_filedir="${5}"
  439. local_filename="${6}"
  440. chmodx="${7:-0}"
  441. run="${8:-0}"
  442. forcedl="${9:-0}"
  443. hash="${10:-0}"
  444. # Download file if missing or download forced.
  445. if [ ! -f "${local_filedir}/${local_filename}" ] || [ "${forcedl}" == "forcedl" ]; then
  446. # If backup fileurl exists include it.
  447. if [ -n "${remote_fileurl_backup}" ]; then
  448. # counter set to 0 to allow second try
  449. counter=0
  450. remote_fileurls_array=(remote_fileurl remote_fileurl_backup)
  451. else
  452. # counter set to 1 to not allow second try
  453. counter=1
  454. remote_fileurls_array=(remote_fileurl)
  455. fi
  456. for remote_fileurl_array in "${remote_fileurls_array[@]}"; do
  457. if [ "${remote_fileurl_array}" == "remote_fileurl" ]; then
  458. fileurl="${remote_fileurl}"
  459. fileurl_name="${remote_fileurl_name}"
  460. elif [ "${remote_fileurl_array}" == "remote_fileurl_backup" ]; then
  461. fileurl="${remote_fileurl_backup}"
  462. fileurl_name="${remote_fileurl_backup_name}"
  463. fi
  464. counter=$((counter + 1))
  465. if [ ! -d "${local_filedir}" ]; then
  466. mkdir -p "${local_filedir}"
  467. fi
  468. # Trap will remove part downloaded files if canceled.
  469. trap fn_fetch_trap INT
  470. curlcmd=(curl --connect-timeout 10 --fail -L -o "${local_filedir}/${local_filename}" --retry 2 -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.${randomint}.212 Safari/537.36")
  471. if [[ "${fileurl}" == https://* ]]; then
  472. curlcmd+=(--proto-redir "=https")
  473. fi
  474. # if is large file show progress, else be silent
  475. local exitcode=""
  476. large_files=("bz2" "gz" "zip" "jar" "xz")
  477. if grep -qE "(^|\s)${local_filename##*.}(\s|$)" <<< "${large_files[@]}"; then
  478. if [ -n "${plainoutput}" ]; then
  479. "${curlcmd[@]}" --silent --show-error "${fileurl}" 2>&1
  480. exitcode=$?
  481. else
  482. echo -e "downloading file [ ${italic}${local_filename}${default} ]"
  483. fn_sleep_time
  484. "${curlcmd[@]}" --progress-bar "${fileurl}" 2>&1
  485. exitcode=$?
  486. fi
  487. echo -en "downloading file [ ${italic}${local_filename}${default} ]"
  488. else
  489. echo -en "fetching ${fileurl_name} [ ${italic}${local_filename}${default} ]\c"
  490. "${curlcmd[@]}" --silent --show-error "${fileurl}" 2>&1
  491. exitcode=$?
  492. fi
  493. # Download will fail if downloads a html file.
  494. if [ -f "${local_filedir}/${local_filename}" ]; then
  495. if head -n 1 "${local_filedir}/${local_filename}" | grep -q "DOCTYPE"; then
  496. rm "${local_filedir:?}/${local_filename:?}"
  497. local exitcode=2
  498. fi
  499. fi
  500. # On first try will error. On second try will fail.
  501. if [ "${exitcode}" -ne 0 ]; then
  502. if [ ${counter} -ge 2 ]; then
  503. fn_print_fail_eol_nl
  504. if [ -f "${lgsmlog}" ]; then
  505. fn_script_log_fail "Downloading ${local_filename}..."
  506. fn_script_log_fail "${fileurl}"
  507. fi
  508. if [ "${local_filename}" == "core_exit.sh" ]; then
  509. exit 1
  510. fi
  511. core_exit.sh
  512. else
  513. fn_print_error_eol_nl
  514. if [ -f "${lgsmlog}" ]; then
  515. fn_script_log_error "Downloading ${local_filename}..."
  516. fn_script_log_error "${fileurl}"
  517. fi
  518. fi
  519. else
  520. fn_print_ok_eol_nl
  521. if [ -f "${lgsmlog}" ]; then
  522. fn_script_log_pass "Downloading ${local_filename}..."
  523. fi
  524. # Make file executable if chmodx is set.
  525. if [ "${chmodx}" == "chmodx" ]; then
  526. chmod +x "${local_filedir}/${local_filename}"
  527. fi
  528. # Remove trap.
  529. trap - INT
  530. break
  531. fi
  532. done
  533. fi
  534. if [ -f "${local_filedir}/${local_filename}" ]; then
  535. fn_dl_hash
  536. # Execute file if run is set.
  537. if [ "${run}" == "run" ]; then
  538. # shellcheck source=/dev/null
  539. source "${local_filedir}/${local_filename}"
  540. fi
  541. fi
  542. }
  543. # GitHub file download modules.
  544. # Used to simplify downloading specific files from GitHub.
  545. # github_file_url_dir: the directory of the file in the GitHub: lgsm/modules
  546. # github_file_url_name: the filename of the file to download from GitHub: core_messages.sh
  547. # github_file_url_dir: the directory of the file in the GitHub: lgsm/modules
  548. # github_file_url_name: the filename of the file to download from GitHub: core_messages.sh
  549. # githuburl: the full GitHub url
  550. # remote_fileurl: The URL of the file: http://example.com/dl/File.tar.bz2
  551. # local_filedir: location the file is to be saved: /home/server/lgsm/tmp
  552. # local_filename: name of file (this can be different from the url name): file.tar.bz2
  553. # chmodx: Optional, set to "chmodx" to make file executable using chmod +x
  554. # run: Optional, set run to execute the file after download
  555. # forcedl: Optional, force re-download of file even if exists
  556. # hash: Optional, set an hash sum and will compare it against the file.
  557. # Fetches files from the Git repo.
  558. fn_fetch_file_github() {
  559. github_file_url_dir="${1}"
  560. github_file_url_name="${2}"
  561. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  562. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  563. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  564. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  565. else
  566. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  567. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  568. fi
  569. remote_fileurl_name="GitHub"
  570. remote_fileurl_backup_name="Bitbucket"
  571. local_filedir="${3}"
  572. local_filename="${github_file_url_name}"
  573. chmodx="${4:-0}"
  574. run="${5:-0}"
  575. forcedl="${6:-0}"
  576. hash="${7:-0}"
  577. # Passes vars to the file download module.
  578. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  579. }
  580. fn_check_file_github() {
  581. github_file_url_dir="${1}"
  582. github_file_url_name="${2}"
  583. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  584. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  585. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  586. else
  587. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  588. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  589. fi
  590. remote_fileurl_name="GitHub"
  591. remote_fileurl_backup_name="Bitbucket"
  592. fn_check_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${github_file_url_name}"
  593. }
  594. # Fetches config files from the Git repo.
  595. fn_fetch_config() {
  596. github_file_url_dir="${1}"
  597. github_file_url_name="${2}"
  598. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  599. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  600. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  601. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  602. else
  603. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  604. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  605. fi
  606. remote_fileurl_name="GitHub"
  607. remote_fileurl_backup_name="Bitbucket"
  608. local_filedir="${3}"
  609. local_filename="${4}"
  610. chmodx="nochmodx"
  611. run="norun"
  612. forcedl="noforce"
  613. hash="nohash"
  614. # Passes vars to the file download module.
  615. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  616. }
  617. # Fetches modules from the Git repo during first download.
  618. fn_fetch_module() {
  619. github_file_url_dir="lgsm/modules"
  620. github_file_url_name="${modulefile}"
  621. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  622. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  623. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  624. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  625. else
  626. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  627. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  628. fi
  629. remote_fileurl_name="GitHub"
  630. remote_fileurl_backup_name="Bitbucket"
  631. local_filedir="${modulesdir}"
  632. local_filename="${github_file_url_name}"
  633. chmodx="chmodx"
  634. run="run"
  635. forcedl="noforce"
  636. hash="nohash"
  637. # Passes vars to the file download module.
  638. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  639. }
  640. # Fetches modules from the Git repo during update-lgsm.
  641. fn_update_module() {
  642. github_file_url_dir="lgsm/modules"
  643. github_file_url_name="${modulefile}"
  644. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  645. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  646. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  647. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  648. else
  649. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  650. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  651. fi
  652. remote_fileurl_name="GitHub"
  653. remote_fileurl_backup_name="Bitbucket"
  654. local_filedir="${modulesdir}"
  655. local_filename="${github_file_url_name}"
  656. chmodx="chmodx"
  657. run="norun"
  658. forcedl="noforce"
  659. hash="nohash"
  660. # Passes vars to the file download module.
  661. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  662. }
  663. # Function to download latest github release.
  664. # $1 GitHub user / organisation.
  665. # $2 Repo name.
  666. # $3 Destination for download.
  667. # $4 Search string in releases (needed if there are more files that can be downloaded from the release pages).
  668. fn_dl_latest_release_github() {
  669. local githubreleaseuser="${1}"
  670. local githubreleaserepo="${2}"
  671. local githubreleasedownloadpath="${3}"
  672. local githubreleasesearch="${4}"
  673. local githublatestreleaseurl="https://api.github.com/repos/${githubreleaseuser}/${githubreleaserepo}/releases/latest"
  674. # Get last github release.
  675. # If no search for the release filename is set, just get the first file from the latest release.
  676. if [ -z "${githubreleasesearch}" ]; then
  677. githubreleaseassets=$(curl -s "${githublatestreleaseurl}" | jq '[ .assets[] ]')
  678. else
  679. githubreleaseassets=$(curl -s "${githublatestreleaseurl}" | jq "[ .assets[]|select(.browser_download_url | contains(\"${githubreleasesearch}\")) ]")
  680. fi
  681. # Check how many releases we got from the api and exit if we have more then one.
  682. if [ "$(echo -e "${githubreleaseassets}" | jq '. | length')" -gt 1 ]; then
  683. fn_print_fatal_nl "Found more than one release to download - Please report this to the LinuxGSM issue tracker"
  684. fn_script_log_fail "Found more than one release to download - Please report this to the LinuxGSM issue tracker"
  685. else
  686. # Set variables for download via fn_fetch_file.
  687. githubreleasefilename=$(echo -e "${githubreleaseassets}" | jq -r '.[]name')
  688. githubreleasedownloadlink=$(echo -e "${githubreleaseassets}" | jq -r '.[]browser_download_url')
  689. # Error if no version is there.
  690. if [ -z "${githubreleasefilename}" ]; then
  691. fn_print_fail_nl "Cannot get version from GitHub API for ${githubreleaseuser}/${githubreleaserepo}"
  692. fn_script_log_fail "Cannot get version from GitHub API for ${githubreleaseuser}/${githubreleaserepo}"
  693. else
  694. # Fetch file from the remote location from the existing module to the ${tmpdir} for now.
  695. fn_fetch_file "${githubreleasedownloadlink}" "" "${githubreleasefilename}" "" "${githubreleasedownloadpath}" "${githubreleasefilename}"
  696. fi
  697. fi
  698. }