core_dl.sh 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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 bzip2, gzip or zip files.
  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. fi
  303. mime=$(file -b --mime-type "${local_filedir}/${local_filename}")
  304. if [ "${mime}" == "application/gzip" ] || [ "${mime}" == "application/x-gzip" ]; then
  305. if [ -n "${extractsrc}" ]; then
  306. extractcmd=$(tar -zxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}")
  307. else
  308. extractcmd=$(tar -zxf "${local_filedir}/${local_filename}" -C "${extractdest}")
  309. fi
  310. elif [ "${mime}" == "application/x-bzip2" ]; then
  311. if [ -n "${extractsrc}" ]; then
  312. extractcmd=$(tar -jxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}")
  313. else
  314. extractcmd=$(tar -jxf "${local_filedir}/${local_filename}" -C "${extractdest}")
  315. fi
  316. elif [ "${mime}" == "application/x-xz" ]; then
  317. if [ -n "${extractsrc}" ]; then
  318. extractcmd=$(tar -Jxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}")
  319. else
  320. extractcmd=$(tar -Jxf "${local_filedir}/${local_filename}" -C "${extractdest}")
  321. fi
  322. elif [ "${mime}" == "application/zip" ]; then
  323. if [ -n "${extractsrc}" ]; then
  324. temp_extractdir="${tmpdir}/${extractsrc}"
  325. extractcmd=$(unzip -qo "${local_filedir}/${local_filename}" "${extractsrc}/*" -d "${temp_extractdir}")
  326. cp -a "${temp_extractdir}/${extractsrc}/." "${extractdest}/"
  327. rm -rf "${temp_extractdir}"
  328. else
  329. extractcmd=$(unzip -qo -d "${extractdest}" "${local_filedir}/${local_filename}")
  330. fi
  331. fi
  332. exitcode=$?
  333. if [ "${exitcode}" -ne 0 ]; then
  334. fn_print_fail_eol_nl
  335. fn_script_log_fail "Extracting ${local_filename}"
  336. if [ -f "${lgsmlog}" ]; then
  337. echo -e "${extractcmd}" >> "${lgsmlog}"
  338. fi
  339. echo -e "${extractcmd}"
  340. core_exit.sh
  341. else
  342. fn_print_ok_eol_nl
  343. fn_script_log_pass "Extracting ${local_filename}"
  344. fi
  345. }
  346. # Trap to remove file download if canceled before completed.
  347. fn_fetch_trap() {
  348. echo -e ""
  349. echo -en "downloading ${local_filename}"
  350. fn_print_canceled_eol_nl
  351. fn_script_log_info "Downloading ${local_filename}...CANCELED"
  352. rm -f "${local_filedir:?}/${local_filename}"
  353. echo -en "downloading ${local_filename}"
  354. fn_print_removed_eol_nl
  355. fn_script_log_info "Downloading ${local_filename}...REMOVED"
  356. core_exit.sh
  357. }
  358. # Will check a file exists and download it. Will not exit if fails to download.
  359. fn_check_file() {
  360. remote_fileurl="${1}"
  361. remote_fileurl_backup="${2}"
  362. remote_fileurl_name="${3}"
  363. remote_fileurl_backup_name="${4}"
  364. remote_filename="${5}"
  365. # If backup fileurl exists include it.
  366. if [ -n "${remote_fileurl_backup}" ]; then
  367. # counter set to 0 to allow second try
  368. counter=0
  369. remote_fileurls_array=(remote_fileurl remote_fileurl_backup)
  370. else
  371. # counter set to 1 to not allow second try
  372. counter=1
  373. remote_fileurls_array=(remote_fileurl)
  374. fi
  375. for remote_fileurl_array in "${remote_fileurls_array[@]}"; do
  376. if [ "${remote_fileurl_array}" == "remote_fileurl" ]; then
  377. fileurl="${remote_fileurl}"
  378. fileurl_name="${remote_fileurl_name}"
  379. elif [ "${remote_fileurl_array}" == "remote_fileurl_backup" ]; then
  380. fileurl="${remote_fileurl_backup}"
  381. fileurl_name="${remote_fileurl_backup_name}"
  382. fi
  383. counter=$((counter + 1))
  384. echo -e "checking ${fileurl_name} ${remote_filename}\c"
  385. curlcmd=$(curl --output /dev/null --silent --head --fail "${fileurl}" 2>&1)
  386. exitcode=$?
  387. # On first try will error. On second try will fail.
  388. if [ "${exitcode}" -ne 0 ]; then
  389. if [ ${counter} -ge 2 ]; then
  390. fn_print_fail_eol_nl
  391. if [ -f "${lgsmlog}" ]; then
  392. fn_script_log_fail "Checking ${remote_filename}"
  393. fn_script_log_fail "${fileurl}"
  394. checkflag=1
  395. fi
  396. else
  397. fn_print_error_eol_nl
  398. if [ -f "${lgsmlog}" ]; then
  399. fn_script_log_error "Checking ${remote_filename}"
  400. fn_script_log_error "${fileurl}"
  401. checkflag=2
  402. fi
  403. fi
  404. else
  405. fn_print_ok_eol
  406. echo -en "\033[2K\\r"
  407. if [ -f "${lgsmlog}" ]; then
  408. fn_script_log_pass "Checking ${remote_filename}"
  409. checkflag=0
  410. fi
  411. break
  412. fi
  413. done
  414. if [ -f "${local_filedir}/${local_filename}" ]; then
  415. fn_dl_hash
  416. # Execute file if run is set.
  417. if [ "${run}" == "run" ]; then
  418. # shellcheck source=/dev/null
  419. source "${local_filedir}/${local_filename}"
  420. fi
  421. fi
  422. }
  423. fn_fetch_file() {
  424. remote_fileurl="${1}"
  425. remote_fileurl_backup="${2}"
  426. remote_fileurl_name="${3}"
  427. remote_fileurl_backup_name="${4}"
  428. local_filedir="${5}"
  429. local_filename="${6}"
  430. chmodx="${7:-0}"
  431. run="${8:-0}"
  432. forcedl="${9:-0}"
  433. hash="${10:-0}"
  434. # Download file if missing or download forced.
  435. if [ ! -f "${local_filedir}/${local_filename}" ] || [ "${forcedl}" == "forcedl" ]; then
  436. # If backup fileurl exists include it.
  437. if [ -n "${remote_fileurl_backup}" ]; then
  438. # counter set to 0 to allow second try
  439. counter=0
  440. remote_fileurls_array=(remote_fileurl remote_fileurl_backup)
  441. else
  442. # counter set to 1 to not allow second try
  443. counter=1
  444. remote_fileurls_array=(remote_fileurl)
  445. fi
  446. for remote_fileurl_array in "${remote_fileurls_array[@]}"; do
  447. if [ "${remote_fileurl_array}" == "remote_fileurl" ]; then
  448. fileurl="${remote_fileurl}"
  449. fileurl_name="${remote_fileurl_name}"
  450. elif [ "${remote_fileurl_array}" == "remote_fileurl_backup" ]; then
  451. fileurl="${remote_fileurl_backup}"
  452. fileurl_name="${remote_fileurl_backup_name}"
  453. fi
  454. counter=$((counter + 1))
  455. if [ ! -d "${local_filedir}" ]; then
  456. mkdir -p "${local_filedir}"
  457. fi
  458. # Trap will remove part downloaded files if canceled.
  459. trap fn_fetch_trap INT
  460. 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")
  461. if [[ "${fileurl}" == https://* ]]; then
  462. curlcmd+=(--proto-redir "=https")
  463. fi
  464. # if is large file show progress, else be silent
  465. local exitcode=""
  466. large_files=("bz2" "gz" "zip" "jar" "xz")
  467. if grep -qE "(^|\s)${local_filename##*.}(\s|$)" <<< "${large_files[@]}"; then
  468. echo -e "downloading file [ ${italic}${local_filename}${default} ]"
  469. fn_sleep_time
  470. "${curlcmd[@]}" --progress-bar "${fileurl}" 2>&1
  471. exitcode=$?
  472. echo -en "downloading file [ ${italic}${local_filename}${default} ]"
  473. else
  474. echo -en "fetching ${fileurl_name} [ ${italic}${local_filename}${default} ]\c"
  475. "${curlcmd[@]}" --silent --show-error "${fileurl}" 2>&1
  476. exitcode=$?
  477. fi
  478. # Download will fail if downloads a html file.
  479. if [ -f "${local_filedir}/${local_filename}" ]; then
  480. if head -n 1 "${local_filedir}/${local_filename}" | grep -q "DOCTYPE"; then
  481. rm "${local_filedir:?}/${local_filename:?}"
  482. local exitcode=2
  483. fi
  484. fi
  485. # On first try will error. On second try will fail.
  486. if [ "${exitcode}" -ne 0 ]; then
  487. if [ ${counter} -ge 2 ]; then
  488. fn_print_fail_eol_nl
  489. if [ -f "${lgsmlog}" ]; then
  490. fn_script_log_fail "Downloading ${local_filename}..."
  491. fn_script_log_fail "${fileurl}"
  492. fi
  493. core_exit.sh
  494. else
  495. fn_print_error_eol_nl
  496. if [ -f "${lgsmlog}" ]; then
  497. fn_script_log_error "Downloading ${local_filename}..."
  498. fn_script_log_error "${fileurl}"
  499. fi
  500. fi
  501. else
  502. fn_print_ok_eol_nl
  503. if [ -f "${lgsmlog}" ]; then
  504. fn_script_log_pass "Downloading ${local_filename}..."
  505. fi
  506. # Make file executable if chmodx is set.
  507. if [ "${chmodx}" == "chmodx" ]; then
  508. chmod +x "${local_filedir}/${local_filename}"
  509. fi
  510. # Remove trap.
  511. trap - INT
  512. break
  513. fi
  514. done
  515. fi
  516. if [ -f "${local_filedir}/${local_filename}" ]; then
  517. fn_dl_hash
  518. # Execute file if run is set.
  519. if [ "${run}" == "run" ]; then
  520. # shellcheck source=/dev/null
  521. source "${local_filedir}/${local_filename}"
  522. fi
  523. fi
  524. }
  525. # GitHub file download modules.
  526. # Used to simplify downloading specific files from GitHub.
  527. # github_file_url_dir: the directory of the file in the GitHub: lgsm/modules
  528. # github_file_url_name: the filename of the file to download from GitHub: core_messages.sh
  529. # github_file_url_dir: the directory of the file in the GitHub: lgsm/modules
  530. # github_file_url_name: the filename of the file to download from GitHub: core_messages.sh
  531. # githuburl: the full GitHub url
  532. # remote_fileurl: The URL of the file: http://example.com/dl/File.tar.bz2
  533. # local_filedir: location the file is to be saved: /home/server/lgsm/tmp
  534. # local_filename: name of file (this can be different from the url name): file.tar.bz2
  535. # chmodx: Optional, set to "chmodx" to make file executable using chmod +x
  536. # run: Optional, set run to execute the file after download
  537. # forcedl: Optional, force re-download of file even if exists
  538. # hash: Optional, set an hash sum and will compare it against the file.
  539. # Fetches files from the Git repo.
  540. fn_fetch_file_github() {
  541. github_file_url_dir="${1}"
  542. github_file_url_name="${2}"
  543. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  544. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  545. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  546. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  547. else
  548. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  549. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  550. fi
  551. remote_fileurl_name="GitHub"
  552. remote_fileurl_backup_name="Bitbucket"
  553. local_filedir="${3}"
  554. local_filename="${github_file_url_name}"
  555. chmodx="${4:-0}"
  556. run="${5:-0}"
  557. forcedl="${6:-0}"
  558. hash="${7:-0}"
  559. # Passes vars to the file download module.
  560. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  561. }
  562. fn_check_file_github() {
  563. github_file_url_dir="${1}"
  564. github_file_url_name="${2}"
  565. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  566. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  567. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  568. else
  569. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  570. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  571. fi
  572. remote_fileurl_name="GitHub"
  573. remote_fileurl_backup_name="Bitbucket"
  574. fn_check_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${github_file_url_name}"
  575. }
  576. # Fetches config files from the Git repo.
  577. fn_fetch_config() {
  578. github_file_url_dir="${1}"
  579. github_file_url_name="${2}"
  580. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  581. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  582. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  583. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  584. else
  585. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  586. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  587. fi
  588. remote_fileurl_name="GitHub"
  589. remote_fileurl_backup_name="Bitbucket"
  590. local_filedir="${3}"
  591. local_filename="${4}"
  592. chmodx="nochmodx"
  593. run="norun"
  594. forcedl="noforce"
  595. hash="nohash"
  596. # Passes vars to the file download module.
  597. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  598. }
  599. # Fetches modules from the Git repo during first download.
  600. fn_fetch_module() {
  601. github_file_url_dir="lgsm/modules"
  602. github_file_url_name="${modulefile}"
  603. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  604. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  605. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  606. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  607. else
  608. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  609. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  610. fi
  611. remote_fileurl_name="GitHub"
  612. remote_fileurl_backup_name="Bitbucket"
  613. local_filedir="${modulesdir}"
  614. local_filename="${github_file_url_name}"
  615. chmodx="chmodx"
  616. run="run"
  617. forcedl="noforce"
  618. hash="nohash"
  619. # Passes vars to the file download module.
  620. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  621. }
  622. # Fetches modules from the Git repo during update-lgsm.
  623. fn_update_module() {
  624. github_file_url_dir="lgsm/modules"
  625. github_file_url_name="${modulefile}"
  626. # If master branch will currently running LinuxGSM version to prevent "version mixing". This is ignored if a fork.
  627. if [ "${githubbranch}" == "master" ] && [ "${githubuser}" == "GameServerManagers" ] && [ "${commandname}" != "UPDATE-LGSM" ]; then
  628. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${version}/${github_file_url_dir}/${github_file_url_name}"
  629. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${version}/${github_file_url_dir}/${github_file_url_name}"
  630. else
  631. remote_fileurl="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  632. remote_fileurl_backup="https://bitbucket.org/${githubuser}/${githubrepo}/raw/${githubbranch}/${github_file_url_dir}/${github_file_url_name}"
  633. fi
  634. remote_fileurl_name="GitHub"
  635. remote_fileurl_backup_name="Bitbucket"
  636. local_filedir="${modulesdir}"
  637. local_filename="${github_file_url_name}"
  638. chmodx="chmodx"
  639. run="norun"
  640. forcedl="noforce"
  641. hash="nohash"
  642. # Passes vars to the file download module.
  643. fn_fetch_file "${remote_fileurl}" "${remote_fileurl_backup}" "${remote_fileurl_name}" "${remote_fileurl_backup_name}" "${local_filedir}" "${local_filename}" "${chmodx}" "${run}" "${forcedl}" "${hash}"
  644. }
  645. # Function to download latest github release.
  646. # $1 GitHub user / organisation.
  647. # $2 Repo name.
  648. # $3 Destination for download.
  649. # $4 Search string in releases (needed if there are more files that can be downloaded from the release pages).
  650. fn_dl_latest_release_github() {
  651. local githubreleaseuser="${1}"
  652. local githubreleaserepo="${2}"
  653. local githubreleasedownloadpath="${3}"
  654. local githubreleasesearch="${4}"
  655. local githublatestreleaseurl="https://api.github.com/repos/${githubreleaseuser}/${githubreleaserepo}/releases/latest"
  656. # Get last github release.
  657. # If no search for the release filename is set, just get the first file from the latest release.
  658. if [ -z "${githubreleasesearch}" ]; then
  659. githubreleaseassets=$(curl -s "${githublatestreleaseurl}" | jq '[ .assets[] ]')
  660. else
  661. githubreleaseassets=$(curl -s "${githublatestreleaseurl}" | jq "[ .assets[]|select(.browser_download_url | contains(\"${githubreleasesearch}\")) ]")
  662. fi
  663. # Check how many releases we got from the api and exit if we have more then one.
  664. if [ "$(echo -e "${githubreleaseassets}" | jq '. | length')" -gt 1 ]; then
  665. fn_print_fatal_nl "Found more than one release to download - Please report this to the LinuxGSM issue tracker"
  666. fn_script_log_fail "Found more than one release to download - Please report this to the LinuxGSM issue tracker"
  667. else
  668. # Set variables for download via fn_fetch_file.
  669. githubreleasefilename=$(echo -e "${githubreleaseassets}" | jq -r '.[]name')
  670. githubreleasedownloadlink=$(echo -e "${githubreleaseassets}" | jq -r '.[]browser_download_url')
  671. # Error if no version is there.
  672. if [ -z "${githubreleasefilename}" ]; then
  673. fn_print_fail_nl "Cannot get version from GitHub API for ${githubreleaseuser}/${githubreleaserepo}"
  674. fn_script_log_fail "Cannot get version from GitHub API for ${githubreleaseuser}/${githubreleaserepo}"
  675. else
  676. # Fetch file from the remote location from the existing module to the ${tmpdir} for now.
  677. fn_fetch_file "${githubreleasedownloadlink}" "" "${githubreleasefilename}" "" "${githubreleasedownloadpath}" "${githubreleasefilename}"
  678. fi
  679. fi
  680. }