forked from liamcottle/rustplus.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpair.js
More file actions
113 lines (86 loc) · 3.58 KB
/
pair.js
File metadata and controls
113 lines (86 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const axios = require('axios');
const express = require('express');
const { v4: uuidv4 } = require('uuid');
const { register, listen } = require('push-receiver');
const app = express();
const port = 3000;
const server = app.listen(port);
var expoPushToken = null;
var steamAuthToken = null;
async function run() {
console.log("Registering with FCM");
const credentials = await register('976529667804');
console.log("Fetching Expo Push Token");
axios.post('https://exp.host/--/api/v2/push/getExpoPushToken', {
deviceId: uuidv4(),
experienceId: '@facepunch/RustCompanion',
appId: 'com.facepunch.rust.companion',
deviceToken: credentials.fcm.token,
type: 'fcm',
development: false,
}).then(async (response) => {
expoPushToken = response.data.data.expoPushToken;
console.log("Received Expo Push Token: " + expoPushToken);
// register callback
app.get('/callback', (req, res) => {
steamAuthToken = req.query.token;
if(steamAuthToken){
console.log("Steam Account Connected.");
res.send('Steam Account successfully linked with rustplus-api. You can now close this window and go back to the console.');
// register with Rust Companion API
console.log("Registering with Rust Companion API");
axios.post('https://companion-rust.facepunch.com:443/api/push/register', {
AuthToken: steamAuthToken,
DeviceId: 'rustplus-api',
PushKind: 0,
PushToken: expoPushToken,
}).then((response) => {
console.log("Successfully registered with Rust Companion API.");
console.log("When you Pair with Servers or Smart Devices in game, notifications will appear here.");
}).catch((error) => {
console.log("Failed to register with Rust Companion API");
console.log(error);
});
} else {
res.send('token missing from request!');
}
});
// ask user to login with steam
console.log("Please open the following URL in your browser to link your Steam Account with rustplus-api.");
console.log("https://companion-rust.facepunch.com/login?returnUrl=" + encodeURIComponent(`http://localhost:${port}/callback`));
console.log("Listening for FCM Notifications");
await listen(credentials, ({ notification, persistentId }) => {
// parse notification body
const body = JSON.parse(notification.data.body);
// log notification body
console.log(body);
});
}).catch((error) => {
console.log("Failed to fetch Expo Push Token");
console.log(error);
});
}
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
async function shutdown() {
// unregister with Rust Companion API
if(steamAuthToken){
console.log("Unregistering from Rust Companion API");
await axios.delete('https://companion-rust.facepunch.com:443/api/push/unregister', {
data: {
AuthToken: steamAuthToken,
PushToken: expoPushToken,
DeviceId: 'rustplus-api',
},
}).then((response) => {
console.log("Successfully unregistered from Rust Companion API");
}).catch((error) => {
console.log(error);
});
}
// stop http server
server.close(() => {
process.exit(0);
});
}
run();