Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Seshpenguin committed Feb 13, 2023
1 parent 1d416c1 commit c3ec70b
Show file tree
Hide file tree
Showing 16 changed files with 251 additions and 93 deletions.
2 changes: 2 additions & 0 deletions APKBUILD
Expand Up @@ -31,4 +31,6 @@ package() {
cp distro-files/prolinuxd.initd "$pkgdir/etc/init.d/prolinuxd"
cp distro-files/prolinux.toml "$pkgdir/opt/prolinuxd/prolinux-default.toml"
cp distro-files/session-wrapper.desktop "$pkgdir/opt/prolinuxd/"
cp distro-files/app-icon.png "$pkgdir/opt/prolinuxd"
cp distro-files/prolinux-config.desktop "$pkgdir/usr/share/applications"
}
34 changes: 27 additions & 7 deletions app-src/prolinux-config.ts
Expand Up @@ -46,23 +46,43 @@ function closeProgressDialog(path: string): Promise<void> {
});
});
}

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
async function main() {
const path = await startProgressDialog('ProLinuxD', 'Connecting to ProLinuxD...');
const path = await startProgressDialog('ProLinuxD', 'ProLinux Config Tool is working...');
await sleep(1000);

let ws = new WebSocket("ws+unix:///tmp/prolinuxd.sock");

await updateProgressDialog(path, 50);
process.on('uncaughtException', async function (err) {
await closeProgressDialog(path);
await alertDialog('ProLinuxD', 'Failed to connect to ProLinuxD: ' + err.message);
console.error(err);
process.exit(1);
});
let ws = new WebSocket("ws+unix:///tmp/prolinuxd.sock");;

//updateProgressDialog(path, 50);
//await sleep(1000);

ws.on('open', async () => {
await closeProgressDialog(path);
console.log('connected');
let token = await textBoxDialog('ProLinuxD Device Access Token', 'Enter the organization device access token:');
console.log(token)
if(token.data.trim() === "") {
await alertDialog('ProLinuxD', 'No token entered');
await sleep(1000);
await closeProgressDialog(path);
process.exit(1);
}
ws.send(JSON.stringify({
action: "set-token",
payload: {
token
token: token.data.trim()
}
}));
console.log('sent token');
await alertDialog('ProLinuxD', 'Set token! Please restart your device.');
await sleep(1000);
await closeProgressDialog(path);
process.exit(1);
});

ws.on("error", async (err) => {
Expand Down
3 changes: 3 additions & 0 deletions cli-src/README.md
@@ -0,0 +1,3 @@
# ProLinux CLI
`plctl` (**P**ro**L**inux **C**on**t**ro**l**) is the command line frontend tool to interact with ProLinuxD, over the
local websocket (/tmp/prolinuxd.sock).
59 changes: 59 additions & 0 deletions cli-src/plctl.ts
@@ -0,0 +1,59 @@
import { WebSocket } from 'ws';
import { Command } from 'commander';
import { v4 as uuidv4 } from 'uuid';

async function connectWS() {
let ws = new WebSocket("ws+unix:///tmp/prolinuxd.sock");
return ws;
}
export function callWS(action: string, payload: any, promise: boolean = true): Promise<any> {
return new Promise (async (resolve, reject) => {
const ws = await connectWS();
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));
}
ws.removeEventListener("message", listener);
}
}
ws.addEventListener("message", listener);
}
ws.send(JSON.stringify(msg));
if(!promise) resolve({});
});
}

const program = new Command();

program
.name('pactl')
.description('ProLinuxD CLI Utility')
.version('0.0.1');

program.command('status')
.description('get the status of ProLinuxD')
.action((str, options) => {

});
program.command('set-device-token')
.description('set the Sineware Cloud device token')
.argument('<token>', 'device token')
.action((str, options) => {

});

program.command('restart')
.description('Restart ProLinuxD')
.action((str, options) => {

});

program.parse();
4 changes: 3 additions & 1 deletion deploy.sh
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
set -e
USER=user
HOST=192.168.2.185
HOST=172.16.42.1
DIR=/opt/prolinuxd/

#swift build -c release --static-swift-stdlib
Expand All @@ -11,6 +11,8 @@ rsync -avz --delete distro-files/session-wrapper.desktop ${USER}@${HOST}:${DIR}
rsync -avz --delete distro-files/prolinux.toml ${USER}@${HOST}:${DIR}
rsync -avz --delete distro-files/prolinuxd ${USER}@${HOST}:${DIR}
rsync -avz --delete distro-files/aarch64-alpine-pty.node ${USER}@${HOST}:${DIR}/session-wrapper/build/Release/pty.node
rsync -avz --delete distro-files/prolinux-config.desktop ${USER}@${HOST}:${DIR}
rsync -avz --delete distro-files/app-icon.png ${USER}@${HOST}:${DIR}


