Skip to content

Commit 931aaa4

Browse files
authored
refactor: update decryption (#290)
* refactor: update decryption
1 parent 511b457 commit 931aaa4

File tree

1 file changed

+71
-14
lines changed

1 file changed

+71
-14
lines changed

lobster.sh

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env sh
22

3-
LOBSTER_VERSION="4.5.7"
3+
LOBSTER_VERSION="4.5.8"
44

55
### General Variables ###
66
config_file="$HOME/.config/lobster/lobster_config.sh"
@@ -18,6 +18,7 @@ nl='
1818
# These are not arbitrary, but determined by rofi kb-custom-1 and kb-custom-2 exit codes
1919
BACK_CODE=10
2020
FORWARD_CODE=11
21+
API_URL="https://dec.eatmynerds.live"
2122

2223
### Notifications ###
2324
command -v notify-send >/dev/null 2>&1 && notify="true" || notify="false" # check if notify-send is installed
@@ -329,7 +330,7 @@ EOF
329330
rc="$BACK_CODE"
330331
choice=${choice#*"$nl"}
331332
;;
332-
"$nl"*) choice=${choice#"$nl"} ;;
333+
"$nl"*) choice=${choice#*"$nl"} ;;
333334
*) exit 1 ;;
334335
esac
335336
fi
@@ -493,7 +494,7 @@ EOF
493494
rc="$BACK_CODE"
494495
choice=${choice#*"$nl"}
495496
;;
496-
"$nl"*) choice=${choice#"$nl"} ;;
497+
"$nl"*) choice=${choice#*"$nl"} ;;
497498
*) exit 1 ;;
498499
esac
499500
ueberzugpp cmd -s "$LOBSTER_UEBERZUG_SOCKET" -a exit
@@ -514,7 +515,7 @@ EOF
514515
rc="$BACK_CODE"
515516
choice=${choice#*"$nl"}
516517
;;
517-
"$nl"*) choice=${choice#"$nl"} ;;
518+
"$nl"*) choice=${choice#*"$nl"} ;;
518519
*) exit 1 ;;
519520
esac
520521
fi
@@ -563,23 +564,79 @@ EOF
563564
}
564565

565566
extract_from_embed() {
566-
json_data=$(curl -s "https://dec.eatmynerds.live/?url=${embed_link}")
567+
challenge_response=$(curl -s "${API_URL}/challenge")
568+
569+
if [ -z "$challenge_response" ]; then
570+
send_notification "ERROR: Failed to get a response from the API server."
571+
return 1
572+
fi
573+
574+
payload=$(printf "%s" "${challenge_response}" | sed -n 's/.*"payload":"\([^"]*\)".*/\1/p')
575+
signature=$(printf "%s" "${challenge_response}" | sed -n 's/.*"signature":"\([^"]*\)".*/\1/p')
576+
difficulty=$(printf "%s" "${challenge_response}" | sed -n 's/.*"difficulty":\([0-9]*\).*/\1/p')
577+
challenge=$(printf "%s" "${payload}" | cut -d'.' -f1)
578+
579+
if [ -z "$payload" ] || [ -z "$signature" ] || [ -z "$difficulty" ]; then
580+
send_notification "FATAL: Could not parse the API challenge response."
581+
return 1
582+
fi
583+
584+
prefix=""
585+
i=0
586+
while [ "$i" -lt "$difficulty" ]; do
587+
prefix="${prefix}0"
588+
i=$((i + 1))
589+
done
590+
591+
nonce=0
592+
while true; do
593+
text_to_hash="${challenge}${nonce}"
594+
hash_val=$(printf "%s" "${text_to_hash}" | sha256sum | awk '{print $1}')
595+
596+
case "$hash_val" in
597+
"${prefix}"*)
598+
break
599+
;;
600+
*) ;;
601+
esac
602+
603+
nonce=$((nonce + 1))
604+
done
605+
606+
final_url="${API_URL}/?url=${embed_link}&payload=${payload}&signature=${signature}&nonce=${nonce}"
607+
json_data=$(curl -s "${final_url}")
608+
609+
case "$json_data" in
610+
*"error"*)
611+
api_error_msg=$(printf "%s" "$json_data" | $sed -n 's/.*"error":"\([^"]*\)".*/\1/p')
612+
send_notification "API Error: $api_error_msg"
613+
return 1
614+
;;
615+
*) ;;
616+
esac
617+
567618
video_link=$(printf "%s" "$json_data" | $sed -nE "s_.*\"file\":\"([^\"]*\.m3u8)\".*_\1_p" | head -1)
568619

569-
[ -n "$quality" ] && video_link=$(printf "%s" "$video_link" | $sed -e "s|/playlist.m3u8|/$quality/index.m3u8|")
620+
[ -n "$quality" ] && video_link=$(printf "%s" "$video_link" | sed -e "s|/playlist.m3u8|/$quality/index.m3u8|")
570621

571622
[ "$json_output" = "true" ] && printf "%s\n" "$json_data" && exit 0
623+
572624
if [ "$no_subs" = "true" ]; then
573625
send_notification "Continuing without subtitles"
574626
else
575-
subs_links=$(printf "%s" "$json_data" | tr "{}" "\n" | $sed -nE "s@.*\"file\":\"([^\"]*)\",\"label\":\"(.$subs_language)[,\"\ ].*@\1@p")
576-
subs_arg="--sub-file"
577-
num_subs=$(printf "%s" "$subs_links" | wc -l)
578-
if [ "$num_subs" -gt 0 ]; then
579-
subs_links=$(printf "%s" "$subs_links" | $sed -e "s/:/\\$path_thing:/g" -e "H;1h;\$!d;x;y/\n/$separator/" -e "s/$separator\$//")
580-
subs_arg="--sub-files"
627+
subs_links=$(printf "%s" "$json_data" | tr '{' '\n' | $sed -n "s/.*\"file\":\"\([^\"]*\)\".*\"label\":\"[^\"]*${subs_language}[^\"]*\".*/\1/Ip")
628+
629+
if [ -z "$subs_links" ]; then
630+
send_notification "No subtitles found for language '$subs_language'"
631+
subs_arg=""
632+
else
633+
subs_arg="--sub-file"
634+
num_subs=$(printf "%s" "$subs_links" | wc -l)
635+
if [ "$num_subs" -gt 0 ]; then
636+
subs_links=$(printf "%s" "$subs_links" | sed -e "s/:/\\$path_thing:/g" -e "H;1h;\$!d;x;y/\n/$separator/" -e "s/$separator\$//")
637+
subs_arg="--sub-files"
638+
fi
581639
fi
582-
[ -z "$subs_links" ] && send_notification "No subtitles found"
583640
fi
584641
}
585642

@@ -921,7 +978,7 @@ EOF
921978
fi
922979
else
923980
if [ "$image_preview" = "true" ]; then
924-
download_video "$video_link" "$title - $season_title - $episode_title" "$download_dir" "$json_data" "$images_cache_dir/ $title ($media_type) $media_id.jpg" &
981+
download_video "$video_link" "$title - $season_title - $episode_title" "$download_dir" "$json_data" "$images_cache_dir/ $title - $season_title - $episode_title ($media_type) $media_id.jpg" &
925982
send_notification "Finished downloading" "5000" "$images_cache_dir/ $title - $season_title - $episode_title ($media_type) $media_id.jpg" "$title - $season_title - $episode_title"
926983
else
927984
download_video "$video_link" "$title - $season_title - $episode_title" "$download_dir" "$json_data" &

0 commit comments

Comments
 (0)