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

Add support for message decryption and a set of get commands #62

Merged
merged 1 commit into from
Nov 27, 2021
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
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ since 2018. It utilizes encryption rules based on a guide found on the internet.
This is not provided by LG, and it is not a complete implementation for every TV
model.

_Note: I wasn't able to implement deciphering of the messages sent back from the
TV, so all commands are "send only"._

**Requirements**

- LG TV (tested on model OLED65B9PUA)
Expand Down Expand Up @@ -110,6 +107,46 @@ Disconnects from the TV. Returns a promise.
await lgtv.disconnect();
```

### `.getCurrentApp()`

Gets the current app. Returns a promise.

```ts
const currentApp = await lgtv.getCurrentApp();
```

### `.getCurrentVolume()`

Gets the current volume. Returns a promise.

```ts
const currentVolume = await lgtv.getCurrentVolume();
```

### `.getIpControlState()`

Gets the ip control state. Returns a promise.

```ts
const ipControlState = await lgtv.getIpControlState();
```

### `.getMacAddress(type: 'wired' | 'wifi')`

Gets the mac address by type. Returns a promise.

```ts
const macAddress = await lgtv.getMacAddress('wired');
```

### `.getMuteState()`

Gets the mute state. Returns a promise.

```ts
const muteState = await lgtv.getMuteState();
```

### `.powerOff()`

Powers the TV off. Returns a promise.
Expand Down
20 changes: 19 additions & 1 deletion src/classes/LGEncryption.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert';
import { createCipheriv, pbkdf2Sync } from 'crypto';
import { createCipheriv, createDecipheriv, pbkdf2Sync } from 'crypto';
import { DefaultSettings } from '../constants/DefaultSettings';

export interface EncryptionSettings {
Expand Down Expand Up @@ -137,4 +137,22 @@ export class LGEncryption {

return Buffer.concat([ivEnc, dataEnc]);
}

decrypt(cipher: Buffer) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests for this function to test/LGEncryption.test.ts with sample payloads?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

const ecbDecypher = createDecipheriv(
'aes-128-ecb',
this.derivedKey,
Buffer.alloc(0)
);
ecbDecypher.setAutoPadding(false);
const iv = ecbDecypher.update(
cipher.slice(0, this.settings.encryptionKeyLength)
);

const cbcDecypher = createDecipheriv('aes-128-cbc', this.derivedKey, iv);
cbcDecypher.setAutoPadding(false);
return cbcDecypher
.update(cipher.slice(this.settings.encryptionKeyLength))
.toString();
}
}
35 changes: 28 additions & 7 deletions src/classes/LGTV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export class LGTV {

private async sendCommand(command: string) {
const encryptedData = this.encryption.encrypt(command);
await this.socket.sendReceive(encryptedData);
const encryptedResponse = await this.socket.sendReceive(encryptedData);
return this.encryption.decrypt(encryptedResponse);
}

async connect() {
Expand All @@ -33,8 +34,28 @@ export class LGTV {
await this.socket.disconnect();
}

async getCurrentApp() {
return await this.sendCommand('CURRENT_APP');
}

async getCurrentVolume() {
return await this.sendCommand('CURRENT_VOL');
}

async getIpControlState() {
return await this.sendCommand('GET_IPCONTROL_STATE');
}

async getMacAddress(type: 'wired' | 'wifi') {
return await this.sendCommand(`GET_MACADDRESS ${type}`);
}

async getMuteState() {
return await this.sendCommand('MUTE_STATE');
}

async powerOff() {
await this.sendCommand(`POWER off`);
return await this.sendCommand(`POWER off`);
}

powerOn() {
Expand All @@ -46,7 +67,7 @@ export class LGTV {
Object.values(Keys).some(availableKey => availableKey === key),
'key must be valid'
);
await this.sendCommand(`KEY_ACTION ${key}`);
return await this.sendCommand(`KEY_ACTION ${key}`);
}

async setEnergySaving(level: EnergySavingLevels) {
Expand All @@ -56,15 +77,15 @@ export class LGTV {
),
'level must be valid'
);
await this.sendCommand(`ENERGY_SAVING ${level}`);
return await this.sendCommand(`ENERGY_SAVING ${level}`);
}

async setInput(input: Inputs) {
assert(
Object.values(Inputs).some(availableInput => availableInput === input),
'input must be valid'
);
await this.sendCommand(`INPUT_SELECT ${input}`);
return await this.sendCommand(`INPUT_SELECT ${input}`);
}

async setVolume(volumeLevel: number) {
Expand All @@ -75,11 +96,11 @@ export class LGTV {
volumeLevel <= 100,
'volumeLevel must be an integer between 0 and 100'
);
await this.sendCommand(`VOLUME_CONTROL ${volumeLevel}`);
return await this.sendCommand(`VOLUME_CONTROL ${volumeLevel}`);
}

async setVolumeMute(isMuted: boolean) {
assert(typeof isMuted === 'boolean', 'isMuted must be a boolean');
await this.sendCommand(`VOLUME_MUTE ${isMuted ? 'on' : 'off'}`);
return await this.sendCommand(`VOLUME_MUTE ${isMuted ? 'on' : 'off'}`);
}
}
18 changes: 18 additions & 0 deletions test/LGEncryption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,21 @@ describe('encrypt', () => {
mocked(Math.random).mockRestore();
});
});

describe('decrypt', () => {
it('works with data from the LG document', () => {
const exampleKeyCode = '12345678';
const encryptedIv = 'd2b21ca0ad6486cb2056a8b815033508';
const encryptedData = 'dfe77a7de05603a59ed5316ec552fac1';
const exampleCipherText = Buffer.from(
`${encryptedIv}${encryptedData}`,
'hex'
);
const expectedPlainText = 'VOLUME_MUTE on\x0d\x01';

const encryption = new LGEncryption(exampleKeyCode);
const decryptedData = encryption.decrypt(exampleCipherText);

expect(decryptedData).toEqual(expectedPlainText);
});
});