core_dl.sh 29 KB

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