Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and complete the secure connection implementation #116

Merged
merged 3 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lavalink/lavalink.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async def initialize(
resume_timeout : inr
How long the node should wait for a connection while disconnected before clearing all players.
secured: bool
Whether to use the `wss://` protocol.
Whether to use the `wss://` and `https://` protocol.
"""
global _loop
_loop = bot.loop
Expand All @@ -85,9 +85,10 @@ async def initialize(
resume_key=resume_key,
resume_timeout=resume_timeout,
bot=bot,
secured=secured,
)

await lavalink_node.connect(timeout=timeout, secured=secured)
await lavalink_node.connect(timeout=timeout)
lavalink_node._retries = 0

bot.add_listener(node.on_socket_response)
Expand Down
8 changes: 4 additions & 4 deletions lavalink/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def __init__(
resume_key: Optional[str] = None,
resume_timeout: int = 60,
bot: Bot = None,
secured: bool = False,
):
"""
Represents a Lavalink node.
Expand Down Expand Up @@ -157,6 +158,7 @@ def __init__(
self._resuming_configured = False
self.num_shards = num_shards
self.user_id = user_id
self.secured = secured

self._ready_event = asyncio.Event()

Expand Down Expand Up @@ -207,14 +209,12 @@ def _gen_key(self):
self._resume_key.__repr__()
return self._resume_key

async def connect(self, timeout=None, secured=False):
async def connect(self, timeout=None):
"""
Connects to the Lavalink player event websocket.

Parameters
----------
secured: bool
Whether to use the `wss://` protocol.
timeout : int
Time after which to timeout on attempting to connect to the Lavalink websocket,
``None`` is considered never, but the underlying code may stop trying past a
Expand All @@ -226,7 +226,7 @@ async def connect(self, timeout=None, secured=False):
If the websocket failed to connect after the given time.
"""
self._is_shutdown = False
if secured:
if self.secured:
uri = f"wss://{self.host}:{self.port}"
else:
uri = f"ws://{self.host}:{self.port}"
Expand Down
7 changes: 6 additions & 1 deletion lavalink/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,13 @@ class RESTClient:

def __init__(self, node):
self.node = node
self.secured = node.secured
self._session = None
self._uri = "http://{}:{}/loadtracks?identifier=".format(node.host, node.port)
if self.secured:
protocol = "https"
else:
protocol = "http"
self._uri = f"{protocol}://{node.host}:{node.port}/loadtracks?identifier="
self._headers = {"Authorization": node.password}

self.state = PlayerState.CONNECTING
Expand Down