From 81c38d63dae21e78d600c278b47ae57b9604a82a Mon Sep 17 00:00:00 2001 From: "Dylan J." Date: Fri, 7 Nov 2025 01:17:41 -0500 Subject: [PATCH 1/7] Add PLS parser with acompanying helper --- bot.py | 16 +++++++++++++-- pls_parser.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 pls_parser.py diff --git a/bot.py b/bot.py index ab2e9f8..647c441 100644 --- a/bot.py +++ b/bot.py @@ -11,6 +11,7 @@ from services.health_monitor import HealthMonitor from services.metadata_monitor import MetadataMonitor from services.state_manager import StateManager +from pls_parser import parse_pls import shout_errors import urllib_hack from dotenv import load_dotenv @@ -810,8 +811,9 @@ async def refresh_stream(interaction: discord.Interaction): await stop_playback(interaction.guild) await play_stream(interaction, url) -# Start playing music from the stream -# Check connection/status of server +# Start playing music from the stream +# Check connection/status of stream +# Check if stream link is .pls and parse it first # Get stream connection to server # Connect to voice channel # Start ffmpeg transcoding stream @@ -821,6 +823,16 @@ async def play_stream(interaction, url): if not url: logger.warning("No stream currently set, can't play nothing") raise shout_errors.NoStreamSelected + + # Handle .pls playlist files + if url.lower().endswith('.pls'): + await interaction.edit_original_response(content="❓ Looks Like this is a `.pls`, Let's see if I can figure it out...") + stream_url = await parse_pls(url) + if not stream_url: + # catch all + logger.error("Failed to parse .pls or no valid stream URL found") + raise shout_errors.StreamOffline() + url = stream_url # Connect to voice channel author is currently in voice_state = getattr(interaction.user, 'voice', None) # voice channel check, explicitly set to None if not found for some reason diff --git a/pls_parser.py b/pls_parser.py new file mode 100644 index 0000000..dd3789d --- /dev/null +++ b/pls_parser.py @@ -0,0 +1,57 @@ +import asyncio +import logging +from typing import Optional + +logger = logging.getLogger('discord') + +async def parse_pls(url: str) -> Optional[str]: + """ + parse a .pls playlist file using curl, Returns the first + valid stream URL found as a string. + + Args: + url: URL to the .pls file + + Returns: + The first valid stream URL found in the playlist, or None if no valid URLs found + """ + try: + # Use curl to stream the playlist file line by line + # iTunes user-agent to mimic a media player (safest bet for servers that block curl) + curl = await asyncio.create_subprocess_exec( + 'curl', '-L', '-N', '-A', '"iTunes/9.1.1"', '--silent', f'{url}', + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE + ) + # check if we have a valid stdout, if not stop early + while True: + output = await curl.stdout.readline() + if not output: + break + # look for "file1=" if we have data + try: + string = output.decode('utf-8').strip() + if string.lower().startswith('file1='): + stream_url = string.split('=', 1)[1].strip() + try: + curl.kill() # we got it, die now + await curl.wait() # Wait for it to die + except: + pass # closed by itself + return stream_url + except UnicodeDecodeError: + continue + + # Was unsuccessful, let's try to clean up + try: + curl.kill() + await curl.wait() + except: + pass # closed by itself + logger.warning(f"No valid stream URL found in playlist: {url}") + return None + + except Exception as e: + # we tried our best + logger.error(f"Error parsing playlist stream: {url} - {str(e)}") + return None From ac6cd7ca529304c40e163030ff697a70804be975 Mon Sep 17 00:00:00 2001 From: "Dylan J." Date: Fri, 7 Nov 2025 15:24:17 -0500 Subject: [PATCH 2/7] Change the way I determine if its a pls Also enforce allowed Scheme types in `FFmpeg` Allowed schemes: `http, https, TLS, Pipe` --- bot.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/bot.py b/bot.py index 647c441..479bf38 100644 --- a/bot.py +++ b/bot.py @@ -825,14 +825,17 @@ async def play_stream(interaction, url): raise shout_errors.NoStreamSelected # Handle .pls playlist files - if url.lower().endswith('.pls'): + slice = urllib.parse.urlparse(url) + path = slice.path + pls = path.find('.pls') + if pls != -1: await interaction.edit_original_response(content="❓ Looks Like this is a `.pls`, Let's see if I can figure it out...") stream_url = await parse_pls(url) - if not stream_url: - # catch all - logger.error("Failed to parse .pls or no valid stream URL found") - raise shout_errors.StreamOffline() - url = stream_url + if not stream_url: + # catch all + logger.error("Failed to parse .pls or no valid stream URL found") + raise shout_errors.StreamOffline() + url = stream_url # Connect to voice channel author is currently in voice_state = getattr(interaction.user, 'voice', None) # voice channel check, explicitly set to None if not found for some reason @@ -896,7 +899,7 @@ async def play_stream(interaction, url): # 4080 bytes per tick * 8 chunks = 32640 + 8 marker bytes = 32648 bits buffer (8 chunks) # 4080 bytes per tick * 4 Chunks = 16320 + 4 marker bytes = 16324 bits per analysis (4 chunks) try: - music_stream = discord.FFmpegOpusAudio(source=url, options="-analyzeduration 16324 -rtbufsize 32648 -filter:a loudnorm=I=-30:LRA=7:TP=-3 -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 120 -tls_verify 0") + music_stream = discord.FFmpegOpusAudio(source=url, options="-analyzeduration 16324 -rtbufsize 32648 -filter:a loudnorm=I=-30:LRA=7:TP=-3 -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 120 -tls_verify 0 -protocol_whitelist http,https,tls,pipe") await asyncio.sleep(1) # Give FFmpeg a moment to start except Exception as e: logger.error(f"Failed to start FFmpeg stream: {e}") From aadfeb4bb64bacad5896b31681d704dff4f11745 Mon Sep 17 00:00:00 2001 From: "Dylan J." Date: Fri, 7 Nov 2025 19:23:35 -0500 Subject: [PATCH 3/7] Fix Missed indent instead of always returning url = stream_url lets only do that when its a .pls, shall we? --- bot.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bot.py b/bot.py index 479bf38..aee4c30 100644 --- a/bot.py +++ b/bot.py @@ -831,12 +831,12 @@ async def play_stream(interaction, url): if pls != -1: await interaction.edit_original_response(content="❓ Looks Like this is a `.pls`, Let's see if I can figure it out...") stream_url = await parse_pls(url) - if not stream_url: - # catch all - logger.error("Failed to parse .pls or no valid stream URL found") - raise shout_errors.StreamOffline() - url = stream_url - + if not stream_url: + # catch all + logger.error("Failed to parse .pls or no valid stream URL found") + raise shout_errors.StreamOffline() + url = stream_url + # Connect to voice channel author is currently in voice_state = getattr(interaction.user, 'voice', None) # voice channel check, explicitly set to None if not found for some reason voice_channel = voice_state.channel if voice_state and getattr(voice_state, 'channel', None) else None From e0dacb9bedc3afac96cd04ea7237efe57d4ebb58 Mon Sep 17 00:00:00 2001 From: "Dylan J." Date: Fri, 7 Nov 2025 19:53:25 -0500 Subject: [PATCH 4/7] Add before and after logging steps --- bot.py | 3 ++- pls_parser.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index aee4c30..32755ba 100644 --- a/bot.py +++ b/bot.py @@ -829,6 +829,7 @@ async def play_stream(interaction, url): path = slice.path pls = path.find('.pls') if pls != -1: + logger.debug(f"Detected .pls file, attempting to parse: {url}") await interaction.edit_original_response(content="❓ Looks Like this is a `.pls`, Let's see if I can figure it out...") stream_url = await parse_pls(url) if not stream_url: @@ -836,7 +837,7 @@ async def play_stream(interaction, url): logger.error("Failed to parse .pls or no valid stream URL found") raise shout_errors.StreamOffline() url = stream_url - + # Connect to voice channel author is currently in voice_state = getattr(interaction.user, 'voice', None) # voice channel check, explicitly set to None if not found for some reason voice_channel = voice_state.channel if voice_state and getattr(voice_state, 'channel', None) else None diff --git a/pls_parser.py b/pls_parser.py index dd3789d..f812ab1 100644 --- a/pls_parser.py +++ b/pls_parser.py @@ -32,12 +32,13 @@ async def parse_pls(url: str) -> Optional[str]: try: string = output.decode('utf-8').strip() if string.lower().startswith('file1='): - stream_url = string.split('=', 1)[1].strip() + stream_url = string.split('=', 1)[1].strip() try: curl.kill() # we got it, die now await curl.wait() # Wait for it to die except: pass # closed by itself + logger.debug(f"Found Stream Link to be: {stream_url}") return stream_url except UnicodeDecodeError: continue From 2acc476a955627043a3099c27932f7aa9338ce28 Mon Sep 17 00:00:00 2001 From: "Dylan J." Date: Fri, 7 Nov 2025 23:10:19 -0500 Subject: [PATCH 5/7] Add Windows runner support to CodeQL workflow --- .github/workflows/codeql.yml | 99 ++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 .github/workflows/codeql.yml diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..066b011 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,99 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL Advanced" + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + schedule: + - cron: '28 3 * * 4' + +jobs: + analyze: + name: Analyze (${{ matrix.language }}) + # Runner size impacts CodeQL analysis time. To learn more, please see: + # - https://gh.io/recommended-hardware-resources-for-running-codeql + # - https://gh.io/supported-runners-and-hardware-resources + # - https://gh.io/using-larger-runners (GitHub.com only) + # Consider using larger runners or machines with greater resources for possible analysis time improvements. + runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' || 'windows-latest' }} + permissions: + # required for all workflows + security-events: write + + # required to fetch internal or private CodeQL packs + packages: read + + # only required for workflows in private repositories + actions: read + contents: read + + strategy: + fail-fast: false + matrix: + include: + - language: python + build-mode: none + # CodeQL supports the following values keywords for 'language': 'actions', 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'rust', 'swift' + # Use `c-cpp` to analyze code written in C, C++ or both + # Use 'java-kotlin' to analyze code written in Java, Kotlin or both + # Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both + # To learn more about changing the languages that are analyzed or customizing the build mode for your analysis, + # see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning. + # If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how + # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + # Add any setup steps before running the `github/codeql-action/init` action. + # This includes steps like installing compilers or runtimes (`actions/setup-node` + # or others). This is typically only required for manual builds. + # - name: Setup runtime (example) + # uses: actions/setup-example@v1 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v4 + with: + languages: ${{ matrix.language }} + build-mode: ${{ matrix.build-mode }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs + # queries: security-extended,security-and-quality + + # If the analyze step fails for one of the languages you are analyzing with + # "We were unable to automatically build your code", modify the matrix above + # to set the build mode to "manual" for that language. Then modify this step + # to build your code. + # ℹ️ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + - name: Run manual build steps + if: matrix.build-mode == 'manual' + shell: bash + run: | + echo 'If you are using a "manual" build mode for one or more of the' \ + 'languages you are analyzing, replace this with the commands to build' \ + 'your code, for example:' + echo ' make bootstrap' + echo ' make release' + exit 1 + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v4 + with: + category: "/language:${{matrix.language}}" From 5246ea724195eec18340a4667d79e74cff7c4466 Mon Sep 17 00:00:00 2001 From: "Dylan J." Date: Fri, 7 Nov 2025 23:27:44 -0500 Subject: [PATCH 6/7] Add CodeQL --- codeql.yml | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 codeql.yml diff --git a/codeql.yml b/codeql.yml new file mode 100644 index 0000000..066b011 --- /dev/null +++ b/codeql.yml @@ -0,0 +1,99 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL Advanced" + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + schedule: + - cron: '28 3 * * 4' + +jobs: + analyze: + name: Analyze (${{ matrix.language }}) + # Runner size impacts CodeQL analysis time. To learn more, please see: + # - https://gh.io/recommended-hardware-resources-for-running-codeql + # - https://gh.io/supported-runners-and-hardware-resources + # - https://gh.io/using-larger-runners (GitHub.com only) + # Consider using larger runners or machines with greater resources for possible analysis time improvements. + runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' || 'windows-latest' }} + permissions: + # required for all workflows + security-events: write + + # required to fetch internal or private CodeQL packs + packages: read + + # only required for workflows in private repositories + actions: read + contents: read + + strategy: + fail-fast: false + matrix: + include: + - language: python + build-mode: none + # CodeQL supports the following values keywords for 'language': 'actions', 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'rust', 'swift' + # Use `c-cpp` to analyze code written in C, C++ or both + # Use 'java-kotlin' to analyze code written in Java, Kotlin or both + # Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both + # To learn more about changing the languages that are analyzed or customizing the build mode for your analysis, + # see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning. + # If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how + # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + # Add any setup steps before running the `github/codeql-action/init` action. + # This includes steps like installing compilers or runtimes (`actions/setup-node` + # or others). This is typically only required for manual builds. + # - name: Setup runtime (example) + # uses: actions/setup-example@v1 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v4 + with: + languages: ${{ matrix.language }} + build-mode: ${{ matrix.build-mode }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs + # queries: security-extended,security-and-quality + + # If the analyze step fails for one of the languages you are analyzing with + # "We were unable to automatically build your code", modify the matrix above + # to set the build mode to "manual" for that language. Then modify this step + # to build your code. + # ℹ️ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + - name: Run manual build steps + if: matrix.build-mode == 'manual' + shell: bash + run: | + echo 'If you are using a "manual" build mode for one or more of the' \ + 'languages you are analyzing, replace this with the commands to build' \ + 'your code, for example:' + echo ' make bootstrap' + echo ' make release' + exit 1 + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v4 + with: + category: "/language:${{matrix.language}}" From ae45a15c22b3e90f558ba2e6b5d11f147d97bffb Mon Sep 17 00:00:00 2001 From: "Dylan J." Date: Fri, 7 Nov 2025 23:32:40 -0500 Subject: [PATCH 7/7] Delete codeql.yml --- codeql.yml | 99 ------------------------------------------------------ 1 file changed, 99 deletions(-) delete mode 100644 codeql.yml diff --git a/codeql.yml b/codeql.yml deleted file mode 100644 index 066b011..0000000 --- a/codeql.yml +++ /dev/null @@ -1,99 +0,0 @@ -# For most projects, this workflow file will not need changing; you simply need -# to commit it to your repository. -# -# You may wish to alter this file to override the set of languages analyzed, -# or to provide custom queries or build logic. -# -# ******** NOTE ******** -# We have attempted to detect the languages in your repository. Please check -# the `language` matrix defined below to confirm you have the correct set of -# supported CodeQL languages. -# -name: "CodeQL Advanced" - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - schedule: - - cron: '28 3 * * 4' - -jobs: - analyze: - name: Analyze (${{ matrix.language }}) - # Runner size impacts CodeQL analysis time. To learn more, please see: - # - https://gh.io/recommended-hardware-resources-for-running-codeql - # - https://gh.io/supported-runners-and-hardware-resources - # - https://gh.io/using-larger-runners (GitHub.com only) - # Consider using larger runners or machines with greater resources for possible analysis time improvements. - runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' || 'windows-latest' }} - permissions: - # required for all workflows - security-events: write - - # required to fetch internal or private CodeQL packs - packages: read - - # only required for workflows in private repositories - actions: read - contents: read - - strategy: - fail-fast: false - matrix: - include: - - language: python - build-mode: none - # CodeQL supports the following values keywords for 'language': 'actions', 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'rust', 'swift' - # Use `c-cpp` to analyze code written in C, C++ or both - # Use 'java-kotlin' to analyze code written in Java, Kotlin or both - # Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both - # To learn more about changing the languages that are analyzed or customizing the build mode for your analysis, - # see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning. - # If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how - # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - # Add any setup steps before running the `github/codeql-action/init` action. - # This includes steps like installing compilers or runtimes (`actions/setup-node` - # or others). This is typically only required for manual builds. - # - name: Setup runtime (example) - # uses: actions/setup-example@v1 - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v4 - with: - languages: ${{ matrix.language }} - build-mode: ${{ matrix.build-mode }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - - # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs - # queries: security-extended,security-and-quality - - # If the analyze step fails for one of the languages you are analyzing with - # "We were unable to automatically build your code", modify the matrix above - # to set the build mode to "manual" for that language. Then modify this step - # to build your code. - # ℹ️ Command-line programs to run using the OS shell. - # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun - - name: Run manual build steps - if: matrix.build-mode == 'manual' - shell: bash - run: | - echo 'If you are using a "manual" build mode for one or more of the' \ - 'languages you are analyzing, replace this with the commands to build' \ - 'your code, for example:' - echo ' make bootstrap' - echo ' make release' - exit 1 - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v4 - with: - category: "/language:${{matrix.language}}"