exit 0
Binary file added distro-files/app-icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions distro-files/prolinux-config.desktop
@@ -0,0 +1,7 @@
[Desktop Entry]
Type=Application
Name=ProLinux Config Tool
Exec=/usr/bin/node /opt/prolinuxd/prolinux-config/index.js
Icon=/opt/prolinuxd/app-icon.png
Terminal=false
X-KDE-FormFactor=desktop;tablet;handset;
1 change: 1 addition & 0 deletions distro-files/prolinux.toml
Expand Up @@ -6,4 +6,5 @@ modules = [

[ocs2]
gateway_url = "wss://update.sineware.ca/gateway"
client_type = "prolinux,plasma-mobile-nightly"
access_token = ""
1 change: 1 addition & 0 deletions distro-files/prolinuxd.initd
Expand Up @@ -13,4 +13,5 @@ error_log="/var/log/prolinuxd.err"

depend() {
want net
before tinydm
}
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"@ltd/j-toml": "^1.37.0",
"commander": "^10.0.0",
"is-reachable": "^5.2.1",
"node-pty": "^0.10.1",
"uuid": "^9.0.0",
Expand Down
116 changes: 65 additions & 51 deletions session-wrapper-src/wrapper.ts
Expand Up @@ -6,8 +6,9 @@ console.log("ProLinuxd Session Wrapper - launching: " + process.argv[2]);
// /var/lib/tinydm/default-session.desktop
// Exec=/usr/bin/node /opt/prolinuxd/session-wrapper/index.js /usr/lib/libexec/plasma-dbus-run-session-if-needed /usr/bin/startplasmamobile

const ws = new WebSocket("ws+unix:///tmp/prolinuxd.sock");
let ws: WebSocket;
let term: any;
let child: ChildProcessWithoutNullStreams; // Plasma Mobile

function log(msg: string, type: string) {
ws.send(JSON.stringify(
Expand All @@ -22,12 +23,8 @@ function log(msg: string, type: string) {
));
console.log(`[prolinuxd-session-wrapper] ${msg}`);
}

ws.on('open', function open() {
console.log('Connected to ProLinuxD!');

// Eventually ProLinuxD will tell us to start/stop the session
const child = spawn("/usr/bin/startplasmamobile", []);
function startPlasmaMobile() {
child = spawn("/usr/bin/startplasmamobile", []);

child.stdout.on("data", (data) => {
log(data.toString(), "stdout");
Expand All @@ -43,51 +40,68 @@ ws.on('open', function open() {
log(`process exited with code ${code}`, "stderr")
console.log(`[prolinuxd-session-wrapper] process exited with code ${code}`);
process.exit(code || 123);
/*setTimeout(() => {
startPlasmaMobile();
}, 2500);*/
});

// Open a bash for device-stream-terminal
term = pty.spawn("/bin/bash", [], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: process.env.HOME,
//@ts-ignore
env: process.env
});
term.onData((data: any) => {
//log(`Sending back term`, "info")
ws.send(JSON.stringify(
{
"action": "device-stream-terminal",
"payload": {
data: Buffer.from(data).toString('base64')
}
}
function connectWS(): Promise<void> {
return new Promise((resolve, reject) => {
ws = new WebSocket("ws+unix:///tmp/prolinuxd.sock");
ws.on('open', function open() {
console.log('Connected to ProLinuxD!');
resolve();
// Open a bash for device-stream-terminal
term = pty.spawn("/bin/bash", [], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: process.env.HOME,
//@ts-ignore
env: process.env
});
term.onData((data: any) => {
//log(`Sending back term`, "info")
ws.send(JSON.stringify(
{
"action": "device-stream-terminal",
"payload": {
data: Buffer.from(data).toString('base64')
}
}
));
});
});

ws.on('message', function message(data) {
console.log('received: %s', data);
let msg = JSON.parse(data.toString());
switch(msg.action) {
case "device-stream-terminal": {
//log(`Received device-stream-terminal: ${msg.payload.data}, ${typeof term}`, "info")
term?.write(Buffer.from(msg.payload.data, 'base64').toString('ascii'));
} break;
}
));
});

ws.on('close', function close() {
console.log('disconnected');
setTimeout(() => {
process.exit(-1);
}, 1000);
});
ws.on('error', function error(err) {
// if prolinuxd is not running, exit and openrc will restart us to try again
console.log('error: ', err);
setTimeout(() => {
process.exit(-1);
}, 1000);
});
});
});

ws.on('message', function message(data) {
console.log('received: %s', data);
let msg = JSON.parse(data.toString());
switch(msg.action) {
case "device-stream-terminal": {
//log(`Received device-stream-terminal: ${msg.payload.data}, ${typeof term}`, "info")
term?.write(Buffer.from(msg.payload.data, 'base64').toString('ascii'));
} break;
}
});
}

ws.on('close', function close() {
console.log('disconnected');
setTimeout(() => {
process.exit(1);
}, 2500);
});
ws.on('error', function error(err) {
// if prolinuxd is not running, exit and openrc will restart us to try again
console.log('error: ', err);
setTimeout(() => {
process.exit(1);
}, 2500);
});
async function main() {
await connectWS();
startPlasmaMobile();
}
main();
13 changes: 11 additions & 2 deletions src/index.ts
Expand Up @@ -13,14 +13,15 @@ log.info("Starting Sineware ProLinuxD... 🚀");
export let cloud: OCS2Connection;
export let localSocket: any;
export let config = {
prolinuxd : {
prolinuxd: {
modules: [
"plasma-mobile-nightly",
"ocs2"
]
},
ocs2: {
gateway_url: "wss://update.sineware.ca/gateway",
client_type: "prolinux,plasma-mobile-nightly",
access_token: ""
}
}
Expand Down Expand Up @@ -63,6 +64,14 @@ async function main() {
text: msg.payload.data
}, false);
} break;
case "set-token": {
config.ocs2.access_token = msg.payload.token;
let tomlConfig = TOML.stringify(config, {
newline: "\n"
});
fs.writeFileSync(process.env.CONFIG_FILE ?? path.join(__dirname, "prolinux.toml"), Buffer.from(tomlConfig), "utf-8");
} break;

}
} catch(e) {
console.log(e);
Expand All @@ -86,7 +95,7 @@ async function main() {

// Start Plasma Mobile Nightly Module
if(config.prolinuxd.modules.includes("plasma-mobile-nightly")) {
console.log("Starting Plasma Mobile Nightly Module...");
log.info("Starting Plasma Mobile Nightly Module...");
await loadPlasmaMobileNightlyModule();
}

Expand Down

0 comments on commit c3ec70b

Please sign in to comment.