Skip to content

Commit

Permalink
Re-fixed issues #13, #15, #16 re-writing whole signing portion of code.
Browse files Browse the repository at this point in the history
  • Loading branch information
dehsgr committed Sep 22, 2021
1 parent 7807a23 commit df849d1
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 59 deletions.
70 changes: 53 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# node-red-contrib-meross

This provides nodes for locally controlling e.g. Meross Smart Plug (without cloud!). It's requiring a Merross token and a corresponding message id and timestamp to be provided by you. You should get this data via network sniffer or traffic interception between your Meross app and your Smart Plug. node-red-contrib-meross delivers 2 nodes:
This provides nodes for locally controlling e.g. Meross Smart Plug (without cloud!). It's requiring your Merross key to be provided by you. You should get this data via network sniffer or traffic interception between your Meross app and your Smart Plug or via user info fetch tool (see below). node-red-contrib-meross delivers 2 nodes:

## Meross Config
Meross Config is a config node to set the token, message id and timestamp for being able to locally communicate with your devices.
Expand Down Expand Up @@ -42,12 +41,49 @@ Meross Garage Door can be used to set garage doors state and/or poll its current
</tr>
</table>

# Getting your Meross key
To be able to use these nodes you must provide your Meross key to your node config.

## How to sniff the key
1. You might download Fiddler.
2. Configure it to capture HTTPS traffic.
3. Transfer Fiddlers HTTPS certificate to your phone and trust it there.
4. Configure your phone to use Fiddler as proxy according to your configuration.
5. Capture traffic while logging in with Meross app into your account.

Now you should see a request in Fiddler like "https://[canvary].meross.com/v1/Auth/signin" with type "POST".

6. Select that request and switch over to response body tab.
7. Change format for the response body to "JSON".

This should look like:
<pre>
{
"apiStatus": 0,
"sysStatus": 0,
"data": {
"token": "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
"key": "abcdef01234567890abcdef01234567890",
"userid": "123456",
"email": "yourmail@domain.tld",
"domain": "https://[canvary].meross.com",
"mqttDomain": "mqtt-[canvary].meross.com"
},
"info": "Success",
"timeStamp": 1632308678
}
</pre>

The key is the value from „key“.

## Howto fetch the key via fetch tool (requires Node.js >= v14)
- Take a look at https://github.com/jixunmoe/meross-login :-)

# Requesting additional devices
To be able to implement additional devices feel free to provide sniffed communication between your app and devices.
You might also feel free to directly contribute in this project.

# Additional Information
## How to sniff required data
## How to sniff required data for additional devices
1. You might download Fiddler.
2. Configure it to capture HTTPS traffic.
3. Transfer Fiddlers HTTPS certificate to your phone and trust it there.
Expand All @@ -62,21 +98,21 @@ Now you should see a request in Fiddle like "http://yourip/config" with type "PO
This should look like:
<pre>
{
"payload": {},
"header": {
"messageId": "1234567890abcdef1234567890abcdef",
"method": "GET",
"from": "http://yourip/config",
"sign": "567890abcdef1234567890abcdef12345678",
"namespace": "Appliance.System.Ability",
"triggerSrc": "iOSLocal",
"timestamp": 1609232320,
"payloadVersion": 1
}
"payload": {},
"header": {
"messageId": "1234567890abcdef1234567890abcdef",
"method": "GET",
"from": "http://yourip/config",
"sign": "567890abcdef1234567890abcdef12345678",
"namespace": "Appliance.System.Ability",
"triggerSrc": "iOSLocal",
"timestamp": 1609232320,
"payloadVersion": 1
}
}
</pre>

The token is the value from „sign“. The corresponding timestamp is the value from „timestamp“. The message id is the value from „messageid“.

