liamcottle / rustplus.js

Unofficial NodeJS library for controlling Smart Switches in the PC game Rust
224 stars 46 forks source link

Listening to smart alarm notifications #17

Closed raine closed 3 years ago

raine commented 3 years ago

Hello, and thank you for the great work on reverse engineering rust+ app!

Is there any information available how to listen for smart alarm notifications?

From what I understand based on the discussion in https://github.com/liamcottle/rustplus.js/issues/13, those smart alarm notifications you see in iOS, for instance, would be available as FCM notifications.

However when I trigger a smart alarm in game, I only appear to get this generic server info message in the listen callback of pair.js. There's nothing about a smart alarm there, however, it does come at the same time when alarm is triggered so I wonder...

{
  img: 'https://files.facepunch.com/Alistair/02/05/0T35W1/server-header.png',
  port: '28083',
  ip: '51.77.57.19',
  name: '[RU] Facepunch 4',
  logo: 'https://files.facepunch.com/Alistair/02/05/1Z61F1/04_07-48-MagnificentLadybug.png',
  id: 'cdfeccce-7c2f-4d02-8a99-b94a183f3ada',
  url: 'http://www.playrust.com/',
  desc: "This is an official server owned and operated by Facepunch. \\n \\n People are free to speak whatever language they like. Don't be surprised if you get banned for being abusive."
}
liamcottle commented 3 years ago

Hi @raine, cheers for the feedback!

The smart alarm notifications that you are referring to, I am assuming are just the generic FCM Push Notifications, such as "Your base is under attack!", or whatever message you have set in the Smart Alarm settings, which are shown on your phone as a normal notification.

These notifications contain this payload:

{
  data: {
    experienceId: '@facepunch/RustCompanion',
    title: 'Alarm',
    message: 'Your base is under attack!',
    body: `{"img":"","port":"[redacted]","ip":"[redacted]","name":"[redacted]","logo":"","id":"[redacted]","url":"","desc":""}`,
    channelId: 'alarm'
  },
  from: '976529667804',
  priority: 'high',
  collapse_key: 'do_not_collapse'
}

However, in the case of the pair.js tool I only show the data in the body of the notification data, as that's all that's relevant for pairing with servers or entities. Which is why you can't see the title and message in the console logs.

The best approach in my opinion for listening to Smart Alarm notifications, is to pair with the server and smart alarm using pair.js, and then use the rustplus.js library to send a getInfo request for the smart alarm entity id. This way, you will receive a broadcast event from the rust+ websocket everytime the smart alarm state is changed.

You could of course just modify the pair.js tool to check the title and message of FCM notifications, however you will notice that you won't receive a notification every time the smart alarm is triggered. This is just a basic rate limit on the server side to prevent spamming notifications to your phone if it's triggered often.

Here's a very rough untested psuedo code for how you could do it:

const RustPlus = require('@liamcottle/rustplus.js');
var rustplus = new RustPlus('hostname', 'port', 'playerId', 'playerToken');

// wait until connected before sending commands
rustplus.on('connected', () => {

  var smartAlarmEntityId = 1234567; // get this from fcm notification

  // get entity info of smart alarm, required to have broadcasts sent out for it
  rustplus.getEntityInfo(smartAlarmEntityId, (message) => {
    console.log("getEntityInfo response message: " + JSON.stringify(message));
    return true;
  });

});

// listen for broadcasts when smart alarm state changes
rustplus.on('message', (message) => {
  if(message.broadcast && message.broadcast.entityChanged){

    var entityChanged = message.broadcast.entityChanged;

    var entityId = entityChanged.entityId;
    var value = entityChanged.payload.value;

    console.log("entity " + entityId + " is now " + (value ? "active" : "inactive"));

  }
});

Hope this helps!

raine commented 3 years ago

Ah, I did in fact check if the message event would trigger when smart alarm triggered, but didn't see anything. Somehow missed the part you just said about getEntityInfo subscribing to changes in the README. RTFM better next time I guess! Thanks and sorry!

raine commented 3 years ago

After looking into this more carefully, at least for now I ended up using the FCM notifications for listening to smart alarms. Mainly because there is no way to get the smart alarm's notification content that you set in game (title and message) on the rustplus API side. Without those, it's not very useful unless you add manual pairing process to consumer side where on pairing you define the alarm text manually and store it on your side.

Screenshot 2021-03-16 at 11 45 36