core_dl.sh 35 KB

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