/
cloudapi.ts
157 lines (146 loc) · 5.68 KB
/
cloudapi.ts
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { v4 as uuidv4 } from 'uuid';
import WebSocket from 'ws';
import fs from 'node:fs';
import { log } from "../../logging";
import { config, localSocket } from '../../index';
import { spawn } from 'node:child_process';
export interface APIOrganization {
id: number;
uuid: string;
name: string;
tier: string;
}
export interface DeviceExecPayload {
deviceUUID: string;
fromDevice: boolean;
fromUUID: string;
idWrapper?: string;
command: string
data?: string
exitCode?: string
}
async function spawnChild(command: string): Promise<{data: string, exitCode: number}> {
const child = spawn('/bin/sh', ["-c", command]);
let data = "";
for await (const chunk of child.stdout) {
console.log('stdout chunk: '+chunk);
data += chunk;
}
for await (const chunk of child.stderr) {
console.error('stderr chunk: '+chunk);
data += chunk;
}
const exitCode: number = await new Promise( (resolve, reject) => {
child.on('close', resolve);
});
return {data, exitCode};
}
export class OCS2Connection {
ready: boolean = false;
connected: boolean = false;
ws?: WebSocket;
uuid?: string;
constructor() {
this.startCloudConnection();
}
startCloudConnection() {
this.ws = new WebSocket(config.ocs2.gateway_url);
this.ws.on('open', () => {
log.info("Connected to the Sineware Cloud Gateway!");
this.connected = true;
});
this.ws.on('message', async (data) => {
try {
let msg = JSON.parse(data.toString());
if(msg.payload?.forAction !== "ping" && msg.action !== "device-stream-terminal") {
console.log(msg);
}
if(msg.action === "hello" && msg.payload.status) {
let machineID = fs.readFileSync("/etc/machine-id", "utf-8").trim();
let hostname = fs.readFileSync("/etc/hostname", "utf-8").trim();
this.uuid = machineID;
let org: APIOrganization = await this.callWS("device-hello", {
clientType: "prolinux,plasma-mobile-nightly",
accessToken: config.ocs2.access_token,
uuid: machineID,
name: hostname
});
log.info(JSON.stringify(org));
setInterval(() => {
this.callWS("ping", {text: ""}, false);
}, 10000);
this.ready = true;
} else if(msg.action === "device-stream-terminal") {
if(!msg.payload.fromDevice) {
localSocket.clients.forEach((client: any) => {
//console.log("Sending message to client: " + msg.payload.text)
client.send(JSON.stringify({
action: "device-stream-terminal",
payload: {
data: msg.payload.text
}
}));
});
}
} else if(msg.action === "device-exec") {
let payload = msg.payload as DeviceExecPayload;
if(!payload.fromDevice) {
let {data, exitCode} = await spawnChild(payload.command);
this.callWS("device-exec", {
deviceUUID: payload.deviceUUID,
fromDevice: true,
fromUUID: payload.fromUUID,
idWrapper: payload.idWrapper,
command: payload.command,
data,
exitCode: exitCode.toString()
}, false);
}
}
} catch(e) {
log.error("Error while parsing message from Sineware Cloud Gateway: " + e);
}
});
this.ws.on('error', (err) => {
log.info("Error while connecting to Sineware Cloud Gateway: " + err.message);
console.error(err);
// try to reconnect
console.log("Reconnecting... " + err.message);
this.startCloudConnection();
});
this.ws.on('close', (code, reason) => {
console.error("Connection closed: " + code + " " + reason);
// try to reconnect
console.log("Reconnecting..." + reason);
this.startCloudConnection();
});
};
callWS(action: string, payload: any, promise: boolean = true): Promise<any> {
if(!this.connected) {
return new Promise((resolve, reject) => {
resolve({})
});
}
return new Promise ((resolve, reject) => {
let id = uuidv4();
let msg = { action, payload, id };
//console.log("[Call] Sending message: " + JSON.stringify(msg));
if(promise) {
const listener = (e: any) => {
let msg = JSON.parse(e.data);
if (msg.id === id) {
if(msg.payload.status) {
resolve(msg.payload.data);
} else {
reject(new Error(msg.payload.data.msg));
}
this.ws?.removeEventListener("message", listener);
}
}
this.ws?.addEventListener("message", listener);
}
this.ws?.send(JSON.stringify(msg));
if(!promise) resolve({});
});
}
}