core_dl.sh 34 KB

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