## Sample Flow
<code>[{"id":"88f87bae.10d938","type":"inject","z":"ec2c2ba0.1671a8","name":"Get State","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":160,"wires":[["f7696f8c.ae16c"]]},{"id":"f7696f8c.ae16c","type":"smartplug-control","z":"ec2c2ba0.1671a8","confignode":"d9dc372f.d15f08","name":"Smart Plug","ip":"10.10.10.10","x":370,"y":120,"wires":[["759b6fbe.d2d68"]]},{"id":"5b5e94ca.a0c614","type":"inject","z":"ec2c2ba0.1671a8","name":"Turn On","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":180,"y":80,"wires":[["f7696f8c.ae16c"]]},{"id":"60c5c941.c24568","type":"inject","z":"ec2c2ba0.1671a8","name":"Turn Off","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":180,"y":120,"wires":[["f7696f8c.ae16c"]]},{"id":"759b6fbe.d2d68","type":"debug","z":"ec2c2ba0.1671a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":120,"wires":[]},{"id":"d9dc372f.d15f08","type":"meross-config","name":"Meross Config","timestamp":"1234567890","token":"1234567890abcdef1234567890abcdef","messageid":"abcdef0123456789abcdef0123456789"}]</code>
# Sample Flow
<code>[{"id":"88f87bae.10d938","type":"inject","z":"ec2c2ba0.1671a8","name":"Get State","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":160,"wires":[["f7696f8c.ae16c"]]},{"id":"f7696f8c.ae16c","type":"smartplug-control","z":"ec2c2ba0.1671a8","confignode":"d9dc372f.d15f08","name":"Smart Plug","ip":"10.10.10.10","x":370,"y":120,"wires":[["759b6fbe.d2d68"]]},{"id":"5b5e94ca.a0c614","type":"inject","z":"ec2c2ba0.1671a8","name":"Turn On","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":180,"y":80,"wires":[["f7696f8c.ae16c"]]},{"id":"60c5c941.c24568","type":"inject","z":"ec2c2ba0.1671a8","name":"Turn Off","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":180,"y":120,"wires":[["f7696f8c.ae16c"]]},{"id":"759b6fbe.d2d68","type":"debug","z":"ec2c2ba0.1671a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":120,"wires":[]},{"id":"d9dc372f.d15f08","type":"meross-config","name":"Meross Config","key":"abcdef01234567890abcdef01234567890"}]</code>
16 changes: 3 additions & 13 deletions meross/meross-config.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
category: 'config',
defaults: {
name: { value: '' },
timestamp: { value: '', required: true, validate: RED.validators.number() },
token: { value: '', required: true },
messageid: { value: '', required: true }
key: { value: '', required: true }
},
label: function() {
return this.name || 'Meross';
Expand All @@ -19,15 +17,7 @@
<input type="text" id="node-config-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-config-input-timestamp"><i class="fa fa-tag"></i> Timestamp</label>
<input type="text" id="node-config-input-timestamp">
</div>
<div class="form-row">
<label for="node-config-input-userid"><i class="fa fa-tag"></i> Token</label>
<input type="text" id="node-config-input-token">
</div>
<div class="form-row">
<label for="node-config-input-messageid"><i class="fa fa-tag"></i> Message ID</label>
<input type="text" id="node-config-input-messageid">
<label for="node-config-input-key"><i class="fa fa-tag"></i> Key</label>
<input type="text" id="node-config-input-key">
</div>
</script>
8 changes: 2 additions & 6 deletions meross/meross-config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
module.exports = function(RED) {
function MerossNode(MyNode) {
RED.nodes.createNode(this, MyNode);
this.messageid = MyNode.messageid;
this.token = MyNode.token;
this.timestamp = MyNode.timestamp;
this.key = MyNode.key;
}

RED.nodes.registerType("meross-config", MerossNode, {
messageid: { type: "text" },
token: { type: "text" },
timestamp: { type: "text" }
key: { type: "password" }
});
};
44 changes: 28 additions & 16 deletions meross/smartgarage-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = function(RED) {

function SmartGarageNode(myNode) {
RED.nodes.createNode(this, myNode);
var Crypto = require('crypto');
var Platform = this;

this.config = RED.nodes.getNode(myNode.confignode);
Expand All @@ -16,34 +17,54 @@ module.exports = function(RED) {
url: 'http://' + Platform.ip + '/config',
headers: {
'Content-Type': 'application/json',
'Content-Length': 0,
'Content-Length': undefined,
},
body: JSON.stringify({
body: {
'header': {
'messageId': Platform.config.messageid,
'messageId': undefined,
'method': (typeof msg.payload === 'boolean') ?
'SET' :
'GET',
'from': 'http://' + Platform.ip + '/config',
'sign': Platform.config.token,
'sign': undefined,
'namespace': (typeof msg.payload === 'boolean') ?
'Appliance.GarageDoor.State' :
'Appliance.System.All',
'timestamp': parseInt(Platform.config.timestamp),
'timestamp': undefined,
'payloadVersion': 1
},
'payload': (typeof msg.payload === 'boolean') ?
{
'state': {
'open': msg.payload ? 1 : 0,
'channel': Platform.channel,
'uuid': makeUUID(16)
'uuid': undefined
}
} :
{}
}),
},
init: function() {
this.body.header.timestamp = Math.floor(Date.now()/1000);
this.body.header.messageId = Crypto.createHash('md5').update(
this.body.header.timestamp.toString()
).digest('hex');
this.body.header.sign = Crypto.createHash('md5').update(
this.body.header.messageId +
Platform.config.key +
this.body.header.timestamp
).digest('hex');

if (this.body.payload !== undefined &&
this.body.payload.state !== undefined)
{
this.body.payload.state.uuid = Crypto.createHash('md5').update(
this.body.header.from
).digest('hex');
}

this.body = JSON.stringify(this.body);
this.headers["Content-Length"] = this.body.length;

delete this.init;

return this;
Expand All @@ -67,14 +88,5 @@ module.exports = function(RED) {
});
}

function makeUUID(size) {
let uuid = '';
const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
while (uuid.length < size) {
uuid += chars.charAt(Math.floor(Math.random() * chars.length));
}
return uuid;
}

RED.nodes.registerType('smartgarage-control', SmartGarageNode);
};
25 changes: 19 additions & 6 deletions meross/smartplug-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = function(RED) {

function SmartPlugNode(myNode) {
RED.nodes.createNode(this, myNode);
var Crypto = require('crypto');
var Platform = this;

this.config = RED.nodes.getNode(myNode.confignode);
Expand All @@ -15,22 +16,22 @@ module.exports = function(RED) {
url: 'http://' + Platform.ip + '/config',
headers: {
'Content-Type': 'application/json',
'Content-Length': 0,
'Content-Length': undefined,
},
body: JSON.stringify({
body: {
'header': {
'messageId': Platform.config.messageid,
'messageId': undefined,
'method': (typeof msg.payload === 'boolean') ?
'SET' :
'GET',
'from': 'http://' + Platform.ip + '/config',
'sign': Platform.config.token,
'sign': undefined,
'namespace': (typeof msg.payload === 'boolean') ?
'Appliance.Control.ToggleX' :
(msg.payload.namespace !== undefined) ?
msg.payload.namespace :
'Appliance.System.All',
'timestamp': parseInt(Platform.config.timestamp),
'timestamp': undefined,
'payloadVersion': 1
},
'payload': (typeof msg.payload === 'boolean') ?
Expand All @@ -41,9 +42,21 @@ module.exports = function(RED) {
}
} :
{}
}),
},
init: function() {
this.body.header.timestamp = Math.floor(Date.now()/1000);
this.body.header.messageId = Crypto.createHash('md5').update(
this.body.header.timestamp.toString()
).digest('hex');
this.body.header.sign = Crypto.createHash('md5').update(
this.body.header.messageId +
Platform.config.key +
this.body.header.timestamp
).digest('hex');

this.body = JSON.stringify(this.body);
this.headers["Content-Length"] = this.body.length;

delete this.init;

return this;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-meross",
"version": "1.5.0",
"version": "2.0.0",
"description": "This provides nodes for controlling Meross devices.",
"author": "d3h56r",
"license": "ISC",
Expand Down

0 comments on commit df849d1

Please sign in to comment.