core_dl.sh 33 KB

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