Skip to content

Commit

Permalink
feat(lib): add connect function to use remote azot API instance
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalygashkov committed Dec 12, 2024
1 parent 74c7953 commit 0765f89
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azot",
"version": "0.7.2",
"version": "0.8.0",
"description": "Swiss Army knife for pentesting DRMs",
"type": "module",
"files": [
Expand Down
54 changes: 54 additions & 0 deletions src/lib/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
type ConnectParams = {
baseUrl: string;
secret: string;
};

export const connect = ({ baseUrl, secret }: ConnectParams) => {
const headers = {
'x-secret-key': secret,
'content-type': 'application/json',
};

const json = (data: any) => JSON.stringify(data);

const post = async (route: string, body: object) => {
const response = await fetch(`${baseUrl}${route}`, {
method: 'POST',
headers,
body: json(body),
});
const contentLength = response.headers.get('content-length');
if (contentLength === '0') return;
return response.json();
};

const get = async (route: string) => {
const response = await fetch(`${baseUrl}${route}`, {
method: 'GET',
headers,
});
return response.json();
};

const createSession = async (client: string) => {
const data = await post(`/session`, { client });
const sessionId = data.id;

const generateRequest = async (initData: string, initDataType: string) => {
const data = await post(`/session/${sessionId}/generate-request`, {
initDataType,
initData,
});
return data.licenseRequest;
};

const update = async (response: string) =>
post(`/session/${sessionId}/update`, { response });

const keys = async () => get(`/session/${sessionId}/keys`);

return { sessionId, generateRequest, update, keys };
};

return { createSession };
};
4 changes: 3 additions & 1 deletion src/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const fetchDecryptionKeys = async (params: FetchDecryptionKeysParams) => {
'message',
async (event: Event) => {
const type = (event as MediaKeyMessageEvent).messageType;
const message = (event as MediaKeyMessageEvent).message as Uint8Array;
const message = (event as MediaKeyMessageEvent)
.message as unknown as Uint8Array;
const isIndividualization = type === 'individualization-request';
const url = isIndividualization
? params.individualizationServer || params.server
Expand Down Expand Up @@ -67,6 +68,7 @@ const fetchDecryptionKeys = async (params: FetchDecryptionKeysParams) => {

export { fetchDecryptionKeys };
export { Session };
export * from './connect';
export * from './utils';
export * from './client';
export * from './key';
26 changes: 26 additions & 0 deletions test/connect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect, test } from 'vitest';
import { connect } from '../src/lib';

test('connection to Azot API instance and generating license request', async () => {
const baseUrl = 'https://azot.pw'; // Set your API base URL here
const secret = 'db44ec40-3e02-47bd-8fc6-373935e30eae'; // Set your API secret here
const client = 'pixel6'; // Set client name related with your API key
if (secret === 'db44ec40-3e02-47bd-8fc6-373935e30eae')
return console.warn('Add your API endpoint & secret to test connection');
const { createSession } = connect({ baseUrl, secret });
const session = await createSession(client);
const licenseRequest = await session.generateRequest(
'AAAAW3Bzc2gAAAAA7e+LqXnWSs6jyCfc1R0h7QAAADsIARIQ62dqu8s0Xpa7z2FmMPGj2hoNd2lkZXZpbmVfdGVzdCIQZmtqM2xqYVNkZmFsa3IzaioCSEQyAA==',
'cenc',
);
const licenseUrl = 'https://cwip-shaka-proxy.appspot.com/no_auth';
const license = await fetch(licenseUrl, {
body: Buffer.from(licenseRequest, 'base64'),
method: 'POST',
})
.then((response) => response.arrayBuffer())
.then((buffer) => Buffer.from(buffer));
await session.update(license.toString('base64'));
const keys = await session.keys();
expect(keys.length).toBe(5);
});

0 comments on commit 0765f89

Please sign in to comment.