Skip to content

Commit

Permalink
Merge pull request #41 from GetWVKeys/feat/replace-pywidevine
Browse files Browse the repository at this point in the history
Implement new pywidevine
  • Loading branch information
Puyodead1 committed Jul 26, 2024
2 parents 382cdea + 4bef7b2 commit d50cc66
Show file tree
Hide file tree
Showing 25 changed files with 901 additions and 1,082 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ Example command to run on port 8081 listening on all interfaces:

- `poetry run gunicorn -w 1 -b 0.0.0.0:8081 getwvkeys.main:app`

or waitress:
- `poetry run waitress-serve --listen=*:8081 getwvkeys.main:app`

_never use more than 1 worker, getwvkeys does not currently support that and you will encounter issues with sessions._

# Other Info
Expand Down
12 changes: 3 additions & 9 deletions alembic/versions/684292138a0a_many_devices_to_many_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
revision: str = "684292138a0a"
down_revision: Union[str, None] = "c8ce82d0c054"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = "c8ce82d0c054"


def upgrade() -> None:
Expand Down Expand Up @@ -91,14 +91,8 @@ def upgrade() -> None:
"user_device",
sa.Column("user_id", sa.VARCHAR(255), nullable=False),
sa.Column("device_code", sa.VARCHAR(255), nullable=False),
sa.ForeignKeyConstraint(
["device_code"],
["devices.code"],
),
sa.ForeignKeyConstraint(
["user_id"],
["users.id"],
),
sa.ForeignKeyConstraint(["device_code"], ["devices.code"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
)

# remove useless columns, these never served any purpose
Expand Down
63 changes: 63 additions & 0 deletions alembic/versions/8fbc59f03986_convert_device_to_wvd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""convert_device_to_wvd
Revision ID: 8fbc59f03986
Revises: 684292138a0a
Create Date: 2024-07-24 16:35:56.530161
"""

import base64
import logging
from typing import Sequence, Union

import sqlalchemy as sa
from pywidevine.device import Device, DeviceTypes
from sqlalchemy.dialects import mysql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "8fbc59f03986"
down_revision: Union[str, None] = "684292138a0a"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = "684292138a0a"

logger = logging.getLogger("alembic.runtime.migration")


def upgrade() -> None:
# create the new wvd column and make it nullable temporarily
op.add_column("devices", sa.Column("wvd", sa.Text(), nullable=True))

# DROP all devices that have an empty private key or client id
op.execute("DELETE FROM devices WHERE device_private_key = '' OR client_id_blob_filename = ''")

# run through all devices and convert them to a WVD
devices = (
op.get_bind().execute(sa.text("SELECT code,client_id_blob_filename,device_private_key FROM devices")).fetchall()
)
for code, client_id, private_key in devices:
logger.info(f"Converting device {code} to WVD")
wvd = Device(
type_=DeviceTypes.ANDROID,
security_level=3,
flags=None,
private_key=base64.b64decode(private_key),
client_id=base64.b64decode(client_id),
)

wvd_b64 = base64.b64encode(wvd.dumps()).decode()
op.get_bind().execute(
sa.text("UPDATE devices SET wvd = :wvd WHERE code = :code"), {"wvd": wvd_b64, "code": code}
)

# make the wvd column non-nullable
op.alter_column("devices", "wvd", existing_type=sa.Text(), nullable=False)

# remove the old columns
op.drop_column("devices", "device_private_key")
op.drop_column("devices", "client_id_blob_filename")


def downgrade() -> None:
raise NotImplementedError("Downgrade is not supported for this migration.")
12 changes: 6 additions & 6 deletions getwvkeys/download/getwvkeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(
auth: str,
verbose: bool = False,
force: bool = False,
deviceCode: str = "",
device_code: str = "",
_headers: dict[str, str] = headers,
**kwargs,
) -> None:
Expand All @@ -64,7 +64,7 @@ def __init__(
self.auth = auth
self.verbose = verbose
self.force = force
self.deviceCode = deviceCode
self.device_code = device_code

self.baseurl = "https://getwvkeys.cc" if API_URL == "__getwvkeys_api_url__" else API_URL
self.api_url = self.baseurl + "/pywidevine"
Expand All @@ -73,7 +73,7 @@ def __init__(
def generate_request(self):
if self.verbose:
print("[+] Generating License Request ")
data = {"pssh": self.pssh, "deviceCode": self.deviceCode, "force": self.force, "license_url": self.url}
data = {"pssh": self.pssh, "device_code": self.device_code, "force": self.force, "license_url": self.url}
header = {"X-API-Key": self.auth, "Content-Type": "application/json"}
r = requests.post(self.api_url, json=data, headers=header)
if not r.ok:
Expand Down Expand Up @@ -108,7 +108,7 @@ def decrypter(self, license_response):
"response": license_response,
"license_url": self.url,
"headers": self.headers,
"deviceCode": self.deviceCode,
"device_code": self.device_code,
"force": self.force,
"session_id": self.session_id,
}
Expand Down Expand Up @@ -187,7 +187,7 @@ def main(self):
default=False,
action="store_true",
)
parser.add_argument("--deviceCode", "-d", default="", help="Use custom device", required=False)
parser.add_argument("--device_code", "-d", default="", help="Use custom device", required=False)
parser.add_argument("--version", "-V", help="Print version and exit", action="store_true")

args = parser.parse_args()
Expand All @@ -211,7 +211,7 @@ def main(self):
if len(sys.argv) == 1:
parser.print_help()
print()
args.deviceCode = ""
args.device_code = ""
args.verbose = False

try:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit d50cc66

Please sign in to comment.